Repository navigation
feat(mosaic,shared): cache active devices with useClerkQuery #10342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
010a7c3
bebaf8c
66467ec
7b851e1
538f63e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/shared': patch | ||
| --- | ||
|
|
||
| Add internal query helpers used by `@clerk/mosaic`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { | ||
| __internal_createCacheKeys, | ||
| __internal_useClearQueriesOnSignOut, | ||
| __internal_useClerkQuery, | ||
| useSession, | ||
| useUser, | ||
| } from '@clerk/shared/react'; | ||
| import { useMemo } from 'react'; | ||
|
|
||
| const USER_SESSIONS_KEY = 'mosaic-user-sessions'; | ||
|
|
||
| export function useUserSessionsQuery() { | ||
| const { user } = useUser(); | ||
| const { session } = useSession(); | ||
| const userId = user?.id; | ||
| const sessionId = session?.id; | ||
|
|
||
| const { queryKey, stableKey, authenticated } = useMemo( | ||
| () => | ||
| __internal_createCacheKeys({ | ||
| stablePrefix: USER_SESSIONS_KEY, | ||
| authenticated: true, | ||
| tracked: { userId, sessionId }, | ||
| untracked: { args: {} }, | ||
| }), | ||
| [userId, sessionId], | ||
| ); | ||
|
|
||
| // Only runs while this hook is mounted, matching the shared hooks. Sign-out after unmount leaves the entry until it is garbage collected. | ||
| __internal_useClearQueriesOnSignOut({ | ||
| isSignedOut: user === null, | ||
| authenticated, | ||
| stableKeys: stableKey, | ||
| }); | ||
|
Comment on lines
+30
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
# Find cleanup owners that can run after the active-devices section unmounts.
rg -n -C 4 'mosaic-user-sessions|useClearQueriesOnSignOut\(|removeQueries\(|getClerkQueryClient\(' packages/mosaic/src packages/shared/src/reactRepository: clerk/javascript Length of output: 39043 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- use-user-sessions-query.ts ---'
nl -ba packages/mosaic/src/queries/use-user-sessions-query.ts
printf '%s\n' '--- useClearQueriesOnSignOut.ts ---'
nl -ba packages/shared/src/react/hooks/useClearQueriesOnSignOut.ts
printf '%s\n' '--- active-devices references ---'
rg -n -F --glob '*.{ts,tsx}' -e 'useUserSessionsQuery' -e 'UserProfileActiveDevicesSection' -e 'ActiveDevices' packages/mosaic/src | head -120
printf '%s\n' '--- active-devices model/section candidate files ---'
find packages/mosaic/src -path '*active-devices*' -type f -maxdepth 12 -printRepository: clerk/javascript Length of output: 23951 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- active-devices model ---'
nl -ba packages/mosaic/src/features/user-profile/user-profile-active-devices-section/user-profile-active-devices-section.model.ts
printf '%s\n' '--- active-devices section ---'
nl -ba packages/mosaic/src/features/user-profile/user-profile-active-devices-section/user-profile-active-devices-section.tsx
printf '%s\n' '--- query client implementation ---'
nl -ba packages/shared/src/react/query/clerk-query-client.ts
nl -ba packages/shared/src/react/query/use-clerk-query-client.ts
printf '%s\n' '--- query package declarations ---'
rg -n -C 3 -F -e '@tanstack/react-query' -e 'gcTime' -e 'cacheTime' packages/shared/package.json packages/mosaic/package.json package.json pnpm-lock.yaml | head -160
printf '%s\n' '--- all production references to the Mosaic key ---'
rg -n -F --glob '!**/__tests__/**' --glob '*.{ts,tsx}' 'mosaic-user-sessions' packagesRepository: clerk/javascript Length of output: 11871 Keep sign-out cleanup active after the section unmounts. If the active-devices section unmounts before sign-out, 🤖 Prompt for AI Agents
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alexcarpenter I think this is valid, but it's also the pattern we already have in shared so it's something we need to rethink outside of this PR. I think this is definitely worth a comment in the code though. |
||
|
|
||
| return __internal_useClerkQuery({ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this also means it gets 3 retries by default, which I think is a good thing. |
||
| queryKey, | ||
| // getSessions() also caches on the user, so a refetch only gets fresh data once the user resource is rebuilt. | ||
| queryFn: () => user?.getSessions(), | ||
| staleTime: 2 * 60_000, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 had this to say which was interesting:
So this is two caches/staleness rules on top of each other. I think we just keep it like this to match the legacy behavior though, but might be a worth a comment? |
||
| enabled: Boolean(user && session), | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should handle errors as well? I guess the old version didn't either so this is still strictly an improvement, but might be nice to tackle here so we don't forget? Feel free to defer though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The query can never fail today:
SessionWithActivities.retrieve()ends in.catch(() => []), so a failed request shows up as an empty list. A real error state would need a clerk-js change, so I will defer it to a follow-up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally missed that, at least it wont stay as pending then which is good. :) Might be worth a comment for the next reader.
Maybe in the future we should make this a real error, but not necessary to tackle now even as a follow-up, my pet peeve is with unhandled errors, not poorly handled ones. 😅