~james/core

core/web/src/lib/components/ui/User.svelte -rw-r--r-- 3.0 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<script module lang="ts">
	import { tv, type VariantProps } from "tailwind-variants";
	import type { AvatarSize } from "#lib/components/ui/Avatar.svelte";

	export const user = tv({
		slots: {
			root: "inline-flex min-w-0 items-center",
			handle: "min-w-0 truncate"
		},
		variants: {
			size: {
				header: {
					root: "gap-xs",
					handle: "typography-heading-4"
				},
				large: {
					root: "gap-1.5",
					handle: "typography-paragraph-large"
				},
				regular: {
					root: "gap-1.5",
					handle: "typography-paragraph-regular"
				},
				small: {
					root: "gap-1.5",
					handle: "typography-paragraph-small"
				},
				mini: {
					root: "gap-2xs",
					handle: "typography-paragraph-mini"
				}
			},
			link: {
				true: {
					root: "text-foreground-default hover:underline"
				}
			}
		},
		defaultVariants: {
			size: "regular",
			link: false
		}
	});

	export type UserVariants = VariantProps<typeof user>;

	const avatarSize = {
		header: "large",
		large: "large",
		regular: "regular",
		small: "small",
		mini: "mini"
	} satisfies Record<NonNullable<UserVariants["size"]>, AvatarSize>;
</script>

<script lang="ts">
	import { isDid, isHandle } from "@atcute/lexicons/syntax";
	import { profileIdentifier } from "#lib/api/identity.ts";
	import { resolve } from "$app/paths";
	import Avatar from "#lib/components/ui/Avatar.svelte";
	import VouchAvatar from "#lib/components/profile/VouchAvatar.svelte";

	interface Props {
		handle?: string;
		did?: string;
		avatarCid?: string | null;
		size?: UserVariants["size"];
		showImage?: boolean;
		showText?: boolean;
		class?: string;
		avatarClass?: string;
		handleClass?: string;
		link?: boolean;
		email?: string;
		vouchBadge?: boolean;
		loading?: "lazy" | "eager";
	}

	let {
		handle,
		did,
		avatarCid,
		size = "regular",
		showImage = true,
		showText = true,
		class: className,
		avatarClass,
		handleClass,
		link = false,
		email,
		vouchBadge = false,
		loading
	}: Props = $props();

	const identified = $derived(isDid(did) ? did : undefined);
	const actor = $derived(
		identified ? profileIdentifier(isHandle(handle) ? handle : undefined, identified) : handle
	);
	const linked = $derived(link && actor !== undefined);
	const slots = $derived(user({ size, link: linked }));
</script>

{#snippet body()}
	{#if showImage}
		{#if vouchBadge && did}
			<VouchAvatar
				{did}
				{avatarCid}
				{handle}
				size={avatarSize[size]}
				{loading}
				class={avatarClass}
			/>
		{:else}
			<Avatar
				{did}
				{avatarCid}
				{handle}
				size={avatarSize[size]}
				{loading}
				class={avatarClass}
			/>
		{/if}
	{/if}
	{#if showText && handle}
		<span class={slots.handle({ class: handleClass })} title={actor}>{actor}</span>
	{/if}
{/snippet}

{#if linked}
	{#if email}
		<a href="mailto:{email}" class={slots.root({ class: className })}>
			{@render body()}
		</a>
	{:else}
		<a href={resolve(`/${actor}` as "/")} class={slots.root({ class: className })}>
			{@render body()}
		</a>
	{/if}
{:else}
	<span class={slots.root({ class: className })}>
		{@render body()}
	</span>
{/if}