import type { ProjectInfo } from '@shumai/dtos' import { client } from '@/ui/api/client' import { Button } from '@/ui/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/ui/components/ui/dialog' import { Input } from '@/ui/components/ui/input' import { Label } from '@/ui/components/ui/label' import { Switch } from '@/ui/components/ui/switch' import { useMutation, useQueryClient } from '@tanstack/react-query' import { useNavigate } from '@tanstack/react-router' import type { InferRequestType, InferResponseType } from 'hono/client' import { Bell, Check, Loader2, Sliders, Sparkles, Upload, X } from 'lucide-react' import { useEffect, useRef, useState } from 'react' interface ProjectDialogProps { open: boolean onOpenChange: (open: boolean) => void mode: 'create' | 'edit' teamId: string project?: ProjectInfo } export function ProjectDialog({ open, onOpenChange, mode, teamId, project }: ProjectDialogProps) { const queryClient = useQueryClient() const navigate = useNavigate() const [name, setName] = useState('') const [enableNotification, setEnableNotification] = useState(true) // Cover Image State const [coverImageKey, setCoverImageKey] = useState(undefined) const [coverImagePreview, setCoverImagePreview] = useState(undefined) const [isUploading, setIsUploading] = useState(false) const fileInputRef = useRef(null) useEffect(() => { if (open) { if (mode === 'edit' && project) { setName(project.name || '') setCoverImageKey(project.coverImageKey) setCoverImagePreview(project.coverImage) setEnableNotification(project.enableNotification !== false) } else { setName('') setCoverImageKey(undefined) setCoverImagePreview(undefined) setEnableNotification(true) } } }, [open, mode, project]) const $post = client.api.teams[':teamId'].projects.$post const { mutate: createProject } = useMutation< InferResponseType, Error, InferRequestType['json'] >({ mutationFn: async (payload) => { const res = await $post({ param: { teamId: teamId }, json: payload }) if (!res.ok) throw new Error('Failed to create project') return await res.json() }, onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'projects'], }) onOpenChange(false) const dataObj = data as Record if (dataObj && typeof dataObj.id === 'string') { navigate({ to: '/projects/$projectId', params: { projectId: dataObj.id }, }) } }, }) const $put = client.api.projects[':projectId'].$put const { mutate: updateProject } = useMutation< InferResponseType, Error, InferRequestType['json'] >({ mutationFn: async (payload) => { const res = await $put({ param: { projectId: project!.id! }, json: payload, }) if (!res.ok) throw new Error('Failed to update project') return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'projects'], }) onOpenChange(false) }, }) const $upload = client.api.teams[':teamId'].files.$post const { mutate: uploadFile } = useMutation< InferResponseType, Error, { file: File } >({ mutationFn: async ({ file }) => { const res = await $upload({ param: { teamId: teamId }, form: { file: file, }, }) if (!res.ok) throw new Error('Failed to upload file') return await res.json() }, onSuccess: (data) => { const obj = data as Record if (obj && typeof obj.key === 'string') { setCoverImageKey(obj.key) } setIsUploading(false) }, onError: () => { setIsUploading(false) }, }) const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) { setIsUploading(true) // Create local preview const objectUrl = URL.createObjectURL(file) setCoverImagePreview(objectUrl) // Upload uploadFile({ file: file, }) } } const handleImageAreaClick = () => { fileInputRef.current?.click() } const handleRemoveCover = (e: React.MouseEvent) => { e.stopPropagation() setCoverImageKey(undefined) setCoverImagePreview(undefined) } const handleSubmit = () => { if (!name.trim()) return if (mode === 'create') { createProject({ name: name.trim(), coverImageKey: coverImageKey, enableNotification: enableNotification, }) } else if (mode === 'edit' && project?.id) { updateProject({ name: name.trim(), coverImageKey: coverImageKey, enableNotification: enableNotification, }) } } return ( {/* Beautiful Glowing Header */}
{mode === 'create' ? 'Create New Project' : 'Project Settings'} {mode === 'create' ? 'Configure your new project workspace and appearance.' : 'Manage and update your project workspace configurations.'}
{/* Scrollable Form Content */}
{/* Project Name Field */}
setName(e.target.value)} placeholder="e.g. Acme Marketing, Q3 Product Launch..." className="h-11 px-4 rounded-xl border-border/80 focus-visible:ring-ring/25 focus-visible:border-primary transition-all shadow-xs pr-10" /> {name.trim() && (
)}

A clear, concise name to identify your project space.

{/* Cover Image & Branding Section */}
{coverImagePreview && ( )}
{/* Centered Square 1:1 Preview Area */}
{/* Uploading Spinner */} {isUploading && (
Uploading...
)} {coverImagePreview ? ( <> Cover Preview {/* Hover Overlay */}
Upload Image
) : (

Upload cover

Drag and drop, or browse. Recommended 1:1 (400×400px).

)}
{/* Preferences Section */}
Activity Notifications

Receive in-app updates for comments, uploads, and automated background analysis tasks inside this project.

{/* Beautiful Footer with Premium Buttons */}
) }