chore: initial import
This commit is contained in:
15
apps/visualizer/src/app/hooks/useInterval.ts
Normal file
15
apps/visualizer/src/app/hooks/useInterval.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
export function useInterval(callback: () => void, delayMs: number | null) {
|
||||
const cbRef = useRef(callback);
|
||||
cbRef.current = callback;
|
||||
|
||||
useEffect(() => {
|
||||
if (delayMs == null) return;
|
||||
if (!Number.isFinite(delayMs) || delayMs < 0) return;
|
||||
|
||||
const id = window.setInterval(() => cbRef.current(), delayMs);
|
||||
return () => window.clearInterval(id);
|
||||
}, [delayMs]);
|
||||
}
|
||||
|
||||
24
apps/visualizer/src/app/hooks/useLocalStorageState.ts
Normal file
24
apps/visualizer/src/app/hooks/useLocalStorageState.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export function useLocalStorageState<T>(key: string, initialValue: T) {
|
||||
const [value, setValue] = useState<T>(() => {
|
||||
try {
|
||||
const raw = window.localStorage.getItem(key);
|
||||
if (raw == null) return initialValue;
|
||||
return JSON.parse(raw) as T;
|
||||
} catch {
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
window.localStorage.setItem(key, JSON.stringify(value));
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}, [key, value]);
|
||||
|
||||
return [value, setValue] as const;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user