Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SettingsOpenSourceLicensesRouteScreen.tsx

240 lines8.4 KB

1import { NativeScrollView } from "../../native/NativeScrollViewMarker";
2import { SearchField } from "../../components/ui/SearchField";
3import { Toolbar } from "../../components/ui/Toolbar";
4import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
5import { LegendList } from "@legendapp/list/react-native";
6import { type StaticScreenProps, useNavigation } from "@react-navigation/native";
7import {
8 filterThirdPartyLicenseEntries,
9 findThirdPartyLicenseEntry,
10 formatLicenseBundles,
11 thirdPartyLicenseEntryKey,
12 type ThirdPartyLicenseEntry,
13} from "@t3tools/shared/thirdPartyLicenses";
14import { useCallback, useMemo, useRef, useState } from "react";
15import { Linking, Platform, Pressable, type TextInputInstance, View } from "react-native";
16import { useSafeAreaInsets } from "react-native-safe-area-context";
17​
18import { SymbolView } from "../../components/ui/AppSymbol";
19import { AppText as Text } from "../../components/ui/AppText";
20import { ListRow } from "../../components/ui/ListRow";
21import { SettingsScreen } from "./components/SettingsScreen";
22import { NativeStackScreenOptions } from "../../native/StackHeader";
23import {
24 createNativeMailSearchToolbarItem,
25 NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET,
26} from "../layout/native-mail-search-toolbar";
27import { useNativeMailSearchToolbar } from "../../native/use-native-mail-search-toolbar";
28​
29import { getMobileThirdPartyLicenses } from "./mobileThirdPartyLicenses";
30​
31function useMobileThirdPartyLicenses() {
32 return useMemo(() => {
33 try {
34 return getMobileThirdPartyLicenses();
35 } catch {
36 return null;
37 }
38 }, []);
40​
41function LicenseRow(props: {
42 readonly entry: ThirdPartyLicenseEntry;
43 readonly onPress: () => void;
44}) {
45 return (
46 <View className="border-b border-border">
47 <ListRow
48 label={props.entry.name}
49 description={[props.entry.version, props.entry.license].filter(Boolean).join(" · ")}
50 onPress={props.onPress}
51 />
52 </View>
53 );
55​
56export function SettingsOpenSourceLicensesRouteScreen() {
57 const searchRef = useRef<TextInputInstance>(null);
58 const navigation = useNavigation();
59 const insets = useSafeAreaInsets();
60 const [query, setQuery] = useState("");
61 const usesNativeMailSearchToolbar = useNativeMailSearchToolbar();
62 const manifest = useMobileThirdPartyLicenses();
63 const entries = manifest?.entries ?? [];
64 const filteredEntries = useMemo(
65 () => filterThirdPartyLicenseEntries(entries, query),
66 [entries, query],
67 );
68 const renderItem = useCallback(
69 ({ item }: { readonly item: ThirdPartyLicenseEntry }) => (
70 <LicenseRow
71 entry={item}
72 onPress={() =>
73 navigation.navigate("SettingsOpenSourceLicense", {
74 entryKey: thirdPartyLicenseEntryKey(item),
75 })
76 }
77 />
78 ),
79 [navigation],
80 );
81​
82 if (!manifest) {
83 return (
84 <SettingsScreen title="Open source licenses">
85 <View className="flex-1 items-center justify-center px-6">
86 <Text className="text-center text-base text-foreground-muted">
87 License notices are unavailable in this build.
88 </Text>
89 </View>
90 </SettingsScreen>
91 );
92 }
93​
94 return (
95 <SettingsScreen title="Open source licenses">
96 {Platform.OS === "ios" ? (
97 <NativeStackScreenOptions
98 options={{
99 unstable_headerToolbarItems: usesNativeMailSearchToolbar
100 ? () => [
101 createNativeMailSearchToolbarItem({
102 onSearchTextChange: setQuery,
103 placeholder: "Search packages",
104 searchTextChangeId: "open-source-licenses-search-text",
105 showsSearchDismissButton: true,
106 }),
107 ]
108 : undefined,
109 headerSearchBarOptions: usesNativeMailSearchToolbar
110 ? undefined
111 : {
112 allowToolbarIntegration: true,
113 autoCapitalize: "none",
114 hideNavigationBar: false,
115 hideWhenScrolling: false,
116 obscureBackground: false,
117 onCancelButtonPress: () => setQuery(""),
118 onChangeText: (event) => setQuery(event.nativeEvent.text),
119 placeholder: "Search packages",
120 },
121 }}
122 />
123 ) : null}
124 {Platform.OS === "ios" && !usesNativeMailSearchToolbar ? (
125 <Toolbar placement="bottom">
126 <Toolbar.SearchBarSlot />
127 </Toolbar>
128 ) : null}
129​
130 <LegendList
131 renderScrollComponent={Platform.OS === "ios" ? NativeScrollView : undefined}
132 className="flex-1"
133 contentContainerStyle={{
134 paddingBottom: usesNativeMailSearchToolbar
135 ? NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET + 18
136 : Platform.OS === "ios"
137 ? 18
138 : Math.max(insets.bottom, 18) + 18,
139 }}
140 contentInsetAdjustmentBehavior="automatic"
141 data={filteredEntries}
142 estimatedItemSize={78}
143 keyboardDismissMode="on-drag"
144 keyboardShouldPersistTaps="handled"
145 keyExtractor={thirdPartyLicenseEntryKey}
146 ListEmptyComponent={
147 <View className="items-center px-6 py-12">
148 <Text className="text-center text-base text-foreground-muted">
149 No licenses match that search.
150 </Text>
151 </View>
152 }
153 ListHeaderComponent={
154 Platform.OS !== "ios" ? (
155 <View className="px-5 pt-4 pb-5">
156 <SearchField
157 inputRef={searchRef}
158 autoFocus={false}
159 accessibilityLabel="Search open-source licenses"
160 clearAccessibilityLabel="Clear package search"
161 onChangeText={setQuery}
162 placeholder="Search packages"
163 value={query}
164 />
165 </View>
166 ) : null
167 }
168 renderItem={renderItem}
169 showsVerticalScrollIndicator={false}
170 />
171 </SettingsScreen>
172 );
174​
175type LicenseDetailProps = StaticScreenProps<{ readonly entryKey: string }>;
176​
177export function SettingsOpenSourceLicenseRouteScreen({ route }: LicenseDetailProps) {
178 const insets = useSafeAreaInsets();
179 const manifest = useMobileThirdPartyLicenses();
180 const entry = manifest
181 ? findThirdPartyLicenseEntry(manifest.entries, route.params.entryKey)
182 : undefined;
183 const sourceUrl = entry?.sourceUrl?.match(/^https?:\/\//) ? entry.sourceUrl : null;
184​
185 if (!entry) {
186 return (
187 <SettingsScreen title="License notice">
188 <View className="flex-1 items-center justify-center px-6">
189 <Text className="text-center text-base text-foreground-muted">
190 This license notice is unavailable.
191 </Text>
192 </View>
193 </SettingsScreen>
194 );
195 }
196​
197 return (
198 <SettingsScreen title="License notice">
199 <ScrollView
200 className="flex-1"
201 contentInsetAdjustmentBehavior="automatic"
202 contentContainerClassName="gap-5 px-5 pt-4"
203 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
204 showsVerticalScrollIndicator={false}
205 >
206 <View className="gap-2 px-1">
207 <Text className="text-2xl font-t3-bold text-foreground">{entry.name}</Text>
208 <Text className="text-base leading-normal text-foreground-muted">
209 {[entry.version, entry.license, formatLicenseBundles(entry.bundles)]
210 .filter((value): value is string => Boolean(value))
211 .join(" · ")}
212 </Text>
213 {sourceUrl ? (
214 <Pressable
215 accessibilityHint="Opens the project website"
216 accessibilityRole="link"
217 onPress={() => void Linking.openURL(sourceUrl)}
218 className="min-h-12 flex-row items-center gap-2 self-start py-2 active:opacity-60"
219 >
220 <Text className="font-t3-medium text-primary-text">Project source</Text>
221 <SymbolView
222 name="arrow.up.right"
223 size={16}
224 tintColorClassName={"accent-primary-text"}
225 type="monochrome"
226 weight="semibold"
227 />
228 </Pressable>
229 ) : null}
230 </View>
231​
232 <View className="overflow-hidden rounded-[24px] border-continuous bg-grouped-card p-4">
233 <Text selectable className="font-mono text-base leading-normal text-foreground">
234 {entry.noticeText}
235 </Text>
236 </View>
237 </ScrollView>
238 </SettingsScreen>
239 );