forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-show-usd.ts
More file actions
42 lines (36 loc) · 1.34 KB
/
Copy pathuse-show-usd.ts
File metadata and controls
42 lines (36 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use client'
import { useState, useEffect } from 'react'
const KEY = 'flowstar-show-usd'
/**
* Hook that tracks whether token amounts should be displayed in USD.
*
* Returns a `[show, toggle]` tuple:
* - `show` — `true` if USD display is enabled, `false` otherwise. Defaults to
* `true` on first use (i.e. USD is shown until the user opts out).
* - `toggle` — setter that updates the preference both in React state and in
* `localStorage` (key: `"flowstar-show-usd"`), so the choice persists across
* page reloads and browser sessions.
*
* The persisted value is read from `localStorage` inside a `useEffect`, so the
* initial render always uses the default (`true`) before hydrating the stored
* preference. This avoids SSR/hydration mismatches.
*
* @returns A tuple of `[show: boolean, toggle: (v: boolean) => void]`.
*
* @example
* const [showUsd, setShowUsd] = useShowUsd();
* // showUsd === true by default
* setShowUsd(false); // hides USD display and persists the preference
*/
export function useShowUsd(): [boolean, (v: boolean) => void] {
const [show, setShow] = useState(true)
useEffect(() => {
const stored = localStorage.getItem(KEY)
if (stored !== null) setShow(stored !== 'false')
}, [])
function toggle(v: boolean) {
setShow(v)
localStorage.setItem(KEY, String(v))
}
return [show, toggle]
}