Fix color scheme update (#1298)

* Fix color scheme update

* remove as

* fix
This commit is contained in:
Weiko 2023-08-24 16:34:46 +02:00 committed by GitHub
parent b0c2881ec0
commit 00f1d2b739
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,22 +1,17 @@
import { useCallback, useMemo } from 'react';
import { useCallback } from 'react';
import { useRecoilState } from 'recoil';
import { currentUserState } from '@/auth/states/currentUserState';
import { ColorScheme, useUpdateUserMutation } from '~/generated/graphql';
export function useColorScheme() {
const [currentUser] = useRecoilState(currentUserState);
const [currentUser, setCurrentUser] = useRecoilState(currentUserState);
const [updateUser] = useUpdateUserMutation();
const colorScheme = useMemo(() => {
if (!currentUser?.settings?.colorScheme) {
// Use system color scheme if user is not logged in or has no settings
return ColorScheme.System;
}
return currentUser.settings.colorScheme;
}, [currentUser?.settings?.colorScheme]);
const colorScheme = !currentUser?.settings?.colorScheme
? ColorScheme.System
: currentUser.settings.colorScheme;
const setColorScheme = useCallback(
async (value: ColorScheme) => {
@ -50,6 +45,17 @@ export function useColorScheme() {
},
}
: undefined,
update: (_cache, { data }) => {
if (data?.updateUser && currentUser) {
setCurrentUser({
...currentUser,
settings: {
...currentUser.settings,
colorScheme: data?.updateUser.settings.colorScheme,
},
});
}
},
});
if (!result.data || result.errors) {
@ -57,7 +63,7 @@ export function useColorScheme() {
}
} catch (err) {}
},
[currentUser, updateUser],
[currentUser, updateUser, setCurrentUser],
);
return {