notes and lists
pockist docs parent-list-tracking.md
4.4 kB 104 lines
Markdown
at main

Parent List Tracking for Archive/Restore #

Overview #

When an item is archived from a list, we store item.meta.parentListId pointing to the list it came from. This enables the ArchivePage restore feature to re-add the item to its original parent list, even if the list's items[] array no longer contains the item's ref (e.g. due to _pruneArchivedRefs or later mutations).

Why We Need This #

The v10 schema stores child relationships via list.items[] — an array of {id, order} refs. When an item is archived, the old behavior was:

  1. Set item.meta.archived = true
  2. Leave the ref in list.items[]

This works for restore, but dead refs accumulate in list.items[] and break reordering (DOM indices ≠ array indices when archived items are filtered out).

Attempted fix (Option B): Remove the ref from list.items[] on archive. This broke restore — the item had no way to know which list it belonged to.

Current approach (Path 2): Keep the ref in list.items[] for backward compatibility, but also store parentListId in item.meta. If the ref is ever lost (pruned, overwritten), restore can still find the parent list via parentListId.

API Changes #

DBManager.archiveItem(id, parentListId?) #

static async archiveItem(id, parentListId) {
    const item = await this.getItem(id);
    if (!item) throw new Error('Item not found');
    item.meta = { ...item.meta, archived: true, updatedAt: new Date().toISOString() };
    if (parentListId) item.meta.parentListId = parentListId;
    await this.saveItem(item);
}
  • parentListId is optional. If provided, stores it in item.meta.parentListId.
  • Single DB write per archive call.

Call Sites #

File Method Parent list ID passed?
ListBase.js _archiveItem(itemId) Yes — listItem.id
ListBase.js _clearCompleted() Yes — listItem.id for each archived item
NoteBase.js (if archiving notes) No — notes don't belong to a list

ArchivePage.js Restore Handler #

const parentListId = item.meta?.parentListId;

// Unarchive the item first
item.meta = { ...item.meta, archived: false, updatedAt: new Date().toISOString() };
await DBManager.saveItem(item);

// Re-add to parent list if known
if (parentListId) {
    const parentList = await DBManager.getItem(parentListId);
    if (parentList && parentList.type === 'list') {
        const items = parentList.items || [];
        if (!items.some(l => l.id === itemId)) {
            items.push({ id: itemId, order: items.length });
            parentList.items = items;
            parentList.meta = { ...parentList.meta, updatedAt: new Date().toISOString() };
            await DBManager.saveItem(parentList);
        }
    }
}

Re-add logic:

  • Appends restored item at the end of the list (order = items.length)
  • Skips if ref already exists (defensive against double-restore)
  • Skips if parent list is deleted/archived/missing (item restored as standalone orphan)

Edge Cases #

Scenario Behavior
Parent list deleted between archive and restore Item restored as standalone orphan
Ref already exists in parent list (double restore) Skip re-adding, no duplicate
Note archived (no parentListId) Note restored as standalone
List mutated after archive (ref pruned from DB) Restore still works via parentListId
parentListId points to wrong list type Skip re-adding (type guard)

Future #

This is a transitional feature for the v10 schema. In the v11 schema migration (parentId + compound index), parentListId will become a first-class top-level field (item.parentId) and this tracking will be automatic for all child items, not just archived ones.

Files Changed #

  • public/services/DBManager.js — archiveItem() signature
  • public/components/ListBase.js — _archiveItem(), _clearCompleted()
  • public/components/ArchivePage.js — restore click handler

Testing #

  1. Create a list with items A, B, C
  2. Archive B → B gets meta.parentListId = list.id
  3. Sort the list or trigger any mutation (prunes B's ref from DB)
  4. Go to Archive page
  5. Restore B → B reappears in the original list at the end
  6. Archive C too, navigate away, come back
  7. Restore C → C also reappears correctly

No refresh needed at any step.