mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 22:30:41 +08:00
Move notification teaser helpers into lib
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
<script lang="ts">
|
||||
import type { RepoRoute } from "@app/views/repo/router";
|
||||
import type { NotificationItem } from "@bindings/cob/inbox/NotificationItem";
|
||||
import type { ComponentProps } from "svelte";
|
||||
|
||||
import { compressActions, notificationActions } from "@app/lib/notification";
|
||||
import {
|
||||
compressActions,
|
||||
notificationActions,
|
||||
notificationIcon,
|
||||
notificationRoute,
|
||||
notificationStatusColor,
|
||||
} from "@app/lib/notification";
|
||||
import { push } from "@app/lib/router";
|
||||
import {
|
||||
absoluteTimestamp,
|
||||
authorForNodeId,
|
||||
formatTimestamp,
|
||||
issueStatusBackgroundColor,
|
||||
issueStatusColor,
|
||||
patchStatusBackgroundColor,
|
||||
patchStatusColor,
|
||||
} from "@app/lib/utils";
|
||||
|
||||
import Button from "@app/components/Button.svelte";
|
||||
@@ -53,63 +53,10 @@
|
||||
}
|
||||
});
|
||||
|
||||
const icon: ComponentProps<typeof Icon>["name"] = $derived.by(() => {
|
||||
const lastDetail = notificationItems.at(-1);
|
||||
if (lastDetail?.type === "issue" && lastDetail.status.status !== "open") {
|
||||
return `issue-${lastDetail.status.status}` as const;
|
||||
} else if (lastDetail?.type === "issue") {
|
||||
return "issue" as const;
|
||||
} else if (
|
||||
lastDetail?.type === "patch" &&
|
||||
lastDetail.status.status !== "open"
|
||||
) {
|
||||
return `patch-${lastDetail.status.status}` as const;
|
||||
} else {
|
||||
return "patch" as const;
|
||||
}
|
||||
});
|
||||
|
||||
const statusColor = $derived.by(() => {
|
||||
const lastDetail = notificationItems.at(-1);
|
||||
if (lastDetail?.type === "patch") {
|
||||
return {
|
||||
color: patchStatusColor[lastDetail.status.status],
|
||||
background: patchStatusBackgroundColor[lastDetail.status.status],
|
||||
};
|
||||
} else if (lastDetail?.type === "issue") {
|
||||
return {
|
||||
color: issueStatusColor[lastDetail.status.status],
|
||||
background: issueStatusBackgroundColor[lastDetail.status.status],
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
color: "var(--color-text-secondary)",
|
||||
background: "var(--color-surface-subtle)",
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
const route = $derived.by(() => {
|
||||
const lastDetail = notificationItems.at(-1);
|
||||
switch (lastDetail?.type) {
|
||||
case "patch":
|
||||
return {
|
||||
resource: "repo.patch",
|
||||
rid,
|
||||
patch: lastDetail.id,
|
||||
status: undefined,
|
||||
} as RepoRoute;
|
||||
case "issue":
|
||||
return {
|
||||
resource: "repo.issue",
|
||||
rid,
|
||||
issue: lastDetail.id,
|
||||
status: "all",
|
||||
} as RepoRoute;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
});
|
||||
const lastItem = $derived(notificationItems.at(-1));
|
||||
const icon = $derived(notificationIcon(lastItem));
|
||||
const statusColor = $derived(notificationStatusColor(lastItem));
|
||||
const route = $derived(notificationRoute(rid, lastItem));
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
||||
+74
-1
@@ -1,11 +1,21 @@
|
||||
import type { RepoRoute } from "@app/views/repo/router";
|
||||
import type { ActionWithAuthor } from "@bindings/cob/inbox/ActionWithAuthor";
|
||||
import type { NotificationItem } from "@bindings/cob/inbox/NotificationItem";
|
||||
import type { Action as IssueAction } from "@bindings/cob/issue/Action";
|
||||
import type { Action as PatchAction } from "@bindings/cob/patch/Action";
|
||||
|
||||
import isEqual from "lodash/isEqual";
|
||||
import uniqWith from "lodash/uniqWith";
|
||||
|
||||
import { emojiToTwemoji, formatOid, pluralize } from "@app/lib/utils";
|
||||
import {
|
||||
emojiToTwemoji,
|
||||
formatOid,
|
||||
issueStatusBackgroundColor,
|
||||
issueStatusColor,
|
||||
patchStatusBackgroundColor,
|
||||
patchStatusColor,
|
||||
pluralize,
|
||||
} from "@app/lib/utils";
|
||||
|
||||
export type Action =
|
||||
ActionWithAuthor<IssueAction> | ActionWithAuthor<PatchAction>;
|
||||
@@ -196,3 +206,66 @@ export function notificationActions(items: { actions: Action[] }[]): Action[] {
|
||||
isEqual,
|
||||
).sort((a, b) => b.timestamp - a.timestamp);
|
||||
}
|
||||
|
||||
export function notificationIcon(
|
||||
item: NotificationItem | undefined,
|
||||
):
|
||||
| "issue"
|
||||
| "issue-closed"
|
||||
| "patch"
|
||||
| "patch-draft"
|
||||
| "patch-archived"
|
||||
| "patch-merged" {
|
||||
if (item?.type === "issue") {
|
||||
return item.status.status === "open"
|
||||
? "issue"
|
||||
: `issue-${item.status.status}`;
|
||||
}
|
||||
if (item?.type === "patch" && item.status.status !== "open") {
|
||||
return `patch-${item.status.status}`;
|
||||
}
|
||||
return "patch";
|
||||
}
|
||||
|
||||
export function notificationStatusColor(item: NotificationItem | undefined): {
|
||||
color: string;
|
||||
background: string;
|
||||
} {
|
||||
switch (item?.type) {
|
||||
case "patch":
|
||||
return {
|
||||
color: patchStatusColor[item.status.status],
|
||||
background: patchStatusBackgroundColor[item.status.status],
|
||||
};
|
||||
case "issue":
|
||||
return {
|
||||
color: issueStatusColor[item.status.status],
|
||||
background: issueStatusBackgroundColor[item.status.status],
|
||||
};
|
||||
default:
|
||||
return {
|
||||
color: "var(--color-text-secondary)",
|
||||
background: "var(--color-surface-subtle)",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function notificationRoute(
|
||||
rid: string,
|
||||
item: NotificationItem | undefined,
|
||||
): RepoRoute | undefined {
|
||||
switch (item?.type) {
|
||||
case "patch":
|
||||
return {
|
||||
resource: "repo.patch",
|
||||
rid,
|
||||
patch: item.id,
|
||||
status: undefined,
|
||||
reviewId: undefined,
|
||||
};
|
||||
case "issue":
|
||||
return { resource: "repo.issue", rid, issue: item.id, status: "all" };
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { NotificationItem } from "@bindings/cob/inbox/NotificationItem";
|
||||
import type { Action as IssueAction } from "@bindings/cob/issue/Action";
|
||||
import type { Action as PatchAction } from "@bindings/cob/patch/Action";
|
||||
|
||||
@@ -8,6 +9,9 @@ import {
|
||||
compressActions,
|
||||
createSummary,
|
||||
notificationActions,
|
||||
notificationIcon,
|
||||
notificationRoute,
|
||||
notificationStatusColor,
|
||||
} from "@app/lib/notification";
|
||||
import { formatOid } from "@app/lib/utils";
|
||||
|
||||
@@ -246,3 +250,78 @@ describe("notificationActions", () => {
|
||||
).toEqual([3, 2, 1]);
|
||||
});
|
||||
});
|
||||
|
||||
function item(
|
||||
type: "issue" | "patch",
|
||||
status: NotificationItem["status"],
|
||||
): NotificationItem {
|
||||
return {
|
||||
type,
|
||||
rowId: "1",
|
||||
id: oid,
|
||||
update: { type: "created", name: "refs/cobs/x", oid },
|
||||
title: "Title",
|
||||
timestamp,
|
||||
status,
|
||||
actions: [],
|
||||
repoId: "rad:z",
|
||||
} as NotificationItem;
|
||||
}
|
||||
|
||||
describe("notification teaser", () => {
|
||||
test.each([
|
||||
[item("issue", { status: "open" }), "issue"],
|
||||
[item("issue", { status: "closed", reason: "solved" }), "issue-closed"],
|
||||
[item("patch", { status: "open" }), "patch"],
|
||||
[item("patch", { status: "draft" }), "patch-draft"],
|
||||
[
|
||||
item("patch", { status: "merged", revision, commit: oid }),
|
||||
"patch-merged",
|
||||
],
|
||||
[undefined, "patch"],
|
||||
])("icon of %# is %j", (notification, expected) => {
|
||||
expect(notificationIcon(notification)).toBe(expected);
|
||||
});
|
||||
|
||||
test("colors follow the status", () => {
|
||||
expect(notificationStatusColor(item("patch", { status: "draft" }))).toEqual(
|
||||
{
|
||||
color: "var(--color-text-draft)",
|
||||
background: "var(--color-surface-draft)",
|
||||
},
|
||||
);
|
||||
expect(
|
||||
notificationStatusColor(
|
||||
item("issue", { status: "closed", reason: "solved" }),
|
||||
),
|
||||
).toEqual({
|
||||
color: "var(--color-text-closed)",
|
||||
background: "var(--color-surface-closed)",
|
||||
});
|
||||
expect(notificationStatusColor(undefined)).toEqual({
|
||||
color: "var(--color-text-secondary)",
|
||||
background: "var(--color-surface-subtle)",
|
||||
});
|
||||
});
|
||||
|
||||
test("routes to the patch or to the issue in every status", () => {
|
||||
expect(
|
||||
notificationRoute("rad:z", item("patch", { status: "open" })),
|
||||
).toEqual({
|
||||
resource: "repo.patch",
|
||||
rid: "rad:z",
|
||||
patch: oid,
|
||||
status: undefined,
|
||||
reviewId: undefined,
|
||||
});
|
||||
expect(
|
||||
notificationRoute("rad:z", item("issue", { status: "open" })),
|
||||
).toEqual({
|
||||
resource: "repo.issue",
|
||||
rid: "rad:z",
|
||||
issue: oid,
|
||||
status: "all",
|
||||
});
|
||||
expect(notificationRoute("rad:z", undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user