overleaf/services/web/frontend/js/features/history/utils/label.ts
ilkin-overleaf 8e0aa685ce Merge pull request #12549 from overleaf/ii-history-react-list-of-all-versions
[web] All versions of history entries

GitOrigin-RevId: 7365ac4913c115b3b2872a3713d893463719c15e
2023-04-12 08:03:47 +00:00

60 lines
1.7 KiB
TypeScript

import { orderBy } from 'lodash'
import { Label, PseudoCurrentStateLabel } from '../services/types/update'
import { Nullable } from '../../../../../types/utils'
export const isPseudoLabel = (
label: Label | PseudoCurrentStateLabel
): label is PseudoCurrentStateLabel => {
return (label as PseudoCurrentStateLabel).isPseudoCurrentStateLabel === true
}
const sortLabelsByVersionAndDate = (
labels: Array<Label | PseudoCurrentStateLabel>
) => {
return orderBy(
labels,
['isPseudoCurrentStateLabel', 'version', 'created_at'],
['asc', 'desc', 'desc']
)
}
const deletePseudoCurrentStateLabelIfExistent = (
labels: Array<Label | PseudoCurrentStateLabel>
) => {
if (labels.length && isPseudoLabel(labels[0])) {
const [, ...rest] = labels
return rest
}
return labels
}
const addPseudoCurrentStateLabelIfNeeded = (
labels: Array<Label | PseudoCurrentStateLabel>,
mostRecentVersion: Nullable<number>
) => {
if (!labels.length || labels[0].version !== mostRecentVersion) {
const pseudoCurrentStateLabel: PseudoCurrentStateLabel = {
id: '1',
isPseudoCurrentStateLabel: true,
version: mostRecentVersion,
created_at: new Date().toISOString(),
}
return [pseudoCurrentStateLabel, ...labels]
}
return labels
}
export const loadLabels = (
labels: Label[],
lastUpdateToV: Nullable<number>
) => {
const sortedLabels = sortLabelsByVersionAndDate(labels)
const labelsWithoutPseudoLabel =
deletePseudoCurrentStateLabelIfExistent(sortedLabels)
const labelsWithPseudoLabelIfNeeded = addPseudoCurrentStateLabelIfNeeded(
labelsWithoutPseudoLabel,
lastUpdateToV
)
return labelsWithPseudoLabelIfNeeded
}