|
3 | 3 |
|
4 | 4 | import { FocusManager } from "@/app/store/focusManager"; |
5 | 5 | import { getSettingsKeyAtom } from "@/app/store/global"; |
| 6 | +import { BlockService } from "@/app/store/services"; |
6 | 7 | import { atomWithThrottle, boundNumber, fireAndForget } from "@/util/util"; |
7 | 8 | import { Atom, atom, Getter, PrimitiveAtom, Setter } from "jotai"; |
8 | 9 | import { splitAtom } from "jotai/utils"; |
@@ -406,6 +407,26 @@ export class LayoutModel { |
406 | 407 | this.persistToBackend(); |
407 | 408 | } |
408 | 409 |
|
| 410 | + private async cleanupOrphanedBlocks() { |
| 411 | + const tab = this.getter(this.tabAtom); |
| 412 | + const layoutBlockIds = new Set<string>(); |
| 413 | + |
| 414 | + walkNodes(this.treeState.rootNode, (node) => { |
| 415 | + if (node.data?.blockId) { |
| 416 | + layoutBlockIds.add(node.data.blockId); |
| 417 | + } |
| 418 | + }); |
| 419 | + |
| 420 | + for (const blockId of tab.blockids || []) { |
| 421 | + if (!layoutBlockIds.has(blockId)) { |
| 422 | + console.log("Cleaning up orphaned block:", blockId); |
| 423 | + if (this.onNodeDelete) { |
| 424 | + await this.onNodeDelete({ blockId }); |
| 425 | + } |
| 426 | + } |
| 427 | + } |
| 428 | + } |
| 429 | + |
409 | 430 | private async handleBackendAction(action: LayoutActionData) { |
410 | 431 | switch (action.actiontype) { |
411 | 432 | case LayoutTreeActionType.InsertNode: { |
@@ -537,6 +558,10 @@ export class LayoutModel { |
537 | 558 | this.treeReducer(splitAction, false); |
538 | 559 | break; |
539 | 560 | } |
| 561 | + case "cleanuporphaned": { |
| 562 | + await this.cleanupOrphanedBlocks(); |
| 563 | + break; |
| 564 | + } |
540 | 565 | default: |
541 | 566 | console.warn("unsupported layout action", action); |
542 | 567 | break; |
@@ -574,6 +599,8 @@ export class LayoutModel { |
574 | 599 | if (contents.gapSizePx !== undefined) { |
575 | 600 | this.setter(this.gapSizePx, contents.gapSizePx); |
576 | 601 | } |
| 602 | + const tab = this.getter(this.tabAtom); |
| 603 | + fireAndForget(() => BlockService.CleanupOrphanedBlocks(tab.oid)); |
577 | 604 | } |
578 | 605 |
|
579 | 606 | /** |
|
0 commit comments