Skip to content

feat(content): archive, delete and set a status from the Library's rows - #1328

Merged
gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/library-row-actions
Oct 22, 2026
Merged

gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/library-row-actions

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

From the user's review of the Library: rows need archive and delete, a "…" menu to set the status (In review, In progress, Ready, Published, Send to Archive), and Created and Updated dates instead of the AEO score column, which always read "—".

Library (content/library)

  • Row menu, which every member gets:
    • Status: In progress · In review · Ready · Published, with the current one checked and disabled.
    • Ready goes through markContentDraftReady, the editor's Mark ready, so it keeps a History version and completes the draft's Action Center task. The others go through the new setContentDraftStatus.
  • Admins and managers also get:
    • Send to Archive, with an Undo toast, or Restore on an archived draft;
    • Delete, behind a confirmation dialog ("This can't be undone").
  • Tabs: All · In progress · In review · Ready · Published · Archived. Archived drafts leave All and the status tabs, and keep their status, so Restore puts them back where they were. Counts past the 500-row cap read the same way.
  • Columns: Content · Type · Method · By · Status · Created · Updated · menu. The AEO score column is gone.
    • To fit at 1024 px, the title column is 180 px wide under xl (320 px from xl up).
    • Dates from this year read "Oct 16", and older ones keep their year. The full date is in the tooltip.
  • "Draft" now reads "In progress" (contentEditor.status.draft and the tab). It's a label only; the stored status stays draft.

Migration 00233_content_drafts_archive.sql

  • content_drafts.archived_at / archived_by. Archive is deliberately not a fifth status, so the Agent API, MCP schemas and the editor keep reading the four statuses unchanged.
  • guard_content_draft_archive trigger. Only an admin or manager of the brand's organization, or the server, can set or clear archived_at. archived_by is set by the trigger rather than trusted from the app.
  • content_drafts: manager delete policy. The table had no delete policy, so nobody could delete a draft before. On delete, the draft's versions, sources and suggestions go too (cascade). Improve usage keeps its row (set null), and the Action Center task it was sent as stays.

Archived drafts are set aside elsewhere too:

  • the Content Plan no longer treats an archived draft as "being written" (getContentPlan);
  • the MCP list_content_drafts tool skips them.

Checked on the dev DB:

  • Admin: archive sets archived_by, restore clears it, and delete works.
  • Analyst (non-manager): can change a status, deletes 0 rows (RLS), and archiving raises 42501 only admins and managers archive content drafts.

Related issue

None. Product feedback from the user.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Apply 00233 first.

  1. Content → Library, as an admin or manager. Each row has a "…" menu.
    • Pick Ready on an In review row: it moves to Ready, as Mark ready does.
    • Pick Published or In progress: the status changes.
  2. Send to Archive. The row leaves All, and the toast's Undo brings it back. The Archived tab lists it; Restore puts it back under its old status.
  3. Delete. A dialog asks first; confirm and the row is gone.
  4. As a member: the menu shows the statuses only.
  5. Columns: Created and Updated show dates, and no AEO score.

Checked in light and dark, at 1024 px and wider.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

yarn test: 281 files, 2,493 tests pass. yarn build passes with CI's placeholders. bash supabase/build-schema.sh was run.

🤖 Generated with Claude Code

