Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(soup): focus after mark done not going to the correct item (#4805)

dev-rba73b6c6b9959
diff --git a/apps/web/src/features/next-soup/actions/make-mark-done-action.ts b/apps/web/src/features/next-soup/actions/make-mark-done-action.ts
index 24a874ba29..347c9909a4 100644
--- a/apps/web/src/features/next-soup/actions/make-mark-done-action.ts
+++ b/apps/web/src/features/next-soup/actions/make-mark-done-action.ts
@@ -218,10 +218,34 @@ export const makeMarkDoneAction = (options: MakeMarkDoneOptions) => {
onNavigate?: (entity: EntityData) => void,
opts?: MarkDoneExecuteOpts
) => {
- const currentIndex = soup.focus.index();
const focusedIdBeforeMarkDone = soup.focus.id();
- const nextRow =
- soup.items.at(currentIndex + 1) ?? soup.items.at(currentIndex - 1);
+ const markedEntityIds = new Set(entities.map((entity) => entity.id));
+ const adjacentRow = (direction: 1 | -1) => {
+ let previousCandidateIndex: number | undefined;
+
+ for (let distance = 1; distance <= soup.items.count(); distance++) {
+ const candidate = soup.navigate.peekOffset(direction * distance, {
+ wrapNavigation: false,
+ skipGroupHeaders: true,
+ skipLoadMore: true,
+ });
+
+ // Peeking clamps at list boundaries, so a repeated index means there
+ // are no more candidates in this direction.
+ if (!candidate || candidate.index === previousCandidateIndex) return;
+ previousCandidateIndex = candidate.index;
+
+ if (
+ candidate.row.id === focusedIdBeforeMarkDone ||
+ markedEntityIds.has(candidate.row.original.id)
+ ) {
+ continue;
+ }
+
+ return candidate.row;
+ }
+ };
+ const nextRow = adjacentRow(1) ?? adjacentRow(-1);
if (soup.collapseEntity.shouldCollapse()) {
const collapse = soup.collapseEntity.callback();
diff --git a/apps/web/src/features/next-soup/create-soup-state.ts b/apps/web/src/features/next-soup/create-soup-state.ts
index 93d3611bba..bec7e6d9b6 100644
--- a/apps/web/src/features/next-soup/create-soup-state.ts
+++ b/apps/web/src/features/next-soup/create-soup-state.ts
@@ -55,6 +55,12 @@ export type SoupRow = {
type NavigationResult = { row: SoupRow; index: number } | undefined;
+type NavigationOptions = {
+ wrapNavigation?: boolean;
+ skipGroupHeaders?: boolean;
+ skipLoadMore?: boolean;
+};
+
export type SortConfig<T> = {
id: string;
fn: (a: T, b: T) => number;
@@ -213,43 +219,49 @@ export const createSoupState = <TId extends string = FilterID>(
return result;
};
- const peek = (offset: number): NavigationResult => {
- const current = focusedIndex();
- if (current === -1) {
- return calculateFocusRow(offset > 0 ? 0 : rows().length - 1);
- }
- return calculateFocusRow(current + offset);
- };
-
- const shouldSkipRow = (row: SoupRow): boolean => {
+ const shouldSkipRow = (
+ row: SoupRow,
+ options?: NavigationOptions
+ ): boolean => {
+ if (row.getIsLoadMore() && options?.skipLoadMore) return true;
if (!row.group) return false;
if (row.getIsGrouped()) {
- return !!skipGroupHeaders;
+ return options?.skipGroupHeaders ?? !!skipGroupHeaders;
}
return !row.group.isExpanded();
};
- const findNextIndex = (startIndex: number, offset: number): number => {
+ const findNextIndex = (
+ startIndex: number,
+ offset: number,
+ options?: NavigationOptions
+ ): number => {
const allRows = rows();
if (allRows.length === 0) return -1;
+ if (offset === 0) return startIndex;
const direction = offset > 0 ? 1 : -1;
let steps = Math.abs(offset);
let cursor = startIndex;
- let lastValid = startIndex;
-
- while (steps > 0) {
+ let lastValid = shouldSkipRow(allRows[startIndex], options)
+ ? -1
+ : startIndex;
+ let iterations = 0;
+ const maxIterations = allRows.length * steps;
+ const shouldWrap = options?.wrapNavigation ?? wrapNavigation;
+
+ while (steps > 0 && iterations < maxIterations) {
+ iterations++;
cursor += direction;
if (cursor < 0 || cursor >= allRows.length) {
- if (!wrapNavigation) break;
+ if (!shouldWrap) break;
cursor = (cursor + allRows.length) % allRows.length;
}
const row = allRows[cursor];
if (!row) break;
-
- if (shouldSkipRow(row)) continue;
+ if (shouldSkipRow(row, options)) continue;
lastValid = cursor;
steps--;
@@ -258,7 +270,10 @@ export const createSoupState = <TId extends string = FilterID>(
return lastValid;
};
- const navigateBy = (offset: number): NavigationResult => {
+ const peek = (
+ offset: number,
+ options?: NavigationOptions
+ ): NavigationResult => {
const current = focusedIndex();
const allRows = rows();
@@ -266,15 +281,29 @@ export const createSoupState = <TId extends string = FilterID>(
const startIndex = offset > 0 ? 0 : allRows.length - 1;
const direction = offset > 0 ? 1 : -1;
let i = startIndex;
- while (i >= 0 && i < allRows.length && shouldSkipRow(allRows[i])) {
+ while (
+ i >= 0 &&
+ i < allRows.length &&
+ shouldSkipRow(allRows[i], options)
+ ) {
i += direction;
}
if (i < 0 || i >= allRows.length) return;
- return setFocus(i);
+
+ return calculateFocusRow(i);
}
- const nextIndex = findNextIndex(current, offset);
- return setFocus(nextIndex);
+ const nextIndex = findNextIndex(current, offset, options);
+ if (nextIndex < 0) return;
+
+ return calculateFocusRow(nextIndex);
+ };
+
+ const navigateBy = (offset: number): NavigationResult => {
+ const peeked = peek(offset);
+ if (!peeked) return;
+
+ return setFocus(peeked.index);
};
const clearFocus = () => {