Skip to content

feat(site): add an organization switcher to the user menu #13269

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

Merged
merged 7 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(site/experimental): add an organization switcher to the user menu
  • Loading branch information
aslilac committed May 14, 2024
commit 25cc464e5da24773df86d131543348e6266a3b5e
7 changes: 7 additions & 0 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,10 @@ export const updateAppearanceSettings = (
},
};
};

export const myOrganizations = () => {
return {
queryKey: ["organizations", "me"],
queryFn: () => API.getOrganizations(),
};
};
2 changes: 1 addition & 1 deletion site/src/contexts/auth/RequireAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const RequireAuth: FC = () => {
};

type RequireKeys<T, R extends keyof T> = Omit<T, R> & {
[K in keyof Pick<T, R>]: NonNullable<T[K]>;
[K in keyof Pick<T, R>]-?: NonNullable<T[K]>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What -? does?

Copy link
Member Author

Choose a reason for hiding this comment

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

"make the mapped key non-optional". it's weird but NonNullable is not enough on it's own. 🙃

};

// We can do some TS magic here but I would rather to be explicit on what
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { css, type Interpolation, type Theme, useTheme } from "@emotion/react";
import Badge from "@mui/material/Badge";
import type { FC } from "react";
import { useQuery } from "react-query";
import type * as TypesGen from "api/typesGenerated";
import { DropdownArrow } from "components/DropdownArrow/DropdownArrow";
import {
Expand All @@ -11,6 +12,7 @@
import { UserAvatar } from "components/UserAvatar/UserAvatar";
import { BUTTON_SM_HEIGHT, navHeight } from "theme/constants";
import { UserDropdownContent } from "./UserDropdownContent";
import { myOrganizations } from "api/queries/users";

Check failure on line 15 in site/src/modules/dashboard/Navbar/UserDropdown/UserDropdown.tsx

View workflow job for this annotation

GitHub Actions / lint

`api/queries/users` import should occur before type import of `api/typesGenerated`

Check failure on line 15 in site/src/modules/dashboard/Navbar/UserDropdown/UserDropdown.tsx

View workflow job for this annotation

GitHub Actions / lint

`api/queries/users` import should occur before type import of `api/typesGenerated`

export interface UserDropdownProps {
user: TypesGen.User;
Expand All @@ -26,6 +28,7 @@
onSignOut,
}) => {
const theme = useTheme();
const organizationsQuery = useQuery(myOrganizations());

return (
<Popover>
Expand Down Expand Up @@ -63,6 +66,7 @@
user={user}
buildInfo={buildInfo}
supportLinks={supportLinks}
organizations={organizationsQuery.data}
onSignOut={onSignOut}
/>
</PopoverContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { ExternalImage } from "components/ExternalImage/ExternalImage";
import { usePopover } from "components/Popover/Popover";
import { Stack } from "components/Stack/Stack";
import { useDashboard } from "modules/dashboard/useDashboard";

export const Language = {
accountLabel: "Account",
Expand Down Expand Up @@ -84,18 +85,21 @@

export interface UserDropdownContentProps {
user: TypesGen.User;
organizations?: TypesGen.Organization[];
buildInfo?: TypesGen.BuildInfoResponse;
supportLinks?: readonly TypesGen.LinkConfig[];
onSignOut: () => void;
}

export const UserDropdownContent: FC<UserDropdownContentProps> = ({
buildInfo,
user,
organizations,
buildInfo,
supportLinks,
onSignOut,
}) => {
const popover = usePopover();
const { organizationId, setOrganizationId } = useDashboard();

const onPopoverClose = () => {
popover.setIsOpen(false);
Expand Down Expand Up @@ -128,6 +132,42 @@

<Divider css={{ marginBottom: 8 }} />

{organizations && (
<div>
<div
css={{
padding: "8px 20px 6px",
textTransform: "uppercase",
letterSpacing: 1.1,
lineHeight: 1.1,
fontSize: "0.8em",
}}
>
My teams
</div>
{organizations.map((org) => (
<MenuItem
key={org.id}
css={styles.menuItem}
onClick={() => {
setOrganizationId(org.id);
popover.setIsOpen(false);
}}
>
{/* <LogoutIcon css={styles.menuItemIcon} /> */}
<Stack direction="row" spacing={1} css={styles.menuItemText}>
{org.name}
{organizationId == org.id && (

Check failure on line 160 in site/src/modules/dashboard/Navbar/UserDropdown/UserDropdownContent.tsx

View workflow job for this annotation

GitHub Actions / fmt

Expected '===' and instead saw '=='

Check failure on line 160 in site/src/modules/dashboard/Navbar/UserDropdown/UserDropdownContent.tsx

View workflow job for this annotation

GitHub Actions / lint

Expected '===' and instead saw '=='

Check failure on line 160 in site/src/modules/dashboard/Navbar/UserDropdown/UserDropdownContent.tsx

View workflow job for this annotation

GitHub Actions / lint

Expected '===' and instead saw '=='
<span css={{ fontSize: 12, color: "gray" }}>Current</span>
)}
</Stack>
</MenuItem>
))}
</div>
)}

<Divider css={{ marginBottom: 8 }} />

<Link to="/settings/account" css={styles.link}>
<MenuItem css={styles.menuItem} onClick={onPopoverClose}>
<AccountIcon css={styles.menuItemIcon} />
Expand Down
Loading