Skip to content

Commit 1714fa1

Browse files
authored
Merge pull request #15 from PriDebnath/feat/zustand
Feat/zustand
2 parents 36d2633 + 6b1a549 commit 1714fa1

12 files changed

Lines changed: 188 additions & 67 deletions

‎frontend/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"react-resizable": "^3.1.3",
4242
"tailwind-merge": "^3.4.0",
4343
"tailwindcss": "^4.1.18",
44-
"vaul": "^1.1.2"
44+
"vaul": "^1.1.2",
45+
"zustand": "^5.0.10"
4546
},
4647
"devDependencies": {
4748
"@eslint/js": "^9.39.1",

‎frontend/src/feature/quote/card/quote.card.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Check, Copy, Maximize2, PenIcon, Trash, Save, CircleArrowDown, LoaderCi
1010
import { useState, useRef } from "react";
1111
import { toPng } from "html-to-image"
1212
import { ListTags } from "@/feature/quote/list.tags";
13-
import { useShowCardInfo } from "@/hook/use-show-card-info.hook";
13+
import { useShowCardInfo } from "@/store/use-card-info.store";
1414

1515
interface Props {
1616
quote: QuoteDetails;
@@ -23,11 +23,12 @@ const QuoteCard = (props: Props) => {
2323
const [copying, setCopying] = useState(false)
2424
const [downloading, setDownloading] = useState(false)
2525
const { buildStyle } = useBackground()
26-
const { info: infoType, stroredCardInfoTyle } = useShowCardInfo()
26+
const { info: infoType } = useShowCardInfo()
2727

2828
const noteRef = useRef<HTMLDivElement>(null)
2929

3030
const cardStyle = buildStyle(quote.texture!, quote.pri_set!)
31+
3132
const exportAsImage = async () => {
3233
if (!noteRef.current) return
3334
setDownloading(true)

‎frontend/src/feature/quote/dialog/setting.dialog.tsx‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ import {
1919
SelectTrigger,
2020
} from "@/components/ui/select"
2121
import { useState } from "react";
22-
import { themeModes, useTheme, type ThemeMode } from '@/hook/use-dark-or-light-theme.hook'
22+
import { themeModes, type ThemeMode } from '@/hook/use-dark-or-light-theme.hook'
2323
import { ArrowLeftIcon, CircleArrowDown, CircleCheckBig, Copy, Images, LoaderCircle, Save, Share, Settings } from "lucide-react";
24-
import { colorThemes, useColorTheme, type ColorTheme } from "@/hook/use-color-theme.hook";
25-
import { showInfo, useShowCardInfo, type ShowInfo } from "@/hook/use-show-card-info.hook";
24+
import { colorThemes, type ColorTheme } from "@/hook/use-color-theme.hook";
25+
import { useColorThemeStore} from "@/store/use-color-theme.store";
26+
import { useThemeStore} from "@/store/use-theme.store";
27+
import { useFontStore, fonts} from "@/store/use-font.store";
28+
import { showInfo, useShowCardInfo, type ShowInfo } from "@/store/use-card-info.store";
2629
import { capitalize } from "@/helper/capitalize";
2730

2831
interface SortOptions { key: SortOption, label: string }
@@ -42,10 +45,11 @@ interface Props {
4245
}
4346

4447
export function SettingComponent(props: Props) {
45-
const { theme, setTheme } = useTheme()
46-
const [open, setOpen] = useState(false)
47-
const { colorTheme, setColorTheme } = useColorTheme()
48+
const { theme, setTheme, isDark } = useThemeStore()
49+
const [open, setOpen] = useState(false)
50+
const { colorTheme, setColorTheme } = useColorThemeStore()
4851
const { info, setInfo } = useShowCardInfo()
52+
const { font, setFont } = useFontStore()
4953

5054
const [sortBy, setSortBy] = useState<SortOptions["key"]>("created_at")
5155

@@ -116,18 +120,20 @@ export function SettingComponent(props: Props) {
116120
</Select>
117121
</div>
118122
<div className="flex justify-between items-center ">
119-
Sort by
120-
<Select value={sortBy} onValueChange={(value) => setSortBy(value as SortOption)}>
123+
Font
124+
<Select value={font} onValueChange={(value) => setFont(value )}>
121125
<SelectTrigger className="">
122-
<SelectValue placeholder="Sort By" />
126+
<SelectValue placeholder="Font" />
123127
</SelectTrigger>
124128
<SelectContent>
125129
{
126-
sortOptions.map((pri) => {
127-
let yo = capitalize(pri.key)
130+
fonts.map((pri) => {
131+
let yoyoyo = pri.replace("font-", "")
132+
let yoyo = yoyoyo.replaceAll("-", " ")
133+
let yo = capitalize(yoyo)
128134
return (
129-
<SelectItem key={pri.key} value={pri.key} className="">
130-
{yo.replaceAll('_', " ")}
135+
<SelectItem key={pri} value={pri} className="">
136+
{yo}
131137
</SelectItem>
132138
)
133139
})
@@ -161,6 +167,27 @@ export function SettingComponent(props: Props) {
161167
</SelectContent>
162168
</Select>
163169
</div>
170+
<div className="flex justify-between items-center ">
171+
Sort by
172+
<Select value={sortBy} onValueChange={(value) => setSortBy(value as SortOption)}>
173+
<SelectTrigger className="">
174+
<SelectValue placeholder="Sort By" />
175+
</SelectTrigger>
176+
<SelectContent>
177+
{
178+
sortOptions.map((pri) => {
179+
let yo = capitalize(pri.key)
180+
return (
181+
<SelectItem key={pri.key} value={pri.key} className="">
182+
{yo.replaceAll('_', " ")}
183+
</SelectItem>
184+
)
185+
})
186+
}
187+
</SelectContent>
188+
</Select>
189+
</div>
190+
164191
</div>
165192

166193
<Separator className="bg-border" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useEffect, useState } from "react"
2+
import { useLocalStorage } from "@/hook/use-localstroage.hook"
3+
import { create } from "zustand"
4+
import { persist } from "zustand/middleware"
5+
import { useFontStore, fonts } from "@/store/use-font.store"
6+
7+
8+
export const useApplyFont = () => {
9+
const font = useFontStore((s) => s.font)
10+
11+
useEffect(() => {
12+
const root = document.body
13+
for (const f of fonts) {
14+
root.classList.remove(f)
15+
}
16+
root.classList.add(font)
17+
}, [font])
18+
}
19+
20+

‎frontend/src/hook/use-color-theme.hook.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { useEffect, useState } from "react"
22
import { useLocalStorage } from "@/hook/use-localstroage.hook"
3+
import { create } from "zustand"
4+
import { persist } from "zustand/middleware"
5+
import { useColorThemeStore } from "@/store/use-color-theme.store"
36

47
export type ColorTheme = "theme-neutral"
58
| "theme-rose"
@@ -23,17 +26,16 @@ export const colorThemes: ColorTheme[] = [
2326
"theme-yellow",
2427
]
2528

26-
export const useColorTheme = () => {
27-
const [stroredColorTheme, setStroredColorTheme] = useLocalStorage<ColorTheme>("app_theme", "theme-rose")
28-
const [colorTheme, setColorTheme] = useState<ColorTheme>(stroredColorTheme as ColorTheme)
29+
export const useApplyColorTheme = () => {
30+
const colorTheme = useColorThemeStore((s) => s.colorTheme)
31+
32+
useEffect(() => {
33+
const root = document.documentElement
34+
for (const t of colorThemes) {
35+
root.classList.remove(t)
36+
}
37+
root.classList.add(colorTheme)
38+
}, [colorTheme])
39+
}
40+
2941

30-
useEffect(() => {
31-
let root = document.documentElement
32-
for (let t of colorThemes) {
33-
root.classList.remove(t)
34-
}
35-
root.classList.add(colorTheme)
36-
setStroredColorTheme(colorTheme)
37-
}, [colorTheme])
38-
return { colorTheme, setColorTheme }
39-
}
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import { useEffect, useState } from "react"
2-
import { useLocalStorage } from "@/hook/use-localstroage.hook"
31

42
export type ThemeMode = "system" | "light" | "dark"
53

64
export const themeModes = ["system" , "light" , "dark"]
75

6+
import { useEffect } from "react"
7+
import { useThemeStore } from "@/store/use-theme.store"
8+
89
const getSystemTheme = () =>
910
window.matchMedia("(prefers-color-scheme: dark)").matches
1011

11-
export const useTheme = () => {
12-
const [ stroredTheme, setStroredTheme ]= useLocalStorage<ThemeMode>( 'app_appearance', "system")
13-
14-
const [theme, setTheme] = useState<ThemeMode>(stroredTheme as ThemeMode)
15-
const [isDark, setIsDark] = useState(false)
12+
export const useApplyTheme = () => {
13+
const { theme, setIsDark } = useThemeStore()
1614

1715
/* Resolve actual theme */
1816
useEffect(() => {
@@ -26,30 +24,23 @@ export const useTheme = () => {
2624
: getSystemTheme()
2725

2826
setIsDark(resolvedDark)
29-
setStroredTheme(theme)
3027
document.documentElement.classList.toggle("dark", resolvedDark)
3128
}, [theme])
3229

33-
/* Listen to OS changes (only when system mode) */
30+
/* Listen to OS changes (system mode only) */
3431
useEffect(() => {
3532
if (typeof window === "undefined") return
3633

34+
if (theme !== "system") return
35+
3736
const media = window.matchMedia("(prefers-color-scheme: dark)")
3837

3938
const handler = () => {
40-
if (theme === "system") {
41-
document.documentElement.classList.toggle("dark", media.matches)
42-
setIsDark(media.matches)
43-
}
39+
setIsDark(media.matches)
40+
document.documentElement.classList.toggle("dark", media.matches)
4441
}
4542

4643
media.addEventListener("change", handler)
4744
return () => media.removeEventListener("change", handler)
4845
}, [theme])
49-
50-
return {
51-
theme, // "system" | "light" | "dark"
52-
setTheme,
53-
isDark, // resolved boolean (useful)
54-
}
5546
}
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +0,0 @@
1-
import { useEffect, useState } from "react"
2-
import { useLocalStorage } from "@/hook/use-localstroage.hook"
3-
4-
export type ShowInfo = "tags" | "created_at" | "updated_at"
5-
6-
export const showInfo: ShowInfo[] = ["tags", "created_at", "updated_at"]
7-
8-
export const useShowCardInfo = () => {
9-
const [stroredCardInfoTyle, setStroredCardInfoTyle] = useLocalStorage<ShowInfo>("card_info", "updated_at")
10-
const [info, setInfo] = useState<ShowInfo>(stroredCardInfoTyle)
11-
useEffect(() => {
12-
setStroredCardInfoTyle(info)
13-
}, [info])
14-
return { info, stroredCardInfoTyle, setInfo }
15-
}

‎frontend/src/routes/__root.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import * as React from 'react'
22
import { Outlet, createRootRoute } from '@tanstack/react-router'
3-
import { useTheme } from '@/hook/use-dark-or-light-theme.hook'
4-
import { useColorTheme } from '@/hook/use-color-theme.hook'
3+
import { useApplyTheme } from '@/hook/use-dark-or-light-theme.hook'
4+
import { useApplyColorTheme} from '@/hook/use-color-theme.hook'
5+
import { useApplyFont} from '@/hook/use-apply-font.hook'
56

67
export const Route = createRootRoute({
78
component: RootComponent,
89
})
910

1011
function RootComponent() {
11-
useTheme()
12-
useColorTheme()
12+
useApplyTheme()
13+
useApplyColorTheme()
14+
useApplyFont()
1315
return (
1416
<React.Fragment>
1517
<Outlet />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { create } from "zustand"
2+
import { persist } from "zustand/middleware"
3+
4+
export type ShowInfo = "tags" | "created_at" | "updated_at"
5+
6+
export const showInfo: ShowInfo[] = ["tags", "created_at", "updated_at"]
7+
8+
type ShowCardInfoStore = {
9+
info: ShowInfo
10+
setInfo: (info: ShowInfo) => void
11+
}
12+
13+
export const useShowCardInfo = create<ShowCardInfoStore>()(
14+
persist(
15+
(set) => ({
16+
info: "updated_at",
17+
setInfo: (info) => set({ info }),
18+
}),
19+
{
20+
name: "card_info", // localStorage key
21+
}
22+
)
23+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { create } from "zustand"
2+
import { persist } from "zustand/middleware"
3+
import { colorThemes, type ColorTheme } from "@/hook/use-color-theme.hook";
4+
5+
6+
type ColorThemeStore = {
7+
colorTheme: ColorTheme
8+
setColorTheme: (theme: ColorTheme) => void
9+
}
10+
11+
export const useColorThemeStore = create<ColorThemeStore>()(
12+
persist(
13+
(set) => ({
14+
colorTheme: "theme-rose",
15+
setColorTheme: (colorTheme) => set({ colorTheme }),
16+
}),
17+
{
18+
name: "app_theme", // localStorage key
19+
}
20+
)
21+
)

0 commit comments

Comments
 (0)