"use client"; import { useState } from "react"; import Link from "next/link"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { Button, Card, CardContent, Dialog, Input, PageHeader, Select, Textarea } from "@/components/ds"; import { useCurrentHub } from "@/modules/workspace/hooks/useCurrentHub"; import { workspaceApi } from "@/modules/workspace/services/workspace-api"; import { BRIEF_FIELDS, CHANNEL, CONTENT_TYPE, labelOf } from "@/modules/workspace/labels"; import { StatusChip } from "@/modules/workspace/components/StatusChip"; import { IdentityUserSelect } from "@/modules/workspace/components/IdentityUserSelect"; import { WorkspaceEmptyState, WorkspacePageError, WorkspacePageLoader } from "@/modules/workspace/pages/shared"; export const WorkspaceContentStudio = function WorkspaceContentStudioPage() { const queryClient = useQueryClient(); const { tenantId, hubId, loading } = useCurrentHub(); const [open, setOpen] = useState(false); const [title, setTitle] = useState(""); const [contentType, setContentType] = useState("instagram_post"); const [projectId, setProjectId] = useState(""); const [brief, setBrief] = useState>({}); const [ownerId, setOwnerId] = useState(""); const [reviewerId, setReviewerId] = useState(""); const [teamId, setTeamId] = useState(""); const [search, setSearch] = useState(""); const types = useQuery({ queryKey: ["workspace", tenantId, "content-types"], queryFn: () => workspaceApi.contentTypes.list(tenantId!), enabled: !!tenantId, }); const projects = useQuery({ queryKey: ["workspace", tenantId, "projects", hubId], queryFn: () => workspaceApi.projects.list(tenantId!, { work_hub_id: hubId || undefined }), enabled: !!tenantId && !!hubId, }); const teams = useQuery({ queryKey: ["workspace", tenantId, "teams", hubId], queryFn: () => workspaceApi.teams.list(tenantId!, hubId || undefined), enabled: !!tenantId && !!hubId, }); const items = useQuery({ queryKey: ["workspace", tenantId, "content", hubId], queryFn: () => workspaceApi.content.list(tenantId!, { work_hub_id: hubId || undefined }), enabled: !!tenantId && !!hubId, }); const create = useMutation({ mutationFn: () => workspaceApi.content.create(tenantId!, { work_hub_id: hubId, project_id: projectId || undefined, title, content_type: contentType, brief, owner_user_id: ownerId || undefined, reviewer_user_id: reviewerId || undefined, team_id: teamId || undefined, }), onSuccess: () => { toast.success("محتوا ایجاد شد"); setOpen(false); setTitle(""); setBrief({}); void queryClient.invalidateQueries({ queryKey: ["workspace", tenantId, "content"] }); }, onError: (err) => toast.error(err instanceof Error ? err.message : "ایجاد محتوا ناموفق بود"), }); if (!tenantId || loading || items.isLoading) return ; if (!hubId) return ; if (items.error) return items.refetch()} />; return (
} /> setSearch(event.target.value)} placeholder="جستجوی عنوان محتوا" /> {(items.data ?? []).filter((item) => String(item.title || "").includes(search)).length === 0 ? ( setOpen(true)}>ایجاد محتوا} /> ) : (
{(items.data ?? []) .filter((item) => String(item.title || "").includes(search)) .map((item) => (

{String(item.title || "محتوا")}

{labelOf(CONTENT_TYPE, String(item.content_type || ""), String(item.content_type || "سایر"))} ·{" "} {labelOf(CHANNEL, String(item.channel || ""), "بدون کانال")}

))}
)} setOpen(false)} title="محتوای جدید" size="lg">
setTitle(event.target.value)} placeholder="عنوان" />
{BRIEF_FIELDS.map((field) => field.multiline ? (