diff --git a/services/web/frontend/js/features/history/context/history-context.tsx b/services/web/frontend/js/features/history/context/history-context.tsx index f392a12bf2..262eaf7c73 100644 --- a/services/web/frontend/js/features/history/context/history-context.tsx +++ b/services/web/frontend/js/features/history/context/history-context.tsx @@ -180,13 +180,8 @@ function useHistory() { Promise.all([updatesPromise, labelsPromise]) .then(([{ updates: updatesData, nextBeforeTimestamp }, labels]) => { - const lastUpdateToV = updatesData.length ? updatesData[0].toV : null - const lastUpdatedTimestamp = updatesData.length - ? updatesData[0].meta.end_ts - : null - if (labels) { - setLabels(loadLabels(labels, lastUpdateToV, lastUpdatedTimestamp)) + setLabels(loadLabels(labels, updatesData)) } const { updates, visibleUpdateCount, freeHistoryLimitHit } = diff --git a/services/web/frontend/js/features/history/hooks/use-add-or-remove-labels.ts b/services/web/frontend/js/features/history/hooks/use-add-or-remove-labels.ts index ccad03f2de..6673989bd5 100644 --- a/services/web/frontend/js/features/history/hooks/use-add-or-remove-labels.ts +++ b/services/web/frontend/js/features/history/hooks/use-add-or-remove-labels.ts @@ -36,11 +36,7 @@ function useAddOrRemoveLabels() { if (labels) { const nonPseudoLabels = labels.filter(isLabel) const processedNonPseudoLabels = labelsHandler(nonPseudoLabels) - const newLabels = loadLabels( - processedNonPseudoLabels, - tempUpdates[0].toV, - tempUpdates[0].meta.end_ts - ) + const newLabels = loadLabels(processedNonPseudoLabels, tempUpdates) setLabels(newLabels) return newLabels diff --git a/services/web/frontend/js/features/history/utils/label.ts b/services/web/frontend/js/features/history/utils/label.ts index 613ef3e36d..75435970a7 100644 --- a/services/web/frontend/js/features/history/utils/label.ts +++ b/services/web/frontend/js/features/history/utils/label.ts @@ -6,6 +6,7 @@ import { } from '../services/types/label' import { Nullable } from '../../../../../types/utils' import { Selection } from '../services/types/selection' +import { Update } from '../services/types/update' export const isPseudoLabel = ( label: LoadedLabel @@ -35,8 +36,7 @@ const deletePseudoCurrentStateLabelIfExistent = (labels: LoadedLabel[]) => { const addPseudoCurrentStateLabelIfNeeded = ( labels: LoadedLabel[], - mostRecentVersion: Nullable, - lastUpdatedTimestamp: Nullable + mostRecentVersion: Nullable ) => { if (!labels.length || labels[0].version !== mostRecentVersion) { const pseudoCurrentStateLabel: PseudoCurrentStateLabel = { @@ -44,39 +44,38 @@ const addPseudoCurrentStateLabelIfNeeded = ( isPseudoCurrentStateLabel: true, version: mostRecentVersion, created_at: new Date().toISOString(), - lastUpdatedTimestamp, + lastUpdatedTimestamp: null, } return [pseudoCurrentStateLabel, ...labels] } return labels } -const addLastUpdatedTimestamp = ( - labels: LoadedLabel[], - lastUpdatedTimestamp: Nullable -) => { - return labels.map(label => ({ - ...label, - lastUpdatedTimestamp, - })) +const addLastUpdatedTimestamp = (labels: LoadedLabel[], updates: Update[]) => { + return labels.map(label => { + const lastUpdatedTimestamp = updates.find(update => + update.labels.find(l => l.id === label.id) + )?.meta.end_ts + + return { + ...label, + lastUpdatedTimestamp: lastUpdatedTimestamp || null, + } + }) } -export const loadLabels = ( - labels: Label[], - lastUpdateToV: Nullable, - lastUpdatedTimestamp: Nullable -) => { +export const loadLabels = (labels: Label[], updates: Update[]) => { + const lastUpdateToV = updates.length ? updates[0].toV : null const sortedLabels = sortLabelsByVersionAndDate(labels) const labelsWithoutPseudoLabel = deletePseudoCurrentStateLabelIfExistent(sortedLabels) const labelsWithPseudoLabelIfNeeded = addPseudoCurrentStateLabelIfNeeded( labelsWithoutPseudoLabel, - lastUpdateToV, - lastUpdatedTimestamp + lastUpdateToV ) const labelsWithLastUpdatedTimestamp = addLastUpdatedTimestamp( labelsWithPseudoLabelIfNeeded, - lastUpdatedTimestamp + updates ) return labelsWithLastUpdatedTimestamp }