Skip to content

feat(prompts): show usage cards above the tracked prompts table - #1054

Merged
gkhngyk merged 22 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompt-usage-cards
Oct 16, 2026
Merged

gkhngyk merged 22 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompt-usage-cards

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Stacked on #1053, which is stacked on #1034. Until those merge, this PR also shows their commits. This part is the last commit, 7705c2f.

Summary

This is PROMPT-012 part 2. It replaces the one-line "781 / 1000 prompt locations · Enterprise plan · 219 slots left" meter above the Tracked Prompts table with the usage block from the mockup: a header, then five cards in two groups. The numbers come from GET /api/prompts/usage (#1053).

The block

  • Header: "Tracked Prompts" with the subtitle that used to sit on the table, plus Plan & usage (cloud only; self-hosted has no billing page).
  • This brand · {brand name}:
    • Active prompts: "{n} paused · being tracked now".
    • Total prompts: "active + paused".
  • Account · {plan pill}:
    • Active prompts: "across {n} brands".
    • Prompt locations: used / limit with a bar.
    • Slots left: "of {limit} on {plan}" with the plan's call to action: Upgrade to Growth (Starter), Upgrade plan (Growth) or Contact us (Enterprise).
  • Unlimited plans: self-hosted, or an Enterprise account without a limit set yet. Locations shows the count with no bar, and Slots left says "Unlimited".
  • States, using the same rule as the plan-limit guard (getPromptUsage):
    • amber from 90%, raised from PROMPT-009's 80% as PROMPT-012 specifies;
    • red when full;
    • red with "over limit" when used > limit after a downgrade. Nothing is deleted.
  • Numbers and text: numbers use the UI locale ("1,248"). All strings are in messages/en.json (prompts.usage), including the plan pill.
  • Layout:
    • all five cards on one row at 1,024px and up, with labels kept on one line;
    • sub-lines wrap rather than cut off, so the call to action stays visible on narrow cards;
    • below 900px the two groups stack, each keeping its cards on one row.

The table

  • Filtering from the cards: clicking a This brand card filters the table. Active is the default, the paused count shows Paused, and Total shows All. The selected card gets a 2px bottom border.
  • Title: the table title follows the filter, e.g. "Active prompts (57) · Filtered by the card above · Show all 61".
  • Account cards: they open Plan & usage. They don't filter, because the table shows one brand.
  • Empty filter: if a filter empties the table (e.g. every prompt is paused), it says "No active prompts match." with Show all, instead of blaming a search the user never typed.

Loading and refresh

  • Loads on its own: the block uses its own request (a route, not a server action, which Next.js would queue behind the table's data). It shows skeletons while loading, then dashes and Retry on error. The table renders regardless.
  • Refreshes after every change: the block refetches right after a prompt is added, paused, resumed, edited, deleted, bulk-imported, accepted from suggestions or tracked from fan-outs.
  • Measured: after resuming a prompt, the cards showed the new counts about 3 s after the click, including the change itself against the remote dev database.
  • Counts during Undo: the table title counts from every row, including a delete still inside its 10 s Undo window, so it agrees with the cards until the delete lands.

No more counting on page view

getPromptsPageData no longer calls getLocationQuota, which scanned prompts on every page view. The Add Prompt dialog and bulk import now take the plan limit from the usage response (locationQuotaFromUsage). This was the open point from #1053's review.

Review

I ran a code review before opening this. It found 10 things.

Fixed (8):

  • the empty Active view gave no way back;
  • the title and the cards disagreed during a delete's Undo window;
  • a memo that never hit;
  • the plan pill was server-built English;
  • the bar colours lacked dark variants;
  • an unknown plan key would have crashed the page;
  • Retry gave no feedback;
  • there was no test for the table filter.

No change needed: members seeing Plan & usage is fine, since the billing tab is open to every role.

Left for later: the bulk action bar in #1029 reloads the table with loadData. Whichever of the two merges second should switch it to reloadAfterChange, so bulk pause, resume and delete refresh the cards too.

Related issue

None. Product spec: PROMPT-012 (Tracked Prompts usage cards), with the mockup PROMPT-012-usage-mockup.html. Depends on #1053 and #1034.

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

  1. Apply migration 00139 (feat(prompts): keep per-brand usage counters for the tracked prompts cards #1053), then open Prompts → Tracked Prompts.
    • The block shows the brand's active and total prompts, and the account's active prompts, locations and slots.
    • The table opens on "Active prompts (n)".
  2. Click {n} paused: the table shows only paused prompts. Click Total prompts: it shows all, with the bottom border on that card.
  3. Pause or resume a prompt from its row menu: the cards update.
  4. In cloud mode on Starter with 45+ of 50 locations, the Locations and Slots left cards turn amber and show "Upgrade to Growth".
  5. Check light and dark mode, 1440px and 1024px, and under 900px (the groups stack).

Screenshots

Screenshots will be added in a comment.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): 0 errors; the 7 warnings are in code this PR doesn't touch
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • yarn test: 563/563. New in _usage-cards.test.tsx, which renders the cards:

    • Starter near the limit (amber, the upgrade link);
    • over limit after a downgrade;
    • Enterprise with Contact us;
    • self-hosted Unlimited, with no plan link;
    • the card filters;
    • the error state with Retry, and the loading skeletons;
    • an unknown plan key;
    • locationQuotaFromUsage;
    • the status filter counts.

    prompt-usage.test.ts is updated for the 90% threshold.

  • yarn build: passed.

  • In the browser on the demo brand (self-hosted):

    • the real numbers (57 active, 4 paused, 74 locations, Unlimited);
    • the three filters and the title;
    • Show all from an empty filter;
    • resuming and re-pausing a prompt refreshed the cards.
  • Plan states: with the usage response stood in by test data in the browser, I checked Starter near the limit, Growth full, Starter over the limit, Enterprise (the mockup's numbers) and the error state, in light and dark mode, at 1440px, 1024px (sidebar collapsed and open) and 768px.

🤖 Generated with Claude Code

cihan-gyk and others added 5 commits October 14, 2026 16:02
Paused prompts don't run, so they no longer take room on the plan
(org_prompt_location_usage now filters on is_active, 00128). Resuming a
paused prompt brings its locations back, so it is metered like adding
them: setPromptActive refuses with a plan_limit value when there is no
room. That closes the pause, add new prompts, resume the old ones way
past the limit. Location edits on paused prompts cost nothing until the
prompt is resumed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…sh safe

Second review pass on the active-only prompt quota:

- the edit dialog shows a plain message for non-plan failures instead of
  the raw database error;
- setPromptActive returns a failed limit check as a value (ansvisor#427) instead
  of throwing;
- the brand Prompts page keeps its last quota when the refresh after a
  toggle fails, so the location pickers don't become uncapped;
- the prompt-usage doc no longer says paused prompts count;
- tests for a missing prompt, a failed limit check, and location edits
  on paused vs active prompts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolves the conflicts with the bulk import (ansvisor#1013): prompt.ts keeps both
setPromptActive and importPrompts, and the Prompts page imports both. The
import RPC (00122) counts locations through org_prompt_location_usage, which
00128 limits to active prompts, so imports follow the same count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…cards

PROMPT-012 part 1: the data behind the usage cards, read without counting
the prompts table on a page view.

- Migration 00139 adds prompt_usage_counters, one row per brand (active,
  paused, active prompt locations). Statement-level triggers on prompts and
  prompt_sets rebuild the affected brands in the same transaction, under a
  per-brand advisory lock. prompt_usage_actual() is the one place the counts
  are computed; it backs the rebuild, the backfill and the nightly check.
- prompt_usage_summary(brand) returns the brand's counts and its account's
  totals from the counter rows only (security invoker).
- Indexes on prompts.prompt_set_id and prompt_sets.brand_id, which the
  rebuild needs and neither had.
- GET /api/prompts/usage?brandId= returns the summary with the plan's
  location limit and call to action.
- MCP tool get_prompt_usage and its REST mirror /api/mcp/prompt-usage.
- The daily run checks the counters against prompts, rebuilds any that
  drifted and logs them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PROMPT-012 part 2: the one-line "used / limit prompt locations" meter
becomes a usage block with two groups and five cards.

- This brand: Active prompts and Total prompts. Clicking them filters the
  table (Active by default, Paused from the paused count, All); the table
  title follows ("Active prompts (57) · Show all 61").
- Account: Active prompts, Prompt locations (with a bar) and Slots left,
  with the plan pill and its call to action (Upgrade to Growth, Upgrade plan
  or Contact us). Unlimited plans say so.
- Amber from 90% of the limit, red when full or over (after a downgrade).
- The cards load from GET /api/prompts/usage on their own, so the table
  never waits for them; skeletons while loading, dashes and Retry on error.
  They refresh after a prompt is added, paused, resumed, edited, deleted,
  imported, accepted from suggestions or tracked from fan-outs.
- The Add Prompt dialog and bulk import take the plan limit from the same
  response, so the page no longer counts prompts on every view.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 15, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk PROMPT-012 part 2: the usage cards above the Tracked Prompts table, filtering the table from the brand cards. Stacked on #1053 (counters + endpoint). Ready for review when you have a moment.

…e-counters

# Conflicts:
#	supabase/schema.sql
…e-counters

# Conflicts:
#	supabase/schema.sql
#	web/src/types/supabase.ts
…e-cards

# Conflicts:
#	web/messages/en.json
#	web/src/app/[locale]/(dashboard)/dashboard/prompts/page.tsx
#	web/src/lib/actions/prompt.ts
…e-counters

# Conflicts:
#	supabase/schema.sql
…e-counters

# Conflicts:
#	supabase/schema.sql
…e-counters

# Conflicts:
#	supabase/schema.sql
…e-counters

# Conflicts:
#	supabase/schema.sql
…e-counters

# Conflicts:
#	supabase/schema.sql

@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.

Thanks, approving. 00139 is applied on production. All 206 brands have a counter row (7,326 locations), and repair_prompt_usage_counters() reports no drift. prompt_usage_summary returns a member's brand and no row for another organization's.

On the tree merged with main, web tests pass (799) and the typecheck, prettier and eslint are clean. The merge with main's late-only filter keeps both filters in the table's memo and reset key. The bulk action bar from #1029 already refreshes the cards, since its pause and resume go through setActiveFor and onChanged, which is reloadAfterChange.

@gkhngyk
gkhngyk merged commit a6bb4bd into ansvisor:main Oct 16, 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