Skip to content

Commit

Permalink
add setNextAvailableHistoryId function
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedhamidawan committed Jul 24, 2024
1 parent c2adaf6 commit d749c97
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions client/src/stores/historyStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});
Expand Down

0 comments on commit d749c97

Please sign in to comment.