"use client"; import { useState } from "react"; import Link from "next/link"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { Button, Card, CardContent, ConfirmDialog, Dialog, Input, PageHeader, Textarea } from "@/components/ds"; import { useCurrentHub, storeHubId } from "@/modules/workspace/hooks/useCurrentHub"; import { workspaceApi } from "@/modules/workspace/services/workspace-api"; import { HUB_STATUS, labelOf } from "@/modules/workspace/labels"; import { WorkspaceEmptyState, WorkspacePageError, WorkspacePageLoader } from "@/modules/workspace/pages/shared"; import type { WorkspaceRecord } from "@/modules/workspace/types"; export const WorkspaceHubs = function WorkspaceHubsPage() { const queryClient = useQueryClient(); const { tenantId, hubs, loading, error, refetch } = useCurrentHub(); const [open, setOpen] = useState(false); const [editing, setEditing] = useState(null); const [archiveId, setArchiveId] = useState(null); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const invalidate = () => void queryClient.invalidateQueries({ queryKey: ["workspace", tenantId, "hubs"] }); const create = useMutation({ mutationFn: () => workspaceApi.hubs.create(tenantId!, { name, description }), onSuccess: (hub) => { if (hub.id) storeHubId(String(hub.id)); toast.success("مرکز کار ایجاد شد"); setOpen(false); setName(""); setDescription(""); invalidate(); }, onError: (err) => toast.error(err instanceof Error ? err.message : "ایجاد مرکز کار ناموفق بود"), }); const update = useMutation({ mutationFn: () => workspaceApi.hubs.update(tenantId!, String(editing?.id), { name, description }), onSuccess: () => { toast.success("مرکز کار به‌روز شد"); setEditing(null); invalidate(); }, onError: (err) => toast.error(err instanceof Error ? err.message : "ویرایش ناموفق بود"), }); const archive = useMutation({ mutationFn: () => workspaceApi.hubs.update(tenantId!, archiveId!, { status: "archived" }), onSuccess: () => { toast.success("مرکز کار بایگانی شد"); setArchiveId(null); invalidate(); }, onError: (err) => toast.error(err instanceof Error ? err.message : "بایگانی ناموفق بود"), }); if (!tenantId || loading) return ; if (error) return refetch()} />; return (
setOpen(true)}>مرکز کار جدید} /> {hubs.length === 0 ? ( setOpen(true)}>ایجاد مرکز کار} /> ) : (
{hubs.map((hub) => (

{String(hub.name || "مرکز کار")}

{String(hub.description || "بدون توضیح")}

{labelOf(HUB_STATUS, String(hub.status || ""))}
{hub.status !== "archived" ? ( ) : null}
))}
)} setOpen(false)} title="مرکز کار جدید">
setName(event.target.value)} placeholder="نام مرکز کار" />