MCPcopy Create free account
hub / github.com/TanStack/db / HeaderControls

Function HeaderControls

examples/react-native/shopping-list/app/_layout.tsx:21–150  ·  view source on GitHub ↗
({ onAppRefresh }: { onAppRefresh: () => void })

Source from the content-addressed store, hash-verified

19import { ShoppingProvider, useShopping } from '../src/db/ShoppingContext'
20
21function HeaderControls({ onAppRefresh }: { onAppRefresh: () => void }) {
22 const {
23 isOnline,
24 isSimulatedOffline,
25 setSimulateOffline,
26 clearLocalState,
27 pendingCount,
28 } = useShopping()
29 const [menuVisible, setMenuVisible] = useState(false)
30 const [isClearingState, setIsClearingState] = useState(false)
31
32 const closeMenu = useCallback(() => {
33 setMenuVisible(false)
34 }, [])
35
36 const toggleSimulatedOffline = useCallback(() => {
37 setMenuVisible(false)
38 void setSimulateOffline(!isSimulatedOffline)
39 }, [isSimulatedOffline, setSimulateOffline])
40
41 const clearAndRefresh = useCallback(() => {
42 setMenuVisible(false)
43 // Delay the alert one tick so iOS can fully dismiss the modal first.
44 setTimeout(() => {
45 Alert.alert(
46 `Clear local state`,
47 `This clears local SQLite data and queued offline transactions, then refreshes the app.`,
48 [
49 { text: `Cancel`, style: `cancel` },
50 {
51 text: `Clear`,
52 style: `destructive`,
53 onPress: () => {
54 if (isClearingState) return
55 setIsClearingState(true)
56 void (async () => {
57 try {
58 await clearLocalState()
59 onAppRefresh()
60 } catch (error) {
61 console.error(`[Shopping] Failed to clear local state`, error)
62 Alert.alert(
63 `Clear failed`,
64 error instanceof Error ? error.message : `Unknown error`,
65 )
66 } finally {
67 setIsClearingState(false)
68 }
69 })()
70 },
71 },
72 ],
73 )
74 }, 0)
75 }, [clearLocalState, isClearingState, onAppRefresh])
76
77 const statusLabel = isOnline
78 ? `Online`

Callers

nothing calls this directly

Calls 2

useShoppingFunction · 0.90
clearLocalStateFunction · 0.85

Tested by

no test coverage detected