Remove useMemo/useCallback across the web UI

This commit is contained in:
proddy
2026-04-27 13:24:07 +02:00
parent e39af36589
commit 1a880f14a0
53 changed files with 1940 additions and 2594 deletions

View File

@@ -1,34 +1,27 @@
import { useEffect, useMemo, useState } from 'react';
import { useEffect, useState } from 'react';
export const usePersistState = <T>(
initial_value: T,
id: string
): [T, (new_state: T) => void] => {
// Set initial value - only computed once on mount
const _initial_value = useMemo(() => {
const [state, setState] = useState<T>(() => {
try {
const local_storage_value_str = localStorage.getItem(`state:${id}`);
// If there is a value stored in localStorage, use that
if (local_storage_value_str) {
return JSON.parse(local_storage_value_str) as T;
const stored = localStorage.getItem(`state:${id}`);
if (stored) {
return JSON.parse(stored) as T;
}
} catch (error) {
// If parsing fails, fall back to initial_value
console.warn(
`Failed to parse localStorage value for key "state:${id}"`,
error
);
}
// Otherwise use initial_value that was passed to the function
return initial_value;
}, [id]); // initial_value intentionally omitted - only read on first mount
const [state, setState] = useState(_initial_value);
});
useEffect(() => {
try {
const state_str = JSON.stringify(state);
localStorage.setItem(`state:${id}`, state_str);
localStorage.setItem(`state:${id}`, JSON.stringify(state));
} catch (error) {
console.warn(
`Failed to save state to localStorage for key "state:${id}"`,