first commit
This commit is contained in:
28
src/app/providers/QueryProvider.tsx
Normal file
28
src/app/providers/QueryProvider.tsx
Normal file
@@ -0,0 +1,28 @@
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
staleTime: 1000 * 60 * 5, // 5 min
|
||||
gcTime: 1000 * 60 * 10, // 10 min
|
||||
retry: 1,
|
||||
refetchOnWindowFocus: false,
|
||||
},
|
||||
mutations: {
|
||||
retry: 0,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
interface QueryProviderProps {
|
||||
children: ReactNode
|
||||
}
|
||||
|
||||
export function QueryProvider({ children }: QueryProviderProps) {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
{children}
|
||||
</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
73
src/app/router/index.tsx
Normal file
73
src/app/router/index.tsx
Normal file
@@ -0,0 +1,73 @@
|
||||
import { lazy, Suspense } from 'react'
|
||||
import { createBrowserRouter, Navigate, Outlet } from 'react-router-dom'
|
||||
import { useAuthStore } from '../store/authStore'
|
||||
import { Spinner } from '@/components/ui/spinner'
|
||||
|
||||
const LoginPage = lazy(() => import('@/pages/login'))
|
||||
const DashboardPage = lazy(() => import('@/pages/dashboard'))
|
||||
|
||||
|
||||
function AuthGuard() {
|
||||
const isAuthenticated = useAuthStore((s) => s.isAuthenticated)
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/login" replace />
|
||||
}
|
||||
|
||||
return <Outlet />
|
||||
}
|
||||
|
||||
function PublicGuard() {
|
||||
const isAuthenticated = useAuthStore((s) => s.isAuthenticated)
|
||||
|
||||
if (isAuthenticated) {
|
||||
return <Navigate to="/dashboard" replace />
|
||||
}
|
||||
|
||||
return <Outlet />
|
||||
}
|
||||
|
||||
function PageLoader() {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-[var(--bg-base)]">
|
||||
<Spinner className="size-8 text-[var(--brand-500)]" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const router = createBrowserRouter([
|
||||
{
|
||||
element: <PublicGuard />,
|
||||
children: [
|
||||
{
|
||||
path: '/login',
|
||||
element: (
|
||||
<Suspense fallback={<PageLoader />}>
|
||||
<LoginPage />
|
||||
</Suspense>
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
element: <AuthGuard />,
|
||||
children: [
|
||||
{
|
||||
path: '/dashboard',
|
||||
element: (
|
||||
<Suspense fallback={<PageLoader />}>
|
||||
<DashboardPage />
|
||||
</Suspense>
|
||||
),
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
element: <Navigate to="/dashboard" replace />,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '*',
|
||||
element: <Navigate to="/" replace />,
|
||||
},
|
||||
])
|
||||
35
src/app/store/authStore.ts
Normal file
35
src/app/store/authStore.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { create } from 'zustand'
|
||||
import { persist } from 'zustand/middleware'
|
||||
|
||||
export interface AuthUser {
|
||||
id: string
|
||||
name: string
|
||||
email: string
|
||||
role: 'admin' | 'manager' | 'viewer'
|
||||
avatar?: string
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
token: string | null
|
||||
user: AuthUser | null
|
||||
isAuthenticated: boolean
|
||||
setAuth: (token: string, user: AuthUser) => void
|
||||
clearAuth: () => void
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
token: null,
|
||||
user: null,
|
||||
isAuthenticated: false,
|
||||
setAuth: (token, user) =>
|
||||
set({ token, user, isAuthenticated: true }),
|
||||
clearAuth: () =>
|
||||
set({ token: null, user: null, isAuthenticated: false }),
|
||||
}),
|
||||
{
|
||||
name: 'tbbank-auth',
|
||||
},
|
||||
),
|
||||
)
|
||||
23
src/app/store/i18nStore.ts
Normal file
23
src/app/store/i18nStore.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { create } from 'zustand'
|
||||
import { persist } from 'zustand/middleware'
|
||||
import i18n, { type SupportedLanguage } from '@/lib/i18n'
|
||||
|
||||
interface I18nState {
|
||||
language: SupportedLanguage
|
||||
setLanguage: (lang: SupportedLanguage) => void
|
||||
}
|
||||
|
||||
export const useI18nStore = create<I18nState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
language: (localStorage.getItem('tbbank-lang') as SupportedLanguage) ?? 'en',
|
||||
setLanguage: (language) => {
|
||||
i18n.changeLanguage(language)
|
||||
set({ language })
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'tbbank-lang',
|
||||
},
|
||||
),
|
||||
)
|
||||
42
src/app/store/themeStore.ts
Normal file
42
src/app/store/themeStore.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { create } from 'zustand'
|
||||
import { persist } from 'zustand/middleware'
|
||||
|
||||
type Theme = 'light' | 'dark'
|
||||
|
||||
interface ThemeState {
|
||||
theme: Theme
|
||||
setTheme: (theme: Theme) => void
|
||||
toggleTheme: () => void
|
||||
}
|
||||
|
||||
function applyTheme(theme: Theme) {
|
||||
const root = document.documentElement
|
||||
if (theme === 'dark') {
|
||||
root.classList.add('dark')
|
||||
} else {
|
||||
root.classList.remove('dark')
|
||||
}
|
||||
}
|
||||
|
||||
export const useThemeStore = create<ThemeState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
theme: 'light',
|
||||
setTheme: (theme) => {
|
||||
applyTheme(theme)
|
||||
set({ theme })
|
||||
},
|
||||
toggleTheme: () => {
|
||||
const next = get().theme === 'light' ? 'dark' : 'light'
|
||||
applyTheme(next)
|
||||
set({ theme: next })
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'tbbank-theme',
|
||||
onRehydrateStorage: () => (state) => {
|
||||
if (state) applyTheme(state.theme)
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user