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

SettingsOpenSourceLicensesRouteScreen.tsx

234 lines8.3 KB

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