Skip to content

Commit

Permalink
Issue/62 (#166)
Browse files Browse the repository at this point in the history
* Updated to address issue 62 -- connecting the theme switch UI to their user setting in the db via the sublinks client api

* Updated to address issue 62 load the initial theme value from the DB if present

* Fix eslint issues

* Updated layout to check for initial theme

* Addressed type issues
  • Loading branch information
chasingtheflow authored Jun 11, 2024
1 parent 9389522 commit 9e2d695
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const RootLayout = async ({
children: React.ReactNode
}) => {
const site = await getSite();
const initialTheme = site?.my_user?.local_user_view?.local_user?.theme ?? 'dark';
const theme = {
menu: {
defaultProps: {
Expand All @@ -71,7 +72,7 @@ const RootLayout = async ({
return (
<UserProvider>
<ThemeProvider value={theme}>
<html lang="en" className="h-full dark">
<html lang="en" className={`h-full ${initialTheme}`}>
<body className={cx(inter.className, 'flex flex-col min-h-full bg-secondary dark:bg-secondary-dark')}>
<Header site={site} />
<BottomNav site={site} />
Expand Down
23 changes: 12 additions & 11 deletions src/components/user-nav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import {
import { MyUserInfo } from 'sublinks-js-client';

import { UserContext } from '@/context/user';
import { useTheme } from '@/hooks/use-theme';
import { Theme, useTheme } from '@/hooks/use-theme';
import { useClickOutside } from '@/hooks/use-click-outside';
import ThemeSwitch from '../theme-switch';
import { LinkText } from '../text';
import Icon, { ICON_SIZE } from '../icon';

const ProfileMenu = ({ myUser }: { myUser: MyUserInfo }) => {
const [theme, setTheme] = useTheme();
const initialTheme = myUser?.local_user_view?.local_user?.theme as Theme;
const [theme, setTheme] = useTheme(initialTheme);
const [open, setOpen] = useState<boolean>(false);

const ref = useClickOutside<HTMLUListElement>(() => open && setOpen(false));
Expand Down Expand Up @@ -64,21 +65,21 @@ const ProfileMenu = ({ myUser }: { myUser: MyUserInfo }) => {
return (
<div className="flex justify-center items-center">
{false && ( // @todo: Change to check if a user is logged in
<Link href="/inbox">
<Icon IconType={BellIcon} size={ICON_SIZE.SMALL} title="Inbox icon" isInteractable />
</Link>
<Link href="/inbox">
<Icon IconType={BellIcon} size={ICON_SIZE.SMALL} title="Inbox icon" isInteractable />
</Link>
)}

{false && ( // @todo: Change to check if a user is a mod or an admin
<Link href="/reports">
<Icon IconType={ShieldExclamationIcon} size={ICON_SIZE.SMALL} title="Reports icon" isInteractable />
</Link>
<Link href="/reports">
<Icon IconType={ShieldExclamationIcon} size={ICON_SIZE.SMALL} title="Reports icon" isInteractable />
</Link>
)}

{false && ( // @todo: Change to check if applications are enabled and the user is an admin
<Link href="/registration_applications">
<Icon IconType={ClipboardIcon} size={ICON_SIZE.SMALL} title="Registration applications icon" isInteractable />
</Link>
<Link href="/registration_applications">
<Icon IconType={ClipboardIcon} size={ICON_SIZE.SMALL} title="Registration applications icon" isInteractable />
</Link>
)}

<Menu
Expand Down
17 changes: 15 additions & 2 deletions src/hooks/use-theme.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
'use client';

import { useLocalStorage } from '@/hooks/use-local-storage';
import { handleSaveUserSettings } from '@/utils/settings';

export function useTheme() {
return useLocalStorage('theme', typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
export type Theme = 'light' | 'dark';

export function useTheme(initialTheme: Theme): [Theme, (theme: Theme) => void] {
const originalTheme = initialTheme ?? (typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');

const [theme, setTheme] = useLocalStorage('theme', originalTheme);

const saveTheme = async (newTheme: Theme) => {
setTheme(newTheme);

await handleSaveUserSettings({ theme: newTheme });
};

return [theme, saveTheme];
}
12 changes: 12 additions & 0 deletions src/utils/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SaveUserSettings } from 'sublinks-js-client';

import SublinksApi from './api-client/client';
import logger from './logger';

export const handleSaveUserSettings = async (userSettings: SaveUserSettings) => {
try {
await SublinksApi.Instance().Client().saveUserSettings(userSettings);
} catch (e) {
logger.error(`Failed to save user settings ${userSettings}`, e);
}
};

0 comments on commit 9e2d695

Please sign in to comment.