import { useCallback, useState } from 'react'; export type PhotoLayout = 'tiles' | 'list'; const KEY = 'slp.media.layout'; /** The layout this user chose last (tiles by default). Browser storage may be blocked, so every access is guarded. */ export function readLayout(): PhotoLayout { try { return window.localStorage.getItem(KEY) === 'list' ? 'list' : 'tiles'; } catch { return 'tiles'; } } /** The layout of the photos and a setter that remembers the choice per user (per browser). */ export function useLayoutPreference(): [PhotoLayout, (layout: PhotoLayout) => void] { const [layout, setLayoutState] = useState(readLayout); const setLayout = useCallback((next: PhotoLayout) => { setLayoutState(next); try { window.localStorage.setItem(KEY, next); } catch { // Blocked or full: the choice then only lasts for this visit. } }, []); return [layout, setLayout]; }