import { useCallback, useState } from 'react' const SESSION_START_KEY = 'inventory_audit_session_start' const LAST_LOCATION_KEY = 'inventory_audit_last_location' function readSessionStart(): number { const raw = sessionStorage.getItem(SESSION_START_KEY) if (raw) return Number(raw) const now = Date.now() sessionStorage.setItem(SESSION_START_KEY, String(now)) return now } /** Tracks the current audit walk: when it started (for the found/not-found split) and the * last location typed in, so the next scanned object can default to the same location. */ export function useAuditSession() { const [sessionStart, setSessionStart] = useState(readSessionStart) const [lastLocation, setLastLocationState] = useState(() => sessionStorage.getItem(LAST_LOCATION_KEY), ) const restart = useCallback(() => { const now = Date.now() sessionStorage.setItem(SESSION_START_KEY, String(now)) sessionStorage.removeItem(LAST_LOCATION_KEY) setSessionStart(now) setLastLocationState(null) }, []) const setLastLocation = useCallback((location: string | null) => { if (location) sessionStorage.setItem(LAST_LOCATION_KEY, location) else sessionStorage.removeItem(LAST_LOCATION_KEY) setLastLocationState(location) }, []) return { sessionStart, restart, lastLocation, setLastLocation } }