'use client'; import { useEventListener, useOnClickOutside } from 'usehooks-ts'; import { useQueryClient } from '@tanstack/react-query'; import { useState, ElementRef, useRef } from 'react'; import { useParams } from 'next/navigation'; import { AlignLeft } from 'lucide-react'; import { toast } from 'sonner'; import { FormTextarea } from '@/components/form/form-textarea'; import { FormSubmit } from '@/components/form/form-submit'; import { Skeleton } from '@/components/ui/skeleton'; import { updateCard } from '@/actions/update-card'; import { Button } from '@/components/ui/button'; import { useAction } from '@/hooks/use-action'; import { CardWithList } from '@/types'; interface DescriptionProps { data: CardWithList; } export const Description = ({ data }: DescriptionProps) => { const queryClient = useQueryClient(); const params = useParams(); const [isEditing, setIsEditing] = useState(false); const textareaRef = useRef>(null); const formRef = useRef>(null); const enaleEditing = () => { setIsEditing(true); setTimeout(() => { textareaRef.current?.focus(); }); }; const disableEditing = () => { setIsEditing(false); }; const onKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { disableEditing(); } }; useEventListener('keydown', onKeyDown); useOnClickOutside(formRef, disableEditing); const { execute, fieldErrors } = useAction(updateCard, { onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ['card', data.id] }); queryClient.invalidateQueries({ queryKey: ['card-logs', data.id] }); toast.success(`Card "${data.title}" updated`); disableEditing(); }, onError: (error) => { toast.error(error); }, }); const onSubmit = (formData: FormData) => { const description = formData.get('description') as string; const boardId = params.boardId as string; execute({ id: data.id, description, boardId, }); }; return (

Description

{isEditing ? (
Save
) : (
{data.description ?? 'Add a more detailed description...'}
)}
); }; Description.Skeleton = function DescriptionSkeleton() { return (
); };