Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: prevent undefined selection crash on keyboard navigation in virtualized tree (#10385)

Mohd Sultan4ca562ac4e7f
diff --git a/apps/web/src/components/virtualized-tree/index.tsx b/apps/web/src/components/virtualized-tree/index.tsx
index ee0f6dc61..76ad76909 100644
--- a/apps/web/src/components/virtualized-tree/index.tsx
+++ b/apps/web/src/components/virtualized-tree/index.tsx
@@ -163,12 +163,12 @@ export function VirtualizedTree<T>(props: TreeViewProps<T>) {
length: nodes.length,
reset: () => deselectAll?.(),
deselect: (index) => {
- const id = nodes[index].id;
+ const id = nodes[index]?.id;
if (!id) return;
onDeselect?.(id);
},
select: (index, toggleable) => {
- const id = nodes[index].id;
+ const id = nodes[index]?.id;
if (!id) return;
if (toggleable && isSelected?.(id)) onDeselect?.(id);
else onSelect?.(id);
@@ -181,7 +181,7 @@ export function VirtualizedTree<T>(props: TreeViewProps<T>) {
bulkSelect?.(ids);
},
focusItemAt: (index) => {
- const id = nodes[index].id;
+ const id = nodes[index]?.id;
if (!id || !list.current) return;
waitForElement(list.current, index, `id_${id}`, (element) =>
@@ -190,7 +190,7 @@ export function VirtualizedTree<T>(props: TreeViewProps<T>) {
},
skip: () => false,
open: (index) => {
- const id = nodes[index].id;
+ const id = nodes[index]?.id;
if (!id || !list.current) return;
waitForElement(list.current, index, `id_${id}`, (element) =>
diff --git a/apps/web/src/hooks/use-keyboard-list-navigation.ts b/apps/web/src/hooks/use-keyboard-list-navigation.ts
index 590491af3..a426035a4 100644
--- a/apps/web/src/hooks/use-keyboard-list-navigation.ts
+++ b/apps/web/src/hooks/use-keyboard-list-navigation.ts
@@ -68,12 +68,12 @@ export function useKeyboardListNavigation(
}, [reset]);
const openActiveItem = useCallback(() => {
- if (!open) return false;
+ if (!open || cursor.current < 0 || cursor.current >= length) return false;
resetSelection();
open(cursor.current);
select(cursor.current);
return true;
- }, [open, resetSelection, select]);
+ }, [open, resetSelection, select, length]);
const onMouseUp = useCallback(
(e: MouseEvent, itemIndex: number) => {
@@ -136,10 +136,16 @@ export function useKeyboardListNavigation(
"Shift-ArrowUp": () => {
if (anchor.current === -1) {
anchor.current = cursor.current;
- select(anchor.current);
+ if (anchor.current >= 0 && anchor.current < length) {
+ select(anchor.current);
+ }
}
- if (direction() === DIRECTION.DOWN) {
+ if (
+ direction() === DIRECTION.DOWN &&
+ cursor.current >= 0 &&
+ cursor.current < length
+ ) {
deselect(cursor.current);
}
@@ -158,10 +164,16 @@ export function useKeyboardListNavigation(
"Shift-ArrowDown": () => {
if (anchor.current === -1) {
anchor.current = cursor.current;
- select(anchor.current);
+ if (anchor.current >= 0 && anchor.current < length) {
+ select(anchor.current);
+ }
}
- if (direction() === DIRECTION.UP) {
+ if (
+ direction() === DIRECTION.UP &&
+ cursor.current >= 0 &&
+ cursor.current < length
+ ) {
deselect(cursor.current);
}