Merge pull request #624 from Xyphyn/optimizations

perf: improve efficiency and performance
This commit is contained in:
Xylight authored and GitHub committed 2025-08-01 21:21:52 +00:00
commit 7e188f218a
32 files changed
+248 -470

No files matched your search

@@ -1,4 +1,5 @@
import type { Comment, CommentView } from 'lemmy-js-client'
import { SvelteMap } from 'svelte/reactivity'
export interface CommentNodeI {
comment_view: CommentView
@@ -28,7 +29,7 @@ export function buildCommentsTree(
baseDepth: number = 0,
filter: (c: CommentView) => boolean = () => true,
): CommentNodeI[] {
const map = new Map<number, CommentNodeI>()
const map = new SvelteMap<number, CommentNodeI>()
let min_depth = Number.MAX_VALUE
for (const comment_view of comments) {
@@ -2,9 +2,11 @@
import { profile } from '$lib/auth.svelte.js'
import { t } from '$lib/i18n/translations'
import { getClient } from '$lib/lemmy.svelte'
import { errorMessage } from '$lib/lemmy/error'
import { isCommentView, isPostView } from '$lib/lemmy/item.js'
import type { CommentView, PostView } from 'lemmy-js-client'
import { Button, Menu, MenuButton, MenuDivider, toast } from 'mono-svelte'
import { Menu, MenuButton, MenuDivider, toast } from 'mono-svelte'
import type { Snippet } from 'svelte'
import {
ArrowsUpDown,
Fire,
@@ -17,23 +19,19 @@
Trash,
} from 'svelte-hero-icons'
import { amMod, ban, isAdmin, remove, viewVotes } from './moderation'
import ShieldIcon from './ShieldIcon.svelte'
import { errorMessage } from '$lib/lemmy/error'
interface Props {
item: PostView | CommentView
target: Snippet<[boolean]>
}
let { item = $bindable(), ...rest }: Props = $props()
let { item = $bindable(), target: passedTarget }: Props = $props()
let locking = $state(false)
let pinning = $state(false)
let acting = $derived(locking || pinning)
let acting = $state(false)
async function lock(lock: boolean) {
if (!profile.current?.jwt || !isPostView(item)) return
locking = true
acting = true
try {
await getClient().lockPost({
@@ -49,13 +47,13 @@
})
}
locking = false
acting = false
}
async function pin(pinned: boolean, toInstance: boolean = false) {
if (!profile.current?.jwt || !isPostView(item)) return
pinning = true
acting = true
try {
await getClient().featurePost({
@@ -72,21 +70,13 @@
})
}
pinning = false
acting = false
}
</script>
<Menu placement="bottom-end">
{#snippet target()}
<Button
class="hover:text-green-500!"
size="square-md"
color="ghost"
loading={acting}
{...rest}
>
<ShieldIcon filled width={16} />
</Button>
{@render passedTarget(acting)}
{/snippet}
{#if (profile.current?.user && amMod(profile.current.user, item.community)) || (profile.current?.user && isAdmin(profile.current.user))}
<MenuDivider>
@@ -99,8 +89,7 @@
<MenuButton
color="warning-subtle"
onclick={() => lock(!item.post.locked)}
loading={locking}
disabled={locking}
loading={acting}
>
{#snippet prefix()}
<Icon src={item.post.locked ? LockOpen : LockClosed} size="16" mini />
@@ -112,8 +101,7 @@
color="success-subtle"
onclick={() =>
pin(isPostView(item) ? !item.post.featured_community : false)}
loading={pinning}
disabled={pinning}
loading={acting}
>
<Icon src={Megaphone} size="16" mini />
<div
+1 -20
View File
@@ -1,7 +1,6 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { profile } from '$lib/auth.svelte'
import { mediaType, postLink } from '$lib/components/lemmy/post/helpers.js'
import { mediaType } from '$lib/components/lemmy/post/helpers.js'
import PostMedia from '$lib/components/lemmy/post/media/PostMedia.svelte'
import PostMediaCompact from '$lib/components/lemmy/post/media/PostMediaCompact.svelte'
import PostActions from '$lib/components/lemmy/post/PostActions.svelte'
@@ -28,15 +27,6 @@
return rule
}
function onClick(e: Event) {
const event = e as Event
const parent = document.getElementById(post.post.id.toString())
if (event.target == parent) {
goto(postLink(post.post))
}
}
interface Props {
post: PostView
actions?: boolean
@@ -86,8 +76,6 @@
This is the sole component for displaying posts.
It adapts to all kinds of form factors for different contexts, such as feeds, full post view, and crosspost list.
-->
<!-- svelte-ignore a11y_no_static_element_interactions -->
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<div
class={[
'post relative max-w-full min-w-0 w-full cursor-pointer outline-hidden group',
@@ -97,13 +85,6 @@
clazz,
]}
id={post.post.id.toString()}
onclick={e => {
onClick(e)
}}
onkeydown={e => {
if (e.key == 'Enter') onClick(e)
}}
tabindex="0"
{style}
>
<PostMeta
@@ -6,7 +6,6 @@
isAdmin,
report,
} from '$lib/components/lemmy/moderation/moderation.js'
import ModerationMenu from '$lib/components/lemmy/moderation/ModerationMenu.svelte'
import TextProps from '$lib/components/ui/text/TextProps.svelte'
import { publishedToDate } from '$lib/components/util/date'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
@@ -46,6 +45,7 @@
Newspaper,
PencilSquare,
Share,
ShieldCheck,
Trash,
UserCircle,
XMark,
@@ -161,14 +161,21 @@
</Button>
{/if}
{#if profile.current?.user && (amMod(profile.current.user, post.community) || isAdmin(profile.current.user))}
<ModerationMenu
size="custom"
color="ghost"
rounding="pill"
class={buttonSquare}
bind:item={post}
community={post.community}
/>
{#await import('$lib/components/lemmy/moderation/ModerationMenu.svelte') then { default: ModerationMenu }}
<ModerationMenu bind:item={post}>
{#snippet target(acting)}
<Button
color="ghost"
rounding="pill"
loading={acting}
class={buttonSquare}
size="custom"
>
<Icon src={ShieldCheck} size="18" mini />
</Button>
{/snippet}
</ModerationMenu>
{/await}
{/if}
{#if profile.current?.jwt}
@@ -6,7 +6,7 @@
post: PostView
}
let { post } = $props()
let { post }: Props = $props()
</script>
<Post {post} class="py-0!" />
+83 -75
View File
@@ -1,4 +1,14 @@
<script lang="ts" module>
type BadgeType =
| 'nsfw'
| 'saved'
| 'featured'
| 'deleted'
| 'removed'
| 'locked'
| 'moderator'
| 'admin'
export interface Tag {
content: string
color?: string
@@ -56,11 +66,11 @@
import { settings, type View } from '$lib/settings.svelte'
import type { Community, Person, SubscribedType } from 'lemmy-js-client'
import { Badge, Popover } from 'mono-svelte'
import Material from 'mono-svelte/materials/Material.svelte'
import {
Bookmark,
ExclamationTriangle,
Icon,
LockClosed,
Megaphone,
PaperAirplane,
Pencil,
@@ -70,7 +80,64 @@
} from 'svelte-hero-icons'
import CommunityHeader from '../community/CommunityHeader.svelte'
import ShieldIcon from '../moderation/ShieldIcon.svelte'
import Material from 'mono-svelte/materials/Material.svelte'
const badgeToData: Map<
BadgeType,
{
icon: IconSource
color: 'red-subtle' | 'yellow-subtle' | 'green-subtle'
label: string
}
> = new Map([
[
'nsfw',
{
icon: ExclamationTriangle,
color: 'red-subtle',
label: t.get('post.badges.nsfw'),
},
],
[
'saved',
{
icon: Bookmark,
color: 'yellow-subtle',
label: t.get('post.badges.saved'),
},
],
[
'featured',
{
icon: Megaphone,
color: 'green-subtle',
label: t.get('post.badges.featured'),
},
],
[
'removed',
{
icon: Trash,
color: 'green-subtle',
label: t.get('post.badges.removed'),
},
],
[
'deleted',
{
icon: Trash,
color: 'red-subtle',
label: t.get('post.badges.deleted'),
},
],
[
'locked',
{
icon: Lock,
color: 'yellow-subtle',
label: t.get('post.badges.locked'),
},
],
])
interface Props {
community?: Community
@@ -83,17 +150,7 @@
read?: boolean
edited?: string
view?: View
// Badges
badges?: {
nsfw: boolean
saved: boolean
featured: boolean
deleted: boolean
removed: boolean
locked: boolean
moderator: boolean
admin: boolean
}
badges?: Record<BadgeType, boolean>
tags?: Tag[]
style?: string
titleClass?: string
@@ -196,10 +253,7 @@
/>
{/if}
<div
class="flex flex-row gap-1.5 items-center
no-list-margin {view == 'compact' && showCommunity
? 'min-[480px]:mx-2'
: ''}"
class="flex flex-row gap-1.5 items-center no-list-margin"
style="grid-area: stats;"
>
{#if user}
@@ -209,7 +263,7 @@
src={PaperAirplane}
size="12"
micro
class="rotate-180 text-slate-400 dark:text-zinc-600 max-[480px]:hidden"
class="rotate-180 text-slate-400 dark:text-zinc-600 max-[480px]:hidden ml-2"
/>
{/if}
<UserLink
@@ -269,66 +323,20 @@
</a>
{/each}
{/if}
{#if badges.nsfw}
<Badge label={$t('post.badges.nsfw')} color="red-subtle" allowIconOnly>
{#each Object.keys(badges)
// filter by ones that are true
.filter(i => badges[i as BadgeType] == true)
// get from predetermined map
.map(i => badgeToData.get(i as BadgeType))
// remove null
.filter(i => i != undefined) as badge}
<Badge label={badge.label} color={badge.color} allowIconOnly>
{#snippet icon()}
<Icon src={ExclamationTriangle} size="14" micro />
<Icon src={badge.icon} micro size="14" />
{/snippet}
{$t('post.badges.nsfw')}
{badge.label}
</Badge>
{/if}
{#if badges.saved}
<Badge
label={$t('post.badges.saved')}
color="yellow-subtle"
allowIconOnly
>
{#snippet icon()}
<Icon src={Bookmark} micro size="14" />
{/snippet}
{$t('post.badges.saved')}
</Badge>
{/if}
{#if badges.locked}
<Badge
label={$t('post.badges.locked')}
color="yellow-subtle"
allowIconOnly
>
{#snippet icon()}
<Icon src={LockClosed} micro size="14" />
{/snippet}
<span class="max-md:hidden">{$t('post.badges.locked')}</span>
</Badge>
{/if}
{#if badges.removed}
<Badge label={$t('post.badges.removed')} color="red-subtle" allowIconOnly>
{#snippet icon()}
<Icon src={Trash} micro size="14" />
{/snippet}
<span class="max-md:hidden">{$t('post.badges.removed')}</span>
</Badge>
{/if}
{#if badges.deleted}
<Badge label={$t('post.badges.deleted')} color="red-subtle" allowIconOnly>
{#snippet icon()}
<Icon src={Trash} micro size="14" />
{/snippet}
<span class="max-md:hidden">{$t('post.badges.deleted')}</span>
</Badge>
{/if}
{#if badges.featured}
<Badge
label={$t('post.badges.featured')}
color="green-subtle"
allowIconOnly
>
{#snippet icon()}
<Icon src={Megaphone} micro size="14" />
{/snippet}
<span class="max-md:hidden">{$t('post.badges.featured')}</span>
</Badge>
{/if}
{/each}
{@render extraBadges?.()}
</div>
</header>
@@ -50,7 +50,6 @@
let hasMore = $state(true)
async function loadMore() {
console.log(hasMore, loading)
if (!hasMore || loading) return
try {
@@ -183,10 +182,10 @@
{#snippet item(row)}
<li
data-index={row}
style={row < 7 ? `--anim-delay: ${row * 50}ms` : ''}
style={row < 9 ? `--anim-delay: ${row * 50}ms` : ''}
class={[
'relative post-container px-3 sm:px-6',
row < 7 && 'pop-in opacity-0',
row < 9 && 'pop-in opacity-0',
]}
>
<Post
@@ -219,12 +218,7 @@
<Icon src={ExclamationTriangle} size="24" solid></Icon>
</div>
<pre class="py-0.5">{error}</pre>
<Button
color="primary"
{loading}
disabled={loading}
onclick={() => loadMore()}
>
<Button color="primary" {loading} onclick={loadMore}>
{$t('message.retry')}
</Button>
</div>
@@ -256,14 +250,7 @@
</EndPlaceholder>
</div>
{/if}
<InfiniteScroll
window
threshold={600}
on:loadMore={() => {
console.log('loadMore triggered')
loadMore()
}}
/>
<InfiniteScroll window threshold={600} on:loadMore={loadMore} />
{/if}
{@render children?.()}
</ul>
@@ -282,6 +269,6 @@
.pop-in {
animation: popIn 0.5s cubic-bezier(0.165, 0.84, 0.44, 1) forwards
var(--anim-delay);
var(--anim-delay, 0ms);
}
</style>
@@ -1,5 +1,6 @@
import { settings } from '$lib/settings.svelte'
import type { PostView } from 'lemmy-js-client'
import { SvelteMap, SvelteSet } from 'svelte/reactivity'
export type PostViewWithCrossposts = PostView & {
withCrossposts: true
@@ -10,12 +11,12 @@ export type PostViewWithoutCrossposts = PostView & { withCrossposts?: false }
export const combineCrossposts = (
posts: PostView[],
): (PostViewWithCrossposts | PostViewWithoutCrossposts)[] => {
const urlMap = new Map<
const urlMap = new SvelteMap<
string,
PostViewWithCrossposts | PostViewWithoutCrossposts
>()
const results: (PostViewWithCrossposts | PostViewWithoutCrossposts)[] = []
const seenUrls = new Set<string>()
const seenUrls = new SvelteSet<string>()
posts?.forEach(post => {
if (
@@ -24,7 +24,7 @@
<svelte:element
this={settings.expandImages ? 'button' : 'a'}
href={postLink(post)}
class="container mx-auto z-10 rounded-xl max-h-[60vh] relative overflow-hidden bg-slate-100 dark:bg-zinc-900"
class="container mx-auto z-10 rounded-xl max-h-[60vh] relative overflow-hidden bg-slate-100 dark:bg-zinc-900 cursor-pointer"
data-sveltekit-preload-data="off"
aria-label={post.name}
onclick={() => showImage(bestImageURL(post, false, -1, null))}
+4 -3
View File
@@ -38,9 +38,10 @@
src={open ? Minus : Plus}
size="16"
micro
class="{open
? ''
: 'rotate-90'} transition-transform duration-300 ease-out"
class={[
open && 'rotate-180',
'transition-transform duration-300 ease-out',
]}
/>
{/if}
<div
+3 -2
View File
@@ -6,6 +6,7 @@
import { fly } from 'svelte/transition'
import { page as pageData } from '$app/state'
import { invalidate } from '$app/navigation'
import { SvelteURLSearchParams } from 'svelte/reactivity'
interface Props {
page?: number
@@ -27,8 +28,8 @@
let customHref = (href?: string) => {
if (href?.startsWith('?')) {
const current = new URLSearchParams(pageData.url.searchParams)
const newParams = new URLSearchParams(href)
const current = new SvelteURLSearchParams(pageData.url.searchParams)
const newParams = new SvelteURLSearchParams(href)
current.delete(Array.from(newParams.keys())[0])
current.append(
@@ -0,0 +1,48 @@
<script lang="ts">
let progress = $state(0)
let increaseInterval: number = -1
let startTimeout: number = -1
let stopTimeout: number = -1
export function start() {
clearTimeout(stopTimeout)
progress = 0
startTimeout = setTimeout(() => {
progress = 1
// schedule a setInterval and run the code immediately
;(function i() {
if (progress < 100 && progress > 0)
// slowly get closer and closer to 90% -- 100% doesnt look very good to be stuck on
progress += (100 - (progress - 10)) / 4
increaseInterval = setInterval(i, 400) as unknown as number
})()
}, 100) as unknown as number
}
export function stop() {
if (progress != 0) progress = 100
// reset progress to 0 after bar disappears
setTimeout(() => (progress = 0), 500)
clearInterval(increaseInterval)
clearTimeout(startTimeout)
}
</script>
<div
class={[
'fixed top-0 left-0 h-px z-[400] bg-primary-900 dark:bg-primary-100',
progress == 100 && 'opacity-0',
progress > 0 && 'use-transition',
]}
style="width: {progress}%;"
id="progress-bar"
></div>
<style>
#progress-bar.use-transition {
transition:
width 0.7s cubic-bezier(0, 0, 0.22, 0.76),
opacity 0.5s linear;
}
</style>
+1 -1
View File
@@ -97,7 +97,7 @@
}
}
@media (min-width: 1280px) {
@media (min-width: 80rem) {
.content {
grid-template-columns: 20% 60% 20%;
justify-items: end center start;
@@ -45,6 +45,7 @@ import {
ViewColumns,
type IconSource,
} from 'svelte-hero-icons'
import { SvelteURL } from 'svelte/reactivity'
export interface Group {
name: string
@@ -411,7 +412,7 @@ export function getGroups(
f.community.name,
f.community.actor_id,
)}`,
detail: new URL(f.community.actor_id).hostname,
detail: new SvelteURL(f.community.actor_id).hostname,
})) ?? [],
},
]
@@ -18,9 +18,7 @@
</script>
<SidebarButton
alignment="left"
loading={switching}
rounding="lg"
loaderWidth={22}
selected={profile.current?.id == prof.id}
onclick={async () => {
@@ -47,9 +45,10 @@
/>
{/snippet}
<span
class="inline-flex flex-col gap-0 {profile.current?.id == prof.id
? 'font-semibold'
: ''}"
class={[
'inline-flex flex-col gap-0',
profile.current?.id == prof.id && 'font-semibold',
]}
>
{prof.username ?? prof.user?.local_user_view.person.name}
{#if !guest && !LINKED_INSTANCE_URL}
+15 -21
View File
@@ -34,29 +34,25 @@
{#if profile.current?.jwt}
<SidebarButton icon={UserCircle} href="/profile">
{#snippet label()}
<span>
{$t('profile.profile')}
</span>
{$t('profile.profile')}
{/snippet}
</SidebarButton>
<SidebarButton icon={Inbox} href="/inbox">
{#snippet label()}
<span class="flex items-center gap-2">
{$t('profile.inbox')}
{#if $notifications.inbox}
<Badge
class="min-w-5 h-5 p-0! px-0.5 grid place-items-center ml-auto"
color="red-subtle"
>
{$notifications.inbox > 99 ? '∞' : $notifications.inbox}
</Badge>
{/if}
</span>
{$t('profile.inbox')}
{#if $notifications.inbox}
<Badge
class="min-w-5 h-5 p-0! px-0.5 grid place-items-center ml-auto"
color="red-subtle"
>
{$notifications.inbox > 99 ? '∞' : $notifications.inbox}
</Badge>
{/if}
{/snippet}
</SidebarButton>
<SidebarButton icon={Bookmark} href="/saved">
{#snippet label()}
<span>{$t('profile.saved')}</span>
{$t('profile.saved')}
{/snippet}
</SidebarButton>
{:else}
@@ -66,7 +62,7 @@
icon={ArrowLeftOnRectangle}
>
{#snippet label()}
<span>{$t('account.login')}</span>
{$t('account.login')}
{/snippet}
</SidebarButton>
<SidebarButton
@@ -75,7 +71,7 @@
icon={Identification}
>
{#snippet label()}
<span>{$t('account.signup')}</span>
{$t('account.signup')}
{/snippet}
</SidebarButton>
<SidebarButton
@@ -84,7 +80,7 @@
icon={Cog6Tooth}
>
{#snippet label()}
<span>{$t('nav.menu.settings')}</span>
{$t('nav.menu.settings')}
{/snippet}
</SidebarButton>
{/if}
@@ -109,9 +105,7 @@
{/snippet}
<div class="w-full flex gap-px flex-col">
{#each [...profile.meta.profiles] as prof, index}
<div class="w-full">
<ProfileButton {index} {prof} />
</div>
<ProfileButton {index} {prof} />
{/each}
</div>
<SidebarButton href="/accounts" icon={UserGroup}>