refactor: Switch to typescript predicates

This commit is contained in:
Xyphyn committed 2023-07-17 13:00:40 -07:00
1 parent 108b744746
commit adc3df0d8f
3 files changed
+43 -67

No files matched your search

+19 -23
View File
@@ -23,19 +23,17 @@
function isPrivateMessage( function isPrivateMessage(
item: CommentReplyView | PersonMentionView | PrivateMessageView item: CommentReplyView | PersonMentionView | PrivateMessageView
) { ): item is PrivateMessageView {
return 'private_message' in item 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 replying = false
let reply = '' let reply = ''
let loading = false let loading = false
async function replyToMessage() { async function replyToMessage(
message: PrivateMessageView | CommentReplyView | PersonMentionView
) {
if (!$authData) return if (!$authData) return
loading = true loading = true
@@ -44,7 +42,7 @@
await getClient().createPrivateMessage({ await getClient().createPrivateMessage({
auth: $authData.token, auth: $authData.token,
content: reply, content: reply,
recipient_id: messageItem.creator.id, recipient_id: message.creator.id,
}) })
addToast( addToast(
@@ -75,28 +73,26 @@
overflow-hidden {read ? 'opacity-50' : ''}" overflow-hidden {read ? 'opacity-50' : ''}"
> >
<span class="font-bold"> <span class="font-bold">
{replyItem.creator.display_name ?? replyItem.creator.name} {item.creator.display_name ?? item.creator.name}
</span> </span>
replied to you in replied to you in
<a <a
href="/post/{replyItem.post.id}" href="/post/{item.post.id}"
class="font-bold max-w-[48ch] overflow-hidden text-ellipsis class="font-bold max-w-[48ch] overflow-hidden text-ellipsis
whitespace-nowrap inline hover:underline" whitespace-nowrap inline hover:underline"
> >
{replyItem.post.name} {item.post.name}
</a> </a>
</div> </div>
<div class="w-max ml-auto"> <div class="w-max ml-auto">
<Link href="/post/{replyItem.post.id}#{replyItem.comment.id}"> <Link href="/post/{item.post.id}#{item.comment.id}">Jump</Link>
Jump
</Link>
</div> </div>
</div> </div>
<div> <div>
<Comment <Comment
postId={replyItem.post.id} postId={item.post.id}
node={{ children: [], comment_view: replyItem, depth: 1 }} node={{ children: [], comment_view: item, depth: 1 }}
replying={false} replying={false}
/> />
</div> </div>
@@ -107,26 +103,26 @@
overflow-hidden flex flex-row items-center gap-1 {read ? 'opacity-80' : ''}" overflow-hidden flex flex-row items-center gap-1 {read ? 'opacity-80' : ''}"
> >
<span class="font-bold"> <span class="font-bold">
{#if messageItem.creator.id == $user?.person.id} {#if item.creator.id == $user?.person.id}
You You
{:else} {:else}
<UserLink avatar user={messageItem.creator} /> <UserLink avatar user={item.creator} />
{/if} {/if}
</span> </span>
<span>messaged</span> <span>messaged</span>
<span class="font-bold"> <span class="font-bold">
{#if messageItem.recipient.id == $user?.person.id} {#if item.recipient.id == $user?.person.id}
You You
{:else} {:else}
<UserLink avatar user={messageItem.recipient} /> <UserLink avatar user={item.recipient} />
{/if} {/if}
</span> </span>
</div> </div>
</div> </div>
<p class="text-sm py-2"> <p class="text-sm py-2">
<Markdown source={messageItem.private_message.content} /> <Markdown source={item.private_message.content} />
</p> </p>
{#if messageItem.recipient.id == $user?.person.id} {#if item.recipient.id == $user?.person.id}
<div class="flex flex-row gap-2"> <div class="flex flex-row gap-2">
<Button color={Color.ghost} on:click={() => (replying = !replying)}> <Button color={Color.ghost} on:click={() => (replying = !replying)}>
<Icon mini src={ChatBubbleOvalLeft} width={16} /> <Icon mini src={ChatBubbleOvalLeft} width={16} />
@@ -134,7 +130,7 @@
</Button> </Button>
</div> </div>
{/if} {/if}
{#if replying} {#if isPrivateMessage(item)}
<div class="mt-2 flex flex-col gap-2"> <div class="mt-2 flex flex-col gap-2">
<TextArea placeholder="Message" bind:value={reply} rows={3} /> <TextArea placeholder="Message" bind:value={reply} rows={3} />
<div class="ml-auto w-24"> <div class="ml-auto w-24">
@@ -142,7 +138,7 @@
disabled={loading} disabled={loading}
{loading} {loading}
large large
on:click={replyToMessage} on:click={() => replyToMessage(item)}
color={Color.accent} color={Color.accent}
> >
Submit Submit
+5 -10
View File
@@ -10,11 +10,8 @@
export let data export let data
const isComment = (item: CommentView | PostView) => 'comment' in item const isComment = (item: CommentView | PostView): item is CommentView =>
'comment' in item
// can't do type conversions in the template, so i have to do this
const asPost = (item: any) => item as PostView
const asComment = (item: any) => item as CommentView
</script> </script>
<svelte:head> <svelte:head>
@@ -41,20 +38,18 @@
{item.post.name} {item.post.name}
</span> </span>
</div> </div>
<Link href="/post/{item.post.id}#{asComment(item).comment.id}"> <Link href="/post/{item.post.id}#{item.comment.id}">Jump</Link>
Jump
</Link>
</div> </div>
<div class="list-none"> <div class="list-none">
<Comment <Comment
postId={item.post.id} postId={item.post.id}
node={{ children: [], comment_view: asComment(item), depth: 1 }} node={{ children: [], comment_view: item, depth: 1 }}
replying={false} replying={false}
/> />
</div> </div>
</Card> </Card>
{:else} {:else}
<Post post={asPost(item)} /> <Post post={item} />
{/if} {/if}
</div> </div>
{/each} {/each}
+19 -34
View File
@@ -25,34 +25,20 @@
user: GetPersonDetailsResponse user: GetPersonDetailsResponse
} }
interface Item { const isComment = (item: CommentView | PostView): item is CommentView =>
post: boolean 'comment' in item
comment: boolean
item: CommentView & PostView
}
function getDateOfItem(item: Item): number { function getDateOfItem(item: CommentView | PostView): number {
if (item.post) { if (isComment(item)) {
return Date.parse(item.item.post.published) return Date.parse(item.comment.published)
} else if (item.comment) {
return Date.parse(item.item.comment.published)
} else { } else {
return Date.now() return Date.parse(item.post.published)
} }
} }
const items: Item[] = [...data.user.comments, ...data.user.posts] const items = [...data.user.comments, ...data.user.posts].sort(
.map((i) => { (a, b) => getDateOfItem(b) - getDateOfItem(a)
const post = 'read' in i )
const comment = !post
return {
post: post,
comment: comment,
item: i as CommentView & PostView,
}
})
.sort((a, b) => getDateOfItem(b) - getDateOfItem(a))
</script> </script>
<svelte:head> <svelte:head>
@@ -62,31 +48,30 @@
<div class="flex flex-row gap-4 max-w-full w-full"> <div class="flex flex-row gap-4 max-w-full w-full">
<div class="flex flex-col gap-4 max-w-full w-full"> <div class="flex flex-col gap-4 max-w-full w-full">
{#each items as item} {#each items as item}
{#if item.post} {#if isComment(item)}<Card
<Post post={item.item} /> class="flex flex-col bg-white rounded-md p-4 flex-1"
{:else} >
<Card class="flex flex-col bg-white rounded-md p-4 flex-1">
<div class="flex flex-row justify-between items-center"> <div class="flex flex-row justify-between items-center">
<div class="flex flex-col gap-1"> <div class="flex flex-col gap-1">
<span class="text-xs dark:text-slate-400 text-zinc-600"> <span class="text-xs dark:text-slate-400 text-zinc-600">
<CommunityLink avatar community={item.item.community} /> <CommunityLink avatar community={item.community} />
</span> </span>
<span class="flex flex-row items-center text-sm font-bold"> <span class="flex flex-row items-center text-sm font-bold">
{item.item.post.name} {item.post.name}
</span> </span>
</div> </div>
<Link href="/post/{item.item.post.id}#{item.item.comment.id}"> <Link href="/post/{item.post.id}#{item.comment.id}">Jump</Link>
Jump
</Link>
</div> </div>
<div class="list-none"> <div class="list-none">
<Comment <Comment
postId={item.item.post.id} postId={item.post.id}
node={{ children: [], comment_view: item.item, depth: 1 }} node={{ children: [], comment_view: item, depth: 1 }}
replying={false} replying={false}
/> />
</div> </div>
</Card> </Card>
{:else}
<Post post={item} />
{/if} {/if}
{/each} {/each}
</div> </div>