Fixed entities
This commit is contained in:
@@ -5,6 +5,7 @@ import AppSidebar, { SidebarTrigger } from "@/components/sidebar/AppSidebar";
|
||||
import TaskView from "@/components/view/TaskView";
|
||||
import useWorkspace from "@/hooks/workspace";
|
||||
import { Task } from "@/lib/types";
|
||||
import { ChevronRight } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
const id = (prefix = "t") =>
|
||||
@@ -20,7 +21,7 @@ export default function Workspace() {
|
||||
title: "New task",
|
||||
status: status,
|
||||
};
|
||||
workspace.entityData[workspace.route].setTasks((c: Task[]) => [...c, task]);
|
||||
workspace.entities[workspace.route].setTasks((c: Task[]) => [...c, task]);
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -30,14 +31,16 @@ export default function Workspace() {
|
||||
<>
|
||||
<SidebarTrigger className="w-full px-4">
|
||||
<div className="px-4">
|
||||
<header className="w-full border-b-2 pb-3 pt-2 hover:bg-accent/20">
|
||||
Workspace
|
||||
<header className="w-full border-b-2 pb-3 pt-2 hover:bg-accent/20 flex items-center text-foreground/90">
|
||||
{workspace.name}
|
||||
<ChevronRight className="h-5" />
|
||||
{workspace.entities[workspace.route].name}
|
||||
</header>
|
||||
</div>
|
||||
</SidebarTrigger>
|
||||
<TaskView
|
||||
tasks={workspace.entityData[workspace.route].setTasks}
|
||||
setTasks={workspace.entityData[workspace.route].tasks}
|
||||
tasks={workspace.entities[workspace.route].tasks}
|
||||
setTasks={workspace.entities[workspace.route].setTasks}
|
||||
createTask={createTask}
|
||||
tags={{
|
||||
website: { color: "yellow", name: "Website" },
|
||||
|
||||
@@ -51,8 +51,8 @@ export default function AppSidebar({
|
||||
</SidebarHeader>
|
||||
<SidebarContent>
|
||||
<SidebarMenu>
|
||||
{workspace.entities.map((entity, i) => (
|
||||
<EntitySwitch entity={entity} key={i} />
|
||||
{workspace.entityLayout.map((entity, i) => (
|
||||
<EntitySwitch entity={entity} workspace={workspace} key={i} />
|
||||
))}
|
||||
</SidebarMenu>
|
||||
</SidebarContent>
|
||||
|
||||
@@ -9,44 +9,61 @@ import {
|
||||
} from "../ui/sidebar";
|
||||
import { Collapsible, CollapsibleContent } from "../ui/collapsible";
|
||||
import { ChevronDown } from "lucide-react";
|
||||
import { Entity } from "@/hooks/workspace";
|
||||
import { Workspace } from "@/hooks/workspace";
|
||||
|
||||
export interface SidebarEntity extends Entity {
|
||||
kind: "group" | "todo";
|
||||
children?: SidebarEntity[];
|
||||
}
|
||||
export type SidebarEntity =
|
||||
| {
|
||||
kind: "group";
|
||||
name: string;
|
||||
children: SidebarEntity[];
|
||||
}
|
||||
| {
|
||||
kind?: "item";
|
||||
id: string;
|
||||
};
|
||||
|
||||
export default function EntitySwitch({ entity }: { entity: SidebarEntity }) {
|
||||
switch (entity.kind) {
|
||||
case "group":
|
||||
return (
|
||||
<Collapsible defaultOpen className="group/collapsible">
|
||||
<SidebarGroup>
|
||||
<SidebarGroupLabel asChild>
|
||||
<SidebarMenuButton asChild>
|
||||
<CollapsibleTrigger>
|
||||
{entity.name}
|
||||
<ChevronDown className="ml-auto transition-transform group-data-[state=open]/collapsible:rotate-180" />
|
||||
</CollapsibleTrigger>
|
||||
</SidebarMenuButton>
|
||||
</SidebarGroupLabel>
|
||||
<CollapsibleContent>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
{entity.children?.map((entity, i) => (
|
||||
<EntitySwitch entity={entity} key={i} />
|
||||
))}
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</CollapsibleContent>
|
||||
</SidebarGroup>
|
||||
</Collapsible>
|
||||
);
|
||||
case "todo":
|
||||
return (
|
||||
<SidebarMenuItem>
|
||||
<SidebarMenuButton>{entity.name}</SidebarMenuButton>
|
||||
</SidebarMenuItem>
|
||||
);
|
||||
export default function EntitySwitch({
|
||||
entity,
|
||||
workspace,
|
||||
}: {
|
||||
entity: SidebarEntity;
|
||||
workspace: Workspace;
|
||||
}) {
|
||||
if (entity.kind === "group") {
|
||||
return (
|
||||
<Collapsible defaultOpen className="group/collapsible">
|
||||
<SidebarGroup>
|
||||
<SidebarGroupLabel asChild>
|
||||
<SidebarMenuButton asChild>
|
||||
<CollapsibleTrigger>
|
||||
{entity.name}
|
||||
<ChevronDown className="ml-auto transition-transform group-data-[state=open]/collapsible:rotate-180" />
|
||||
</CollapsibleTrigger>
|
||||
</SidebarMenuButton>
|
||||
</SidebarGroupLabel>
|
||||
<CollapsibleContent>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
{entity.children?.map((entity, i) => (
|
||||
<EntitySwitch entity={entity} workspace={workspace} key={i} />
|
||||
))}
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</CollapsibleContent>
|
||||
</SidebarGroup>
|
||||
</Collapsible>
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
<SidebarMenuItem>
|
||||
<SidebarMenuButton
|
||||
onClick={() => {
|
||||
workspace.setRoute(entity.id);
|
||||
}}
|
||||
>
|
||||
{workspace.entities[entity.id].name}
|
||||
</SidebarMenuButton>
|
||||
</SidebarMenuItem>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+30
-17
@@ -2,19 +2,21 @@ import { SidebarEntity } from "@/components/sidebar/Entity";
|
||||
import { SetState, Task } from "@/lib/types";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export interface Entity {
|
||||
id: string;
|
||||
export type Entity = {
|
||||
name: string;
|
||||
}
|
||||
kind: "todo";
|
||||
setTasks: SetState<Task[]>;
|
||||
tasks: Task[];
|
||||
};
|
||||
|
||||
export type Workspace = {
|
||||
export interface Workspace {
|
||||
name: string;
|
||||
entities: SidebarEntity[];
|
||||
entityData: Record<string, any>;
|
||||
entityLayout: SidebarEntity[];
|
||||
entities: Record<string, Entity>;
|
||||
|
||||
route: string;
|
||||
setRoute: SetState<string>;
|
||||
};
|
||||
}
|
||||
|
||||
function useTasks() {
|
||||
return useState<Task[]>([]);
|
||||
@@ -40,21 +42,32 @@ export default function useWorkspace(): Workspace {
|
||||
|
||||
return {
|
||||
name: "Personal Workspace",
|
||||
entities: [
|
||||
{
|
||||
id: "alpha",
|
||||
name: "Project alpha",
|
||||
kind: "group",
|
||||
children: [{ id: "alpha_tasks", name: "Tasks", kind: "todo" }],
|
||||
},
|
||||
],
|
||||
|
||||
entityData: {
|
||||
entities: {
|
||||
alpha_tasks: {
|
||||
name: "Alpha tasks",
|
||||
kind: "todo",
|
||||
tasks,
|
||||
setTasks,
|
||||
},
|
||||
beta_tasks: {
|
||||
name: "Beta tasks",
|
||||
kind: "todo",
|
||||
tasks,
|
||||
setTasks,
|
||||
},
|
||||
},
|
||||
entityLayout: [
|
||||
{
|
||||
kind: "group",
|
||||
name: "Project alpha",
|
||||
children: [{ id: "alpha_tasks" }],
|
||||
},
|
||||
{
|
||||
kind: "group",
|
||||
name: "Project beta",
|
||||
children: [{ id: "beta_tasks" }],
|
||||
},
|
||||
],
|
||||
|
||||
route,
|
||||
setRoute,
|
||||
|
||||
Reference in New Issue
Block a user