From 41df02c9ec720a9afac80307f2950dd59669f900 Mon Sep 17 00:00:00 2001 From: John Jeong Date: Thu, 29 May 2025 16:28:38 -0700 Subject: [PATCH] feat(event-chip): sort events by start date Sorts the filtered events by start date in descending order. This ensures that the most recent events are displayed first in the event chip dropdown. The sorting logic handles cases where the start date is invalid or missing. --- .../note-header/chips/event-chip.tsx | 51 ++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/apps/desktop/src/components/editor-area/note-header/chips/event-chip.tsx b/apps/desktop/src/components/editor-area/note-header/chips/event-chip.tsx index 5217d27720..91233eacea 100644 --- a/apps/desktop/src/components/editor-area/note-header/chips/event-chip.tsx +++ b/apps/desktop/src/components/editor-area/note-header/chips/event-chip.tsx @@ -1,6 +1,6 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@hypr/ui/components/ui/tooltip"; import { Trans } from "@lingui/react/macro"; -import { useMutation, useQuery } from "@tanstack/react-query"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { openUrl } from "@tauri-apps/plugin-opener"; import { format, isSameDay, subDays } from "date-fns"; import { CalendarIcon, SearchIcon, SpeechIcon, VideoIcon, XIcon } from "lucide-react"; @@ -28,9 +28,16 @@ export function EventChip({ sessionId }: EventChipProps) { const { userId, onboardingSessionId } = useHypr(); const [isEventSelectorOpen, setIsEventSelectorOpen] = useState(false); const [searchQuery, setSearchQuery] = useState(""); + const queryClient = useQueryClient(); - const { sessionCreatedAt } = useSession(sessionId, (s) => ({ + const { + sessionCreatedAt, + updateTitle, + session: currentSessionDetails, + } = useSession(sessionId, (s) => ({ sessionCreatedAt: s.session.created_at, + updateTitle: s.updateTitle, + session: s.session, })); const event = useQuery({ @@ -84,10 +91,27 @@ export function EventChip({ sessionId }: EventChipProps) { const assignEvent = useMutation({ mutationFn: async (eventId: string) => { await dbCommands.setSessionEvent(sessionId, eventId); + return eventId; }, - onSuccess: () => { + onSuccess: async (assignedEventId) => { event.refetch(); eventsInPastWithoutAssignedSession.refetch(); + queryClient.invalidateQueries({ queryKey: ["sessions"] }); + + if (assignedEventId && updateTitle && currentSessionDetails) { + try { + const eventDetails = await dbCommands.getEvent(assignedEventId); + + if (eventDetails?.name) { + if (!currentSessionDetails.title?.trim()) { + updateTitle(eventDetails.name); + queryClient.invalidateQueries({ queryKey: ["session", sessionId] }); + } + } + } catch (error) { + console.error("Failed to update session title after event assignment:", error); + } + } }, }); @@ -98,6 +122,7 @@ export function EventChip({ sessionId }: EventChipProps) { onSuccess: () => { event.refetch(); eventsInPastWithoutAssignedSession.refetch(); + queryClient.invalidateQueries({ queryKey: ["sessions"] }); setIsEventSelectorOpen(false); }, onError: (error) => { @@ -118,6 +143,7 @@ export function EventChip({ sessionId }: EventChipProps) { onSuccess: () => { event.refetch(); eventsInPastWithoutAssignedSession.refetch(); + queryClient.invalidateQueries({ queryKey: ["sessions"] }); setIsEventSelectorOpen(false); }, onError: (error) => { @@ -253,9 +279,22 @@ export function EventChip({ sessionId }: EventChipProps) { ); } - const filteredEvents = (eventsInPastWithoutAssignedSession.data || []).filter((ev: Event) => - ev.name.toLowerCase().includes(searchQuery.toLowerCase()) - ); + const filteredEvents = (eventsInPastWithoutAssignedSession.data || []) + .filter((ev: Event) => ev.name.toLowerCase().includes(searchQuery.toLowerCase())) + .sort((a, b) => { + const dateA = new Date(a.start_date); + const dateB = new Date(b.start_date); + if (isNaN(dateA.getTime()) && isNaN(dateB.getTime())) { + return 0; + } + if (isNaN(dateA.getTime())) { + return 1; + } + if (isNaN(dateB.getTime())) { + return -1; + } + return dateB.getTime() - dateA.getTime(); + }); if (filteredEvents.length === 0) { return (