Skip to content

feat(site): show favorite workspaces in ui #11875

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 9 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1688,3 +1688,11 @@ export const updateHealthSettings = async (
);
return response.data;
};

export const putFavoriteWorkspace = async (workspaceID: string) => {
await axios.put(`/api/v2/workspaces/${workspaceID}/favorite`);
};

export const deleteFavoriteWorkspace = async (workspaceID: string) => {
await axios.delete(`/api/v2/workspaces/${workspaceID}/favorite`);
};
21 changes: 21 additions & 0 deletions site/src/api/queries/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,24 @@ const updateWorkspaceBuild = async (
queryKey: workspaceBuildsKey(build.workspace_id),
});
};

export const toggleFavorite = (
workspace: Workspace,
queryClient: QueryClient,
) => {
return {
mutationFn: () => {
if (workspace.favorite) {
return API.deleteFavoriteWorkspace(workspace.id);
} else {
return API.putFavoriteWorkspace(workspace.id);
}
},
onSuccess: async () => {
queryClient.setQueryData(
workspaceByOwnerAndNameKey(workspace.owner_name, workspace.name),
{ ...workspace, favorite: !workspace.favorite },
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You also may want to invalidate the "workspace list" query data.

},
};
};
3 changes: 3 additions & 0 deletions site/src/pages/WorkspacePage/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface WorkspaceProps {
handleSettings: () => void;
handleChangeVersion: () => void;
handleDormantActivate: () => void;
handleToggleFavorite: () => void;
isUpdating: boolean;
isRestarting: boolean;
workspace: TypesGen.Workspace;
Expand Down Expand Up @@ -64,6 +65,7 @@ export const Workspace: FC<WorkspaceProps> = ({
handleSettings,
handleChangeVersion,
handleDormantActivate,
handleToggleFavorite,
workspace,
isUpdating,
isRestarting,
Expand Down Expand Up @@ -131,6 +133,7 @@ export const Workspace: FC<WorkspaceProps> = ({
handleBuildRetryDebug={handleBuildRetryDebug}
handleChangeVersion={handleChangeVersion}
handleDormantActivate={handleDormantActivate}
handleToggleFavorite={handleToggleFavorite}
canRetryDebugMode={canRetryDebugMode}
canChangeVersions={canChangeVersions}
isUpdating={isUpdating}
Expand Down
23 changes: 23 additions & 0 deletions site/src/pages/WorkspacePage/WorkspaceActions/Buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import OutlinedBlockIcon from "@mui/icons-material/BlockOutlined";
import PowerSettingsNewIcon from "@mui/icons-material/PowerSettingsNew";
import RetryIcon from "@mui/icons-material/BuildOutlined";
import RetryDebugIcon from "@mui/icons-material/BugReportOutlined";
import Star from "@mui/icons-material/Star";
import StarBorder from "@mui/icons-material/StarBorder";
import { type FC } from "react";
import type { Workspace, WorkspaceBuildParameter } from "api/typesGenerated";
import { BuildParametersPopover } from "./BuildParametersPopover";
Expand Down Expand Up @@ -190,3 +192,24 @@ export const RetryButton: FC<RetryButtonProps> = ({
</TopbarButton>
);
};

interface FavoriteButtonProps {
handleAction: (workspaceID: string) => void;
workspaceID: string;
isFavorite: boolean;
}

export const FavoriteButton: FC<FavoriteButtonProps> = ({
handleAction,
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's an implicit convention, but when dealing with event handlers, we usually name them using the on prefix like onSomething instead of handleSomething. So instead of handleActions, I think a better name would be onToggle.

Also, you can move the query inside this button 😁. We're trying to put things close to where they're used.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, you can move the query inside this button 😁. We're trying to put things close to where they're used.

I'm not quite sure how to accomplish this; React doesn't seem to like me putting useMutation inside a component.

Copy link
Member Author

Choose a reason for hiding this comment

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

Created #11892 to follow-up.

workspaceID,
isFavorite,
}) => {
return (
<TopbarButton
startIcon={isFavorite ? <Star /> : <StarBorder />}
onClick={() => handleAction(workspaceID)}
>
{isFavorite ? "Unfavorite" : "Favorite"}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this button can have two states, favorite and unfavorite, I would create a story in a storybook for both scenarios.

</TopbarButton>
);
};
12 changes: 12 additions & 0 deletions site/src/pages/WorkspacePage/WorkspaceActions/WorkspaceActions.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

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

One small thing about the design is that I don't think it should be a primary action, located in the same place as workspace actions like start, stop, refresh, etc. I think it should be in the "more options" menu.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think @matifali ?

Copy link
Member

@matifali matifali Jan 29, 2024

Choose a reason for hiding this comment

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

Hmm. I like it on the front page, but what about making a filled vs. an empty star only and removing the text altogether?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd prefer to leave the text on, as a button should show what will happen when you click it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@BrunoQuaresma I think we have enough space to leave the button up top for now; hiding it in the menu makes it less discoverable.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
UpdateButton,
ActivateButton,
RetryButton,
FavoriteButton,
} from "./Buttons";

import Divider from "@mui/material/Divider";
Expand All @@ -30,6 +31,7 @@ import MoreVertOutlined from "@mui/icons-material/MoreVertOutlined";

export interface WorkspaceActionsProps {
workspace: Workspace;
handleToggleFavorite: () => void;
handleStart: (buildParameters?: WorkspaceBuildParameter[]) => void;
handleStop: () => void;
handleRestart: (buildParameters?: WorkspaceBuildParameter[]) => void;
Expand All @@ -51,6 +53,7 @@ export interface WorkspaceActionsProps {

export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
workspace,
handleToggleFavorite,
handleStart,
handleStop,
handleRestart,
Expand Down Expand Up @@ -131,6 +134,13 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
activating: <ActivateButton loading handleAction={handleDormantActivate} />,
retry: <RetryButton handleAction={handleRetry} />,
retryDebug: <RetryButton debug handleAction={handleRetryDebug} />,
toggleFavorite: (
<FavoriteButton
workspaceID={workspace.id}
isFavorite={workspace.favorite}
handleAction={handleToggleFavorite}
/>
),
};

return (
Expand All @@ -150,6 +160,8 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({

{showCancel && <CancelButton handleAction={handleCancel} />}

{buttonMapping.toggleFavorite}

<MoreMenu>
<MoreMenuTrigger>
<TopbarIconButton
Expand Down
1 change: 1 addition & 0 deletions site/src/pages/WorkspacePage/WorkspaceActions/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const actionTypes = [
"updating",
"activate",
"activating",
"toggleFavorite",

// There's no need for a retrying state because retrying starts a transition
// into one of the starting, stopping, or deleting states (based on the
Expand Down
9 changes: 9 additions & 0 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
updateWorkspace,
stopWorkspace,
startWorkspace,
toggleFavorite,
cancelBuild,
} from "api/queries/workspaces";
import { Alert } from "components/Alert/Alert";
Expand Down Expand Up @@ -144,6 +145,11 @@ export const WorkspaceReadyPage: FC<WorkspaceReadyPageProps> = ({
startWorkspace(workspace, queryClient),
);

// Toggle workspace favorite
const toggleFavoriteMutation = useMutation(
toggleFavorite(workspace, queryClient),
);

// Cancel build
const cancelBuildMutation = useMutation(cancelBuild(workspace, queryClient));

Expand Down Expand Up @@ -217,6 +223,9 @@ export const WorkspaceReadyPage: FC<WorkspaceReadyPageProps> = ({
displayError(message);
}
}}
handleToggleFavorite={() => {
toggleFavoriteMutation.mutate();
}}
latestVersion={latestVersion}
canChangeVersions={canChangeVersions}
hideSSHButton={featureVisibility["browser_only"]}
Expand Down
3 changes: 3 additions & 0 deletions site/src/pages/WorkspacePage/WorkspaceTopbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface WorkspaceProps {
template: TypesGen.Template;
permissions: WorkspacePermissions;
latestVersion?: TypesGen.TemplateVersion;
handleToggleFavorite: () => void;
}

export const WorkspaceTopbar: FC<WorkspaceProps> = ({
Expand All @@ -75,6 +76,7 @@ export const WorkspaceTopbar: FC<WorkspaceProps> = ({
handleSettings,
handleChangeVersion,
handleDormantActivate,
handleToggleFavorite,
workspace,
isUpdating,
isRestarting,
Expand Down Expand Up @@ -278,6 +280,7 @@ export const WorkspaceTopbar: FC<WorkspaceProps> = ({
handleRetryDebug={handleBuildRetryDebug}
handleChangeVersion={handleChangeVersion}
handleDormantActivate={handleDormantActivate}
handleToggleFavorite={handleToggleFavorite}
canRetryDebug={canRetryDebugMode}
canChangeVersions={canChangeVersions}
isUpdating={isUpdating}
Expand Down
4 changes: 4 additions & 0 deletions site/src/pages/WorkspacesPage/WorkspacesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TableRow from "@mui/material/TableRow";
import Checkbox from "@mui/material/Checkbox";
import Skeleton from "@mui/material/Skeleton";
import KeyboardArrowRight from "@mui/icons-material/KeyboardArrowRight";
import Star from "@mui/icons-material/Star";
import { useTheme } from "@emotion/react";
import { type FC, type ReactNode } from "react";
import { useNavigate } from "react-router-dom";
Expand Down Expand Up @@ -150,6 +151,9 @@ export const WorkspacesTable: FC<WorkspacesTableProps> = ({
alignItems="center"
>
{workspace.name}
{workspace.favorite && (
<Star css={{ width: 16, height: 16 }} />
)}
{workspace.outdated && (
<WorkspaceOutdatedTooltip
templateName={workspace.template_name}
Expand Down
34 changes: 34 additions & 0 deletions site/src/pages/WorkspacesPage/batchActions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useMutation } from "react-query";
import {
deleteWorkspace,
deleteFavoriteWorkspace,
putFavoriteWorkspace,
startWorkspace,
stopWorkspace,
updateWorkspace,
Expand Down Expand Up @@ -63,12 +65,44 @@ export function useBatchActions(options: UseBatchActionsProps) {
},
});

const favoriteAllMutation = useMutation({
mutationFn: (workspaces: Workspace[]) => {
return Promise.all(
workspaces
.filter((w) => !w.favorite)
.map((w) => putFavoriteWorkspace(w.id)),
);
},
onSuccess,
onError: () => {
displayError("Failed to favorite some workspaces");
},
});

const unfavoriteAllMutation = useMutation({
mutationFn: (workspaces: Workspace[]) => {
return Promise.all(
workspaces
.filter((w) => w.favorite)
.map((w) => deleteFavoriteWorkspace(w.id)),
);
},
onSuccess,
onError: () => {
displayError("Failed to unfavorite some workspaces");
},
});

return {
favoriteAll: favoriteAllMutation.mutateAsync,
unfavoriteAll: unfavoriteAllMutation.mutateAsync,
startAll: startAllMutation.mutateAsync,
stopAll: stopAllMutation.mutateAsync,
deleteAll: deleteAllMutation.mutateAsync,
updateAll: updateAllMutation.mutateAsync,
isLoading:
favoriteAllMutation.isLoading ||
unfavoriteAllMutation.isLoading ||
startAllMutation.isLoading ||
stopAllMutation.isLoading ||
deleteAllMutation.isLoading,
Expand Down