From adc3df0d8f314eb0ea1d3a247ea137510967c5f3 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Mon, 17 Jul 2023 13:00:40 -0700 Subject: [PATCH] refactor: Switch to typescript predicates --- src/routes/inbox/InboxItem.svelte | 42 +++++++++++------------- src/routes/saved/+page.svelte | 15 +++------ src/routes/u/[name]/+page.svelte | 53 +++++++++++-------------------- 3 files changed, 43 insertions(+), 67 deletions(-) diff --git a/src/routes/inbox/InboxItem.svelte b/src/routes/inbox/InboxItem.svelte index 6a470795..65c1698c 100644 --- a/src/routes/inbox/InboxItem.svelte +++ b/src/routes/inbox/InboxItem.svelte @@ -23,19 +23,17 @@ function isPrivateMessage( item: CommentReplyView | PersonMentionView | PrivateMessageView - ) { + ): item is PrivateMessageView { return 'private_message' in item } - // I hate how you can't type cast in Svelte easily - const replyItem = item as CommentReplyView | PersonMentionView - const messageItem = item as PrivateMessageView - let replying = false let reply = '' let loading = false - async function replyToMessage() { + async function replyToMessage( + message: PrivateMessageView | CommentReplyView | PersonMentionView + ) { if (!$authData) return loading = true @@ -44,7 +42,7 @@ await getClient().createPrivateMessage({ auth: $authData.token, content: reply, - recipient_id: messageItem.creator.id, + recipient_id: message.creator.id, }) addToast( @@ -75,28 +73,26 @@ overflow-hidden {read ? 'opacity-50' : ''}" > - {replyItem.creator.display_name ?? replyItem.creator.name} + {item.creator.display_name ?? item.creator.name} replied to you in - {replyItem.post.name} + {item.post.name}
- - Jump - + Jump
@@ -107,26 +103,26 @@ overflow-hidden flex flex-row items-center gap-1 {read ? 'opacity-80' : ''}" > - {#if messageItem.creator.id == $user?.person.id} + {#if item.creator.id == $user?.person.id} You {:else} - + {/if} messaged - {#if messageItem.recipient.id == $user?.person.id} + {#if item.recipient.id == $user?.person.id} You {:else} - + {/if}

- +

- {#if messageItem.recipient.id == $user?.person.id} + {#if item.recipient.id == $user?.person.id}
{/if} - {#if replying} + {#if isPrivateMessage(item)}