From b1535b55089fe35e6bfa9b2bb8c96370ccd5bb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Juvenal?= Date: Fri, 3 Jan 2025 17:10:04 -0300 Subject: [PATCH] upsertObjectArray -> syncResourceArray --- contexts/ChatContext.tsx | 10 +++++----- utils/array.ts | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/contexts/ChatContext.tsx b/contexts/ChatContext.tsx index ecec8fb..bab408f 100644 --- a/contexts/ChatContext.tsx +++ b/contexts/ChatContext.tsx @@ -17,7 +17,7 @@ import { useMedplum, useSubscription } from "@medplum/react-hooks"; import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react"; import type { ChatMessage, Thread } from "@/types/chat"; -import { upsertObjectArray } from "@/utils/array"; +import { syncResourceArray } from "@/utils/array"; import { getQueryString } from "@/utils/url"; function communicationToThread( @@ -321,7 +321,7 @@ export function ChatProvider({ // If this is a thread (no partOf), update thread list if (!communication.partOf?.length) { - setThreads((prev) => upsertObjectArray(prev, communication)); + setThreads((prev) => syncResourceArray(prev, communication)); return; } @@ -348,7 +348,7 @@ export function ChatProvider({ } else if (isIncoming) { onMessageReceived?.(communication); } - return new Map([...prev, [threadId, upsertObjectArray(existing, communication)]]); + return new Map([...prev, [threadId, syncResourceArray(existing, communication)]]); }); }, { @@ -439,7 +439,7 @@ export function ChatProvider({ }); setThreadCommMap((prev) => { const existing = prev.get(currentThreadId) || []; - return new Map([...prev, [currentThreadId, upsertObjectArray(existing, newCommunication)]]); + return new Map([...prev, [currentThreadId, syncResourceArray(existing, newCommunication)]]); }); setMessage(""); }, [message, profile, currentThreadId, medplum]); @@ -469,7 +469,7 @@ export function ChatProvider({ const existing = prev.get(currentThreadId) || []; return new Map([ ...prev, - [currentThreadId, upsertObjectArray(existing, updatedCommunication)], + [currentThreadId, syncResourceArray(existing, updatedCommunication)], ]); }); }, diff --git a/utils/array.ts b/utils/array.ts index 808fa8c..6cff26a 100644 --- a/utils/array.ts +++ b/utils/array.ts @@ -1,7 +1,22 @@ -export const upsertObjectArray = (arr: T[], obj: T): T[] => { +import { Resource } from "@medplum/fhirtypes"; + +/** + * Syncs an array of resources by adding the new resource if it doesn't exist, + * or replacing it if it does and has a newer lastUpdated date. + * @param arr - The array of resources. + * @param obj - The resource to sync. + * @returns The updated array of resources. + */ +export const syncResourceArray = (arr: T[], obj: T): T[] => { const existingIndex = arr.findIndex((t) => t.id != null && obj.id != null && t.id === obj.id); if (existingIndex !== -1) { - return [...arr.slice(0, existingIndex), obj, ...arr.slice(existingIndex + 1)]; + if ( + obj.meta?.lastUpdated != null && + arr[existingIndex].meta?.lastUpdated != null && + obj.meta?.lastUpdated > arr[existingIndex].meta?.lastUpdated + ) { + return [...arr.slice(0, existingIndex), obj, ...arr.slice(existingIndex + 1)]; + } } return [...arr, obj]; };