Skip to content

refactor: use WidgetsIcon for null resources #3754

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
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
Prev Previous commit
refactor: clean up types in ResourceAvatar.tsx
Before, we were using `string` for `type` in `ResourceAvatar`. This
meant it wasn't tied to the types generated from the backend.

Now it imports `WorkspaceResource` so that there is a single source of
truth and they always stay in sync.
  • Loading branch information
jsjoeio committed Aug 30, 2022
commit 6716accab8938498f8eb8c5b539181e601d6a412
5 changes: 3 additions & 2 deletions site/src/components/Resources/ResourceAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import ImageIcon from "@material-ui/icons/ImageOutlined"
import MemoryIcon from "@material-ui/icons/MemoryOutlined"
import WidgetsIcon from "@material-ui/icons/WidgetsOutlined"
import React from "react"
import { WorkspaceResource } from "../../api/typesGenerated"

// For this special case, we need to apply a different style because how this
// particular icon has been designed
const AdjustedMemoryIcon: typeof MemoryIcon = ({ style, ...props }) => {
return <MemoryIcon style={{ ...style, fontSize: 24 }} {...props} />
}

const iconByResource: Record<string, typeof MemoryIcon> = {
const iconByResource: Record<WorkspaceResource["type"], typeof MemoryIcon | undefined> = {
docker_volume: FolderIcon,
docker_container: AdjustedMemoryIcon,
docker_image: ImageIcon,
Expand All @@ -25,7 +26,7 @@ const iconByResource: Record<string, typeof MemoryIcon> = {
null_resource: WidgetsIcon,
}

export type ResourceAvatarProps = { type: string }
export type ResourceAvatarProps = { type: WorkspaceResource["type"] }

export const ResourceAvatar: React.FC<ResourceAvatarProps> = ({ type }) => {
const IconComponent = iconByResource[type] ?? WidgetsIcon
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the resource can be undefined, I modified the return type of iconByResource to match that, which lets us remove this eslint line.

Expand Down