2023-05-08 04:07:45 -04:00
|
|
|
import { orderBy, groupBy } 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'
|
2023-05-09 04:56:40 -04:00
|
|
|
import { Selection } from '../services/types/selection'
|
2023-12-05 04:39:12 -05:00
|
|
|
import { Update } from '../services/types/update'
|
2023-04-11 04:35:33 -04:00
|
|
|
|
|
|
|
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-18 04:14:07 -04:00
|
|
|
export const isLabel = (label: LoadedLabel): label is Label => {
|
|
|
|
return !isPseudoLabel(label)
|
|
|
|
}
|
|
|
|
|
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-12-05 04:39:12 -05:00
|
|
|
mostRecentVersion: Nullable<number>
|
2023-04-11 04:35:33 -04:00
|
|
|
) => {
|
|
|
|
if (!labels.length || labels[0].version !== mostRecentVersion) {
|
|
|
|
const pseudoCurrentStateLabel: PseudoCurrentStateLabel = {
|
|
|
|
id: '1',
|
|
|
|
isPseudoCurrentStateLabel: true,
|
|
|
|
version: mostRecentVersion,
|
|
|
|
created_at: new Date().toISOString(),
|
2023-12-05 04:39:12 -05:00
|
|
|
lastUpdatedTimestamp: null,
|
2023-04-11 04:35:33 -04:00
|
|
|
}
|
|
|
|
return [pseudoCurrentStateLabel, ...labels]
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2023-12-05 04:39:12 -05:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
})
|
2023-12-04 08:15:20 -05:00
|
|
|
}
|
|
|
|
|
2023-12-05 04:39:12 -05:00
|
|
|
export const loadLabels = (labels: Label[], updates: Update[]) => {
|
|
|
|
const lastUpdateToV = updates.length ? updates[0].toV : null
|
2023-04-11 04:35:33 -04:00
|
|
|
const sortedLabels = sortLabelsByVersionAndDate(labels)
|
|
|
|
const labelsWithoutPseudoLabel =
|
|
|
|
deletePseudoCurrentStateLabelIfExistent(sortedLabels)
|
|
|
|
const labelsWithPseudoLabelIfNeeded = addPseudoCurrentStateLabelIfNeeded(
|
|
|
|
labelsWithoutPseudoLabel,
|
2023-12-05 04:39:12 -05:00
|
|
|
lastUpdateToV
|
2023-04-11 04:35:33 -04:00
|
|
|
)
|
2023-12-04 08:15:20 -05:00
|
|
|
const labelsWithLastUpdatedTimestamp = addLastUpdatedTimestamp(
|
|
|
|
labelsWithPseudoLabelIfNeeded,
|
2023-12-05 04:39:12 -05:00
|
|
|
updates
|
2023-12-04 08:15:20 -05:00
|
|
|
)
|
|
|
|
return labelsWithLastUpdatedTimestamp
|
2023-04-11 04:35:33 -04:00
|
|
|
}
|
2023-04-26 03:52:25 -04:00
|
|
|
|
2023-05-08 04:07:45 -04:00
|
|
|
export const getVersionWithLabels = (labels: Nullable<LoadedLabel[]>) => {
|
|
|
|
let versionWithLabels: { version: number; labels: LoadedLabel[] }[] = []
|
|
|
|
|
|
|
|
if (labels) {
|
|
|
|
const groupedLabelsHash = groupBy(labels, 'version')
|
|
|
|
versionWithLabels = Object.keys(groupedLabelsHash).map(key => ({
|
|
|
|
version: parseInt(key, 10),
|
|
|
|
labels: groupedLabelsHash[key],
|
|
|
|
}))
|
|
|
|
versionWithLabels = orderBy(versionWithLabels, ['version'], ['desc'])
|
|
|
|
}
|
|
|
|
|
|
|
|
return versionWithLabels
|
|
|
|
}
|
2023-05-09 04:56:40 -04:00
|
|
|
|
|
|
|
export const isAnyVersionMatchingSelection = (
|
|
|
|
labels: Nullable<LoadedLabel[]>,
|
|
|
|
selection: Selection
|
|
|
|
) => {
|
|
|
|
// build an Array<number> of available versions
|
|
|
|
const versions = getVersionWithLabels(labels).map(v => v.version)
|
|
|
|
const selectedVersion = selection.updateRange?.toV
|
|
|
|
|
|
|
|
return selectedVersion && !versions.includes(selectedVersion)
|
|
|
|
}
|