({ onAppRefresh }: { onAppRefresh: () => void })
| 19 | import { ShoppingProvider, useShopping } from '../src/db/ShoppingContext' |
| 20 | |
| 21 | function 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` |
nothing calls this directly
no test coverage detected