({
id,
currentChatId,
title,
onSelect,
refetchChats,
}: SideBarItemProps)
| 33 | currentChatId: string; |
| 34 | title: string; |
| 35 | createdAt?: string | number | Date; |
| 36 | onSelect: (id: string) => void; |
| 37 | refetchChats: () => void; |
| 38 | } |
| 39 | |
| 40 | const relativeTime = (value?: string | number | Date) => { |
| 41 | if (!value) return ''; |
| 42 | const then = new Date(value).getTime(); |
| 43 | if (Number.isNaN(then)) return ''; |
| 44 | const minutes = Math.round((Date.now() - then) / 60000); |
| 45 | if (minutes < 1) return 'now'; |
| 46 | if (minutes < 60) return `${minutes}m`; |
| 47 | const hours = Math.round(minutes / 60); |
| 48 | if (hours < 24) return `${hours}h`; |
| 49 | return `${Math.round(hours / 24)}d`; |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * One row in the chat rail. |
| 54 | * |
| 55 | * The row is a container, not a button — the previous version nested the menu |
| 56 | * button and a dialog inside the row's own <button>, which is invalid markup |
| 57 | * and needed stopPropagation on every child to stay usable. |
| 58 | */ |
| 59 | function SideBarItemComponent({ |
| 60 | id, |
| 61 | currentChatId, |
| 62 | title, |
| 63 | createdAt, |
| 64 | onSelect, |
| 65 | refetchChats, |
| 66 | }: SideBarItemProps) { |
| 67 | const [isDialogOpen, setIsDialogOpen] = useState(false); |
| 68 | const [renaming, setRenaming] = useState(false); |
| 69 | const [draft, setDraft] = useState(title); |
| 70 | const inputRef = useRef<HTMLInputElement>(null); |
| 71 | const router = useRouter(); |
| 72 | const isSelected = currentChatId === id; |
| 73 | |
| 74 | useEffect(() => setDraft(title), [title]); |
| 75 | useEffect(() => { |
| 76 | if (renaming) inputRef.current?.select(); |
| 77 | }, [renaming]); |
| 78 | |
| 79 | const [updateTitle] = useMutation(UPDATE_CHAT_TITLE, { |
| 80 | onCompleted: () => refetchChats(), |
| 81 | onError: (error) => { |
| 82 | logger.error('Error renaming chat:', error); |
| 83 | toast.error('Could not rename this chat'); |
| 84 | setDraft(title); |
| 85 | }, |
| 86 | }); |
| 87 | |
| 88 | const [clearHistory] = useMutation(CLEAR_CHAT_HISTORY, { |
| 89 | onCompleted: () => { |
| 90 | toast.success('History cleared'); |
| 91 | // The open conversation is now stale — reload it from the server. |
| 92 | if (isSelected) window.dispatchEvent(new Event(EventEnum.CHAT)); |
nothing calls this directly
no test coverage detected