Skip to content

Commit a038d03

Browse files
committed
fix(notifications): render body from payload or Slack JSON
1 parent d5b2748 commit a038d03

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/components/Notifications/NotificationSendHistory.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,14 @@ export default function NotificationSendHistoryList({
186186
enableGrouping={true}
187187
onRowClick={(row: NotificationSendHistoryWithSubRows) => {
188188
searchParams.set("id", row.id);
189-
if (row.body) {
190-
searchParams.delete("hasBodyPayload");
191-
} else {
189+
190+
const shouldFetchDetail = !!row.body_payload;
191+
if (shouldFetchDetail) {
192192
searchParams.set("hasBodyPayload", "1");
193+
} else {
194+
searchParams.delete("hasBodyPayload");
193195
}
196+
194197
setSearchParam(searchParams);
195198
}}
196199
manualPageCount={pageCount}

src/components/Notifications/NotificationSendHistory/NotificationDetails.tsx

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,37 @@ export default function NotificationDetails({
5454
const bodyMarkdown =
5555
"body_markdown" in notification ? notification.body_markdown : undefined;
5656

57-
// Legacy notifications stored the rendered body directly in the summary view.
57+
// Legacy notifications stored Slack BlockKit JSON directly in the summary body field.
5858
const legacyBody = notification.body;
59-
const isLegacySlackMessage =
60-
!bodyMarkdown &&
61-
(legacyBody?.startsWith("[{") || legacyBody?.startsWith('{"blocks"'));
6259

63-
let slackBody: string | undefined;
64-
if (isLegacySlackMessage && legacyBody) {
60+
const legacySlackMessage = useMemo<SlackMessage | undefined>(() => {
61+
if (bodyMarkdown || !legacyBody) {
62+
return undefined;
63+
}
64+
6565
try {
66-
const parsed = legacyBody.startsWith("[{")
67-
? (JSON.parse(legacyBody) as SlackMessage[])[0]
68-
: (JSON.parse(legacyBody) as SlackMessage);
69-
slackBody = blockKitToMarkdown(parsed);
70-
} catch (error) {
71-
slackBody = legacyBody;
66+
const parsed = JSON.parse(legacyBody.trim()) as
67+
| SlackMessage
68+
| SlackMessage[];
69+
70+
if (Array.isArray(parsed)) {
71+
const [firstMessage] = parsed;
72+
return Array.isArray(firstMessage?.blocks) ? firstMessage : undefined;
73+
}
74+
75+
return Array.isArray(parsed?.blocks) ? parsed : undefined;
76+
} catch {
77+
return undefined;
7278
}
73-
}
79+
}, [bodyMarkdown, legacyBody]);
80+
81+
const slackBody = useMemo(() => {
82+
if (!legacySlackMessage) {
83+
return undefined;
84+
}
85+
86+
return blockKitToMarkdown(legacySlackMessage);
87+
}, [legacySlackMessage]);
7488

7589
const { data: silencer } = useQuery({
7690
queryKey: ["notification_silence", notification.silenced_by],
@@ -169,15 +183,15 @@ export default function NotificationDetails({
169183
)}
170184
</div>
171185

172-
{(bodyMarkdown || legacyBody) && (
186+
{(bodyMarkdown || slackBody || legacyBody) && (
173187
<div className="flex flex-col gap-2">
174188
<label className="truncate text-sm text-gray-500">Body:</label>
175189
{bodyMarkdown ? (
176190
<DisplayMarkdown
177191
md={bodyMarkdown}
178192
className="whitespace-pre-wrap break-all rounded bg-black p-4 text-white"
179193
/>
180-
) : isLegacySlackMessage ? (
194+
) : slackBody ? (
181195
<DisplayMarkdown
182196
md={slackBody}
183197
className="whitespace-pre-wrap break-all rounded bg-black p-4 text-white"

0 commit comments

Comments
 (0)