Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

refactor: :recycle: improving sidebar code

Andrej44ebe6524d5c
diff --git a/apps/web/src/components/common/sidebar/index.tsx b/apps/web/src/components/common/sidebar/index.tsx
index 844afc6a2..5de2fc151 100644
--- a/apps/web/src/components/common/sidebar/index.tsx
+++ b/apps/web/src/components/common/sidebar/index.tsx
@@ -1,65 +1,19 @@
-import useAuth from "@/components/providers/auth-provider/hooks/use-auth";
-import { cn } from "@/lib/utils";
import { motion } from "framer-motion";
-import { X } from "lucide-react";
-import { useState } from "react";
-import { Logo } from "../logo";
-import { MobileHeader } from "../mobile-header";
import { SidebarContent } from "./sidebar-content";
+import SidebarFooter from "./sidebar-footer";
import { SidebarHeader } from "./sidebar-header";
export function Sidebar() {
- const [isOpen, setIsOpen] = useState(false);
- const { user } = useAuth();
-
return (
<motion.div
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
className="w-full"
>
- <MobileHeader onMenuClick={() => setIsOpen(true)} />
-
- <div
- className={cn(
- "fixed inset-0 z-40 bg-white/80 dark:bg-zinc-950/80 backdrop-blur-sm md:hidden",
- isOpen ? "opacity-100" : "opacity-0 pointer-events-none",
- )}
- >
- <div
- className={cn(
- "fixed inset-y-0 left-0 w-64 bg-white dark:bg-zinc-900 transform transition-transform duration-200 ease-in-out",
- isOpen ? "translate-x-0" : "-translate-x-full",
- )}
- >
- <div className="flex items-center justify-between p-4 border-b border-zinc-200 dark:border-zinc-800">
- <Logo />
- <button
- type="button"
- onClick={() => setIsOpen(false)}
- className="p-2 text-zinc-500 hover:text-zinc-700 dark:text-zinc-400 dark:hover:text-zinc-300 hover:bg-zinc-100 dark:hover:bg-zinc-800 rounded-lg transition-colors"
- >
- <X className="h-5 w-5" />
- </button>
- </div>
-
- <SidebarContent
- workspaces={[]}
- currentWorkspace={"Andrej's workspace"}
- setCurrentWorkspace={() => {}}
- currentUser={user}
- />
- </div>
- </div>
-
- <div className="hidden md:flex w-64 bg-white dark:bg-zinc-900 border-r border-zinc-200 dark:border-zinc-800 h-screen flex-col">
+ <div className="flex w-64 bg-white dark:bg-zinc-900 border-r border-zinc-200 dark:border-zinc-800 h-screen flex-col">
<SidebarHeader />
- <SidebarContent
- workspaces={[]}
- currentWorkspace={"Andrej's workspace"}
- setCurrentWorkspace={() => {}}
- currentUser={user}
- />
+ <SidebarContent />
+ <SidebarFooter />
</div>
</motion.div>
);
diff --git a/apps/web/src/components/common/sidebar/sections/bottom-actions/index.tsx b/apps/web/src/components/common/sidebar/sections/bottom-actions/index.tsx
new file mode 100644
index 000000000..9c03cbd74
--- /dev/null
+++ b/apps/web/src/components/common/sidebar/sections/bottom-actions/index.tsx
@@ -0,0 +1,19 @@
+import { Settings } from "lucide-react";
+import SignOutButton from "./sign-out-button";
+
+function BottomActions() {
+ return (
+ <div className="flex gap-1">
+ <button
+ type="button"
+ className="flex-1 px-2 py-1.5 text-xs text-zinc-600 hover:bg-zinc-100 dark:text-zinc-300 dark:hover:bg-zinc-800 rounded-lg flex items-center justify-center transition-colors"
+ >
+ <Settings className="w-3 h-3 mr-1" />
+ Settings
+ </button>
+ <SignOutButton />
+ </div>
+ );
+}
+
+export default BottomActions;
diff --git a/apps/web/src/components/common/sidebar/sign-out-button.tsx b/apps/web/src/components/common/sidebar/sections/bottom-actions/sign-out-button.tsx
similarity index 100%
rename from apps/web/src/components/common/sidebar/sign-out-button.tsx
rename to apps/web/src/components/common/sidebar/sections/bottom-actions/sign-out-button.tsx
diff --git a/apps/web/src/components/common/sidebar/sections/team.tsx b/apps/web/src/components/common/sidebar/sections/team.tsx
new file mode 100644
index 000000000..d7411c2e3
--- /dev/null
+++ b/apps/web/src/components/common/sidebar/sections/team.tsx
@@ -0,0 +1,20 @@
+import { Users } from "lucide-react";
+
+function Team() {
+ return (
+ <div>
+ <h2 className="text-xs font-semibold text-zinc-500 dark:text-zinc-400 uppercase tracking-wider mb-2 px-3">
+ Team
+ </h2>
+ <button
+ type="button"
+ className="w-full text-left px-3 py-2 text-zinc-700 hover:bg-zinc-100 dark:text-zinc-300 dark:hover:bg-zinc-800 rounded-lg flex items-center transition-colors"
+ >
+ <Users className="w-4 h-4 mr-2 text-zinc-500 dark:text-zinc-400" />
+ Manage Team
+ </button>
+ </div>
+ );
+}
+
+export default Team;
diff --git a/apps/web/src/components/common/sidebar/sections/user-info.tsx b/apps/web/src/components/common/sidebar/sections/user-info.tsx
new file mode 100644
index 000000000..a54603b93
--- /dev/null
+++ b/apps/web/src/components/common/sidebar/sections/user-info.tsx
@@ -0,0 +1,24 @@
+import useAuth from "@/components/providers/auth-provider/hooks/use-auth";
+import { Avatar, AvatarFallback } from "@/components/ui/avatar";
+
+function UserInfo() {
+ const { user } = useAuth();
+
+ return (
+ <div className="flex items-center gap-3 mb-3">
+ <Avatar className="text-zinc-900 dark:text-zinc-100">
+ <AvatarFallback>{user?.name?.charAt(0)}</AvatarFallback>
+ </Avatar>
+ <div className="flex-1 min-w-0">
+ <p className="text-sm font-medium text-zinc-900 dark:text-zinc-100 truncate">
+ {user?.name}
+ </p>
+ <p className="text-xs text-zinc-500 dark:text-zinc-400 truncate">
+ {user?.email}
+ </p>
+ </div>
+ </div>
+ );
+}
+
+export default UserInfo;
diff --git a/apps/web/src/components/common/sidebar/sections/workspaces/add-workspace.tsx b/apps/web/src/components/common/sidebar/sections/workspaces/add-workspace.tsx
new file mode 100644
index 000000000..bebacca42
--- /dev/null
+++ b/apps/web/src/components/common/sidebar/sections/workspaces/add-workspace.tsx
@@ -0,0 +1,15 @@
+import { Plus } from "lucide-react";
+
+function AddWorkspace() {
+ return (
+ <button
+ type="button"
+ className="w-full text-left px-3 py-2 text-zinc-700 hover:bg-zinc-100 dark:text-zinc-300 dark:hover:bg-zinc-800 rounded-lg flex items-center transition-colors"
+ >
+ <Plus className="w-4 h-4 mr-2 text-zinc-500 dark:text-zinc-400" />
+ Add Workspace
+ </button>
+ );
+}
+
+export default AddWorkspace;
diff --git a/apps/web/src/components/common/sidebar/sections/workspaces/index.tsx b/apps/web/src/components/common/sidebar/sections/workspaces/index.tsx
new file mode 100644
index 000000000..5f41e2810
--- /dev/null
+++ b/apps/web/src/components/common/sidebar/sections/workspaces/index.tsx
@@ -0,0 +1,38 @@
+import AddWorkspace from "./add-workspace";
+import WorkspaceItemButton from "./workspace-item-button";
+
+const workspaces = [
+ {
+ id: "1",
+ name: "Personal Workspace",
+ },
+];
+
+function Workspaces() {
+ const selectedWorkspace = {
+ id: "1",
+ name: "Personal Workspace",
+ };
+
+ return (
+ <div>
+ <h2 className="text-xs font-semibold text-zinc-500 dark:text-zinc-400 uppercase tracking-wider mb-2 px-3">
+ Workspaces
+ </h2>
+ <div className="space-y-1">
+ {workspaces.map((workspace) => (
+ <WorkspaceItemButton
+ key={workspace.id}
+ workspace={workspace}
+ onSelectWorkspace={() => console.log("Selected")}
+ isSelected={workspace.id === selectedWorkspace.id}
+ />
+ ))}
+
+ <AddWorkspace />
+ </div>
+ </div>
+ );
+}
+
+export default Workspaces;
diff --git a/apps/web/src/components/common/sidebar/sections/workspaces/workspace-item-button.tsx b/apps/web/src/components/common/sidebar/sections/workspaces/workspace-item-button.tsx
new file mode 100644
index 000000000..46f8d98a2
--- /dev/null
+++ b/apps/web/src/components/common/sidebar/sections/workspaces/workspace-item-button.tsx
@@ -0,0 +1,41 @@
+import { Folder } from "lucide-react";
+
+type WorkspaceItemButtonProps = {
+ workspace: {
+ id: string;
+ name: string;
+ };
+ isSelected: boolean;
+ onSelectWorkspace: (id: string) => void;
+};
+
+const buttonStyles: Record<string, string> = {
+ true: "bg-indigo-50 text-indigo-600 dark:bg-indigo-500/10 dark:text-indigo-400",
+ false:
+ "text-zinc-700 hover:bg-zinc-100 dark:text-zinc-300 dark:hover:bg-zinc-800",
+};
+
+const iconStyles: Record<string, string> = {
+ true: "text-indigo-600 dark:text-indigo-400",
+ false: "text-zinc-500 dark:text-zinc-400",
+};
+
+function WorkspaceItemButton({
+ workspace,
+ isSelected,
+ onSelectWorkspace,
+}: WorkspaceItemButtonProps) {
+ return (
+ <button
+ type="button"
+ key={workspace.id}
+ onClick={() => onSelectWorkspace(workspace.id)}
+ className={`w-full text-left px-3 py-2 rounded-lg flex items-center transition-colors ${buttonStyles[String(isSelected)]}`}
+ >
+ <Folder className={`w-4 h-4 mr-2 ${iconStyles[String(isSelected)]}`} />
+ {workspace.name}
+ </button>
+ );
+}
+
+export default WorkspaceItemButton;
diff --git a/apps/web/src/components/common/sidebar/sidebar-content.tsx b/apps/web/src/components/common/sidebar/sidebar-content.tsx
index ef4d26073..3bbd737ca 100644
--- a/apps/web/src/components/common/sidebar/sidebar-content.tsx
+++ b/apps/web/src/components/common/sidebar/sidebar-content.tsx
@@ -1,105 +1,13 @@
-import type { User } from "@/types/user";
-import { Folder, Plus, Settings, Users } from "lucide-react";
-import { Avatar, AvatarFallback } from "../../ui/avatar";
-import SignOutButton from "./sign-out-button";
-
-interface SidebarContentProps {
- // biome-ignore lint/suspicious/noExplicitAny: Still WIP
- workspaces: any[];
- currentWorkspace: string | null;
- setCurrentWorkspace: (id: string) => void;
- currentUser: User;
-}
-
-export function SidebarContent({
- workspaces,
- currentWorkspace,
- setCurrentWorkspace,
- currentUser,
-}: SidebarContentProps) {
- if (!currentUser) return null;
+import Team from "./sections/team";
+import Workspaces from "./sections/workspaces";
+export function SidebarContent() {
return (
- <>
- <nav className="flex-1 overflow-y-auto p-3">
- <div className="space-y-4">
- <div>
- <h2 className="text-xs font-semibold text-zinc-500 dark:text-zinc-400 uppercase tracking-wider mb-2 px-3">
- Workspaces
- </h2>
- <div className="space-y-1">
- {workspaces.map((workspace) => (
- <button
- type="button"
- key={workspace.id}
- onClick={() => setCurrentWorkspace(workspace.id)}
- className={`w-full text-left px-3 py-2 rounded-lg flex items-center transition-colors ${
- currentWorkspace === workspace.id
- ? "bg-indigo-50 text-indigo-600 dark:bg-indigo-500/10 dark:text-indigo-400"
- : "text-zinc-700 hover:bg-zinc-100 dark:text-zinc-300 dark:hover:bg-zinc-800"
- }`}
- >
- <Folder
- className={`w-4 h-4 mr-2 ${
- currentWorkspace === workspace.id
- ? "text-indigo-600 dark:text-indigo-400"
- : "text-zinc-500 dark:text-zinc-400"
- }`}
- />
- {workspace.name}
- </button>
- ))}
-
- <button
- type="button"
- className="w-full text-left px-3 py-2 text-zinc-700 hover:bg-zinc-100 dark:text-zinc-300 dark:hover:bg-zinc-800 rounded-lg flex items-center transition-colors"
- >
- <Plus className="w-4 h-4 mr-2 text-zinc-500 dark:text-zinc-400" />
- Add Workspace
- </button>
- </div>
- </div>
-
- <div>
- <h2 className="text-xs font-semibold text-zinc-500 dark:text-zinc-400 uppercase tracking-wider mb-2 px-3">
- Team
- </h2>
- <button
- type="button"
- className="w-full text-left px-3 py-2 text-zinc-700 hover:bg-zinc-100 dark:text-zinc-300 dark:hover:bg-zinc-800 rounded-lg flex items-center transition-colors"
- >
- <Users className="w-4 h-4 mr-2 text-zinc-500 dark:text-zinc-400" />
- Manage Team
- </button>
- </div>
- </div>
- </nav>
-
- <div className="p-4 border-t border-zinc-200 dark:border-zinc-800">
- <div className="flex items-center gap-3 mb-3">
- <Avatar className="text-zinc-900 dark:text-zinc-100">
- <AvatarFallback>{currentUser.name.charAt(0)}</AvatarFallback>
- </Avatar>
- <div className="flex-1 min-w-0">
- <p className="text-sm font-medium text-zinc-900 dark:text-zinc-100 truncate">
- {currentUser.name}
- </p>
- <p className="text-xs text-zinc-500 dark:text-zinc-400 truncate">
- {currentUser.email}
- </p>
- </div>
- </div>
- <div className="flex gap-1">
- <button
- type="button"
- className="flex-1 px-2 py-1.5 text-xs text-zinc-600 hover:bg-zinc-100 dark:text-zinc-300 dark:hover:bg-zinc-800 rounded-lg flex items-center justify-center transition-colors"
- >
- <Settings className="w-3 h-3 mr-1" />
- Settings
- </button>
- <SignOutButton />
- </div>
+ <nav className="flex-1 overflow-y-auto p-3">
+ <div className="space-y-4">
+ <Workspaces />
+ <Team />
</div>
- </>
+ </nav>
);
}
diff --git a/apps/web/src/components/common/sidebar/sidebar-footer.tsx b/apps/web/src/components/common/sidebar/sidebar-footer.tsx
new file mode 100644
index 000000000..d87663af3
--- /dev/null
+++ b/apps/web/src/components/common/sidebar/sidebar-footer.tsx
@@ -0,0 +1,13 @@
+import BottomActions from "./sections/bottom-actions";
+import UserInfo from "./sections/user-info";
+
+function SidebarFooter() {
+ return (
+ <div className="p-4 border-t border-zinc-200 dark:border-zinc-800">
+ <UserInfo />
+ <BottomActions />
+ </div>
+ );
+}
+
+export default SidebarFooter;
diff --git a/apps/web/src/components/providers/auth-provider/index.tsx b/apps/web/src/components/providers/auth-provider/index.tsx
index a2caf0a3c..f1e3b3866 100644
--- a/apps/web/src/components/providers/auth-provider/index.tsx
+++ b/apps/web/src/components/providers/auth-provider/index.tsx
@@ -10,7 +10,7 @@ import {
export const AuthContext = createContext<{
user: User | null;
- setUser: (user: User | null) => void;
+ setUser: (user: User) => void;
}>({
user: null,
setUser: () => {},
@@ -25,7 +25,7 @@ function AuthProvider({ children }: PropsWithChildren) {
createdAt: Date;
} | null>(null);
- const { data, isFetching } = useGetMe(user?.id);
+ const { data, isFetching } = useGetMe();
useEffect(() => {
if (data) {
diff --git a/apps/web/src/hooks/queries/use-get-me.ts b/apps/web/src/hooks/queries/use-get-me.ts
index d242f7bdf..e3d99d731 100644
--- a/apps/web/src/hooks/queries/use-get-me.ts
+++ b/apps/web/src/hooks/queries/use-get-me.ts
@@ -1,11 +1,9 @@
import me from "@/fetchers/user/me";
import { useQuery } from "@tanstack/react-query";
-function useGetMe(userId: string | undefined) {
- console.log({ userId });
-
+function useGetMe() {
return useQuery({
- queryKey: ["me", userId],
+ queryKey: ["me"],
queryFn: () => me(),
retry: 0,
});
diff --git a/apps/web/src/routes/dashboard/index.tsx b/apps/web/src/routes/dashboard/index.tsx
index 6c51a72d7..c637521d2 100644
--- a/apps/web/src/routes/dashboard/index.tsx
+++ b/apps/web/src/routes/dashboard/index.tsx
@@ -1,11 +1,18 @@
import { Sidebar } from "@/components/common/sidebar";
+import type { User } from "@/types/user";
import { redirect } from "@tanstack/react-router";
import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/dashboard/")({
component: DashboardIndexRouteComponent,
- async beforeLoad({ context: { user } }) {
- if (!user) {
+ async beforeLoad({ context: { user, queryClient } }) {
+ const queryData = await queryClient.ensureQueryData<{
+ data: User;
+ }>({
+ queryKey: ["me"],
+ });
+
+ if (!user && !queryData?.data) {
throw redirect({
to: "/auth/sign-in",
});