Skip to content

Commit 6182d98

Browse files
committed
Refactor types
1 parent 8ea7089 commit 6182d98

File tree

10 files changed

+137
-71
lines changed

10 files changed

+137
-71
lines changed

coderd/apidoc/docs.go

+16-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+16-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/inboxnotifications.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
notificationFormatPlaintext = "plaintext"
3030
)
3131

32-
var fallbackIcons = map[uuid.UUID]string{
32+
var fallbackIcons = map[uuid.UUID]codersdk.InboxNotificationFallbackIcon{
3333
// workspace related notifications
3434
notifications.TemplateWorkspaceCreated: codersdk.FallbackIconWorkspace,
3535
notifications.TemplateWorkspaceManuallyUpdated: codersdk.FallbackIconWorkspace,
@@ -80,7 +80,7 @@ func convertInboxNotificationResponse(ctx context.Context, logger slog.Logger, n
8080
Targets: notif.Targets,
8181
Title: notif.Title,
8282
Content: notif.Content,
83-
Icon: notif.Icon,
83+
Icon: codersdk.InboxNotificationFallbackIcon(notif.Icon),
8484
Actions: func() []codersdk.InboxNotificationAction {
8585
var actionsList []codersdk.InboxNotificationAction
8686
err := json.Unmarshal([]byte(notif.Actions), &actionsList)

coderd/inboxnotifications_internal_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ func TestInboxNotifications_ensureNotificationIcon(t *testing.T) {
1616

1717
tests := []struct {
1818
name string
19-
icon string
19+
icon codersdk.InboxNotificationFallbackIcon
2020
templateID uuid.UUID
21-
expectedIcon string
21+
expectedIcon codersdk.InboxNotificationFallbackIcon
2222
}{
2323
{"WorkspaceCreated", "", notifications.TemplateWorkspaceCreated, codersdk.FallbackIconWorkspace},
2424
{"UserAccountCreated", "", notifications.TemplateUserAccountCreated, codersdk.FallbackIconAccount},

codersdk/inboxnotification.go

+16-14
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,26 @@ import (
1010
"github.com/google/uuid"
1111
)
1212

13+
type InboxNotificationFallbackIcon string
14+
1315
const (
14-
FallbackIconWorkspace = "DEFAULT_ICON_WORKSPACE"
15-
FallbackIconAccount = "DEFAULT_ICON_ACCOUNT"
16-
FallbackIconTemplate = "DEFAULT_ICON_TEMPLATE"
17-
FallbackIconOther = "DEFAULT_ICON_OTHER"
16+
FallbackIconWorkspace InboxNotificationFallbackIcon = "DEFAULT_ICON_WORKSPACE"
17+
FallbackIconAccount InboxNotificationFallbackIcon = "DEFAULT_ICON_ACCOUNT"
18+
FallbackIconTemplate InboxNotificationFallbackIcon = "DEFAULT_ICON_TEMPLATE"
19+
FallbackIconOther InboxNotificationFallbackIcon = "DEFAULT_ICON_OTHER"
1820
)
1921

2022
type InboxNotification struct {
21-
ID uuid.UUID `json:"id" format:"uuid"`
22-
UserID uuid.UUID `json:"user_id" format:"uuid"`
23-
TemplateID uuid.UUID `json:"template_id" format:"uuid"`
24-
Targets []uuid.UUID `json:"targets" format:"uuid"`
25-
Title string `json:"title"`
26-
Content string `json:"content"`
27-
Icon string `json:"icon"`
28-
Actions []InboxNotificationAction `json:"actions"`
29-
ReadAt *time.Time `json:"read_at"`
30-
CreatedAt time.Time `json:"created_at" format:"date-time"`
23+
ID uuid.UUID `json:"id" format:"uuid"`
24+
UserID uuid.UUID `json:"user_id" format:"uuid"`
25+
TemplateID uuid.UUID `json:"template_id" format:"uuid"`
26+
Targets []uuid.UUID `json:"targets" format:"uuid"`
27+
Title string `json:"title"`
28+
Content string `json:"content"`
29+
Icon InboxNotificationFallbackIcon `json:"icon"`
30+
Actions []InboxNotificationAction `json:"actions"`
31+
ReadAt *time.Time `json:"read_at"`
32+
CreatedAt time.Time `json:"created_at" format:"date-time"`
3133
}
3234

3335
type InboxNotificationAction struct {

docs/reference/api/notifications.md

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/api/schemas.md

+32-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/api/typesGenerated.ts

+15-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/modules/notifications/NotificationsInbox/InboxAvatar.stories.tsx

+14-8
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,32 @@ const meta: Meta<typeof InboxAvatar> = {
99
export default meta;
1010
type Story = StoryObj<typeof InboxAvatar>;
1111

12-
export const Workspace: Story = {
12+
export const Custom: Story = {
1313
args: {
14-
icon: "DEFAULT_WORKSPACE_ICON",
14+
icon: "/icon/git.svg",
1515
},
1616
};
1717

18-
export const Account: Story = {
18+
export const FallbackWorkspace: Story = {
1919
args: {
20-
icon: "DEFAULT_ACCOUNT_ICON",
20+
icon: "DEFAULT_ICON_WORKSPACE",
2121
},
2222
};
2323

24-
export const Template: Story = {
24+
export const FallbackAccount: Story = {
2525
args: {
26-
icon: "DEFAULT_TEMPLATE_ICON",
26+
icon: "DEFAULT_ICON_ACCOUNT",
2727
},
2828
};
2929

30-
export const Other: Story = {
30+
export const FallbackTemplate: Story = {
3131
args: {
32-
icon: "DEFAULT_OTHER_ICON",
32+
icon: "DEFAULT_ICON_TEMPLATE",
33+
},
34+
};
35+
36+
export const FallbackOther: Story = {
37+
args: {
38+
icon: "DEFAULT_ICON_OTHER",
3339
},
3440
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
InboxNotificationFallbackIcons,
3+
type InboxNotificationFallbackIcon,
4+
} from "api/typesGenerated";
15
import { Avatar } from "components/Avatar/Avatar";
26
import {
37
InfoIcon,
@@ -8,23 +12,28 @@ import {
812
import type { FC } from "react";
913
import type React from "react";
1014

11-
export type InboxIcon =
12-
| "DEFAULT_WORKSPACE_ICON"
13-
| "DEFAULT_ACCOUNT_ICON"
14-
| "DEFAULT_TEMPLATE_ICON"
15-
| "DEFAULT_OTHER_ICON";
16-
17-
const inboxIcons: Record<InboxIcon, React.ReactNode> = {
18-
DEFAULT_WORKSPACE_ICON: <LaptopIcon />,
19-
DEFAULT_ACCOUNT_ICON: <UserIcon />,
20-
DEFAULT_TEMPLATE_ICON: <LayoutTemplateIcon />,
21-
DEFAULT_OTHER_ICON: <InfoIcon />,
15+
const fallbackIcons: Record<InboxNotificationFallbackIcon, React.ReactNode> = {
16+
DEFAULT_ICON_WORKSPACE: <LaptopIcon />,
17+
DEFAULT_ICON_ACCOUNT: <UserIcon />,
18+
DEFAULT_ICON_TEMPLATE: <LayoutTemplateIcon />,
19+
DEFAULT_ICON_OTHER: <InfoIcon />,
2220
};
2321

2422
type InboxAvatarProps = {
25-
icon: InboxIcon;
23+
icon: string;
2624
};
2725

2826
export const InboxAvatar: FC<InboxAvatarProps> = ({ icon }) => {
29-
return <Avatar variant="icon">{inboxIcons[icon]}</Avatar>;
27+
if (isInboxNotificationFallbackIcon(icon)) {
28+
return <Avatar variant="icon">{fallbackIcons[icon]}</Avatar>;
29+
}
30+
31+
console.log("ICON");
32+
return <Avatar variant="icon" src={icon} />;
3033
};
34+
35+
function isInboxNotificationFallbackIcon(
36+
icon: string,
37+
): icon is InboxNotificationFallbackIcon {
38+
return (InboxNotificationFallbackIcons as string[]).includes(icon);
39+
}

0 commit comments

Comments
 (0)