Skip to content

Invalidation of queries#718

Merged
feruzm merged 3 commits into
developfrom
invalidate
Mar 25, 2026
Merged

Invalidation of queries#718
feruzm merged 3 commits into
developfrom
invalidate

Conversation

@feruzm
Copy link
Copy Markdown
Member

@feruzm feruzm commented Mar 25, 2026

Summary by CodeRabbit

  • Improvements
    • Account key add/revoke operations now update the UI immediately (optimistic updates) and then verify/sync in the background for faster, more responsive permissions management.
  • Bug Fixes
    • Revocation flows now reflect key removals instantly, avoiding delayed UI states.
  • Tests
    • Added unit tests covering optimistic updates, revocations, additions, duplicate prevention, and edge cases.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 25, 2026

📝 Walkthrough

Walkthrough

Adds an exported utility to optimistically update a user's full-account React Query cache and updates two permission-management components to use that utility on success; also adds unit tests for the new cache-update behavior.

Changes

Cohort / File(s) Summary
Permission UI Success Handlers
apps/web/src/app/(dynamicPages)/profile/[username]/permissions/_components/add-keys-steps/step-4-confirm.tsx, apps/web/src/app/(dynamicPages)/profile/[username]/permissions/_components/manage-keys-dialog.tsx
Replaced direct post-success invalidateQueries calls with calls to the new updateAccountKeysCache(...) to perform optimistic cache mutations (apply revokeMap, optionally addMap, update memoKey) and rely on background invalidation afterward.
Cache mutation utility
apps/web/src/api/mutations/update-account-keys-cache.ts
New exported function updateAccountKeysCache(queryClient, username, update) that computes the full-account query key, applies optimistic mutations to key_auths per authority (removals via revokeMap, additions via addMap with weight 1), updates memo_key when provided, then triggers invalidateQueries for background sync. Exports related types (AuthorityName, RevokeKeysUpdate, AddKeysUpdate).
Tests
apps/web/src/specs/api/update-account-keys-cache.spec.ts
New Vitest suite covering optimistic mutation behaviors: revocations across authorities, adding keys (no-duplication), memo_key updates, no-op when cache missing, and partial revokeMap handling. Mocks getAccountFullQueryOptions to a stable query key.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

patch

Poem

🐰 I nibbled at the query tree,
Pushed keys in place quite optimistically,
I pruned the ones no longer true,
Refetched later—just to view,
Hops of joy for cached harmony 🥕✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Invalidation of queries' is vague and does not accurately reflect the main change, which shifts from query invalidation to optimistic cache updates with background refetch. Consider a more descriptive title like 'Implement optimistic cache updates for account key management' that better captures the core architectural change.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch invalidate

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot]

This comment was marked as resolved.

@feruzm feruzm marked this pull request as draft March 25, 2026 18:10
@feruzm feruzm marked this pull request as ready for review March 25, 2026 18:11
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
apps/web/src/specs/api/update-account-keys-cache.spec.ts (1)

6-11: Use the real getAccountFullQueryOptions with partial mocking to avoid queryKey drift.

The mock hardcodes queryKey: ["accounts", "full", username], but the SDK's QueryKeys.accounts.full(username) actually returns ["get-account-full", username]. Import getAccountFullQueryOptions from @ecency/sdk and use vi.spyOn() to mock only the queryFn, ensuring the queryKey matches the real SDK and adheres to using QueryKeys as the single source of truth.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/web/src/specs/api/update-account-keys-cache.spec.ts` around lines 6 -
11, The current mock replaces getAccountFullQueryOptions entirely and hardcodes
a queryKey that drifts from the SDK; instead import getAccountFullQueryOptions
from "@ecency/sdk" and use vi.spyOn(getAccountFullQueryOptionsModule,
"getAccountFullQueryOptions") or vi.spyOn(`@ecency/sdk`,
"getAccountFullQueryOptions") to call through and only replace the returned
queryFn (e.g., spyOn(...).mockImplementationOnce(opts => ({ ...opts, queryFn:
vi.fn() }))) so the real queryKey (produced by QueryKeys.accounts.full(username)
/ getAccountFullQueryOptions) is preserved and tests remain aligned with the
SDK.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@apps/web/src/specs/api/update-account-keys-cache.spec.ts`:
- Around line 6-11: The current mock replaces getAccountFullQueryOptions
entirely and hardcodes a queryKey that drifts from the SDK; instead import
getAccountFullQueryOptions from "@ecency/sdk" and use
vi.spyOn(getAccountFullQueryOptionsModule, "getAccountFullQueryOptions") or
vi.spyOn(`@ecency/sdk`, "getAccountFullQueryOptions") to call through and only
replace the returned queryFn (e.g., spyOn(...).mockImplementationOnce(opts => ({
...opts, queryFn: vi.fn() }))) so the real queryKey (produced by
QueryKeys.accounts.full(username) / getAccountFullQueryOptions) is preserved and
tests remain aligned with the SDK.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9f389f38-1975-44ba-b646-506b1f9442e7

📥 Commits

Reviewing files that changed from the base of the PR and between 15186c8 and 8331405.

📒 Files selected for processing (4)
  • apps/web/src/api/mutations/update-account-keys-cache.ts
  • apps/web/src/app/(dynamicPages)/profile/[username]/permissions/_components/add-keys-steps/step-4-confirm.tsx
  • apps/web/src/app/(dynamicPages)/profile/[username]/permissions/_components/manage-keys-dialog.tsx
  • apps/web/src/specs/api/update-account-keys-cache.spec.ts

@feruzm feruzm merged commit c96057a into develop Mar 25, 2026
1 check passed
@feruzm feruzm deleted the invalidate branch March 25, 2026 18:31
@coderabbitai coderabbitai Bot mentioned this pull request Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant