Skip to content

Commit

Permalink
upsertObjectArray -> syncResourceArray
Browse files Browse the repository at this point in the history
  • Loading branch information
fjsj committed Jan 3, 2025
1 parent 50667e4 commit b1535b5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
10 changes: 5 additions & 5 deletions contexts/ChatContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)]]);
});
},
{
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -469,7 +469,7 @@ export function ChatProvider({
const existing = prev.get(currentThreadId) || [];
return new Map([
...prev,
[currentThreadId, upsertObjectArray(existing, updatedCommunication)],
[currentThreadId, syncResourceArray(existing, updatedCommunication)],
]);
});
},
Expand Down
19 changes: 17 additions & 2 deletions utils/array.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
export const upsertObjectArray = <T extends { id?: string }>(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 = <T extends Resource>(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];
};

0 comments on commit b1535b5

Please sign in to comment.