feat: add sorts to user pages

This commit is contained in:
Xyphyn committed 2023-07-27 12:25:26 -07:00
1 parent d427d4b2fc
commit c0aaf08a9f
12 files changed
+129 -69

No files matched your search

@@ -79,7 +79,7 @@
</Button>
<span class="text-xs opacity-80 py-1 my-1 px-4">Comment actions</span>
{#if $authData?.token}
{#if comment.creator.id == $user?.person.id}
{#if comment.creator.id == $user?.local_user_view.person.id}
<MenuButton on:click={() => dispatcher('edit', comment)}>
<Icon src={PencilSquare} mini size="16" />
<span>Edit</span>
@@ -89,7 +89,7 @@
<Icon src={comment.saved ? BookmarkSlash : Bookmark} mini size="16" />
<span>{comment.saved ? 'Unsave' : 'Save'}</span>
</MenuButton>
{#if $user && $authData && isCommentMutable(comment, $user)}
{#if $user && $authData && isCommentMutable(comment, $user.local_user_view)}
<MenuButton
color={Color.dangerSecondary}
on:click={async () => {
@@ -116,7 +116,7 @@
<span>Delete</span>
</MenuButton>
{/if}
{#if $authData && $user?.person.id != comment.creator.id}
{#if $authData && $user?.local_user_view.person.id != comment.creator.id}
<MenuButton
on:click={() => report(comment)}
color={Color.dangerSecondary}
@@ -126,7 +126,7 @@
</MenuButton>
<hr class="w-[90%] mx-auto opacity-100 dark:opacity-10 my-2" />
<li class="mx-4 text-xs opacity-80 text-left my-1 py-1">Actions</li>
{#if $user && $user.person.id == post.creator.id}
{#if $user && $user.local_user_view.person.id == post.creator.id}
<MenuButton on:click={() => (editing = true)}>
<Icon src={PencilSquare} width={16} mini />
Edit
@@ -147,13 +147,13 @@
<Icon src={post.saved ? BookmarkSlash : Bookmark} width={16} mini />
{post.saved ? 'Unsave' : 'Save'}
</MenuButton>
{#if $user && isMutable(post, $user)}
{#if $user && isMutable(post, $user.local_user_view)}
<MenuButton on:click={deletePost} color={Color.dangerSecondary}>
<Icon src={Trash} width={16} mini />
Delete
</MenuButton>
{/if}
{#if $user?.person.id != post.creator.id}
{#if $user?.local_user_view.person.id != post.creator.id}
<MenuButton on:click={() => report(post)} color={Color.dangerSecondary}>
<Icon src={Flag} width={16} mini />
Report
+6 -3
View File
@@ -100,7 +100,7 @@
{/if}
{#if $user}
<img
src={$user.person.avatar ??
src={$user.local_user_view.person.avatar ??
'https://xylo.xylight.dev/img/logo-background.svg'}
alt=""
width={32}
@@ -113,10 +113,13 @@
{/if}
</button>
<li class="text-xs opacity-80 text-left mx-4 my-1 py-1">
{$user ? $user.person.name : 'Profile'}
{$user ? $user.local_user_view.person.name : 'Profile'}
</li>
{#if $user && $authData}
<MenuButton link href="/u/{$user.person.name}@{$authData.instance}">
<MenuButton
link
href="/u/{$user.local_user_view.person.name}@{$authData.instance}"
>
<Icon src={UserCircle} mini width={16} /> Profile
</MenuButton>
<MenuButton link href="/inbox">
+3 -3
View File
@@ -1,4 +1,4 @@
import { LemmyHttp, type PersonView } from 'lemmy-js-client'
import { LemmyHttp, type MyUserInfo, type PersonView } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { userSettings } from '$lib/settings.js'
@@ -42,7 +42,7 @@ export interface UserData {
}
export const authData = writable<AuthData | undefined>()
export const user = writable<(PersonView & UserData) | undefined | null>(
export const user = writable<(MyUserInfo & UserData) | undefined | null>(
undefined
)
@@ -86,7 +86,7 @@ authData.subscribe(async (data) => {
user.set(null)
throw Error('Missing user')
}
user.set({ ...site.my_user.local_user_view, unreads: 0 })
user.set({ ...site.my_user, unreads: 0 })
if (typeof localStorage != 'undefined') {
localStorage.setItem('user', JSON.stringify(data))
}
+18
View File
@@ -20,3 +20,21 @@ export function getInboxItemPublished(item: InboxItemView): string {
return item.person_mention.published
}
export function isRead(
item: PersonMentionView | CommentReplyView | PrivateMessageView
) {
if ('person_mention' in item) {
return (item as PersonMentionView).person_mention.read
}
if ('comment_reply' in item) {
return (item as CommentReplyView).comment_reply.read
}
if ('private_message' in item) {
return (item as PrivateMessageView).private_message.read
}
return false
}
+26
View File
@@ -0,0 +1,26 @@
import type {
CommentView,
CommunityView,
PersonView,
PostView,
} from 'lemmy-js-client'
export function getItemPublished(
item: PostView | CommentView | PersonView | CommunityView
) {
if ('comment' in item) {
return item.comment.published
} else if ('post' in item) {
return item.post.published
}
if ('person' in item) {
return item.person.published
}
if ('community' in item) {
return item.community.published
}
return ''
}
+18
View File
@@ -0,0 +1,18 @@
import { authData, getClient } from '$lib/lemmy.js'
import type { MyUserInfo, PersonView } from 'lemmy-js-client'
import { get } from 'svelte/store'
export const blockUser = async (block: boolean, id: number) => {
const auth = get(authData)?.token
if (!auth) throw new Error('Unauthorized')
const response = await getClient().blockPerson({
auth: auth,
block: block,
person_id: id,
})
}
export const isBlocked = (me: MyUserInfo, user: number) =>
me.person_blocks.map((b) => b.target.id).includes(user)
+1 -18
View File
@@ -10,29 +10,12 @@
import { authData, getClient, user } from '$lib/lemmy.js'
import { goto } from '$app/navigation'
import { page } from '$app/stores'
import { isRead } from '$lib/lemmy/inbox.js'
export let data: {
data: PersonMentionView[] | CommentReplyView[] | PrivateMessageView[]
}
function isRead(
item: PersonMentionView | CommentReplyView | PrivateMessageView
) {
if ('person_mention' in item) {
return (item as PersonMentionView).person_mention.read
}
if ('comment_reply' in item) {
return (item as CommentReplyView).comment_reply.read
}
if ('private_message' in item) {
return (item as PrivateMessageView).private_message.read
}
return false
}
let markingAsRead = false
async function markAllAsRead() {
+3 -3
View File
@@ -101,7 +101,7 @@
overflow-hidden flex flex-row items-center gap-1 {read ? 'opacity-80' : ''}"
>
<span class="font-bold">
{#if item.creator.id == $user?.person.id}
{#if item.creator.id == $user?.local_user_view.person.id}
You
{:else}
<UserLink avatar user={item.creator} />
@@ -109,7 +109,7 @@
</span>
<span>messaged</span>
<span class="font-bold">
{#if item.recipient.id == $user?.person.id}
{#if item.recipient.id == $user?.local_user_view.person.id}
You
{:else}
<UserLink avatar user={item.recipient} />
@@ -120,7 +120,7 @@
<p class="text-sm py-2">
<Markdown source={item.private_message.content} />
</p>
{#if item.recipient.id == $user?.person.id}
{#if item.recipient.id == $user?.local_user_view.person.id}
<div class="flex flex-row gap-2">
<Button color="ghost" on:click={() => (replying = !replying)}>
<Icon mini src={ChatBubbleOvalLeft} width={16} />
+2 -22
View File
@@ -1,4 +1,5 @@
import { authData, getClient } from '$lib/lemmy.js'
import { getItemPublished } from '$lib/lemmy/item.js'
import type {
CommentView,
CommunityView,
@@ -10,26 +11,6 @@ import type {
} from 'lemmy-js-client'
import { get } from 'svelte/store'
function getSearchItemPublished(
item: PostView | CommentView | PersonView | CommunityView
) {
if ('comment' in item) {
return item.comment.published
} else if ('post' in item) {
return item.post.published
}
if ('person' in item) {
return item.person.published
}
if ('community' in item) {
return item.community.published
}
return ''
}
export async function load({ url }) {
const query = url.searchParams.get('q')
const page = Number(url.searchParams.get('page')) || 1
@@ -58,8 +39,7 @@ export async function load({ url }) {
const everything = [...posts, ...comments, ...users, ...communities].sort(
(a, b) =>
Date.parse(getSearchItemPublished(b)) -
Date.parse(getSearchItemPublished(a))
Date.parse(getItemPublished(b)) - Date.parse(getItemPublished(a))
)
return {
+24 -1
View File
@@ -17,6 +17,7 @@
Calendar,
ChatBubbleOvalLeftEllipsis,
Icon,
NoSymbol,
PencilSquare,
} from 'svelte-hero-icons'
import Button from '$lib/components/input/Button.svelte'
@@ -25,6 +26,9 @@
import { page } from '$app/stores'
import { goto } from '$app/navigation'
import { isComment } from '$lib/components/lemmy/post/helpers.js'
import { authData, user } from '$lib/lemmy.js'
import { isBlocked } from '$lib/lemmy/user.js'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
export let data
</script>
@@ -35,7 +39,18 @@
<div class="flex flex-col-reverse lg:flex-row gap-4 max-w-full w-full">
<div class="flex flex-col gap-4 max-w-full w-full">
{#each data.items as item}
<MultiSelect
options={['New', 'TopAll']}
optionNames={['New', 'Top']}
selected={data.sort}
on:select={(e) => {
$page.url.searchParams.set('sort', e.detail)
goto($page.url.toString(), {
invalidateAll: true,
})
}}
/>
{#each data.items as item (item.counts.id)}
{#if isComment(item) && (data.type == 'all' || data.type == 'comments')}
<CommentItem comment={item} />
{:else if !isComment(item) && (data.type == 'all' || data.type == 'posts')}
@@ -76,6 +91,14 @@
<FormattedNumber number={data.person_view.counts.comment_count} />
</span>
</div>
{#if $user && $authData && data.person_view.person.id != $user.local_user_view.person.id}
<div class="flex flex-col gap-2">
<Button size="lg" color="danger">
<Icon slot="icon" mini size="16" src={NoSymbol} />
{isBlocked($user, data.person_view.person.id) ? 'Unblock' : 'Block'}
</Button>
</div>
{/if}
<div>
<h1 class="font-bold text-lg">
{data.person_view.person.display_name ?? data.person_view.person.name}
+22 -13
View File
@@ -1,35 +1,44 @@
import { isComment } from '$lib/components/lemmy/post/helpers.js'
import { authData, getClient } from '$lib/lemmy.js'
import type { CommentView, PostView } from 'lemmy-js-client'
import { getItemPublished } from '$lib/lemmy/item.js'
import type { CommentView, PostView, SortType } from 'lemmy-js-client'
import { get } from 'svelte/store'
function getDateOfItem(item: CommentView | PostView): number {
if (isComment(item)) {
return Date.parse(item.comment.published)
} else {
return Date.parse(item.post.published)
}
}
export async function load({ params, url }) {
const page = Number(url.searchParams.get('page')) || 1
const type: 'comments' | 'posts' | 'all' =
(url.searchParams.get('type') as 'comments' | 'posts' | 'all') || 'all'
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'New'
const user = await getClient().getPersonDetails({
limit: 20,
page: page,
username: params.name,
sort: 'New',
sort: sort,
auth: get(authData)?.token,
})
const items = [...user.posts, ...user.comments]
if (sort == 'TopAll') {
items.sort(
(a, b) =>
b.counts.upvotes -
b.counts.downvotes -
(a.counts.upvotes - a.counts.downvotes)
)
} else if (sort == 'New') {
items.sort(
(a, b) =>
Date.parse(getItemPublished(b)) - Date.parse(getItemPublished(a))
)
}
return {
type: type,
page: page,
sort: sort,
person_view: user.person_view,
items: [...user.posts, ...user.comments].sort(
(a, b) => getDateOfItem(b) - getDateOfItem(a)
),
items,
}
}