From 3a6957558830de4af0f7a218c497f1e8959e23ec Mon Sep 17 00:00:00 2001 From: Ahmed Awan Date: Wed, 24 Jul 2024 12:25:46 -0500 Subject: [PATCH] add `setNextAvailableHistoryId` function --- client/src/stores/historyStore.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/client/src/stores/historyStore.ts b/client/src/stores/historyStore.ts index f6e9f7e11962..72b23e073dd7 100644 --- a/client/src/stores/historyStore.ts +++ b/client/src/stores/historyStore.ts @@ -195,18 +195,22 @@ export const useHistoryStore = defineStore("historyStore", () => { return filteredHistoryIds[0]; } + async function setNextAvailableHistoryId(excludedIds: string[]) { + if (currentHistoryId.value && excludedIds.includes(currentHistoryId.value)) { + const nextAvailableHistoryId = getNextAvailableHistoryId(excludedIds); + if (nextAvailableHistoryId) { + await setCurrentHistory(nextAvailableHistoryId); + } else { + await createNewHistory(); + } + } + } + async function deleteHistory(historyId: string, purge = false) { try { const { data } = await deleteHistoryById({ history_id: historyId, purge }); const deletedHistory = data as AnyHistory; - if (currentHistoryId.value === historyId) { - const nextAvailableHistoryId = getNextAvailableHistoryId([deletedHistory.id]); - if (nextAvailableHistoryId) { - await setCurrentHistory(nextAvailableHistoryId); - } else { - await createNewHistory(); - } - } + await setNextAvailableHistoryId([deletedHistory.id]); del(storedHistories.value, deletedHistory.id); await handleTotalCountChange(1, true); } catch (error) { @@ -219,14 +223,7 @@ export const useHistoryStore = defineStore("historyStore", () => { const { data } = await deleteHistoriesByIds({ ids, purge }); const deletedHistories = data as AnyHistory[]; const historyIds = deletedHistories.map((x) => String(x.id)); - if (currentHistoryId.value && historyIds.includes(currentHistoryId.value)) { - const nextAvailableHistoryId = getNextAvailableHistoryId(historyIds); - if (nextAvailableHistoryId) { - await setCurrentHistory(nextAvailableHistoryId); - } else { - await createNewHistory(); - } - } + await setNextAvailableHistoryId(historyIds); deletedHistories.forEach((history) => { del(storedHistories.value, history.id); });