impr: restructure src/lib

This commit is contained in:
Xylight committed 2025-09-24 17:12:19 -07:00
1 parent d85b889409
commit 75f907743e
376 files changed
+1000 -1033

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
<script lang="ts">
import { profile } from '$lib/app/auth.svelte'
import { client } from '$lib/api/client.svelte'
import type { LocalImage, Person } from '$lib/api/types'
import UserLink from '$comp/lemmy/user/UserLink.svelte'
import { publishedToDate } from '$comp/util/date'
import RelativeDate from '$comp/util/RelativeDate.svelte'
import { t } from '$lib/app/i18n'
import { instance } from '$lib/app/instance.svelte'
import { instanceToURL } from '$lib/util.svelte'
import { action, Button, modal, toast } from 'mono-svelte'
import { ArrowDownTray, Trash } from 'svelte-hero-icons'
let loading = $state(false)
async function deleteImage(image: LocalImage) {
if (!profile.current?.jwt) return
try {
loading = true
const res = await client().deleteImage({
token: image.pictrs_delete_token,
filename: image.pictrs_alias,
})
ondelete?.(res)
} catch (e) {
toast({ content: e as string, type: 'error' })
}
loading = false
}
interface Props {
image: LocalImage
user?: Person
ondelete?: (res: boolean) => void
}
let { image, user, ondelete }: Props = $props()
</script>
<div class="flex flex-col gap-1">
<img
src="{instanceToURL(instance.data)}/pictrs/image/{image.pictrs_alias}"
width="500"
height="500"
class="aspect-square w-full h-full object-cover rounded-xl"
alt="pictrs"
/>
{#if user}
<UserLink {user} />
{/if}
<div class="flex items-center gap-2">
<RelativeDate
class="text-sm text-slate-700 dark:text-zinc-300"
date={publishedToDate(image.published)}
/>
<Button
title={$t('routes.profile.media.download')}
href="{instanceToURL(instance.data)}/pictrs/image/{image.pictrs_alias}"
size="square-md"
class="ml-auto"
icon={ArrowDownTray}
></Button>
<Button
title={$t('post.actions.more.delete')}
onclick={() => {
modal({
title: $t('routes.theme.preset.delete.confirm'),
body: '',
actions: [
action({
close: true,
content: $t('common.cancel'),
}),
action({
type: 'danger',
action: () => deleteImage(image),
close: true,
content: $t('post.actions.more.delete'),
}),
],
})
}}
size="square-md"
{loading}
disabled={loading}
icon={Trash}
></Button>
</div>
</div>
+64
View File
@@ -0,0 +1,64 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { page } from '$app/state'
import { profile, type ProfileInfo } from '$lib/app/auth.svelte'
import SidebarButton from '$comp/ui/sidebar/SidebarButton.svelte'
import { LINKED_INSTANCE_URL } from '$lib/app/instance.svelte'
import ProfileAvatar from '$lib/feature/legacy/ProfileAvatar.svelte'
import { Icon, QuestionMarkCircle } from 'svelte-hero-icons'
let switching: boolean = $state(false)
interface Props {
prof: ProfileInfo
index: number
guest?: boolean
}
let { prof, index, guest = false }: Props = $props()
</script>
<SidebarButton
alignment="left"
loading={switching}
rounding="lg"
loaderWidth={22}
selected={profile.current?.id == prof.id}
onclick={async () => {
switching = true
if (profile.current?.id != prof.id) {
profile.meta.profile = prof.id
}
await goto(page.url, {
invalidateAll: true,
})
switching = false
}}
class="w-full font-normal"
>
{#snippet customIcon()}
<ProfileAvatar
profile={prof}
{index}
{guest}
selected={profile.current?.id == prof.id}
/>
{/snippet}
<span
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}
<span class="text-slate-500 dark:text-zinc-400 font-normal text-xs">
{prof.instance}
</span>
{/if}
</span>
{#if !prof.jwt}
<Icon src={QuestionMarkCircle} size="14" micro class="ml-auto opacity-50" />
{/if}
</SidebarButton>
@@ -0,0 +1,107 @@
<script lang="ts">
import { Badge, Button, Menu, MenuButton } from 'mono-svelte'
import {
CheckCircle,
ChevronUpDown,
Icon,
QuestionMarkCircle,
UserGroup,
} from 'svelte-hero-icons'
import Avatar from '../Avatar.svelte'
import { profile, type ProfileInfo } from '$lib/app/auth.svelte'
import { goto } from '$app/navigation'
import { page } from '$app/state'
import { t } from '$lib/app/i18n'
import { DEFAULT_CLIENT_TYPE } from '$lib/api/base'
import { LINKED_INSTANCE_URL } from '$lib/app/instance.svelte'
let { profiles }: { profiles: ProfileInfo[] } = $props()
function switchTo(id: number) {
profile.meta.profile = id
goto(page.url, {
invalidateAll: true,
})
}
</script>
<Menu placement="bottom">
{#snippet target(attachment)}
<Button
{@attach attachment}
color="tertiary"
alignment="left"
size="md"
rounding="xl"
class="flex flex-row gap-2! items-center"
aria-label={$t('aria.sidebar.accounts')}
>
{#snippet prefix()}
<Avatar
url={profile.current.avatar}
alt={profile.current.username}
width={24}
/>
{/snippet}
<div class="flex-1">
<div class="font-medium">{profile.current.username}</div>
{#if !LINKED_INSTANCE_URL}
<div class="text-xs text-slate-500 dark:text-zinc-500">
<span class="capitalize">
{profile.current.client?.name ?? DEFAULT_CLIENT_TYPE.name}
</span>
• {profile.current.instance}
</div>
{/if}
</div>
{#snippet suffix()}
<Icon
src={ChevronUpDown}
size="16"
micro
class="block justify-self-end"
/>
{/snippet}
</Button>
{/snippet}
{#each profiles as p}
{@const selected = profile.meta.profile == p.id}
<MenuButton
onclick={() => switchTo(p.id)}
class={[selected && 'bg-slate-100! dark:bg-zinc-800!', 'gap-2!']}
>
{#snippet prefix()}
<Avatar url={p.avatar} alt={p.username} width={24} />
{/snippet}
<div>
<div class="font-medium text-sm">{p.username}</div>
{#if !LINKED_INSTANCE_URL}
<div class="text-xs text-slate-500 dark:text-zinc-500">
<span class="capitalize">
{p.client?.name ?? DEFAULT_CLIENT_TYPE.name}
</span>
• {p.instance}
</div>
{/if}
</div>
<div class="flex-1"></div>
{#if !p.jwt}
<Badge color="gray-subtle" class="p-1!">
<Icon src={QuestionMarkCircle} size="16" micro />
</Badge>
{/if}
{#if selected}
<Icon
src={CheckCircle}
class="text-primary-900 dark:text-primary-100"
size="16"
micro
/>
{/if}
</MenuButton>
{/each}
<MenuButton href="/accounts" icon={UserGroup}>
{$t('account.accounts')}
</MenuButton>
</Menu>
@@ -0,0 +1,82 @@
<script lang="ts">
import { profile } from '$lib/app/auth.svelte'
import Avatar from '$lib/ui/generic/Avatar.svelte'
import { getClient } from '$lib/api/client.svelte'
import type { ListingType, Person } from '$lib/api/types'
import { MenuButton, Search } from 'mono-svelte'
import { createEventDispatcher } from 'svelte'
import { Icon, XCircle } from 'svelte-hero-icons'
import { fly } from 'svelte/transition'
interface Props {
q?: string
instance?: string | undefined
listing_type?: ListingType
showWhenEmpty?: boolean
hideOwnUser?: boolean
placeholder?: string
onselect?: (e: Person) => void
}
let {
q = $bindable(''),
instance = undefined,
listing_type = 'Subscribed',
showWhenEmpty = false,
hideOwnUser = false,
...rest
}: Props = $props()
const dispatcher = createEventDispatcher<{ select: Person | undefined }>()
</script>
<Search
search={async (q) => {
const users = (
await getClient(instance).search({
q: q || ' ',
type_: 'Users',
limit: 20,
listing_type: listing_type,
sort: 'TopAll',
})
).users.map((c) => c.person)
if (hideOwnUser) {
const myself = profile.current.user?.local_user_view.person.id
return users.filter((c) => c.id != myself)
}
return users
}}
extractName={(c) => `${c.name}@${new URL(c.actor_id).hostname}`}
bind:query={q}
{...rest}
>
{#snippet noresults()}
<div class="w-full h-full">
{#if showWhenEmpty}
<MenuButton onclick={() => dispatcher('select', undefined)}>
<Icon src={XCircle} size="16" mini />
<div class="flex flex-col text-left">
<span>None</span>
</div>
</MenuButton>
{:else}
<span class="mx-auto my-auto">No results.</span>
{/if}
</div>
{/snippet}
{#snippet children({ item, select })}
<div in:fly|global={{ y: -4, opacity: 0 }}>
<MenuButton onclick={() => select(item)}>
<Avatar url={item.avatar} alt={item.name} width={24} />
<div class="flex flex-col text-left">
<span>{item.name}</span>
<span class="text-xs opacity-80">
{new URL(item.actor_id).hostname}
</span>
</div>
</MenuButton>
</div>
{/snippet}
</Search>
+69
View File
@@ -0,0 +1,69 @@
<script lang="ts">
import Avatar from '$lib/ui/generic/Avatar.svelte'
import LabelStat from '$lib/ui/info/LabelStat.svelte'
import { userLink } from '$lib/util.svelte'
import type { PersonView } from '$lib/api/types'
interface Props {
user: PersonView
view?: 'cozy' | 'compact'
showCounts?: boolean
class?: string
icon?: import('svelte').Snippet
}
let {
user,
view = 'compact',
showCounts = true,
class: clazz = 'flex flex-col gap-4 text-sm max-w-full relative',
icon,
}: Props = $props()
</script>
<div class={clazz}>
<div
class="flex
{view == 'cozy'
? 'flex-col gap-2'
: 'flex-row'} items-center max-w-full w-full"
>
<a href={userLink(user.person)} class="flex-1">
<div
class="flex {view == 'cozy'
? 'flex-col gap-2'
: 'flex-row'} gap-2 items-center"
>
{#if icon}{@render icon()}{:else}
<Avatar url={user.person.avatar} width={32} alt={user.person.name} />
{/if}
<div class="flex flex-col">
<div class="font-medium text-base">
{user.person.display_name ?? user.person.name}
</div>
<div class="text-sm text-slate-600 dark:text-zinc-400">
{new URL(user.person.actor_id).hostname}
</div>
</div>
</div>
</a>
</div>
{#if showCounts}
<div class="flex flex-row gap-3 items-center justify-center">
{#if user.counts.post_count}
<LabelStat
content={user.counts.post_count.toString()}
formatted
label="Posts"
/>
{/if}
{#if user.counts.comment_count}
<LabelStat
content={user.counts.comment_count.toString()}
formatted
label="Comments"
/>
{/if}
</div>
{/if}
</div>
+168
View File
@@ -0,0 +1,168 @@
<script lang="ts" module>
import { env } from '$env/dynamic/public'
import type { IconSource } from 'svelte-hero-icons'
function parseBadge() {
try {
if (env.PUBLIC_BADGES) {
return JSON.parse(env.PUBLIC_BADGES)
} else {
return {}
}
} catch {
return {}
}
}
const badges = parseBadge()
const getEnvBadge = (
actor_id: string,
):
| {
classes: string
icon: 'photon' | IconSource
iconClass?: string
}
| false => {
if (badges.photon && badges.photon?.includes?.(actor_id)) {
return {
classes:
'bg-linear-to-r bg-clip-text text-transparent from-pink-500 to-fuchsia-500 dark:from-pink-400 dark:to-purple-400',
icon: 'photon',
}
}
if (badges.translator && badges.translator?.includes?.(actor_id)) {
return {
classes:
'bg-linear-to-r bg-clip-text text-transparent from-sky-500 to-blue-700 dark:from-blue-300 dark:to-indigo-500',
icon: Language,
iconClass: 'text-blue-500 dark:text-blue-400',
}
}
return false
}
</script>
<script lang="ts">
import Avatar from '$lib/ui/generic/Avatar.svelte'
import Logo from '$lib/ui/generic/Logo.svelte'
import { settings } from '$lib/settings.svelte.js'
import type { Person } from '$lib/api/types'
import { Icon, Language, NoSymbol } from 'svelte-hero-icons'
import { Badge } from 'mono-svelte'
interface Props {
user: Person
avatar?: boolean
avatarSize?: number
badges?: boolean
inComment?: boolean
showInstance?: boolean
displayName?: boolean
instanceClass?: string
class?: string
children?: import('svelte').Snippet
extraBadges?: import('svelte').Snippet
}
let {
user,
avatar = false,
avatarSize = 24,
badges = true,
inComment = false,
showInstance = settings.showInstances.user ||
(settings.showInstances.comments && inComment),
displayName = settings.displayNames,
instanceClass = '',
class: clazz = '',
children,
extraBadges,
}: Props = $props()
let envBadge = $derived(getEnvBadge(user.actor_id))
</script>
<a
class="items-center inline-flex flex-row gap-1 hover:underline max-w-full min-w-0 {clazz}"
href="/u/{user.name}@{new URL(user.actor_id).hostname}"
data-sveltekit-preload-data="tap"
>
{@render children?.()}
{#if avatar}
<Avatar
url={user.avatar}
alt={user.name}
width={avatarSize}
class="shrink-0"
/>
{/if}
<span
class="flex gap-0 items-center shrink max-w-full min-w-0"
class:ml-0.5={avatar}
>
<span
class:font-medium={showInstance}
class="username-text {envBadge && envBadge.classes}"
>
{displayName ? user.display_name || user.name : user.name}
</span>
{#if showInstance}
<span
class="text-slate-500 dark:text-zinc-500 font-normal instance-text shrink {instanceClass ??
''}"
>
@{new URL(user.actor_id).hostname}
</span>
{/if}
</span>
{#if badges}
{#if user.banned}
<div class="text-red-500" title="Banned">
<Icon src={NoSymbol} mini size="12" />
</div>
{/if}
{#if user.bot_account}
<div class="text-blue-500 font-bold" title="Bot">BOT</div>
{/if}
{#if envBadge}
{#if envBadge.icon == 'photon'}
<Logo width={16} />
{:else}
<Icon
src={envBadge.icon}
micro
size="16"
class={envBadge.iconClass ?? envBadge.classes}
/>
{/if}
{/if}
{#if user.flair}
<Badge color="blue-subtle" class="px-1.5! py-0.5! whitespace-nowrap">
{user.flair}
</Badge>
{/if}
{@render extraBadges?.()}
{/if}
</a>
<style>
.instance-text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
min-width: 0;
max-width: 100%;
flex: 1;
}
.username-text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
+41
View File
@@ -0,0 +1,41 @@
import { profile, type ProfileInfo } from '$lib/app/auth.svelte'
import { client } from '$lib/api/client.svelte'
import type { Community, MyUserInfo } from '$lib/api/types'
// TODO obliterate this file
export async function blockUser(block: boolean, id: number) {
const auth = profile.current?.jwt
if (!auth) throw new Error('Unauthorized')
await client().blockPerson({
block: block,
person_id: id,
})
}
export function isBlocked(me: MyUserInfo, user: number) {
return me.person_blocks.map((b) => b.target.id).includes(user)
}
export function addSubscription(
community: Community,
subscribe: boolean = true,
) {
const index = profile.current.user?.follows
.map((f) => f.community.id)
.indexOf(community.id)
if (subscribe && index == -1) {
profile.current.user?.follows.push({
follower: profile.current.user.follows[0]?.follower,
community: community,
})
} else {
profile.current.user?.follows.splice(index ?? 0, 1)
}
}
export function hasFavorite(profile: ProfileInfo, id: number): boolean {
return profile.favorites?.map((i) => i.id).includes(id) ?? false
}