Skip to content

Commit a2822b7

Browse files
fix relative days, dont use itemRefs
1 parent f87829b commit a2822b7

4 files changed

Lines changed: 46 additions & 47 deletions

File tree

src/lib/components/WebhookMessageList.svelte

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,39 @@
22
import { onMount } from 'svelte';
33
import type { WebhookMessage } from '../shared/types';
44
import WebhookMessageListItem from './WebhookMessageListItem.svelte';
5+
import { webhookMessages } from '$lib/client/webhookMessage.svelte';
6+
import { formatRelativeDays } from '$lib/shared/formatters';
7+
import { forwardWebhook } from '$lib/client/webhook-forwarder';
8+
import { ulid } from 'ulid';
59
6-
interface WebhookMessageWithRef extends WebhookMessage {
7-
ref?: WebhookMessageListItem;
8-
}
10+
let webhookMessagesWithDays = $derived(
11+
webhookMessages.state.map((msg) => {
12+
return {
13+
...msg,
14+
formattedRelativeDays: msg.timestamp ? formatRelativeDays(msg.timestamp) : undefined
15+
};
16+
})
17+
);
918
10-
interface Props {
11-
messages: WebhookMessageWithRef[];
12-
onReplay: (message: WebhookMessage) => void;
13-
}
19+
onMount(() => {
20+
scheduleNextMidnightUpdate();
21+
return () => {
22+
if (midnightTimeout) {
23+
clearTimeout(midnightTimeout);
24+
}
25+
};
26+
});
1427
15-
let { messages, onReplay }: Props = $props();
28+
function updateRelativeDays() {
29+
webhookMessagesWithDays = webhookMessages.state.map((msg) => {
30+
return {
31+
...msg,
32+
formattedRelativeDays: msg.timestamp ? formatRelativeDays(msg.timestamp) : undefined
33+
};
34+
});
35+
}
1636
1737
let midnightTimeout: ReturnType<typeof setTimeout>;
18-
1938
function getMsUntilMidnight() {
2039
const now = new Date();
2140
// eslint-disable-next-line svelte/prefer-svelte-reactivity
@@ -29,26 +48,25 @@
2948
function scheduleNextMidnightUpdate() {
3049
const msUntilMidnight = getMsUntilMidnight();
3150
midnightTimeout = setTimeout(() => {
32-
messages.forEach((msg) => {
33-
msg.ref?.dayChanged?.();
34-
});
51+
updateRelativeDays();
3552
scheduleNextMidnightUpdate();
3653
}, msUntilMidnight);
3754
}
3855
39-
onMount(() => {
40-
scheduleNextMidnightUpdate();
41-
return () => {
42-
if (midnightTimeout) {
43-
clearTimeout(midnightTimeout);
44-
}
45-
};
46-
});
56+
async function handleReplay(message: WebhookMessage) {
57+
const status = await forwardWebhook(message);
58+
webhookMessages.state.unshift({
59+
...message,
60+
id: ulid(),
61+
status,
62+
timestamp: Date.now()
63+
});
64+
}
4765
</script>
4866

4967
<ul>
50-
{#each messages as message (message.id)}
51-
<WebhookMessageListItem bind:this={message.ref} {message} {onReplay} />
68+
{#each webhookMessagesWithDays as message (message)}
69+
<WebhookMessageListItem {message} onReplay={handleReplay} />
5270
{/each}
5371
</ul>
5472

src/lib/components/WebhookMessageListItem.svelte

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
<script lang="ts">
22
import type { WebhookMessage } from '../shared/types';
3-
import { formatRelativeDays, formatTime } from '$lib/shared/formatters';
3+
import { formatTime } from '$lib/shared/formatters';
44
55
interface Props {
66
message: WebhookMessage;
77
onReplay: (message: WebhookMessage) => void;
88
}
99
1010
let { message, onReplay }: Props = $props();
11-
12-
let formattedRelativeDays = $state(
13-
message.timestamp ? formatRelativeDays(message.timestamp) : null
14-
);
15-
16-
export function dayChanged() {
17-
if (message.timestamp) {
18-
formattedRelativeDays = formatRelativeDays(message.timestamp);
19-
}
20-
}
2111
</script>
2212

2313
<li>
@@ -36,8 +26,8 @@
3626
{#if message.headers['content-type']}
3727
<span class="content-type">[{message.headers['content-type']}]</span>
3828
{/if}
39-
{#if message.timestamp}
40-
<span class="timestamp">{formattedRelativeDays} {formatTime(message.timestamp)}</span>
29+
{#if message.formattedRelativeDays && message.timestamp}
30+
<span class="timestamp">{message.formattedRelativeDays} {formatTime(message.timestamp)}</span>
4131
{/if}
4232
<button class="btn-secondary replay-btn" onclick={() => onReplay(message)}>↻ Replay</button>
4333
</li>

src/lib/shared/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface WebhookMessage {
2020
body: string | null;
2121
status?: number | null;
2222
timestamp?: number;
23+
formattedRelativeDays?:string;
2324
}
2425

2526
export type ArrayElement<ArrayType extends readonly unknown[]> =

src/routes/+page.svelte

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import { createEndpoint, removeEndpoint } from './webhooks.remote';
1111
import { browser, dev } from '$app/environment';
1212
import EndpointEditModal from '$lib/components/EndpointEditModal.svelte';
13-
import type { Endpoint, WebhookMessage } from '../lib/shared/types.js';
13+
import type { Endpoint } from '../lib/shared/types.js';
1414
import { ulid } from 'ulid';
1515
1616
interface Props {
@@ -57,16 +57,6 @@
5757
}
5858
}
5959
60-
async function handleReplay(message: WebhookMessage) {
61-
const status = await forwardWebhook(message);
62-
webhookMessages.state.unshift({
63-
...message,
64-
id: ulid(),
65-
status,
66-
timestamp: Date.now()
67-
});
68-
}
69-
7060
function handleGenerateRandomWebhook() {
7161
if (endpoints.length === 0) return;
7262
@@ -157,7 +147,7 @@
157147
? window.location.hostname
158148
: 'this domain'} (CORS).
159149
</p>
160-
<WebhookMessageList messages={webhookMessages.state} onReplay={handleReplay} />
150+
<WebhookMessageList />
161151
</section>
162152
</main>
163153

0 commit comments

Comments
 (0)