mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-16 07:56:22 -05:00
* Initial * Faster user page * Remove redundant function * Favorite badge sorting * Upgrade deps * Simplify entry.server * Bun tests initial * Update package.json npm -> bun * Update README * Type safe translations again * Don't load streams info for finalized tournaments * Translations as an object * More unit test work * Convert match.server.test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * test * Test & all done * Working cf * Bun GA try * No cache * spacing * spacing 2 * Add SQL logging * Remove NR * Hmm * Hmm 2 * Interesting * SKALOP_SYSTEM_MESSAGE_URL * . * . * ? * . * ? * Server.ts adjust * Downgrade Tldraw * E2E test fix * Fix lint
34 lines
813 B
TypeScript
34 lines
813 B
TypeScript
import * as React from "react";
|
|
|
|
// TODO: fix causes memory leak
|
|
/** @link https://stackoverflow.com/a/64983274 */
|
|
export const useTimeoutState = <T>(
|
|
defaultState: T,
|
|
): [
|
|
T,
|
|
(action: React.SetStateAction<T>, opts?: { timeout: number }) => void,
|
|
] => {
|
|
const [state, _setState] = React.useState<T>(defaultState);
|
|
const [currentTimeoutId, setCurrentTimeoutId] = React.useState<
|
|
Timer | undefined
|
|
>();
|
|
|
|
const setState = React.useCallback(
|
|
(action: React.SetStateAction<T>, opts?: { timeout: number }) => {
|
|
if (currentTimeoutId != null) {
|
|
clearTimeout(currentTimeoutId);
|
|
}
|
|
|
|
_setState(action);
|
|
|
|
const id = setTimeout(
|
|
() => _setState(defaultState),
|
|
opts?.timeout ?? 4000,
|
|
);
|
|
setCurrentTimeoutId(id);
|
|
},
|
|
[currentTimeoutId, defaultState],
|
|
);
|
|
return [state, setState];
|
|
};
|