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