-
Notifications
You must be signed in to change notification settings - Fork 6k
feat: Persist user preferences in database #3746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ceviixx
wants to merge
4
commits into
umami-software:master
Choose a base branch
from
ceviixx:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+274
−17
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "user" ADD COLUMN "date_range" VARCHAR(50), | ||
| ADD COLUMN "timezone" VARCHAR(100), | ||
| ADD COLUMN "language" VARCHAR(10), | ||
| ADD COLUMN "theme" VARCHAR(20); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,41 @@ | ||
| import { Row, Button, Icon, useTheme } from '@umami/react-zen'; | ||
| import { useMessages, usePreferences } from '@/components/hooks'; | ||
| import { Sun, Moon } from '@/components/icons'; | ||
|
|
||
| export function ThemeSetting() { | ||
| const { theme, setTheme } = useTheme(); | ||
| const { formatMessage, labels } = useMessages(); | ||
| const { updatePreferences } = usePreferences(); | ||
|
|
||
| const handleChange = (value: 'light' | 'dark') => { | ||
| setTheme(value); | ||
| updatePreferences({ theme: value }); | ||
| }; | ||
|
|
||
| const handleReset = () => { | ||
| setTheme('light'); | ||
| updatePreferences({ theme: null }); | ||
| }; | ||
ceviixx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <Row gap> | ||
| <Button variant={theme === 'light' ? 'primary' : undefined} onPress={() => setTheme('light')}> | ||
| <Button | ||
| variant={theme === 'light' ? 'primary' : undefined} | ||
| onPress={() => handleChange('light')} | ||
| > | ||
| <Icon> | ||
| <Sun /> | ||
| </Icon> | ||
| </Button> | ||
| <Button variant={theme === 'dark' ? 'primary' : undefined} onPress={() => setTheme('dark')}> | ||
| <Button | ||
| variant={theme === 'dark' ? 'primary' : undefined} | ||
| onPress={() => handleChange('dark')} | ||
| > | ||
| <Icon> | ||
| <Moon /> | ||
| </Icon> | ||
| </Button> | ||
| <Button onPress={handleReset}>{formatMessage(labels.reset)}</Button> | ||
| </Row> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { z } from 'zod'; | ||
| import { canUpdateUser, canViewUser } from '@/permissions'; | ||
| import { getUserPreferences, updateUserPreferences } from '@/queries/prisma'; | ||
| import { json, unauthorized } from '@/lib/response'; | ||
| import { parseRequest } from '@/lib/request'; | ||
|
|
||
| export async function GET(request: Request, { params }: { params: Promise<{ userId: string }> }) { | ||
| const { auth, error } = await parseRequest(request); | ||
|
|
||
| if (error) { | ||
| return error(); | ||
| } | ||
|
|
||
| const { userId } = await params; | ||
|
|
||
| if (!(await canViewUser(auth, userId))) { | ||
| return unauthorized(); | ||
| } | ||
|
|
||
| const preferences = await getUserPreferences(userId); | ||
|
|
||
| return json(preferences); | ||
| } | ||
|
|
||
| export async function POST(request: Request, { params }: { params: Promise<{ userId: string }> }) { | ||
| const schema = z.object({ | ||
| dateRange: z.string().max(50).nullable().optional(), | ||
| timezone: z.string().max(100).nullable().optional(), | ||
| language: z.string().max(10).nullable().optional(), | ||
| theme: z.string().max(20).nullable().optional(), | ||
| }); | ||
|
|
||
| const { auth, body, error } = await parseRequest(request, schema); | ||
|
|
||
| if (error) { | ||
| return error(); | ||
| } | ||
|
|
||
| const { userId } = await params; | ||
|
|
||
| if (!(await canUpdateUser(auth, userId))) { | ||
| return unauthorized(); | ||
| } | ||
|
|
||
| const data = Object.fromEntries(Object.entries(body).filter(([, value]) => value !== undefined)); | ||
|
|
||
| const preferences = await updateUserPreferences(userId, data); | ||
|
|
||
| return json(preferences); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,39 @@ | ||
| import { useEffect } from 'react'; | ||
| import { useTheme } from '@umami/react-zen'; | ||
| import { useApp, setUser } from '@/store/app'; | ||
| import { useApi } from '../useApi'; | ||
| import { setClientPreferences } from '@/lib/client'; | ||
| import { DEFAULT_THEME } from '@/lib/constants'; | ||
|
|
||
| const selector = (state: { user: any }) => state.user; | ||
|
|
||
| export function useLoginQuery() { | ||
| const { post, useQuery } = useApi(); | ||
| const user = useApp(selector); | ||
| const { setTheme } = useTheme(); | ||
|
|
||
| const query = useQuery({ | ||
| queryKey: ['login'], | ||
| queryFn: async () => { | ||
| const data = await post('/auth/verify'); | ||
|
|
||
| if (data.preferences) { | ||
| setClientPreferences(data.preferences); | ||
| } | ||
|
|
||
| setUser(data); | ||
|
|
||
| return data; | ||
| }, | ||
| enabled: !user, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (query.data?.preferences !== undefined) { | ||
| const themeValue = query.data.preferences.theme || DEFAULT_THEME; | ||
| setTheme(themeValue); | ||
| } | ||
| }, [query.data, setTheme]); | ||
|
|
||
| return { user, setUser, ...query }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { useApi } from './useApi'; | ||
| import { useApp } from '@/store/app'; | ||
|
|
||
| const userSelector = (state: { user: any }) => state.user; | ||
ceviixx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| export function usePreferences() { | ||
| const { post } = useApi(); | ||
| const user = useApp(userSelector); | ||
|
|
||
| const updatePreferences = async (preferences: { | ||
| dateRange?: string | null; | ||
| timezone?: string | null; | ||
| language?: string | null; | ||
| theme?: string | null; | ||
| }) => { | ||
| if (!user?.id) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await post(`/users/${user.id}/preferences`, preferences); | ||
| } catch { | ||
| // Silent fail: sync next login | ||
| } | ||
| }; | ||
|
|
||
| return { updatePreferences }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.