|
|
@@ -2,55 +2,56 @@ import { useTranslation } from 'react-i18next'
|
|
|
import { useHistoryContext } from '../../context/history-context'
|
|
|
import { getUpdateForVersion } from '../../utils/history-details'
|
|
|
import { computeUpdateRange } from '../../utils/range'
|
|
|
+import { isAnyVersionMatchingSelection } from '../../utils/label'
|
|
|
+import { HistoryContextValue } from '../../context/types/history-context-value'
|
|
|
|
|
|
-type ToggleSwitchProps = {
|
|
|
- labelsOnly: boolean
|
|
|
- setLabelsOnly: React.Dispatch<
|
|
|
- React.SetStateAction<ToggleSwitchProps['labelsOnly']>
|
|
|
- >
|
|
|
-}
|
|
|
+type ToggleSwitchProps = Pick<
|
|
|
+ HistoryContextValue,
|
|
|
+ 'labelsOnly' | 'setLabelsOnly'
|
|
|
+>
|
|
|
|
|
|
function ToggleSwitch({ labelsOnly, setLabelsOnly }: ToggleSwitchProps) {
|
|
|
const { t } = useTranslation()
|
|
|
- const { selection, setSelection, resetSelection, updatesInfo } =
|
|
|
+ const { selection, setSelection, resetSelection, updatesInfo, labels } =
|
|
|
useHistoryContext()
|
|
|
|
|
|
const handleChange = (isLabelsOnly: boolean) => {
|
|
|
- let isSelectionReset = false
|
|
|
-
|
|
|
- // using the switch toggle should reset the selection when in `compare` mode
|
|
|
if (selection.comparing) {
|
|
|
- isSelectionReset = true
|
|
|
+ // using the switch toggle should reset the selection when in `compare` mode
|
|
|
resetSelection()
|
|
|
- }
|
|
|
-
|
|
|
- // in labels only mode the `fromV` is equal to `toV` value
|
|
|
- // switching to all history mode and triggering immediate comparison with
|
|
|
- // an older version would cause a bug if the computation below is skipped
|
|
|
- if (!isLabelsOnly && !isSelectionReset) {
|
|
|
- const update = selection.updateRange?.toV
|
|
|
- ? getUpdateForVersion(selection.updateRange.toV, updatesInfo.updates)
|
|
|
- : null
|
|
|
+ } else {
|
|
|
+ if (isLabelsOnly) {
|
|
|
+ if (isAnyVersionMatchingSelection(labels, selection)) {
|
|
|
+ resetSelection()
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // in labels only mode the `fromV` is equal to `toV` value
|
|
|
+ // switching to all history mode and triggering immediate comparison with
|
|
|
+ // an older version would cause a bug if the computation below is skipped.
|
|
|
+ const update = selection.updateRange?.toV
|
|
|
+ ? getUpdateForVersion(selection.updateRange.toV, updatesInfo.updates)
|
|
|
+ : null
|
|
|
|
|
|
- const { updateRange } = selection
|
|
|
+ const { updateRange } = selection
|
|
|
|
|
|
- if (
|
|
|
- updateRange &&
|
|
|
- update &&
|
|
|
- (update.fromV !== updateRange.fromV || update.toV !== updateRange.toV)
|
|
|
- ) {
|
|
|
- const range = computeUpdateRange(
|
|
|
- updateRange,
|
|
|
- update.fromV,
|
|
|
- update.toV,
|
|
|
- update.meta.end_ts
|
|
|
- )
|
|
|
+ if (
|
|
|
+ updateRange &&
|
|
|
+ update &&
|
|
|
+ (update.fromV !== updateRange.fromV || update.toV !== updateRange.toV)
|
|
|
+ ) {
|
|
|
+ const range = computeUpdateRange(
|
|
|
+ updateRange,
|
|
|
+ update.fromV,
|
|
|
+ update.toV,
|
|
|
+ update.meta.end_ts
|
|
|
+ )
|
|
|
|
|
|
- setSelection({
|
|
|
- updateRange: range,
|
|
|
- comparing: false,
|
|
|
- files: [],
|
|
|
- })
|
|
|
+ setSelection({
|
|
|
+ updateRange: range,
|
|
|
+ comparing: false,
|
|
|
+ files: [],
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|