Skip to content

chore(site): refactor filters #13394

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

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d7a4be0
Add base structure for the new filter component
BrunoQuaresma May 15, 2024
f6df4fa
Add error state
BrunoQuaresma May 15, 2024
d41d307
Simplify component
BrunoQuaresma May 15, 2024
47fd44b
Make it accessible
BrunoQuaresma May 15, 2024
d28e9b4
Add simple dropdown button component
BrunoQuaresma May 15, 2024
16bbf5a
Refactor search and add preset filter menu
BrunoQuaresma May 15, 2024
5d8829e
Add status menu
BrunoQuaresma May 15, 2024
c89cf8c
Add status menu to workspaces search
BrunoQuaresma May 15, 2024
0c501e5
WIP: Create base UserMenu
BrunoQuaresma May 17, 2024
c38cd7b
Finish user menu
BrunoQuaresma May 24, 2024
38b10a5
Add search
BrunoQuaresma May 24, 2024
3797ae9
Refactor search field to be more extensible
BrunoQuaresma May 24, 2024
b552828
Extract reusable menu elements
BrunoQuaresma May 24, 2024
498f6cd
Simplify usage
BrunoQuaresma May 24, 2024
de83eda
Improve MenuSearch component
BrunoQuaresma May 24, 2024
c2aa478
Keep search fixed on top
BrunoQuaresma May 24, 2024
30a8373
Add more tests
BrunoQuaresma May 24, 2024
542c377
Merge branch 'main' of https://github.com/coder/coder into bq/improve…
BrunoQuaresma May 28, 2024
01a6616
Handle filtering
BrunoQuaresma May 28, 2024
e97ce00
Add focus on arrow down
BrunoQuaresma May 28, 2024
7638595
Add template menu
BrunoQuaresma May 28, 2024
76224c3
Merge branch 'main' of https://github.com/coder/coder into bq/improve…
BrunoQuaresma May 29, 2024
6d1ce99
Use users paginated request
BrunoQuaresma May 29, 2024
d3c9ef5
Remove forgotten debuggers
BrunoQuaresma May 29, 2024
3250b48
Merge branch 'main' of https://github.com/coder/coder into bq/improve…
BrunoQuaresma Jun 7, 2024
d1b71b1
Remove unecessary prop
BrunoQuaresma Jun 7, 2024
a804a40
Apply michaels review
BrunoQuaresma Jun 7, 2024
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
Prev Previous commit
Next Next commit
Finish user menu
  • Loading branch information
BrunoQuaresma committed May 24, 2024
commit c38cd7bd4acae52d5498bd8f8f024cd95ac124b5
13 changes: 13 additions & 0 deletions site/src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ export const usePopover = () => {
return context;
};

export const withPopover =
<TProps extends object>(
Component: FC<TProps>,
popoverProps?: Omit<PopoverProps, "children">,
): FC<TProps> =>
(props: TProps) => {
return (
<Popover {...popoverProps}>
<Component {...props} />
</Popover>
);
};

export const PopoverTrigger = (
props: HTMLAttributes<HTMLElement> & { children: TriggerElement },
) => {
Expand Down
59 changes: 59 additions & 0 deletions site/src/pages/WorkspacesPage/WorkspaceSearch/UserMenu.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { Meta, StoryObj } from "@storybook/react";
import { userEvent, within } from "@storybook/test";
import { useState } from "react";
import { UserMenu } from "./UserMenu";

const meta: Meta<typeof UserMenu> = {
title: "pages/WorkspacesPage/UserMenu",
component: UserMenu,
args: {
placeholder: "All users",
},
parameters: {
queries: [
{
key: ["users", {}],
data: {
users: [
{ id: "1", name: "Alice", username: "alice", avatar_url: "" },
{ id: "2", name: "Bob", username: "bob", avatar_url: "" },
{ id: "3", name: "Charlie", username: "charlie", avatar_url: "" },
],
},
},
],
},
};

export default meta;
type Story = StoryObj<typeof UserMenu>;

export const Close: Story = {};

export const Open: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const button = canvas.getByRole("button", { name: /Select user/i });
await userEvent.click(button);
},
};

export const Default: Story = {
args: {
selected: "2",
},
};

export const SelectOption: Story = {
render: function UserMenuWithState(args) {
const [selected, setSelected] = useState<string | undefined>(undefined);
return <UserMenu {...args} selected={selected} onSelect={setSelected} />;
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const button = canvas.getByRole("button", { name: /Select user/i });
await userEvent.click(button);
const option = canvas.getByText("Charlie");
await userEvent.click(option);
},
};
92 changes: 52 additions & 40 deletions site/src/pages/WorkspacesPage/WorkspaceSearch/UserMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { type Theme, useTheme } from "@emotion/react";
import CheckOutlined from "@mui/icons-material/CheckOutlined";
import Button from "@mui/material/Button";
import MenuItem from "@mui/material/MenuItem";
import MenuList from "@mui/material/MenuList";
import type { FC, ReactNode } from "react";
import { useQuery } from "react-query";
import { users } from "api/queries/users";
import { DropdownArrow } from "components/DropdownArrow/DropdownArrow";
import { Loader } from "components/Loader/Loader";
import {
Popover,
PopoverContent,
PopoverTrigger,
usePopover,
withPopover,
} from "components/Popover/Popover";
import { Stack } from "components/Stack/Stack";
import { UserAvatar } from "components/UserAvatar/UserAvatar";

type Option = {
label: string;
Expand Down Expand Up @@ -48,46 +52,54 @@ type UserMenuProps = {
onSelect: (value: string) => void;
};

export const UserMenu: FC<UserMenuProps> = (props) => {
export const UserMenu = withPopover<UserMenuProps>((props) => {
const popover = usePopover();
const { placeholder, selected, onSelect } = props;
const selectedOption = options.find((option) => option.value === selected);
const userOptionsQuery = useQuery({
...users({}),
enabled: selected !== undefined || popover.isOpen,
});
const options = userOptionsQuery.data?.users.map((u) => ({
label: u.name ?? u.username,
value: u.id,
addon: <UserAvatar size="xs" username={u.username} src={u.avatar_url} />,
}));
const selectedOption = options?.find((option) => option.value === selected);

return (
<Popover>
{({ setIsOpen }) => {
return (
<>
<PopoverTrigger>
<Button
aria-label="Select status"
endIcon={<DropdownArrow />}
startIcon={selectedOption?.addon}
<>
<PopoverTrigger>
<Button
aria-label="Select user"
endIcon={<DropdownArrow />}
startIcon={<span>{selectedOption?.addon}</span>}
>
{selectedOption ? selectedOption.label : placeholder}
</Button>
</PopoverTrigger>
<PopoverContent>
{options ? (
<MenuList dense>
{options.map((option) => (
<MenuItem
selected={option.value === selected}
key={option.value}
onClick={() => {
popover.setIsOpen(false);
onSelect(option.value);
}}
>
{selectedOption ? selectedOption.label : placeholder}
</Button>
</PopoverTrigger>
<PopoverContent>
<MenuList dense>
{options.map((option) => (
<MenuItem
selected={option.value === selected}
key={option.value}
onClick={() => {
setIsOpen(false);
onSelect(option.value);
}}
>
<SelectLabel
option={option}
selected={option.value === selected}
/>
</MenuItem>
))}
</MenuList>
</PopoverContent>
</>
);
}}
</Popover>
<SelectLabel
option={option}
selected={option.value === selected}
/>
</MenuItem>
))}
</MenuList>
) : (
<Loader />
)}
</PopoverContent>
</>
);
};
});