Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 0feb4e2

Browse files
authored
feat(gui): inform user when previous/next goes back to end/start of list (#789)
1 parent 2fbdd4c commit 0feb4e2

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

api-editor/gui/src/features/menuBar/MenuBar.tsx

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
MenuOptionGroup,
1515
Spacer,
1616
useColorMode,
17+
useToast,
1718
} from '@chakra-ui/react';
1819
import React from 'react';
1920
import { FaArrowLeft, FaArrowRight, FaArrowUp, FaChevronDown, FaRedo, FaUndo } from 'react-icons/fa';
@@ -69,6 +70,7 @@ export const MenuBar: React.FC<MenuBarProps> = function ({ displayInferErrors })
6970
const { colorMode, toggleColorMode } = useColorMode();
7071
const dispatch = useAppDispatch();
7172
const navigate = useNavigate();
73+
const toast = useToast();
7274

7375
const annotationStore = useAppSelector(selectAnnotationStore);
7476
const sortingMode = useAppSelector(selectSortingMode);
@@ -119,8 +121,24 @@ export const MenuBar: React.FC<MenuBarProps> = function ({ displayInferErrors })
119121
return;
120122
}
121123

122-
let navStr = getPreviousElementPath(allDeclarations, declaration, pythonFilter, annotations, usages);
124+
let { id: navStr, wrappedAround } = getPreviousElementPath(
125+
allDeclarations,
126+
declaration,
127+
pythonFilter,
128+
annotations,
129+
usages,
130+
);
123131
if (navStr !== null) {
132+
if (wrappedAround) {
133+
toast({
134+
title: 'Start of List',
135+
description: 'You reached the start of the list. Going back to its end\u2026',
136+
status: 'info',
137+
duration: 4000,
138+
isClosable: true,
139+
});
140+
}
141+
124142
//navigate to element
125143
navigate(`/${navStr}`);
126144

@@ -134,8 +152,24 @@ export const MenuBar: React.FC<MenuBarProps> = function ({ displayInferErrors })
134152
return;
135153
}
136154

137-
let navStr = getNextElementPath(allDeclarations, declaration, pythonFilter, annotations, usages);
155+
let { id: navStr, wrappedAround } = getNextElementPath(
156+
allDeclarations,
157+
declaration,
158+
pythonFilter,
159+
annotations,
160+
usages,
161+
);
138162
if (navStr !== null) {
163+
if (wrappedAround) {
164+
toast({
165+
title: 'End of List',
166+
description: 'You reached the end of the list. Going back to its start\u2026',
167+
status: 'info',
168+
duration: 4000,
169+
isClosable: true,
170+
});
171+
}
172+
139173
//navigate to element
140174
navigate(`/${navStr}`);
141175

@@ -460,17 +494,23 @@ const getPreviousElementPath = function (
460494
filter: AbstractPythonFilter,
461495
annotations: AnnotationStore,
462496
usages: UsageCountStore,
463-
): string {
497+
): { id: string; wrappedAround: boolean } {
464498
let currentIndex = getPreviousIndex(declarations, getIndex(declarations, start));
465499
let current = getElementAtIndex(declarations, currentIndex);
500+
let wrappedAround = false;
466501
while (current !== null && current !== start) {
467502
if (filter.shouldKeepDeclaration(current, annotations, usages)) {
468-
return current.id;
503+
return { id: current.id, wrappedAround };
469504
}
505+
506+
const oldIndex = currentIndex;
470507
currentIndex = getPreviousIndex(declarations, currentIndex);
471508
current = getElementAtIndex(declarations, currentIndex);
509+
if (oldIndex !== null && currentIndex !== null && currentIndex >= oldIndex) {
510+
wrappedAround = true;
511+
}
472512
}
473-
return start.id;
513+
return { id: start.id, wrappedAround };
474514
};
475515

476516
const getNextElementPath = function (
@@ -479,17 +519,23 @@ const getNextElementPath = function (
479519
filter: AbstractPythonFilter,
480520
annotations: AnnotationStore,
481521
usages: UsageCountStore,
482-
): string {
522+
): { id: string; wrappedAround: boolean } {
483523
let currentIndex = getNextIndex(declarations, getIndex(declarations, start));
484524
let current = getElementAtIndex(declarations, currentIndex);
525+
let wrappedAround = false;
485526
while (current !== null && current !== start) {
486527
if (filter.shouldKeepDeclaration(current, annotations, usages)) {
487-
return current.id;
528+
return { id: current.id, wrappedAround };
488529
}
530+
531+
const oldIndex = currentIndex;
489532
currentIndex = getNextIndex(declarations, currentIndex);
490533
current = getElementAtIndex(declarations, currentIndex);
534+
if (oldIndex !== null && currentIndex !== null && currentIndex <= oldIndex) {
535+
wrappedAround = true;
536+
}
491537
}
492-
return start.id;
538+
return { id: start.id, wrappedAround };
493539
};
494540

495541
const getPreviousIndex = function (declarations: PythonDeclaration[], currentIndex: number | null): number | null {

0 commit comments

Comments
 (0)