refactor(hooks): move import polling and settings load into hooks

useImportPolling and useSettingsLoad take the effects out of ImportView
and SettingsPage, line for line. useSettingsLoad no longer sets state
after unmount. Tests for both hooks. Issue #6.
This commit is contained in:
lobo committed 2026-10-05 15:37:16 +02:00
1 parent de199a20b7
commit 2b20b389b7
6 files changed
+318 -93

No files matched your search

+3 -23
View File
@@ -13,8 +13,7 @@ import PreferencesCard from '../components/PreferencesCard.jsx';
import ProfileCard from '../components/ProfileCard.jsx';
import SteamAccountCard from '../components/SteamAccountCard.jsx';
import {useCurrentUserContext} from '../context/CurrentUserContext.jsx';
import {refreshSession} from '../utils/api.js';
import {markSession} from '../utils/auth.js';
import useSettingsLoad from '../hooks/useSettingsLoad.js';
// The `steam` query parameter Auth adds when "Sign in through Steam" sends the user back.
const STEAM_LINK_TOASTS = {
@@ -26,14 +25,11 @@ const STEAM_LINK_TOASTS = {
export default function SettingsPage() {
const {t} = useTranslation();
const {user: profile, updateUser: updateProfile, reload: reloadUser} = useCurrentUserContext();
const [isLoading, setIsLoading] = useState(true);
const [loadError, setLoadError] = useState(null);
// From this page's own load: the context user can still be empty when the form mounts.
const [displayName, setDisplayName] = useState('');
const {user: profile, updateUser: updateProfile} = useCurrentUserContext();
const [searchParams, setSearchParams] = useSearchParams();
// Read once: removing the parameter below must not load the profile again.
const [steamOutcome] = useState(() => searchParams.get('steam'));
const {isLoading, loadError, displayName} = useSettingsLoad(steamOutcome);
// A language change gives a new `t` and runs the effect again; the toast shows once.
const steamToastShown = useRef(false);
@@ -48,22 +44,6 @@ export default function SettingsPage() {
}, {replace: true});
}, [steamOutcome, setSearchParams, t]);
useEffect(() => {
const load = async () => {
if (steamOutcome === 'linked') {
// Refresh reads the new Steam ID from the database, so Core gets it in the token at once.
await refreshSession().catch(() => false);
markSession();
}
const {ok, status, data} = await reloadUser();
if (ok) setDisplayName(data.displayName ?? '');
else if (status !== 401) setLoadError(status === 0 ? 'INFO.ERROR.NETWORK_FAILURE' : 'INFO.ERROR.LOAD_PROFILE');
setIsLoading(false);
};
load();
}, [reloadUser, steamOutcome]);
if (isLoading) {
return (
<div className="w-full max-w-2xl mx-auto p-4 md:p-8 space-y-6">