|
1 | | -import type React from 'react' |
2 | | -import { useCallback, useMemo, useState } from 'react' |
3 | | -import { ScrollView, StyleSheet } from 'react-native' |
| 1 | +import { useMemo, useState } from 'react' |
| 2 | +import { ScrollView, StyleSheet, Text } from 'react-native' |
4 | 3 | import { SafeAreaView } from 'react-native-safe-area-context' |
5 | | -import { getItem } from 'react-native-sensitive-info' |
6 | | -import { |
7 | | - useSecureStorage, |
8 | | - useSecurityAvailability, |
9 | | -} from 'react-native-sensitive-info/hooks' |
10 | | -import ActionsPanel from './components/ActionsPanel' |
11 | | -import Header from './components/Header' |
12 | | -import KeyRotationPanel from './components/KeyRotationPanel' |
13 | | -import ModeSelector from './components/ModeSelector' |
14 | | -import SecretForm from './components/SecretForm' |
15 | | -import SecretsList from './components/SecretsList' |
| 4 | +import AccessControlCard from './components/AccessControlCard' |
| 5 | +import DiagnosticsCard from './components/DiagnosticsCard' |
| 6 | +import KeyRotationCard from './components/KeyRotationCard' |
| 7 | +import StorageCard from './components/StorageCard' |
16 | 8 | import { |
17 | 9 | ACCESS_MODES, |
18 | | - DEFAULT_KEY, |
19 | | - DEFAULT_SECRET, |
| 10 | + BIOMETRIC_PROMPT, |
20 | 11 | DEFAULT_SERVICE, |
21 | | - INITIAL_STATUS, |
22 | 12 | type ModeKey, |
23 | 13 | } from './constants' |
24 | | -import { formatError } from './utils/formatError' |
25 | 14 |
|
26 | | -const App: React.FC = () => { |
27 | | - const [service, setService] = useState(DEFAULT_SERVICE) |
28 | | - const [keyName, setKeyName] = useState(DEFAULT_KEY) |
29 | | - const [secret, setSecret] = useState(DEFAULT_SECRET) |
| 15 | +const App = () => { |
30 | 16 | const [mode, setMode] = useState<ModeKey>('open') |
31 | | - const [status, setStatus] = useState(INITIAL_STATUS) |
32 | | - const [pending, setPending] = useState(false) |
33 | | - |
34 | | - const trimmedService = useMemo(() => { |
35 | | - const next = service.trim() |
36 | | - return next.length > 0 ? next : DEFAULT_SERVICE |
37 | | - }, [service]) |
38 | | - |
39 | | - const selectedMode = useMemo(() => { |
40 | | - const fallback = ACCESS_MODES[0] |
41 | | - return ACCESS_MODES.find((candidate) => candidate.key === mode) ?? fallback |
42 | | - }, [mode]) |
43 | | - |
44 | | - const authenticationPrompt = useMemo(() => { |
45 | | - if (selectedMode.key !== 'biometric') { |
46 | | - return undefined |
47 | | - } |
48 | 17 |
|
| 18 | + const { options, prompt } = useMemo(() => { |
| 19 | + const entry = |
| 20 | + ACCESS_MODES.find((candidate) => candidate.key === mode) ?? |
| 21 | + ACCESS_MODES[0] |
| 22 | + const isBiometric = entry.key === 'biometric' |
49 | 23 | return { |
50 | | - title: 'Unlock your secret', |
51 | | - subtitle: 'Biometric authentication is required to continue', |
52 | | - description: 'This demo stores data behind your biometric enrollment.', |
53 | | - cancel: 'Cancel', |
54 | | - } as const |
55 | | - }, [selectedMode.key]) |
56 | | - |
57 | | - const secureOptions = useMemo( |
58 | | - () => ({ |
59 | | - service: trimmedService, |
60 | | - accessControl: selectedMode.accessControl, |
61 | | - authenticationPrompt, |
62 | | - includeValues: true, |
63 | | - }), |
64 | | - [authenticationPrompt, selectedMode.accessControl, trimmedService] |
65 | | - ) |
66 | | - |
67 | | - const { |
68 | | - items, |
69 | | - isLoading, |
70 | | - error, |
71 | | - saveSecret, |
72 | | - removeSecret, |
73 | | - clearAll, |
74 | | - refreshItems, |
75 | | - } = useSecureStorage(secureOptions) |
76 | | - |
77 | | - const { data: availability } = useSecurityAvailability() |
78 | | - |
79 | | - const handleSave = useCallback(async () => { |
80 | | - const normalizedKey = keyName.trim() |
81 | | - if (normalizedKey.length === 0) { |
82 | | - setStatus('Please provide a key before saving.') |
83 | | - return |
84 | | - } |
85 | | - |
86 | | - setPending(true) |
87 | | - try { |
88 | | - const result = await saveSecret(normalizedKey, secret) |
89 | | - if (result.success) { |
90 | | - setStatus('Secret saved securely.') |
91 | | - } else { |
92 | | - setStatus(result.error?.message ?? 'Unable to save the secret.') |
93 | | - } |
94 | | - } catch (err) { |
95 | | - setStatus(formatError(err)) |
96 | | - } finally { |
97 | | - setPending(false) |
| 24 | + options: { |
| 25 | + service: DEFAULT_SERVICE, |
| 26 | + accessControl: entry.accessControl, |
| 27 | + ...(isBiometric ? { authenticationPrompt: BIOMETRIC_PROMPT } : {}), |
| 28 | + }, |
| 29 | + prompt: isBiometric ? BIOMETRIC_PROMPT : undefined, |
98 | 30 | } |
99 | | - }, [keyName, saveSecret, secret]) |
100 | | - |
101 | | - const handleReveal = useCallback(async () => { |
102 | | - const normalizedKey = keyName.trim() |
103 | | - if (normalizedKey.length === 0) { |
104 | | - setStatus('Provide the key you would like to reveal.') |
105 | | - return |
106 | | - } |
107 | | - |
108 | | - setPending(true) |
109 | | - try { |
110 | | - const item = await getItem(normalizedKey, { |
111 | | - service: trimmedService, |
112 | | - accessControl: selectedMode.accessControl, |
113 | | - authenticationPrompt, |
114 | | - includeValue: true, |
115 | | - }) |
116 | | - |
117 | | - if (item?.value) { |
118 | | - setStatus(`Secret for "${normalizedKey}" → ${item.value}`) |
119 | | - } else { |
120 | | - setStatus('That key has no stored value yet.') |
121 | | - } |
122 | | - } catch (err) { |
123 | | - setStatus(formatError(err)) |
124 | | - } finally { |
125 | | - setPending(false) |
126 | | - } |
127 | | - }, [ |
128 | | - authenticationPrompt, |
129 | | - keyName, |
130 | | - selectedMode.accessControl, |
131 | | - trimmedService, |
132 | | - ]) |
133 | | - |
134 | | - const handleRemove = useCallback(async () => { |
135 | | - const normalizedKey = keyName.trim() |
136 | | - if (normalizedKey.length === 0) { |
137 | | - setStatus('Provide the key you would like to forget.') |
138 | | - return |
139 | | - } |
140 | | - |
141 | | - setPending(true) |
142 | | - try { |
143 | | - const result = await removeSecret(normalizedKey) |
144 | | - setStatus( |
145 | | - result.success ? 'Secret deleted.' : 'Secret could not be deleted.' |
146 | | - ) |
147 | | - } catch (err) { |
148 | | - setStatus(formatError(err)) |
149 | | - } finally { |
150 | | - setPending(false) |
151 | | - } |
152 | | - }, [keyName, removeSecret]) |
153 | | - |
154 | | - const handleClear = useCallback(async () => { |
155 | | - setPending(true) |
156 | | - try { |
157 | | - const result = await clearAll() |
158 | | - setStatus( |
159 | | - result.success |
160 | | - ? 'All secrets cleared for this service.' |
161 | | - : 'Nothing to clear.' |
162 | | - ) |
163 | | - } catch (err) { |
164 | | - setStatus(formatError(err)) |
165 | | - } finally { |
166 | | - setPending(false) |
167 | | - } |
168 | | - }, [clearAll]) |
169 | | - |
170 | | - const handleRefresh = useCallback(async () => { |
171 | | - setPending(true) |
172 | | - try { |
173 | | - await refreshItems() |
174 | | - setStatus('Inventory refreshed.') |
175 | | - } catch (err) { |
176 | | - setStatus(formatError(err)) |
177 | | - } finally { |
178 | | - setPending(false) |
179 | | - } |
180 | | - }, [refreshItems]) |
| 31 | + }, [mode]) |
181 | 32 |
|
182 | 33 | return ( |
183 | 34 | <SafeAreaView style={styles.safeArea}> |
184 | 35 | <ScrollView |
| 36 | + contentContainerStyle={styles.scroll} |
185 | 37 | keyboardShouldPersistTaps="handled" |
186 | | - contentContainerStyle={styles.scrollContent} |
187 | 38 | > |
188 | | - <Header |
189 | | - title="Sensitive Info Playground" |
190 | | - subtitle="Store a small secret, lock it with biometrics if you like, and review the inventory below." |
191 | | - /> |
192 | | - |
193 | | - <SecretForm |
194 | | - service={service} |
195 | | - onServiceChange={setService} |
196 | | - keyName={keyName} |
197 | | - onKeyNameChange={setKeyName} |
198 | | - secret={secret} |
199 | | - onSecretChange={setSecret} |
200 | | - servicePlaceholder={DEFAULT_SERVICE} |
201 | | - keyPlaceholder={DEFAULT_KEY} |
202 | | - secretPlaceholder={DEFAULT_SECRET} |
203 | | - /> |
204 | | - |
205 | | - <ModeSelector |
206 | | - modes={ACCESS_MODES} |
207 | | - selectedKey={selectedMode.key} |
208 | | - onSelect={setMode} |
209 | | - availability={availability ?? null} |
210 | | - /> |
211 | | - |
212 | | - <ActionsPanel |
213 | | - onSave={handleSave} |
214 | | - onReveal={handleReveal} |
215 | | - onRemove={handleRemove} |
216 | | - onClear={handleClear} |
217 | | - onRefresh={handleRefresh} |
218 | | - pending={pending} |
219 | | - status={status} |
220 | | - errorMessage={error?.message} |
221 | | - /> |
222 | | - |
223 | | - <SecretsList |
224 | | - items={items} |
225 | | - isLoading={isLoading} |
226 | | - service={trimmedService} |
227 | | - /> |
228 | | - |
229 | | - <KeyRotationPanel service={trimmedService} /> |
| 39 | + <Text style={styles.heading}>Sensitive Info Playground</Text> |
| 40 | + <AccessControlCard mode={mode} onChange={setMode} /> |
| 41 | + <StorageCard options={options} authenticationPrompt={prompt} /> |
| 42 | + <KeyRotationCard options={options} /> |
| 43 | + <DiagnosticsCard options={options} /> |
230 | 44 | </ScrollView> |
231 | 45 | </SafeAreaView> |
232 | 46 | ) |
233 | 47 | } |
234 | 48 |
|
235 | 49 | const styles = StyleSheet.create({ |
236 | | - safeArea: { |
237 | | - flex: 1, |
238 | | - backgroundColor: '#f6f8fb', |
239 | | - }, |
240 | | - scrollContent: { |
241 | | - padding: 20, |
242 | | - paddingBottom: 32, |
| 50 | + safeArea: { flex: 1, backgroundColor: '#f1f5f9' }, |
| 51 | + scroll: { padding: 16, paddingBottom: 32 }, |
| 52 | + heading: { |
| 53 | + fontSize: 22, |
| 54 | + fontWeight: '700', |
| 55 | + color: '#0f172a', |
| 56 | + marginBottom: 16, |
243 | 57 | }, |
244 | 58 | }) |
245 | 59 |
|
|
0 commit comments