refactor(hooks): one optimistic-toggle hook behind follow, block and blog-follow - #376
Merged
Merged
Conversation
…blog-follow useFollow, useBlock and useBlogFollow were the same 150-line hook three times: read the shared cache, query if missing, flip optimistically, write, roll back and toast on failure, prompt for login when logged out. They diverged only in the service calls, the copy, the self-target guard, and blog-follow's follower counter. hooks/use-toggle-relation.ts is that hook once, parameterised by those things; the three public hooks keep their exact signatures and become 40-60 lines of wiring each. The guard that useBlogFollow alone had (a cancelled flag so a slow status check cannot land after the subject changed) is now a request counter every relation gets. The follower counter follows the flip through onOptimistic/onRollback instead of being threaded through the toggle's closure. lib/caches/user-status-cache.ts had the same TTL-Map block three times (get/set/delete/seed, ~35 lines each). lib/caches/status-cache.ts is a StatusCache class instantiated three times; batch enrichers call .seed() on the instance instead of a per-relation seed function, and the viewer:subject key is built in one place instead of by every caller. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…t on logout A post card mounts useFollow before progressive enrichment resolves, so initialValue goes undefined -> boolean while the hook's own query is in flight. The effect cleanup orphaned that query and the early return never cleared isLoading, leaving the button disabled. Adopt the prefetched value (or a newer cache entry) and settle. Also clear isOn when the viewer logs out, matching the previous blog-follow behaviour, narrow selfError instead of casting, and make StatusCache.key private.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
load() reruns whenever initialValue changes; if that lands while turnOn/turnOff is still pending it cleared isLoading early, reopening the toggle guard to a duplicate click. Track the pending write in a ref and let its finally block settle state.
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hooks/use-toggle-relation.ts: one hook owning the optimistic flip, rollback on failure, shared TTL cache, request-counter guard, login prompt for logged-out viewers, and the self-target guard.useFollow,useBlockanduseBlogFolloware now thin wrappers with unchanged public signatures.lib/caches/status-cache.ts: a viewer-scopedStatusCacheclass replaces the three hand-rolled follow/block/blog-follow map+TTL triples inuser-status-cache.ts; the batch enrichers (use-progressive-enrichment,post-enrichment-helpers,blog-discovery) seed it viaseed(), anduse-media-gatereads it viaget().isLoadingand adopt the prefetched value wheninitialValuearrives after the hook's own query started (the normal post-card path via progressive enrichment), resetisOnon logout (matches the previous blog-follow behaviour), narrowselfErrorinstead of casting, and makeStatusCache.keyprivate.Part of the anti-slop cleanup series (after #370–#375).
Test plan
npm run lint(zero warnings),tsc --noEmit,npm run test,npx knip,npm run buildall green locally🤖 Generated with Claude Code