Open History to see the last commit
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 }, []);
38}
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 );
53}
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("SettingsSheet", {
73 screen: "SettingsContent",
74 params: {
75 screen: "SettingsOpenSourceLicense",
76 params: { entryKey: thirdPartyLicenseEntryKey(item) },
77 },
78 })
79 }
80 />
81 ),
82 [navigation],
83 );
84
85 if (!manifest) {
86 return (
87 <SettingsScreen title="Open source licenses">
88 <View className="flex-1 items-center justify-center px-6">
89 <Text className="text-center text-base text-foreground-muted">
90 License notices are unavailable in this build.
91 </Text>
92 </View>
93 </SettingsScreen>
94 );
95 }
96
97 return (
98 <SettingsScreen title="Open source licenses">
99 {Platform.OS === "ios" ? (
100 <NativeStackScreenOptions
101 options={{
102 unstable_headerToolbarItems: usesNativeMailSearchToolbar
103 ? () => [
104 createNativeMailSearchToolbarItem({
105 onSearchTextChange: setQuery,
106 placeholder: "Search packages",
107 searchTextChangeId: "open-source-licenses-search-text",
108 showsSearchDismissButton: true,
109 }),
110 ]
111 : undefined,
112 headerSearchBarOptions: usesNativeMailSearchToolbar
113 ? undefined
114 : {
115 allowToolbarIntegration: true,
116 autoCapitalize: "none",
117 hideNavigationBar: false,
118 hideWhenScrolling: false,
119 obscureBackground: false,
120 onCancelButtonPress: () => setQuery(""),
121 onChangeText: (event) => setQuery(event.nativeEvent.text),
122 placeholder: "Search packages",
123 },
124 }}
125 />
126 ) : null}
127 {Platform.OS === "ios" && !usesNativeMailSearchToolbar ? (
128 <Toolbar placement="bottom">
129 <Toolbar.SearchBarSlot />
130 </Toolbar>
131 ) : null}
132
133 <LegendList
134 className="flex-1"
135 contentContainerStyle={{
136 paddingBottom: usesNativeMailSearchToolbar
137 ? NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET + 18
138 : Platform.OS === "ios"
139 ? 18
140 : Math.max(insets.bottom, 18) + 18,
141 }}
142 contentInsetAdjustmentBehavior="automatic"
143 data={filteredEntries}
144 estimatedItemSize={78}
145 keyboardDismissMode="on-drag"
146 keyboardShouldPersistTaps="handled"
147 keyExtractor={thirdPartyLicenseEntryKey}
148 ListEmptyComponent={
149 <View className="items-center px-6 py-12">
150 <Text className="text-center text-base text-foreground-muted">
151 No licenses match that search.
152 </Text>
153 </View>
154 }
155 ListHeaderComponent={
156 Platform.OS !== "ios" ? (
157 <View className="px-5 pt-4 pb-5">
158 <SearchField
159 inputRef={searchRef}
160 autoFocus={false}
161 accessibilityLabel="Search open-source licenses"
162 clearAccessibilityLabel="Clear package search"
163 onChangeText={setQuery}
164 placeholder="Search packages"
165 value={query}
166 />
167 </View>
168 ) : null
169 }
170 renderItem={renderItem}
171 showsVerticalScrollIndicator={false}
172 />
173 </SettingsScreen>
174 );
175}
176
177type LicenseDetailProps = StaticScreenProps<{ readonly entryKey: string }>;
178
179export function SettingsOpenSourceLicenseRouteScreen({ route }: LicenseDetailProps) {
180 const insets = useSafeAreaInsets();
181 const manifest = useMobileThirdPartyLicenses();
182 const entry = manifest
183 ? findThirdPartyLicenseEntry(manifest.entries, route.params.entryKey)
184 : undefined;
185 const sourceUrl = entry?.sourceUrl?.match(/^https?:\/\//) ? entry.sourceUrl : null;
186
187 if (!entry) {
188 return (
189 <SettingsScreen title="License notice">
190 <View className="flex-1 items-center justify-center px-6">
191 <Text className="text-center text-base text-foreground-muted">
192 This license notice is unavailable.
193 </Text>
194 </View>
195 </SettingsScreen>
196 );
197 }
198
199 return (
200 <SettingsScreen title="License notice">
201 <ScrollView
202 className="flex-1"
203 contentInsetAdjustmentBehavior="automatic"
204 contentContainerClassName="gap-5 px-5 pt-4"
205 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
206 showsVerticalScrollIndicator={false}
207 >
208 <View className="gap-2 px-1">
209 <Text className="text-2xl font-t3-bold text-foreground">{entry.name}</Text>
210 <Text className="text-base leading-normal text-foreground-muted">
211 {[entry.version, entry.license, formatLicenseBundles(entry.bundles)]
212 .filter((value): value is string => Boolean(value))
213 .join(" · ")}
214 </Text>
215 {sourceUrl ? (
216 <Pressable
217 accessibilityHint="Opens the project website"
218 accessibilityRole="link"
219 onPress={() => void Linking.openURL(sourceUrl)}
220 className="min-h-12 flex-row items-center gap-2 self-start py-2 active:opacity-60"
221 >
222 <Text className="font-t3-medium text-primary-text">Project source</Text>
223 <SymbolView
224 name="arrow.up.right"
225 size={16}
226 tintColorClassName={"accent-primary-text"}
227 type="monochrome"
228 weight="semibold"
229 />
230 </Pressable>
231 ) : null}
232 </View>
233
234 <View className="overflow-hidden rounded-[24px] border-continuous bg-grouped-card p-4">
235 <Text selectable className="font-mono text-base leading-normal text-foreground">
236 {entry.noticeText}
237 </Text>
238 </View>
239 </ScrollView>
240 </SettingsScreen>
241 );
242}