feat : add chessCom games import

This commit is contained in:
GuillaumeSD
2024-02-27 03:32:46 +01:00
parent b2b80b1cc1
commit 13a4bc06b6
13 changed files with 270 additions and 30 deletions

View File

@@ -0,0 +1,29 @@
import { PrimitiveAtom, SetStateAction, useAtom } from "jotai";
import { useEffect, useState } from "react";
export function useAtomLocalStorage<T>(
key: string,
atom: PrimitiveAtom<T>
): [T, (value: SetStateAction<T>) => void] {
const [keyTemp, setKeyTemp] = useState("");
const [storedValue, setStoredValue] = useAtom(atom);
useEffect(() => {
const item = window.localStorage.getItem(key);
if (item) {
setStoredValue(parseJSON<T>(item));
}
setKeyTemp(key);
}, [key, setStoredValue]);
useEffect(() => {
if (keyTemp !== key) return;
window.localStorage.setItem(key, JSON.stringify(storedValue));
}, [key, keyTemp, storedValue]);
return [storedValue, setStoredValue];
}
function parseJSON<T>(value: string): T {
return value === "undefined" ? undefined : JSON.parse(value);
}