Each Library row gets a menu: its status (In progress · In review ·
Ready · Published; Ready goes through Mark ready, keeping a History
version and completing the draft's task) and, for admins and managers,
Send to Archive (with Undo) or Restore, and Delete behind a
confirmation. An Archived tab holds archived drafts, which keep their
status and leave the other tabs, the Plan and the MCP list. Created and
Updated replace the AEO score column, and "Draft" reads "In progress".

Migration 00233 adds content_drafts.archived_at/archived_by, a trigger
letting only admins and managers archive, and a manager delete policy
(drafts had none).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk These are the user's Library row actions: a status menu, archive and restore, delete, and Created/Updated columns instead of the AEO score. Migration 00233 comes after #1326's 00232.

  • Archive is a column, not a fifth status (archived_at / archived_by). The Agent API, the MCP schemas and the editor keep the four statuses. A trigger lets only admins and managers set or clear it; members can still change a status.

  • content_drafts had no delete policy, so nobody could delete a draft. The new one is admin/manager only. On delete:

    • versions, sources and suggestions cascade;
    • Improve usage is set null;
    • the Action Center task stays.

    Tell me if deleting should be blocked while the draft is linked to an open task.

  • "Draft" now reads "In progress", as the label only (contentEditor.status.draft).

  • Ready from the row menu reuses markContentDraftReady, so History and the task completion behave as in the editor.

  • Archived drafts are left out of getContentPlan and MCP list_content_drafts.

Checked on the dev DB:

  • an analyst can change a status, deletes 0 rows and gets 42501 on archive;
  • an admin can archive, restore and delete.

Web checks: 2,493 tests and the build pass. Checked in light and dark, and at 1024 px with no overflow.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few fixes before merge:

  1. supabase/migrations/00233_content_drafts_archive.sql:60-61: the guard runs before update only. content_drafts: member insert lets any member insert a draft with archived_at already set and archived_by set to anyone, an admin for example. So archived_by is trusted from the app on insert after all, and the admin-or-manager rule doesn't hold for new rows. Run the trigger before insert or update. On insert, unless the role bypasses RLS, raise 42501 when either column is non-null, as guard_content_draft_server_columns does for its columns. Folding both columns into that guard's insert branch also works.
  2. web/src/lib/actions/content-drafts.ts:477-487: readActionDrafts still returns archived drafts. The Action Center drawer lists them, and a task's Start reopens an archived draft (startDraftFromAction → findActionDraft, :575). If archived means set aside, as on the Plan and in MCP, filter on archived_at is null here too, or label them as archived in the drawer.
  3. web/src/lib/content/drafts.ts:243: aeoScoreText and its test, plus scoreBefore / scoreAfter on ContentDraftListItem and in the select at content-drafts.ts:143, are unused now that the column is gone. Remove them.

Checked on prod: only content_draft_versions, content_draft_sources and content_draft_suggestions (cascade) and prompt_improve_usage (set null) reference content_drafts. Referential actions run as the table owner, so child-table RLS doesn't block the cascade. The delete policy is limited to admins and managers of the brand's organization. Apply 00233 before this merges: the Library and the MCP list select archived_at.

cihan-gyk and others added 2 commits October 22, 2026 12:25
…of the Action Center

- 00233: guard_content_draft_archive runs before insert or update. A
  member's new draft with archived_at or archived_by set is refused
  (42501), so archived_by is never the app's word; the server, which
  bypasses RLS, is unaffected.
- readActionDrafts skips archived drafts: the Action Center drawer doesn't
  list them, and a task's Start writes a new draft instead of reopening one.
- Remove aeoScoreText and the list's scoreBefore/scoreAfter, unused since
  the AEO Score column went.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Thanks, all three are in c11162d9, on top of a merge of main (a9897ff7; only schema.sql conflicted, rebuilt):

  1. The guard runs on insert too. content_drafts_guard_archive is now before insert or update. On insert, unless the role bypasses RLS, a row with archived_at or archived_by set raises 42501, as guard_content_draft_server_columns does for its columns. Re-applied on the dev DB and tried as a member in a rolled-back transaction: an archived insert was refused, and a plain one went through.
  2. readActionDrafts skips archived drafts (.is('archived_at', null)). The drawer doesn't list them, and startDraftFromAction → findActionDraft writes a new draft rather than reopen an archived one, as on the Plan and in MCP. A test covers the filter.
  3. aeoScoreText and its test are removed, as are scoreBefore / scoreAfter on ContentDraftListItem and in the Library's select.

Web: 2,505 tests, lint, tsc, Prettier and the build pass. Archive and Restore still work from the Library in the browser.

As you noted, 00233 needs to be on the hosted database before this merges, and #1329 follows it with schema.sql rebuilt.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One fix before merge. The three earlier findings are done in c11162d9:

  • 00233's guard runs before insert or update. Any role that doesn't bypass RLS gets 42501 when it inserts a draft with archived_at or archived_by set.
  • readActionDrafts skips archived drafts.
  • aeoScoreText, its test, and scoreBefore / scoreAfter are gone. The Library select no longer reads score_before / score_after.

The merge of main (a9897ff7) only resolves schema.sql. Re-merging its parents gives the same tree in every other file.

  1. Start fails for good once an action's draft is archived. web/src/lib/actions/content-drafts.ts:487 now leaves archived drafts out of readActionDrafts. So startDraftFromAction (:575) doesn't find the archived draft, imports the page, and inserts a new one. That insert hits 00218's content_drafts_action_work_key, which is unique on (action_id, method, md5(source_url)) for every row, archived ones included. The 23505 retry (:656) reads through the same filter, finds no winner, and throws, so the task's Start button returns failed until someone restores the draft, and the page import is wasted on every click. Pick one:

    • (preferred, matches the commit message) In 00233 (not on prod yet), recreate the index as where action_id is not null and archived_at is null. Restore can then hit 23505 when a newer draft exists for the same work, so archiveContentDraft should map that to its own error and the Library should show a toast for it.
    • Or keep archived drafts in the lookup Start uses: give readActionDrafts an includeArchived flag, and pass true from startDraftFromAction, both for the first lookup and for the 23505 retry. Only the drawer (getActionDrafts) would filter them out, so Start reopens the archived draft.

    Either way, add a test in content-drafts-start.test.ts for an action whose matching draft is archived.

Merge notes

  • Merges cleanly on current main. It contains no other open PR.
  • It conflicts with #1321, and so with #1329, #1334 and #1337, which all carry #1321. The conflicts are only in content/library/page.test.tsx and lib/actions/content-plan.test.ts, both mock setup, and the fix is to keep both sides. With #1329 it also conflicts in schema.sql.
  • 00233 isn't on prod yet (no archived_at column). Apply it before this merges: the Library, the Plan and the MCP list all filter on archived_at.

cihan-gyk and others added 2 commits October 22, 2026 15:09
…-actions

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/content/library/page.test.tsx
#	web/src/lib/actions/content-plan.test.ts
…chived

00218's content_drafts_action_work_key counted archived drafts too, so a
task whose draft was archived could never start again: readActionDrafts
skips the archived one, the insert hit the key, and the 23505 retry found
no winner. 00233 recreates the key over drafts not archived. Restoring the
archived one while a newer draft does the same work is refused (23505),
named newer_draft, and the Library says what to do. getOpportunityDrafts
(from ansvisor#1334, now on main) skips archived drafts too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Thanks. Your preferred fix is in 24994a90, on top of a merge of main (1b75c73c; the mock conflicts in library/page.test.tsx and content-plan.test.ts keep both sides, and schema.sql is rebuilt):

Web: 2,614 tests, lint, tsc, Prettier and the build pass. As before, 00233 needs to be on prod before this merges. The index swap there is a plain drop/create on content_drafts, which is small.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The round-6 item is fixed. 00233 now scopes content_drafts_action_work_key to drafts that aren't archived. A restore that collides with a newer draft returns newer_draft, with its own Library toast, and both paths have tests. getOpportunityDrafts filters archived drafts too.

One thing before merge: #1333 is landing now, and its getDraftForPrompt (web/src/lib/actions/content-drafts.ts) doesn't filter archived drafts yet. Please merge main in once it's there, then add:

  • .is('archived_at', null) to that query;
  • the matching filter assertion in content-drafts-start.test.ts.

After that I'll apply 00233 and merge.

cihan-gyk and others added 2 commits October 22, 2026 18:44
getDraftForPrompt (ansvisor#1333) skips archived drafts, as getOpportunityDrafts
does, so Start opens the work in hand, never one sent to the Archive.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk merged main in once #1333 landed (d430e75), then cae03ce:

  • getDraftForPrompt (web/src/lib/actions/content-drafts.ts) adds .is('archived_at', null);
  • content-drafts-start.test.ts asserts that filter.

schema.sql matches build-schema.sh after the merge. Checks: web lint (0 errors), typecheck, 2,733 tests and build pass. 00233 is ready for you to apply.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The open item is fixed in cae03cee:

  • getDraftForPrompt now skips archived drafts with .is('archived_at', null) (web/src/lib/actions/content-drafts.ts:359-360).
  • content-drafts-start.test.ts:273 checks that filter.

The merge of main (d430e75b) re-merges to exactly the same tree, so it carries no hidden changes. schema.sql matches build-schema.sh. Since the last review, the only other main change that reads content_drafts is the Content Agent's writer (#1351). It works on one draft by id, so it needs no archive filter.

Merge notes

  • It merges cleanly on current main (a08cbc68) and contains no other open PR. It also merges cleanly with #1312 and #1358.
  • 00233 isn't on prod yet: there is no archived_at column, no guard function or trigger, no delete policy, and the work key is still 00218's. I'll apply it, then merge.

00233 is applied on production: both columns, the guard trigger, the work key limited to drafts not archived, and the manager delete policy. Tested on top of main together with #1358: web and server tests, tsc, lint and Prettier pass, and schema.sql matches a rebuild.

@gkhngyk
gkhngyk merged commit 56a4d25 into ansvisor:main Oct 22, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants