-
Notifications
You must be signed in to change notification settings - Fork 23
refactor: extract a generic useStorageValue hook for localStorage state #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MkDev11
merged 2 commits into
MkDev11:main
from
cleanjunc:refactor/use-storage-value-hook
May 26, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,49 @@ | ||
| 'use client'; | ||
|
|
||
| import { useEffect, useState, useCallback } from 'react'; | ||
| import { useCallback } from 'react'; | ||
| import { useStorageValue } from './use-storage-value'; | ||
|
|
||
| const STORAGE_KEY = 'gittensor.trackedMiners'; | ||
| const EVENT_NAME = 'tracked-miners-changed'; | ||
| const EMPTY: Set<string> = new Set(); | ||
|
|
||
| function readStorage(): Set<string> { | ||
| function parse(raw: string | null): Set<string> { | ||
| if (!raw) return new Set(); | ||
| const arr = JSON.parse(raw); | ||
| return new Set(Array.isArray(arr) ? arr : []); | ||
| } | ||
|
|
||
| function serialize(set: Set<string>): string { | ||
| return JSON.stringify(Array.from(set)); | ||
| } | ||
|
|
||
| function readFresh(): Set<string> { | ||
| if (typeof window === 'undefined') return new Set(); | ||
| try { | ||
| const raw = localStorage.getItem(STORAGE_KEY); | ||
| if (!raw) return new Set(); | ||
| const arr = JSON.parse(raw); | ||
| return new Set(Array.isArray(arr) ? arr : []); | ||
| return parse(localStorage.getItem(STORAGE_KEY)); | ||
| } catch { | ||
| return new Set(); | ||
| } | ||
| } | ||
|
|
||
| function writeStorage(set: Set<string>) { | ||
| if (typeof window === 'undefined') return; | ||
| localStorage.setItem(STORAGE_KEY, JSON.stringify(Array.from(set))); | ||
| window.dispatchEvent(new Event('tracked-miners-changed')); | ||
| } | ||
|
|
||
| export function useTrackedMiners() { | ||
| const [tracked, setTracked] = useState<Set<string>>(new Set()); | ||
|
|
||
| useEffect(() => { | ||
| setTracked(readStorage()); | ||
| const handler = () => setTracked(readStorage()); | ||
| window.addEventListener('tracked-miners-changed', handler); | ||
| window.addEventListener('storage', handler); | ||
| return () => { | ||
| window.removeEventListener('tracked-miners-changed', handler); | ||
| window.removeEventListener('storage', handler); | ||
| }; | ||
| }, []); | ||
|
|
||
| const toggle = useCallback((id: string) => { | ||
| const next = new Set(readStorage()); | ||
| if (next.has(id)) next.delete(id); | ||
| else next.add(id); | ||
| writeStorage(next); | ||
| }, []); | ||
| const [tracked, setTracked] = useStorageValue<Set<string>>( | ||
| STORAGE_KEY, | ||
| parse, | ||
| serialize, | ||
| EMPTY, | ||
| EVENT_NAME, | ||
| ); | ||
|
|
||
| const toggle = useCallback( | ||
| (id: string) => { | ||
| const next = readFresh(); | ||
| if (next.has(id)) next.delete(id); | ||
| else next.add(id); | ||
| setTracked(next); | ||
| }, | ||
| [setTracked], | ||
| ); | ||
|
|
||
| return { tracked, toggle }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 'use client'; | ||
|
|
||
| import { useEffect, useState, useCallback, useRef } from 'react'; | ||
|
|
||
| export function useStorageValue<T>( | ||
| key: string, | ||
| parser: (raw: string | null) => T, | ||
| serializer: (value: T) => string, | ||
| defaultValue: T, | ||
| eventName?: string, | ||
| ): [T, (value: T) => void] { | ||
| const [value, setValue] = useState<T>(defaultValue); | ||
| const parserRef = useRef(parser); | ||
| const serializerRef = useRef(serializer); | ||
| const defaultRef = useRef(defaultValue); | ||
| parserRef.current = parser; | ||
| serializerRef.current = serializer; | ||
| defaultRef.current = defaultValue; | ||
|
|
||
| useEffect(() => { | ||
| const read = (): T => { | ||
| if (typeof window === 'undefined') return defaultRef.current; | ||
| try { | ||
| return parserRef.current(localStorage.getItem(key)); | ||
| } catch { | ||
| return defaultRef.current; | ||
| } | ||
| }; | ||
| setValue(read()); | ||
| const handler = () => setValue(read()); | ||
| if (eventName) window.addEventListener(eventName, handler); | ||
| window.addEventListener('storage', handler); | ||
| return () => { | ||
| if (eventName) window.removeEventListener(eventName, handler); | ||
| window.removeEventListener('storage', handler); | ||
| }; | ||
| }, [key, eventName]); | ||
|
|
||
| const write = useCallback( | ||
| (next: T) => { | ||
| if (typeof window === 'undefined') return; | ||
| localStorage.setItem(key, serializerRef.current(next)); | ||
| // Local fallback for the no-eventName case (the native `storage` event | ||
| // doesn't fire same-tab). When eventName is set, the dispatched event's | ||
| // synchronous handler will overwrite this with the parsed disk value — | ||
| // matching the original "state = parse(serialize(x))" semantics. | ||
| setValue(next); | ||
| if (eventName) window.dispatchEvent(new Event(eventName)); | ||
| }, | ||
| [key, eventName], | ||
| ); | ||
|
|
||
| return [value, write]; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.