add full page width preference

This commit is contained in:
Philipinho
2024-07-03 11:00:42 +01:00
parent d1ae117f76
commit 8f056d1071
10 changed files with 135 additions and 38 deletions

View File

@ -1,5 +1,16 @@
import { atomWithStorage } from "jotai/utils";
import { ICurrentUser } from "@/features/user/types/user.types";
import { focusAtom } from "jotai-optics";
export const currentUserAtom = atomWithStorage<ICurrentUser | null>("currentUser", null);
export const currentUserAtom = atomWithStorage<ICurrentUser | null>(
"currentUser",
null,
);
export const userAtom = focusAtom(currentUserAtom, (optic) =>
optic.prop("user"),
);
export const workspaceAtom = focusAtom(currentUserAtom, (optic) =>
optic.prop("workspace"),
);

View File

@ -0,0 +1,42 @@
import { Group, Text, Switch } from "@mantine/core";
import { useAtom } from "jotai/index";
import { userAtom } from "@/features/user/atoms/current-user-atom.ts";
import { updateUser } from "@/features/user/services/user-service.ts";
import React, { useState } from "react";
export default function PageWidthPref() {
return (
<Group justify="space-between" wrap="nowrap" gap="xl">
<div>
<Text size="md">Full page width</Text>
<Text size="sm" c="dimmed">
Choose your preferred page width.
</Text>
</div>
<WidthToggle />
</Group>
);
}
function WidthToggle() {
const [user, setUser] = useAtom(userAtom);
const [checked, setChecked] = useState(
user.settings?.preferences?.fullPageWidth,
);
const handleChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
const value = event.currentTarget.checked;
const updatedUser = await updateUser({ fullPageWidth: value });
setChecked(value);
setUser(updatedUser);
};
return (
<Switch
defaultChecked={checked}
onChange={handleChange}
aria-label="Toggle full page width"
/>
);
}

View File

@ -7,7 +7,7 @@ export interface IUser {
emailVerifiedAt: Date;
avatarUrl: string;
timezone: string;
settings: any;
settings: IUserSettings;
invitedById: string;
lastLoginAt: string;
lastActiveAt: Date;
@ -17,9 +17,16 @@ export interface IUser {
workspaceId: string;
deactivatedAt: Date;
deletedAt: Date;
fullPageWidth: boolean; // used for update
}
export interface ICurrentUser {
user: IUser;
workspace: IWorkspace;
}
export interface IUserSettings {
preferences: {
fullPageWidth: boolean;
};
}