import { client } from '@/ui/api/client' import { useMutation, useQueryClient } from '@tanstack/react-query' import type { InferResponseType, InferRequestType } from 'hono/client' import type { ProjectInfo } from '@/dtos/project' 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 { useNavigate } from '@tanstack/react-router' import { Loader2, Upload } 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('') // 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) } else { setName('') setCoverImageKey(undefined) setCoverImagePreview(undefined) } } }, [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) navigate({ to: '/projects/$projectId', params: { projectId: data.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) => { if (typeof data === 'object' && 'key' in data) { setCoverImageKey(data.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 handleSubmit = () => { if (!name.trim()) return if (mode === 'create') { createProject({ name: name.trim(), coverImageKey: coverImageKey, }) } else if (mode === 'edit' && project?.id) { updateProject({ name: name.trim(), coverImageKey: coverImageKey, }) } } return ( {mode === 'create' ? 'Create Project' : 'Project Settings'} {mode === 'create' ? 'Configure your new project.' : 'Update your project configuration.'}
{/* Cover Image Upload Section */}
{isUploading ? (
) : null} {coverImagePreview ? ( Cover Preview ) : (
Upload Image
)}
setName(e.target.value)} className="col-span-3" />
) }