-
Notifications
You must be signed in to change notification settings - Fork 2k
Dashboard: only use sites the user is a member of for the omnibar and recent sites #112479
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
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { | ||
| omnibarSiteIdQuery, | ||
| queryClient, | ||
| siteByIdQuery, | ||
| userPreferenceQuery, | ||
| userPreferenceOptimisticMutation, | ||
| } from '@automattic/api-queries'; | ||
|
|
@@ -11,14 +12,22 @@ import { useEffect } from 'react'; | |
| import { AUTH_QUERY_KEY } from '../auth'; | ||
| import type { Site, User } from '@automattic/api-core'; | ||
|
|
||
| function isMemberOfSite( site: Site ) { | ||
|
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. Nit: maybe add this as
Contributor
Author
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. but can't any one view the site 🤔
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. Hmm... that's right... maybe
Contributor
Author
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. yes, but it doesn't seem to match functions in this file... so I just inlined it 🙈 |
||
| // If the user is a member of the site, the capabilities property will exist | ||
| return !! site.capabilities; | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the current site for the omnibar, which is extracted from the URL, | ||
| * or the most recent sites, or the user's primary blog, in that priority. | ||
| */ | ||
| export function useInitializeOmnibarSite() { | ||
| const user = queryClient.getQueryData< User >( AUTH_QUERY_KEY ); | ||
|
|
||
| const { data: recentSiteIds } = useQuery( userPreferenceQuery( 'recentSites' ), queryClient ); | ||
| const { data: recentSiteIds, isLoading: isLoadingRecentSiteIds } = useQuery( | ||
| userPreferenceQuery( 'recentSites' ), | ||
| queryClient | ||
| ); | ||
| const { mutate: updateRecentSites } = useMutation( | ||
| userPreferenceOptimisticMutation( 'recentSites' ), | ||
| queryClient | ||
|
|
@@ -39,24 +48,36 @@ export function useInitializeOmnibarSite() { | |
| ( location.search as Record< string, string | undefined > ).origin_site_id | ||
| ); | ||
| const originSiteId = originSiteIdParam > 0 ? originSiteIdParam : undefined; | ||
| const recentSiteId = | ||
| queryClient.getQueryData< number | null >( omnibarSiteIdQuery().queryKey ) || | ||
|
arthur791004 marked this conversation as resolved.
|
||
| recentSiteIds?.[ 0 ]; | ||
|
|
||
| const selectedSiteId = routeSite?.ID || originSiteId; | ||
| const fallbackSiteId = recentSiteIds?.[ 0 ] || user?.primary_blog; | ||
| const { data: originSite, isLoading: isLoadingOriginSite } = useQuery( { | ||
| ...siteByIdQuery( originSiteId ?? 0 ), | ||
| enabled: !! originSiteId, | ||
| } ); | ||
|
|
||
| const { data: recentSite, isLoading: isLoadingRecentSite } = useQuery( { | ||
| ...siteByIdQuery( recentSiteId ?? 0 ), | ||
| enabled: !! recentSiteId, | ||
| } ); | ||
|
|
||
| useEffect( () => { | ||
| // Wait until the route and recent sites are fully loaded, to avoid flicker. | ||
| if ( ! isRouteLoaded || ! recentSiteIds ) { | ||
| // Wait until the required data are fully loaded, to avoid flicker. | ||
| if ( ! isRouteLoaded || isLoadingRecentSiteIds || isLoadingOriginSite || isLoadingRecentSite ) { | ||
| return; | ||
| } | ||
|
|
||
| // `omnibarSiteIdQuery` is used as cross-tree shared state — its placeholder | ||
| // queryFn resolves to `null`. If it's still in flight when we write here, | ||
| // the resolution will overwrite our value and the omnibar loses the site. | ||
| queryClient.cancelQueries( { queryKey: omnibarSiteIdQuery().queryKey } ); | ||
| queryClient.setQueryData( | ||
| omnibarSiteIdQuery().queryKey, | ||
| ( currentSiteId ) => selectedSiteId || currentSiteId || fallbackSiteId | ||
|
|
||
| const omnibarSite = [ routeSite, originSite, recentSite ].find( | ||
| ( site ) => site && isMemberOfSite( site ) | ||
| ); | ||
| const omnibarSiteId = omnibarSite?.ID ?? user?.primary_blog; | ||
|
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. Is it possible that the user's primary blog is a blog they no longer have access to? Does the user need to be validated as a member as well?
Contributor
Author
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. The primary blog is configured through Preferences > Account Defaults, where you can select any of your sites. However, if you choose a site that you don’t own as your primary blog, you may be removed from that site. That said, the likelihood of this happening is very low. |
||
| queryClient.setQueryData( omnibarSiteIdQuery().queryKey, () => omnibarSiteId ); | ||
|
|
||
| // Remove the `origin_site_id` query param from the URL. | ||
| if ( originSiteId ) { | ||
|
|
@@ -67,16 +88,22 @@ export function useInitializeOmnibarSite() { | |
| ); | ||
| } | ||
|
|
||
| // Push the selected site id as the most recent site. | ||
| if ( selectedSiteId && selectedSiteId !== recentSiteIds[ 0 ] ) { | ||
| updateRecentSites( [ ...new Set( [ selectedSiteId, ...recentSiteIds ] ) ].slice( 0, 5 ) ); | ||
| if ( omnibarSiteId && omnibarSiteId !== recentSiteIds?.[ 0 ] ) { | ||
| updateRecentSites( | ||
| [ ...new Set( [ omnibarSiteId, ...( recentSiteIds || [] ) ] ) ].slice( 0, 5 ) | ||
| ); | ||
| } | ||
| }, [ | ||
| isRouteLoaded, | ||
| routeSite, | ||
| originSiteId, | ||
| selectedSiteId, | ||
| fallbackSiteId, | ||
| originSite, | ||
| isLoadingOriginSite, | ||
| recentSite, | ||
| isLoadingRecentSite, | ||
| recentSiteIds, | ||
| isLoadingRecentSiteIds, | ||
| user, | ||
| updateRecentSites, | ||
| ] ); | ||
| } | ||
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'm surprised then how come it's working until now ??