⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
refactor: :sparkles: improving code for projects fetching
Andrej8df81c6036f0
diff --git a/apps/api/src/workspace/controllers/get-workspaces.ts b/apps/api/src/workspace/controllers/get-workspaces.tsindex 632363da4..c876598af 100644--- a/apps/api/src/workspace/controllers/get-workspaces.ts+++ b/apps/api/src/workspace/controllers/get-workspaces.ts@@ -5,7 +5,6 @@ import {workspaceTable,workspaceUserTable,} from "../../database/schema";-import groupWorkspacesWithProjects from "../utils/group-workspaces-with-project";async function getWorkspaces({ userId }: { userId: string }) {const workspacesWithProjects = await db@@ -29,7 +28,7 @@ async function getWorkspaces({ userId }: { userId: string }) {),);- return groupWorkspacesWithProjects(workspacesWithProjects);+ return workspacesWithProjects;}export default getWorkspaces;diff --git a/apps/api/src/workspace/utils/group-workspaces-with-project.ts b/apps/api/src/workspace/utils/group-workspaces-with-project.tsdeleted file mode 100644index 5b95b4779..000000000--- a/apps/api/src/workspace/utils/group-workspaces-with-project.ts+++ /dev/null@@ -1,45 +0,0 @@-function groupWorkspacesWithProjects(- workspacesWithProjects: {- id: string;- name: string;- ownerId: string;- projectId: string | null;- projectName: string | null;- }[],-) {- const groupedWorkspaces = new Map<- string,- {- id: string;- name: string;- ownerId: string;- projects: { projectId: string; projectName: string }[];- }- >();-- for (const {- id,- name,- ownerId,- projectId,- projectName,- } of workspacesWithProjects) {- if (!groupedWorkspaces.has(id)) {- groupedWorkspaces.set(id, {- id,- name,- ownerId,- projects: [],- });- }-- const workspace = groupedWorkspaces.get(id);- if (workspace && projectId && projectName) {- workspace.projects.push({ projectId, projectName });- }- }-- return Array.from(groupedWorkspaces.values());-}--export default groupWorkspacesWithProjects;diff --git a/apps/web/src/components/common/sidebar/sections/projects/add-project-modal.tsx b/apps/web/src/components/common/sidebar/sections/projects/add-project-modal.tsxindex 2567f9926..e692b0dde 100644--- a/apps/web/src/components/common/sidebar/sections/projects/add-project-modal.tsx+++ b/apps/web/src/components/common/sidebar/sections/projects/add-project-modal.tsx@@ -28,7 +28,7 @@ function AddProjectModal({ open, onClose }: AddProjectModalProps) {if (!name.trim()) return;await mutateAsync();- await queryClient.invalidateQueries({ queryKey: ["workspaces"] });+ await queryClient.invalidateQueries({ queryKey: ["projects"] });setName("");onClose();diff --git a/apps/web/src/components/common/sidebar/sections/projects/index.tsx b/apps/web/src/components/common/sidebar/sections/projects/index.tsxnew file mode 100644index 000000000..31ebd62fd--- /dev/null+++ b/apps/web/src/components/common/sidebar/sections/projects/index.tsx@@ -0,0 +1,26 @@+import useGetProjects from "@/hooks/queries/project/use-get-projects";+import ProjectItem from "./project-item";++type ProjectsProps = {+ workspaceId: string;+};++function Projects({ workspaceId }: ProjectsProps) {+ const { data: projects } = useGetProjects({ workspaceId });++ return (+ <div className="ml-8 space-y-0.5 mt-1">+ {projects &&+ projects.length > 0 &&+ projects.map((project) => (+ <ProjectItem+ key={project.id}+ workspaceId={workspaceId}+ projectId={project?.id}+ />+ ))}+ </div>+ );+}++export default Projects;diff --git a/apps/web/src/components/common/sidebar/sections/projects/project-item.tsx b/apps/web/src/components/common/sidebar/sections/projects/project-item.tsxindex 9e44ca91e..6e3884bcf 100644--- a/apps/web/src/components/common/sidebar/sections/projects/project-item.tsx+++ b/apps/web/src/components/common/sidebar/sections/projects/project-item.tsx@@ -1,23 +1,30 @@+import useGetProject from "@/hooks/queries/project/use-get-project";import { cn } from "@/lib/utils";import useProjectStore from "@/store/project";import { Layout } from "lucide-react";+import { useEffect } from "react";type ProjectItemProps = {- project: {- projectId: string;- projectName: string;- };+ workspaceId: string;+ projectId: string;};-function ProjectItem({ project }: ProjectItemProps) {+function ProjectItem({ workspaceId, projectId }: ProjectItemProps) {+ const { data } = useGetProject({ workspaceId, id: projectId });const { project: selectedProject, setProject } = useProjectStore();- const isSelected = project?.projectId === selectedProject?.projectId;++ const isSelected = selectedProject?.id === projectId;++ useEffect(() => {+ if (data && isSelected) {+ setProject(data);+ }+ }, [data, setProject, isSelected]);return (<buttontype="button"- key={project.projectId}- onClick={() => setProject(project)}+ onClick={() => setProject(data)}className={cn("w-full text-left px-2 py-1.5 rounded-md flex items-center text-sm transition-all group",isSelected@@ -41,7 +48,7 @@ function ProjectItem({ project }: ProjectItemProps) {: "text-zinc-600 dark:text-zinc-400 group-hover:text-zinc-900 dark:group-hover:text-zinc-300",)}>- {project.projectName}+ {data?.name}</span></button>);diff --git a/apps/web/src/components/common/sidebar/sections/workspaces/components/workspace-item.tsx b/apps/web/src/components/common/sidebar/sections/workspaces/components/workspace-item.tsxindex 6593d98c6..a87afb8f5 100644--- a/apps/web/src/components/common/sidebar/sections/workspaces/components/workspace-item.tsx+++ b/apps/web/src/components/common/sidebar/sections/workspaces/components/workspace-item.tsx@@ -1,15 +1,13 @@import { cn } from "@/lib/utils";-import type { Project } from "@/types/project";import { ChevronDown, ChevronRight, Folder, FolderOpen } from "lucide-react";import { Fragment } from "react/jsx-runtime";-import ProjectItem from "../../projects/project-item";+import Projects from "../../projects";import WorkspaceMenu from "./workspace-menu";type WorkspaceItemProps = {workspace: {id: string;name: string;- projects: Project[];};isExpanded: boolean;onExpandWorkspace: (id: string) => void;@@ -70,13 +68,7 @@ function WorkspaceItem({<WorkspaceMenu id={workspace?.id} /></div>- {isExpanded && workspace.projects.length > 0 && (- <div className="ml-8 space-y-0.5 mt-1">- {workspace.projects.map((project) => (- <ProjectItem key={project.projectId} project={project} />- ))}- </div>- )}+ {isExpanded && <Projects workspaceId={workspace?.id} />}</Fragment>);}diff --git a/apps/web/src/components/common/sidebar/sections/workspaces/components/workspace-menu.tsx b/apps/web/src/components/common/sidebar/sections/workspaces/components/workspace-menu.tsxindex 654c6cb47..ff9ec7580 100644--- a/apps/web/src/components/common/sidebar/sections/workspaces/components/workspace-menu.tsx+++ b/apps/web/src/components/common/sidebar/sections/workspaces/components/workspace-menu.tsx@@ -20,7 +20,7 @@ function WorkspaceMenu({ id }: WorkspaceMenuProps) {async function handleDeleteWorkspace() {await deleteWorkspace();queryClient.invalidateQueries({- queryKey: ["workspaces"],+ queryKey: ["workspaces", "projects"],});}diff --git a/apps/web/src/components/common/sidebar/sections/workspaces/index.tsx b/apps/web/src/components/common/sidebar/sections/workspaces/index.tsxindex 4a66ae5be..a29b77d41 100644--- a/apps/web/src/components/common/sidebar/sections/workspaces/index.tsx+++ b/apps/web/src/components/common/sidebar/sections/workspaces/index.tsx@@ -77,16 +77,17 @@ function Workspaces() {Workspaces</h2><div className="space-y-1">- {workspaces.map((workspace) => (- <div key={workspace.id} className="space-y-1">- <WorkspaceItem- workspace={workspace}- isExpanded={expandedWorkspaces.includes(workspace.id)}- onSelectWorkspace={handleSelectWorkspace}- onExpandWorkspace={handleExpandWorkspace}- />- </div>- ))}+ {workspaces.length > 0 &&+ workspaces.map((workspace) => (+ <div key={workspace.id} className="space-y-1">+ <WorkspaceItem+ workspace={workspace}+ isExpanded={expandedWorkspaces.includes(workspace.id)}+ onSelectWorkspace={handleSelectWorkspace}+ onExpandWorkspace={handleExpandWorkspace}+ />+ </div>+ ))}<AddWorkspace /></div></div>diff --git a/apps/web/src/components/kanban-board/index.tsx b/apps/web/src/components/kanban-board/index.tsxindex 76c08cfa5..3881a5172 100644--- a/apps/web/src/components/kanban-board/index.tsx+++ b/apps/web/src/components/kanban-board/index.tsx@@ -1,4 +1,5 @@import generateProject from "@/lib/workspace/generate-project";+import useProjectStore from "@/store/project";import {DndContext,type DragEndEvent,@@ -19,6 +20,7 @@ function KanbanBoard() {tasksPerColumn: 4,}),);+ const { project: selectedProject } = useProjectStore();const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null);const handleDragStart = (event: DragStartEvent) => {@@ -124,7 +126,7 @@ function KanbanBoard() {<header className="mb-6 space-y-6 flex-shrink-0 px-4 md:px-0"><div className="flex items-center justify-between"><h1 className="text-2xl font-semibold text-zinc-900 dark:text-zinc-100">- {project.name}+ {selectedProject?.name}</h1></div></header>diff --git a/apps/web/src/fetchers/project/get-project.ts b/apps/web/src/fetchers/project/get-project.tsnew file mode 100644index 000000000..42ce3a9ca--- /dev/null+++ b/apps/web/src/fetchers/project/get-project.ts@@ -0,0 +1,16 @@+import { api } from "@kaneo/libs";++async function getProject({+ id,+ workspaceId,+}: { id: string; workspaceId: string }) {+ const response = await api.project({ id }).get({ query: { workspaceId } });++ if (response.error) {+ throw new Error(response.error.value.message);+ }++ return response?.data;+}++export default getProject;diff --git a/apps/web/src/hooks/queries/project/use-get-project.ts b/apps/web/src/hooks/queries/project/use-get-project.tsnew file mode 100644index 000000000..d2d9c59a0--- /dev/null+++ b/apps/web/src/hooks/queries/project/use-get-project.ts@@ -0,0 +1,15 @@+import getProject from "@/fetchers/project/get-project";+import { useQuery } from "@tanstack/react-query";++function useGetProject({+ id,+ workspaceId,+}: { id: string; workspaceId: string }) {+ return useQuery({+ queryFn: () => getProject({ id, workspaceId }),+ queryKey: ["projects", id],+ enabled: !!id,+ });+}++export default useGetProject;diff --git a/apps/web/src/store/project.ts b/apps/web/src/store/project.tsindex 4f6d8bd5a..088f068f7 100644--- a/apps/web/src/store/project.ts+++ b/apps/web/src/store/project.ts@@ -2,10 +2,10 @@ import type { Project } from "@/types/project";import { create } from "zustand";const useProjectStore = create<{- project: Project | null;- setProject: (updatedProject: Project) => void;+ project: Project | undefined;+ setProject: (updatedProject: Project | undefined) => void;}>((set) => ({- project: null,+ project: undefined,setProject: (updatedProject) => set(() => ({ project: updatedProject })),}));diff --git a/apps/web/src/types/project/index.ts b/apps/web/src/types/project/index.tsindex 70ffcde88..bec45af5a 100644--- a/apps/web/src/types/project/index.ts+++ b/apps/web/src/types/project/index.ts@@ -1,4 +1,6 @@export type Project = {- projectId: string;- projectName: string;+ id: string;+ name: string;+ description: string | null;+ workspaceId: string;};