Skip to content
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

fix(workspace ui): stop re-rendering user bar and compositions on route change #9137

Merged
merged 5 commits into from
Aug 27, 2024
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
11 changes: 8 additions & 3 deletions scopes/cloud/cloud/cloud.graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export function cloudSchema(cloud: CloudMain): Schema {
}
type Query {
getCurrentUser: CloudUser
loginUrl(redirectUrl: String!): String!
loginUrl: String!
getCloudScopes(ids: [String!]): [CloudScope!]
isLoggedIn: Boolean
}
type Mutation {
logout: Boolean
setRedirectUrl(redirectUrl: String!): Boolean
}
`,
resolvers: {
Expand All @@ -38,8 +39,8 @@ export function cloudSchema(cloud: CloudMain): Schema {
isLoggedIn: () => {
return cloud.isLoggedIn();
},
loginUrl: (_, { redirectUrl }: { redirectUrl?: string }) => {
return cloud.getLoginUrl({ redirectUrl });
loginUrl: () => {
return cloud.getLoginUrl();
},
getCurrentUser: async () => {
const user = await cloud.getCurrentUser();
Expand All @@ -57,6 +58,10 @@ export function cloudSchema(cloud: CloudMain): Schema {
cloud.logout();
return true;
},
setRedirectUrl: async (_, { redirectUrl }: { redirectUrl: string }) => {
cloud.setRedirectUrl(redirectUrl);
return true;
},
},
},
};
Expand Down
4 changes: 4 additions & 0 deletions scopes/cloud/cloud/cloud.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ export class CloudMain {
this.globalConfig.delSync(CFG_USER_NAME_KEY);
}

setRedirectUrl(redirectUrl: string) {
this.REDIRECT_URL = redirectUrl;
}

async whoami(): Promise<string | undefined> {
const currentUser = await this.getCurrentUser();
if (!currentUser?.username) {
Expand Down
16 changes: 10 additions & 6 deletions scopes/cloud/cloud/cloud.ui.runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class CloudUI {
return flatten(this.userBarSectionSlot.values());
}

CloudUserBar = () => {
return <UserBar sections={this.listUserBarSections()} items={this.listUserBarItems()} />;
};

static runtime = UIRuntime;

static dependencies = [WorkspaceAspect, LanesAspect, ComponentAspect];
Expand Down Expand Up @@ -150,13 +154,13 @@ export class CloudUI {
// },
// },
]);
const userBarItems = cloudUI.listUserBarItems();
const userBarSections = cloudUI.listUserBarSections();
const CloudUserBar = () => <UserBar sections={userBarSections} items={userBarItems} />;
workspace.registerMenuWidget([CloudUserBar]);
workspace.registerMenuWidget([cloudUI.CloudUserBar]);
if (workspace) {
lanes.registerMenuWidget(CloudUserBar);
component.registerRightSideMenuItem({ item: <CloudUserBar key={'cloud-user-bar-comp-menu'} />, order: 100 });
lanes.registerMenuWidget(cloudUI.CloudUserBar);
component.registerRightSideMenuItem({
item: <cloudUI.CloudUserBar key={'cloud-user-bar-comp-menu'} />,
order: 100,
});
}
return cloudUI;
}
Expand Down
28 changes: 22 additions & 6 deletions scopes/cloud/hooks/use-current-user/use-current-user.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import React from 'react';
import { useDataQuery } from '@teambit/ui-foundation.ui.hooks.use-data-query';
import { gql } from '@apollo/client';
import { useMutation, gql } from '@apollo/client';
import { CloudUser } from '@teambit/cloud.models.cloud-user';

export const SET_REDIRECT_URL_MUTATION = gql`
mutation SetRedirectUrl($redirectUrl: String!) {
setRedirectUrl(redirectUrl: $redirectUrl)
}
`;

export const CURRENT_USER_QUERY = gql`
query CurrentUser($redirectUrl: String!) {
query CurrentUser {
getCurrentUser {
username
displayName
profileImage
}
loginUrl(redirectUrl: $redirectUrl)
loginUrl
isLoggedIn
}
`;
Expand All @@ -20,11 +27,20 @@ export function useCurrentUser(): {
isLoggedIn?: boolean;
loading?: boolean;
} {
const [setRedirectUrl] = useMutation(SET_REDIRECT_URL_MUTATION);

React.useEffect(() => {
const redirectUrl = window.location.href;
setRedirectUrl({ variables: { redirectUrl } }).catch((error) => {
// eslint-disable-next-line no-console
console.error('Error setting redirect URL:', error);
});
}, [window.location.href]);

const { data, loading } = useDataQuery(CURRENT_USER_QUERY, {
variables: {
redirectUrl: window.location.href,
},
fetchPolicy: 'cache-first',
});

return {
currentUser: {
username: data?.getCurrentUser?.username ?? undefined,
Expand Down
2 changes: 1 addition & 1 deletion scopes/cloud/ui/login/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function Login({ loginText = 'Login', loginUrl }: LoginProps) {

return (
<div className={styles.login}>
<Link className={styles.text} href={loginUrl}>
<Link external className={styles.text} href={loginUrl}>
{loginText}
</Link>
</div>
Expand Down
2 changes: 1 addition & 1 deletion scopes/compositions/compositions/compositions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function Compositions({ menuBarWidgets, emptyState }: CompositionsProp) {
pathSegments[pathSegments.length - 1] = composition.identifier.toLowerCase();
}

const urlParams = new URLSearchParams();
const urlParams = new URLSearchParams(searchParams);
if (versionFromQueryParams) {
urlParams.set('version', versionFromQueryParams);
}
Expand Down