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): split ids not stable during url sync (#2381)

dev-rbf67eb9960e61
diff --git a/js/app/packages/app/component/split-layout/layoutManager.ts b/js/app/packages/app/component/split-layout/layoutManager.ts
index 08da63f4ab..1bb5b7d89b 100644
--- a/js/app/packages/app/component/split-layout/layoutManager.ts
+++ b/js/app/packages/app/component/split-layout/layoutManager.ts
@@ -842,58 +842,57 @@ export function createSplitLayout(
}
function reconcileSplits(newSplits: SplitContent[]) {
- const newState: SplitState[] = [];
- const currentCompositeSplits = state.splits.map(keyOfSplitState);
- const newCompositeSplits = newSplits.map(keyOfSplitContent);
- const changed =
- newCompositeSplits.join(',') !== currentCompositeSplits.join(',');
+ const currentKeys = state.splits.map(keyOfSplitState);
+ const newKeys = newSplits.map(keyOfSplitContent);
+ const changed = newKeys.join(',') !== currentKeys.join(',');
if (!changed) return;
- const originalSplits = [...state.splits];
-
- const lookup = (type: BlockName | BlockAlias, id: string) =>
- originalSplits.find(
- (s) => s.content.type === type && s.content.id === id
- );
-
- const splitsToRemove = [
- // just remount all the components
- ...state.splits.filter((s) => s.content.type === 'component'),
- // previous blocks that are not in the new splits
- ...state.splits.filter(
- (s) =>
- s.content.type !== 'component' &&
- !newCompositeSplits.includes(keyOfSplitState(s))
- ),
- ];
-
- for (const splitToRemove of splitsToRemove) {
- removeSplit(splitToRemove.id, false);
+ // Build the result array by position
+ const resultSplits: SplitState[] = [];
+ const usedIds = new Set<SplitId>();
+
+ for (let i = 0; i < newSplits.length; i++) {
+ const newContent = newSplits[i];
+ const splitAtSameIndex = state.splits[i];
+
+ // Reuse split at same index if content matches
+ if (
+ splitAtSameIndex &&
+ sameContent(splitAtSameIndex.content, newContent)
+ ) {
+ resultSplits.push(splitAtSameIndex);
+ usedIds.add(splitAtSameIndex.id);
+ } else {
+ // Build new split with fresh history
+ const newSplit = buildSplit({
+ initialContent: newContent,
+ referredFrom: null,
+ });
+ // Reuse the ID from the split at the same index to keep ids stable
+ if (splitAtSameIndex) {
+ newSplit.id = splitAtSameIndex.id;
+ usedIds.add(splitAtSameIndex.id);
+ }
+ resultSplits.push(newSplit);
+ }
}
- for (const split of newSplits) {
- if (split.type === 'component') {
- newState.push(
- buildSplit({
- initialContent: split,
- isDefault: false,
- referredFrom: null,
+ // Clean up contentChangeListeners and splitNamesById for removed splits
+ for (const split of state.splits) {
+ if (!usedIds.has(split.id)) {
+ contentChangeListeners.delete(split.id);
+ setSplitNamesById(
+ produce((map) => {
+ delete map[split.id];
+ return map;
})
);
- } else {
- newState.push(
- lookup(split.type, split.id) ??
- buildSplit({
- initialContent: split,
- isDefault: false,
- referredFrom: null,
- })
- );
}
}
- setState('splits', reconcile(newState));
+ // Update state in a single batch
+ setState('splits', resultSplits);
}
const lastEvent = createMemo(() => state.events[state.events.length - 1]);