33 lines
960 B
TypeScript
33 lines
960 B
TypeScript
import { useCallback, useState } from "react";
|
|
import {
|
|
DEFAULT_FEED_TAB,
|
|
FEED_TABS,
|
|
type FeedTab,
|
|
} from "../../config/feedTabs.ts";
|
|
|
|
const STORAGE_KEY = "default-feed-tab";
|
|
|
|
function readStored(): FeedTab {
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
return stored && (FEED_TABS as readonly string[]).includes(stored)
|
|
? stored as FeedTab
|
|
: DEFAULT_FEED_TAB;
|
|
}
|
|
|
|
/**
|
|
* User preference for which feed tab the index opens on, persisted to
|
|
* localStorage (defaults to DEFAULT_FEED_TAB). Returns the stored tab and a setter that
|
|
* persists the choice. Consumers that only need to honour the preference (e.g.
|
|
* the index default) can ignore the setter.
|
|
*/
|
|
export function useDefaultFeedTab(): [FeedTab, (tab: FeedTab) => void] {
|
|
const [tab, setTabState] = useState<FeedTab>(readStored);
|
|
|
|
const setTab = useCallback((t: FeedTab) => {
|
|
localStorage.setItem(STORAGE_KEY, t);
|
|
setTabState(t);
|
|
}, []);
|
|
|
|
return [tab, setTab];
|
|
}
|