Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dashboard/app/interim-omnibar/interim-omnibar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function InterimOmnibar( {
onToggleNotifications,
}: Props ) {
const user = userProp ?? emptyUser;
const siteId = user.primary_blog ?? null;

Copy link
Copy Markdown
Contributor

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 ??

const siteId = site?.ID ?? null;
const siteSlug = site?.slug ?? null;
const siteAdminUrl = site?.options?.admin_url ?? null;
const isUnlaunchedSite = !! site && site.launch_status === 'unlaunched' && ! site.is_a4a_dev_site;
Expand Down
53 changes: 40 additions & 13 deletions client/dashboard/app/omnibar/site.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
omnibarSiteIdQuery,
queryClient,
siteByIdQuery,
userPreferenceQuery,
userPreferenceOptimisticMutation,
} from '@automattic/api-queries';
Expand All @@ -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 ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: maybe add this as canViewSite() in client/dashboard/sites/features.ts so that it's paired nicely with canManageSite() in that file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

but can't any one view the site 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm... that's right... maybe isMemberOfSite() is clearer after all 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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 ) ||
Comment thread
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 ) {
Expand All @@ -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,
] );
}
Loading