Open History to see the last commit
1import { NativeScrollView } from "../../native/NativeScrollViewMarker";
2import { useSearchToolbarInset } from "../../components/ui/useBarInset";
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 { useMemo, useState } from "react";
14import { Linking, Platform, Pressable, 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";
21
22import { getMobileThirdPartyLicenses } from "./mobileThirdPartyLicenses";
23
24function useMobileThirdPartyLicenses() {
25 return useMemo(() => {
26 try {
27 return getMobileThirdPartyLicenses();
28 } catch {
29 return null;
30 }
31 }, []);
32}
33
34function LicenseRow(props: {
35 readonly entry: ThirdPartyLicenseEntry;
36 readonly onPress: () => void;
37}) {
38 return (
39 <View className="border-b border-border">
40 <ListRow
41 label={props.entry.name}
42 description={[props.entry.version, props.entry.license].filter(Boolean).join(" · ")}
43 onPress={props.onPress}
44 />
45 </View>
46 );
47}
48
49export function SettingsOpenSourceLicensesRouteScreen() {
50 const navigation = useNavigation();
51 const insets = useSafeAreaInsets();
52 const [query, setQuery] = useState("");
53 const search = {
54 value: query,
55 onChangeText: setQuery,
56 placeholder: "Search packages",
57 accessibilityLabel: "Search open-source licenses",
58 clearAccessibilityLabel: "Clear package search",
59 };
60 const searchInset = useSearchToolbarInset(search);
61 const manifest = useMobileThirdPartyLicenses();
62 const entries = manifest?.entries ?? [];
63 const filteredEntries = filterThirdPartyLicenseEntries(entries, query);
64 const renderItem = ({ item }: { readonly item: ThirdPartyLicenseEntry }) => (
65 <LicenseRow
66 entry={item}
67 onPress={() =>
68 navigation.navigate("SettingsOpenSourceLicense", {
69 entryKey: thirdPartyLicenseEntryKey(item),
70 })
71 }
72 />
73 );
74
75 if (!manifest) {
76 return (
77 <SettingsScreen title="Open source licenses">
78 <View className="flex-1 items-center justify-center px-6">
79 <Text className="text-center text-base text-foreground-muted">
80 License notices are unavailable in this build.
81 </Text>
82 </View>
83 </SettingsScreen>
84 );
85 }
86
87 return (
88 <SettingsScreen title="Open source licenses" search={search}>
89 <LegendList
90 renderScrollComponent={NativeScrollView}
91 className="flex-1"
92 contentContainerStyle={{
93 paddingBottom:
94 searchInset + (Platform.OS === "ios" ? 18 : Math.max(insets.bottom, 18) + 18),
95 }}
96 contentInsetAdjustmentBehavior="automatic"
97 data={filteredEntries}
98 estimatedItemSize={78}
99 keyboardDismissMode="on-drag"
100 keyboardShouldPersistTaps="handled"
101 keyExtractor={thirdPartyLicenseEntryKey}
102 ListEmptyComponent={
103 <View className="items-center px-6 py-12">
104 <Text className="text-center text-base text-foreground-muted">
105 No licenses match that search.
106 </Text>
107 </View>
108 }
109 renderItem={renderItem}
110 showsVerticalScrollIndicator={false}
111 />
112 </SettingsScreen>
113 );
114}
115
116type LicenseDetailProps = StaticScreenProps<{ readonly entryKey: string }>;
117
118export function SettingsOpenSourceLicenseRouteScreen({ route }: LicenseDetailProps) {
119 const insets = useSafeAreaInsets();
120 const manifest = useMobileThirdPartyLicenses();
121 const entry = manifest
122 ? findThirdPartyLicenseEntry(manifest.entries, route.params.entryKey)
123 : undefined;
124 const sourceUrl = entry?.sourceUrl?.match(/^https?:\/\//) ? entry.sourceUrl : null;
125
126 if (!entry) {
127 return (
128 <SettingsScreen title="License notice">
129 <View className="flex-1 items-center justify-center px-6">
130 <Text className="text-center text-base text-foreground-muted">
131 This license notice is unavailable.
132 </Text>
133 </View>
134 </SettingsScreen>
135 );
136 }
137
138 return (
139 <SettingsScreen title="License notice">
140 <ScrollView
141 className="flex-1"
142 contentInsetAdjustmentBehavior="automatic"
143 contentContainerClassName="gap-5 px-5 pt-4"
144 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
145 showsVerticalScrollIndicator={false}
146 >
147 <View className="gap-2 px-1">
148 <Text className="text-2xl font-t3-bold text-foreground">{entry.name}</Text>
149 <Text className="text-base leading-normal text-foreground-muted">
150 {[entry.version, entry.license, formatLicenseBundles(entry.bundles)]
151 .filter((value): value is string => Boolean(value))
152 .join(" · ")}
153 </Text>
154 {sourceUrl ? (
155 <Pressable
156 accessibilityHint="Opens the project website"
157 accessibilityRole="link"
158 onPress={() => void Linking.openURL(sourceUrl)}
159 className="min-h-12 flex-row items-center gap-2 self-start py-2 active:opacity-60"
160 >
161 <Text className="font-t3-medium text-primary-text">Project source</Text>
162 <SymbolView
163 name="arrow.up.right"
164 size={16}
165 tintColorClassName={"accent-primary-text"}
166 type="monochrome"
167 weight="semibold"
168 />
169 </Pressable>
170 ) : null}
171 </View>
172
173 <View className="overflow-hidden rounded-[24px] border-continuous bg-grouped-card p-4">
174 <Text selectable className="font-mono text-base leading-normal text-foreground">
175 {entry.noticeText}
176 </Text>
177 </View>
178 </ScrollView>
179 </SettingsScreen>
180 );
181}