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(splits): filter split histories on delete items (#322)

Seamus1133d46c232b
diff --git a/js/app/packages/app/component/SoupContext.tsx b/js/app/packages/app/component/SoupContext.tsx
index 51bb7b65ca..c9de8cc42d 100644
--- a/js/app/packages/app/component/SoupContext.tsx
+++ b/js/app/packages/app/component/SoupContext.tsx
@@ -1,3 +1,4 @@
+import { globalSplitManager } from '@app/signal/splitLayout';
import { fileTypeToBlockName } from '@core/constant/allBlocks';
import { HotkeyTags } from '@core/hotkey/constants';
import { activeScope, hotkeyScopeTree } from '@core/hotkey/state';
@@ -59,6 +60,7 @@ import {
} from './command/state';
import { useGlobalNotificationSource } from './GlobalAppState';
import type { SplitHandle } from './split-layout/layoutManager';
+import { globalRemoveFromSplitHistory } from './split-layout/layoutUtils';
import {
createEntityActionRegistry,
type EntityActionRegistry,
@@ -370,6 +372,13 @@ export function createNavigationEntityListShortcut({
entities: entitiesToDelete,
onFinish: () => {
afterEntityAction(next, true);
+ const splitManager = globalSplitManager();
+ if (splitManager) {
+ const entityIdSet = new Set(entitiesToDelete.map(({ id }) => id));
+ globalRemoveFromSplitHistory(splitManager, (entry) =>
+ entityIdSet.has(entry.id)
+ );
+ }
},
onCancel: () => {
afterEntityAction(prev);
diff --git a/js/app/packages/app/component/split-layout/history.ts b/js/app/packages/app/component/split-layout/history.ts
index ae7093717d..b8f9ad67c5 100644
--- a/js/app/packages/app/component/split-layout/history.ts
+++ b/js/app/packages/app/component/split-layout/history.ts
@@ -9,6 +9,7 @@ export type History<T extends object> = {
canGoForward: () => boolean;
push: (next: T) => void;
merge: (next: T) => void;
+ remove: (predicate: (item: T) => boolean) => T | null;
};
const inc = (x: number) => x + 1;
@@ -63,6 +64,38 @@ export function createHistory<T extends object>(): History<T> {
return items[index()];
};
+ const remove = (predicate: (item: T) => boolean) => {
+ const prevItems = items;
+ const prevIndex = index();
+
+ let newIndex = prevIndex;
+ const nextItems: T[] = [];
+
+ for (let i = 0; i < prevItems.length; i++) {
+ const item = prevItems[i];
+ if (predicate(item)) {
+ if (i < prevIndex) {
+ newIndex -= 1;
+ }
+ continue;
+ }
+ nextItems.push(item);
+ }
+
+ if (nextItems.length === 0) {
+ items = [];
+ setIndex(-1);
+ return null;
+ }
+
+ if (newIndex >= nextItems.length) {
+ newIndex = nextItems.length - 1;
+ }
+
+ items = nextItems;
+ setIndex(newIndex);
+ return items[newIndex] ?? null;
+ };
return {
get items() {
return items;
@@ -76,5 +109,6 @@ export function createHistory<T extends object>(): History<T> {
forward,
canGoBack,
canGoForward,
+ remove,
};
}
diff --git a/js/app/packages/app/component/split-layout/layoutManager.ts b/js/app/packages/app/component/split-layout/layoutManager.ts
index d6705d47c0..dde315193a 100644
--- a/js/app/packages/app/component/split-layout/layoutManager.ts
+++ b/js/app/packages/app/component/split-layout/layoutManager.ts
@@ -157,6 +157,7 @@ export type SplitHandle = {
cb: (payload: SplitEventPayload[SplitEvent.ContentChange]) => void
) => void;
replace: (next: SplitContent, mergeHistory?: boolean) => void;
+ removeFromHistory: (predicate: (content: SplitContent) => boolean) => void;
toggleSpotlight: (force?: boolean) => void;
setDisplayName: (name: string) => void;
canGoForward: () => boolean;
@@ -212,15 +213,6 @@ function sameNonComponentIdentity(a: SplitContent, b: SplitContent): boolean {
return a.id === b.id;
}
-function _findDuplicateSplit(
- splits: SplitState[],
- content: SplitContent
-): SplitState | null {
- return (
- splits.find((s) => sameNonComponentIdentity(s.content, content)) ?? null
- );
-}
-
function isDuplicateSplit(
splits: SplitState[],
content: SplitContent
@@ -362,6 +354,20 @@ export function createSplitLayout(
reattach(split, next);
}
+ function removeFromHistory(
+ id: SplitId,
+ predicate: (content: SplitContent) => boolean
+ ) {
+ const i = state.splits.findIndex((s) => s.id === id);
+ if (i < 0) return console.error(`Split with id ${id} not found`);
+
+ const split = state.splits[i];
+ const next = split.history.remove(predicate);
+ if (!next) return;
+
+ reattach(split, next);
+ }
+
/**
* Replace the content of a split with the provided content. If mergeHistory is true, the current history index will be replaced with the new content.
*/
@@ -469,6 +475,9 @@ export function createSplitLayout(
goForward: () => forward(currentSplit.id),
replace: (next, mergeHistory = false) =>
replace(currentSplit.id, next, mergeHistory),
+ removeFromHistory: (predicate: (content: SplitContent) => boolean) => {
+ removeFromHistory(currentSplit.id, predicate);
+ },
close: () => {
// If there's only one split and it's the default split, then no-op
if (state.splits.length <= 1) {
diff --git a/js/app/packages/app/component/split-layout/layoutUtils.ts b/js/app/packages/app/component/split-layout/layoutUtils.ts
index 1307990bbf..dc65c7312e 100644
--- a/js/app/packages/app/component/split-layout/layoutUtils.ts
+++ b/js/app/packages/app/component/split-layout/layoutUtils.ts
@@ -1,7 +1,7 @@
import { createCallback } from '@solid-primitives/rootless';
import { useContext } from 'solid-js';
import { SplitLayoutContext, SplitPanelContext } from './context';
-import type { SplitContent } from './layoutManager';
+import type { SplitContent, SplitManager } from './layoutManager';
export function decodePairs(segments: string[]): SplitContent[] {
const pairs: SplitContent[] = [];
@@ -61,3 +61,19 @@ export function useSplitPanelOrThrow() {
export function useSplitPanel() {
return useContext(SplitPanelContext);
}
+
+/**
+ * Remove all the items from all split histories that meet a certain criteria.
+ * @param manager
+ * @param predicate A function that returns true to remove a SplitContent entry
+ * from all splits' histories.
+ */
+export function globalRemoveFromSplitHistory(
+ manager: SplitManager,
+ predicate: (item: SplitContent) => boolean
+) {
+ for (const split of manager.splits()) {
+ const handle = manager.getSplit(split.id);
+ handle?.removeFromHistory(predicate);
+ }
+}