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