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

SettingsOpenSourceLicensesRouteScreen.tsx

241 lines8.5 KB

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