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

SettingsOpenSourceLicensesRouteScreen.tsx

259 lines8.9 KB

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