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

SettingsOpenSourceLicensesRouteScreen.tsx

238 lines8.2 KB

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