From 057b62f57494442fb8371b2a58d90e71086eb6cd Mon Sep 17 00:00:00 2001 From: Maik Date: Sat, 18 May 2024 13:40:02 +0200 Subject: [PATCH] refactor(frontend): Use default as current profile on startup if possible --- .../dashboard/components/header/profile-nav.tsx | 15 +++++++-------- .../dashboard/components/sidebar/side-nav.tsx | 2 +- .../dashboard/components/sidebar/sidebar.tsx | 2 +- .../dashboard/stores/profile-set-store.ts | 16 ---------------- .../{hooks => stores}/use-current-profile.tsx | 0 .../dashboard/{hooks => stores}/use-sidebar.tsx | 0 6 files changed, 9 insertions(+), 26 deletions(-) delete mode 100644 src/sections/dashboard/stores/profile-set-store.ts rename src/sections/dashboard/{hooks => stores}/use-current-profile.tsx (100%) rename src/sections/dashboard/{hooks => stores}/use-sidebar.tsx (100%) diff --git a/src/sections/dashboard/components/header/profile-nav.tsx b/src/sections/dashboard/components/header/profile-nav.tsx index 1e126fc..2df3973 100644 --- a/src/sections/dashboard/components/header/profile-nav.tsx +++ b/src/sections/dashboard/components/header/profile-nav.tsx @@ -12,7 +12,7 @@ import { Avatar, AvatarFallback } from '@/components/ui/avatar'; import React, { useEffect } from 'react'; import { ChevronDown, ChevronUp } from 'lucide-react'; import { Profile } from '@/modules/profiles/domain'; -import { useCurrentProfile } from '@/sections/dashboard/hooks/use-current-profile'; +import { useCurrentProfile } from '@/sections/dashboard/stores/use-current-profile'; import { useProfileSet } from '@/sections/dashboard/hooks/use-profile-set'; interface ProfileNavItemProps { @@ -61,16 +61,15 @@ export function ProfileNav() { const [open, setOpen] = React.useState(false); const { profileSet, error, isLoading } = useProfileSet(); const { current, setCurrent } = useCurrentProfile(); - // const [profileSet, setProfileSet] = useState(); useEffect(() => { if (!isLoading) { - // const parsed: ProfileSet = profileSetSchema.parse(data); - // setProfileSet(parsed); - - // const initialProfile: Profile = parsed.profiles[0]; - const initialProfile: Profile = profileSet!.profiles[0]; - // setCurrent(initialProfile.name); + const defaultProfile = profileSet!.profiles.find( + (value: Profile) => value.name === 'default' + ); + const initialProfile: Profile = defaultProfile + ? defaultProfile + : profileSet!.profiles[0]; setCurrent(initialProfile.name); } }, [profileSet, error, isLoading, setCurrent]); diff --git a/src/sections/dashboard/components/sidebar/side-nav.tsx b/src/sections/dashboard/components/sidebar/side-nav.tsx index 60a54f7..1de6307 100644 --- a/src/sections/dashboard/components/sidebar/side-nav.tsx +++ b/src/sections/dashboard/components/sidebar/side-nav.tsx @@ -12,7 +12,7 @@ import { AccordionItem, AccordionTrigger, } from '@/sections/dashboard/components/sidebar/subnav-accordion'; -import { useSidebar } from '@/sections/dashboard/hooks/use-sidebar'; +import { useSidebar } from '@/sections/dashboard/stores/use-sidebar'; import { type LucideIcon } from 'lucide-react'; diff --git a/src/sections/dashboard/components/sidebar/sidebar.tsx b/src/sections/dashboard/components/sidebar/sidebar.tsx index 50bb5be..b3dfcc2 100644 --- a/src/sections/dashboard/components/sidebar/sidebar.tsx +++ b/src/sections/dashboard/components/sidebar/sidebar.tsx @@ -6,7 +6,7 @@ import { cn } from '@/lib/css-utils'; import { Separator } from '@/components/ui/separator'; import { Button } from '@/components/ui/button'; import { ChevronRight, Settings } from 'lucide-react'; -import { useSidebar } from '@/sections/dashboard/hooks/use-sidebar'; +import { useSidebar } from '@/sections/dashboard/stores/use-sidebar'; import { NavItem, SideNav, diff --git a/src/sections/dashboard/stores/profile-set-store.ts b/src/sections/dashboard/stores/profile-set-store.ts deleted file mode 100644 index 94749db..0000000 --- a/src/sections/dashboard/stores/profile-set-store.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { create } from 'zustand'; -import { ProfileSet } from '@/modules/profiles/domain'; - -interface ProfileSetStore { - profileSet: ProfileSet; - setProfileSet: (profiles: ProfileSet) => void; - error?: Error; - setError: (error: Error) => void; -} - -export const useProfileSetStore = create((set) => ({ - profileSet: { profiles: [] }, - setProfileSet: (profileSet) => set(() => ({ profileSet: profileSet })), - error: undefined, - setError: (error) => set(() => ({ error: error })), -})); diff --git a/src/sections/dashboard/hooks/use-current-profile.tsx b/src/sections/dashboard/stores/use-current-profile.tsx similarity index 100% rename from src/sections/dashboard/hooks/use-current-profile.tsx rename to src/sections/dashboard/stores/use-current-profile.tsx diff --git a/src/sections/dashboard/hooks/use-sidebar.tsx b/src/sections/dashboard/stores/use-sidebar.tsx similarity index 100% rename from src/sections/dashboard/hooks/use-sidebar.tsx rename to src/sections/dashboard/stores/use-sidebar.tsx