Repository navigation
feat(desktop): implement session refetch subscription and update billing success URLs - #1525
Ipriyankrajai wants to merge 5 commits into
Conversation
…ser plan determination Refactor the usePaywall hook to incorporate active subscription data using useLiveQuery and useCollections. This change ensures that the user's plan is accurately derived from their active subscription status, improving access control for gated features.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 📝 WalkthroughWalkthroughAdds a session-refetch event flow: a new TRPC subscription ( Changes
Sequence DiagramsequenceDiagram
participant User
participant Billing
participant Main
participant AuthEvents
participant TRPCAuthRouter
participant AuthProvider
User->>Billing: Complete upgrade payment
Billing->>User: Redirect to success URL
User->>Main: Open deep link (protocol://settings/billing?success=true)
Main->>AuthEvents: Emit "session-refetch"
AuthEvents->>TRPCAuthRouter: Observable emits true
TRPCAuthRouter-->>AuthProvider: Subscription push (onSessionRefetch)
AuthProvider->>AuthProvider: Call refetchSession()
AuthProvider-->>User: Session updated
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@apps/desktop/src/renderer/components/Paywall/usePaywall.ts`:
- Around line 17-20: The code unsafely casts activeSubscription?.plan to
UserPlan; change this to runtime-validate the plan value before assigning
userPlan and handle multiple active subscriptions deterministically: inspect
subscriptionsData to (a) choose the correct active subscription (e.g., sort by
createdAt or status priority instead of using find) and (b) check that
activeSubscription.plan is one of the known UserPlan string literals (e.g.,
"free", "pro", "premium") before using it; if it isn't, fall back to "free" (or
a safe default) and ensure hasAccess uses this validated userPlan. Use the
symbols activeSubscription, subscriptionsData, userPlan, and hasAccess to locate
and update the logic.
Update the logic in the usePaywall hook to derive the user plan based on the active subscription's plan value. This change ensures that users are correctly assigned either a "pro" or "free" plan, enhancing the accuracy of access control for gated features.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@apps/desktop/src/renderer/components/Paywall/usePaywall.ts`:
- Around line 13-16: The useLiveQuery call depends on the unstable collections
object causing unnecessary re-runs; extract a stable reference like const
subscriptions = collections.subscriptions (or obtain subscriptions directly from
useCollections) and update the query and dependency array to use that stable
reference instead of collections (i.e., pass (q) => q.from({ subscriptions })
and [subscriptions] as the dependency). This change should be applied where
useLiveQuery and the collections variable are used (look for useLiveQuery and
collections.subscriptions in the file) to prevent re-execution on every render.
|
I've pushed the fix for the unstable const collections = useCollections();
+ const subscriptions = collections.subscriptions;
const { data: subscriptionsData } = useLiveQuery(
- (q) => q.from({ subscriptions: collections.subscriptions }),
- [collections],
+ (q) => q.from({ subscriptions }),
+ [subscriptions],
);You can cherry-pick the commit: |
…y dep Extract collections.subscriptions to a local variable so useLiveQuery depends on the stable collection reference instead of the reconstructed collections object, preventing unnecessary re-executions on every render.
|
we should probably refetch the state session instead of using sessions imo. probably when we come back from stripe with deeplink we should just refetch the session state. real-time collections can be flaky atm |
…ing success URLs - Added a new subscription for session refetch requests in the auth router to handle session updates after specific events, such as Stripe checkout. - Emitted a session refetch event upon successful billing deep link navigation. - Updated success URLs in billing components to use a protocol scheme constant for consistency and improved link handling. - Refactored the usePaywall hook to simplify user plan determination based on session data.
|
@Kitenite Made the changes and used deeplink approach |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
apps/desktop/src/renderer/routes/_authenticated/settings/billing/components/BillingOverview/BillingOverview.tsx (1)
34-52:⚠️ Potential issue | 🟠 MajorSame unstable
collectionsdependency issue — extract stable references.Same problem as in
plans/page.tsx. Extractcollections.subscriptionsandcollections.membersinto local variables before passing touseLiveQuery.♻️ Proposed fix
+ const subscriptions = collections.subscriptions; + const members = collections.members; + // Get subscription from Electric (preloaded, instant) const { data: subscriptionsData } = useLiveQuery( - (q) => q.from({ subscriptions: collections.subscriptions }), - [collections], + (q) => q.from({ subscriptions }), + [subscriptions], ); ... // Get member count from Electric const { data: membersData } = useLiveQuery( (q) => q - .from({ members: collections.members }) + .from({ members }) .select(({ members }) => ({ id: members.id })), - [collections], + [members], );🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/desktop/src/renderer/routes/_authenticated/settings/billing/components/BillingOverview/BillingOverview.tsx` around lines 34 - 52, The useLiveQuery hooks are depending directly on the unstable collections object (collections.subscriptions and collections.members); extract stable local references (e.g., const subscriptionsColl = collections.subscriptions and const membersColl = collections.members) and use those variables inside the query functions and in the dependency arrays for useLiveQuery (replace collections with subscriptionsColl/membersColl) so subscriptionsData, membersData, subscriptionData and derived plan remain stable and avoid unnecessary re-renders; update both useLiveQuery calls (the one that queries subscriptions and the one that selects members.id) accordingly.apps/desktop/src/renderer/routes/_authenticated/settings/billing/plans/page.tsx (1)
208-225:⚠️ Potential issue | 🟠 MajorUnstable
collectionsreference inuseLiveQuerydependency arrays causes unnecessary re-execution.
collectionsis a new object each render, so[collections]triggers useLiveQuery continuously. Extract the specific collection references into stable local variables, as the reviewer Kitenite already suggested.♻️ Proposed fix
+ const subscriptions = collections.subscriptions; + const members = collections.members; + // Get subscription from Electric (preloaded, instant) const { data: subscriptionsData } = useLiveQuery( - (q) => q.from({ subscriptions: collections.subscriptions }), - [collections], + (q) => q.from({ subscriptions }), + [subscriptions], ); const subscriptionData = subscriptionsData?.find( (s) => s.status === "active", ); const currentPlan: PlanTier = (subscriptionData?.plan as PlanTier) ?? "free"; const cancelAt = subscriptionData?.cancelAt; const { data: membersData } = useLiveQuery( (q) => q - .from({ members: collections.members }) + .from({ members }) .select(({ members }) => ({ id: members.id })), - [collections], + [members], );🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/desktop/src/renderer/routes/_authenticated/settings/billing/plans/page.tsx` around lines 208 - 225, The useLiveQuery calls pass the entire unstable collections object in their dependency arrays causing repeated re-runs; extract the specific collection references into stable local constants (e.g., const subscriptions = collections.subscriptions and const members = collections.members) and update the queries to reference these constants (useLiveQuery((q) => q.from({ subscriptions }), [subscriptions]) and similarly for members), ensuring subscriptionData, subscriptionsData, membersData and the existing selects still work but depend only on the stable collection references.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@apps/desktop/src/renderer/providers/AuthProvider/AuthProvider.tsx`:
- Around line 30-34: The subscription callback in
electronTrpc.auth.onSessionRefetch.useSubscription calls refetchSession() and
currently discards its return value, allowing unhandled promise rejections;
change the onData handler to call refetchSession() and attach rejection handling
(e.g., refetchSession().catch(err => { /* log or ignore */ })) so all errors are
handled consistently with the other refetch usage; update the onData
implementation in the useSubscription callback to explicitly handle promise
rejections for refetchSession.
---
Outside diff comments:
In
`@apps/desktop/src/renderer/routes/_authenticated/settings/billing/components/BillingOverview/BillingOverview.tsx`:
- Around line 34-52: The useLiveQuery hooks are depending directly on the
unstable collections object (collections.subscriptions and collections.members);
extract stable local references (e.g., const subscriptionsColl =
collections.subscriptions and const membersColl = collections.members) and use
those variables inside the query functions and in the dependency arrays for
useLiveQuery (replace collections with subscriptionsColl/membersColl) so
subscriptionsData, membersData, subscriptionData and derived plan remain stable
and avoid unnecessary re-renders; update both useLiveQuery calls (the one that
queries subscriptions and the one that selects members.id) accordingly.
In
`@apps/desktop/src/renderer/routes/_authenticated/settings/billing/plans/page.tsx`:
- Around line 208-225: The useLiveQuery calls pass the entire unstable
collections object in their dependency arrays causing repeated re-runs; extract
the specific collection references into stable local constants (e.g., const
subscriptions = collections.subscriptions and const members =
collections.members) and update the queries to reference these constants
(useLiveQuery((q) => q.from({ subscriptions }), [subscriptions]) and similarly
for members), ensuring subscriptionData, subscriptionsData, membersData and the
existing selects still work but depend only on the stable collection references.
Updated the session refetch subscription in AuthProvider to catch and handle potential errors during the refetch process, ensuring smoother user experience and preventing unhandled promise rejections.
| annual, | ||
| seats: memberCount, | ||
| successUrl: `${env.NEXT_PUBLIC_WEB_URL}/settings/billing?success=true`, | ||
| successUrl: `${PROTOCOL_SCHEME}://settings/billing?success=true`, |
There was a problem hiding this comment.
What's the reason we're changing this?
There was a problem hiding this comment.
We want it to redirect it to our app instead of the web
There was a problem hiding this comment.
Where we will referch the session
Summary by CodeRabbit