2023-04-11 04:35:33 -04:00
|
|
|
import { orderBy } from 'lodash'
|
2023-04-12 04:34:56 -04:00
|
|
|
import {
|
|
|
|
LoadedLabel,
|
|
|
|
Label,
|
|
|
|
PseudoCurrentStateLabel,
|
|
|
|
} from '../services/types/label'
|
2023-04-11 04:35:33 -04:00
|
|
|
import { Nullable } from '../../../../../types/utils'
|
|
|
|
|
|
|
|
export const isPseudoLabel = (
|
2023-04-12 04:34:56 -04:00
|
|
|
label: LoadedLabel
|
2023-04-11 04:35:33 -04:00
|
|
|
): label is PseudoCurrentStateLabel => {
|
|
|
|
return (label as PseudoCurrentStateLabel).isPseudoCurrentStateLabel === true
|
|
|
|
}
|
|
|
|
|
2023-04-12 04:34:56 -04:00
|
|
|
const sortLabelsByVersionAndDate = (labels: LoadedLabel[]) => {
|
2023-04-11 04:35:33 -04:00
|
|
|
return orderBy(
|
|
|
|
labels,
|
|
|
|
['isPseudoCurrentStateLabel', 'version', 'created_at'],
|
|
|
|
['asc', 'desc', 'desc']
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-12 04:34:56 -04:00
|
|
|
const deletePseudoCurrentStateLabelIfExistent = (labels: LoadedLabel[]) => {
|
2023-04-11 04:35:33 -04:00
|
|
|
if (labels.length && isPseudoLabel(labels[0])) {
|
|
|
|
const [, ...rest] = labels
|
|
|
|
return rest
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
const addPseudoCurrentStateLabelIfNeeded = (
|
2023-04-12 04:34:56 -04:00
|
|
|
labels: LoadedLabel[],
|
2023-04-11 04:35:33 -04:00
|
|
|
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
|
|
|
|
}
|