feat: Implement initial Process Scheduling Simulator application with core logic, algorithms, UI components, and project setup.
This commit is contained in:
commit
083fc0027e
54 files changed
+8815
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { useState, useCallback } from "react";
|
||||
|
||||
/**
|
||||
* Generic hook for reading/writing typed values to localStorage.
|
||||
*
|
||||
* Returns [value, setValue, removeValue].
|
||||
* Read/write errors are silently ignored.
|
||||
*/
|
||||
export function useLocalStorage<T>(
|
||||
key: string,
|
||||
initialValue: T,
|
||||
): readonly [T, (value: T | ((prev: T) => T)) => void, () => void] {
|
||||
const [storedValue, setStoredValue] = useState<T>(() => {
|
||||
try {
|
||||
const item = window.localStorage.getItem(key);
|
||||
return item !== null ? (JSON.parse(item) as T) : initialValue;
|
||||
} catch {
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
|
||||
const setValue = useCallback(
|
||||
(value: T | ((prev: T) => T)) => {
|
||||
setStoredValue((prev) => {
|
||||
const next =
|
||||
typeof value === "function"
|
||||
? (value as (prev: T) => T)(prev)
|
||||
: value;
|
||||
try {
|
||||
window.localStorage.setItem(key, JSON.stringify(next));
|
||||
} catch {
|
||||
// ignore write errors (e.g. private mode quota exceeded)
|
||||
}
|
||||
return next;
|
||||
});
|
||||
},
|
||||
[key],
|
||||
);
|
||||
|
||||
const removeValue = useCallback(() => {
|
||||
try {
|
||||
window.localStorage.removeItem(key);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
setStoredValue(initialValue);
|
||||
}, [key, initialValue]);
|
||||
|
||||
return [storedValue, setValue, removeValue] as const;
|
||||
}
|
||||
Reference in new issue
Block a user