~james/core

core/web/src/lib/components/repo/LabelPanel.svelte -rw-r--r-- 7.4 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<script module lang="ts">
	import type { ResolvedPathname } from "$app/types";

	export interface Label {
		key: string;
		name: string;
		color: string;
		href?: ResolvedPathname;
	}

	const COLLAPSED_LABEL_COUNT = 10;
	const CREATE_PREFIX = "\0create:";
	const EDIT_VALUE = "\0edit";

	const labelColorFor = (name: string): string => {
		let hash = 0x811c9dc5;
		for (const char of name) hash = Math.imul(hash ^ char.codePointAt(0)!, 0x01000193);
		return `#${((hash >>> 0) & 0xffffff).toString(16).padStart(6, "0")}`;
	};
</script>

<script lang="ts">
	import type { Snippet } from "svelte";
	import { goto } from "$app/navigation";
	import { resolve } from "$app/paths";
	import Pencil from "$icon/pencil";
	import TagPlus from "$icon/tag-plus";
	import Plus from "$icon/plus";
	import Button from "#lib/components/ui/Button.svelte";
	import ColorBall from "#lib/components/ui/ColorBall.svelte";
	import Link from "#lib/components/ui/Link.svelte";
	import Combobox from "#lib/components/ui/Combobox.svelte";
	import Dropdown from "#lib/components/ui/Dropdown.svelte";
	import DropdownItem from "#lib/components/ui/DropdownItem.svelte";
	import LabelSummary, { labelChip } from "#lib/components/repo/LabelSummary.svelte";
	import type { SelectOption } from "#lib/components/ui/selectField.ts";
	import { plural } from "#lib/format.ts";

	interface Props {
		labels?: Label[];
		canEdit?: boolean;
		allLabels?: Label[];
		onLabelsChange?: (keys: string[]) => void;
		onCreateLabel?: (name: string, color: string) => void;
		settingsHref?: string;
		summarize?: boolean;
	}

	let {
		labels = [],
		canEdit = false,
		allLabels = [],
		onLabelsChange,
		onCreateLabel,
		settingsHref,
		summarize = false
	}: Props = $props();

	let expanded = $state(false);

	const pickedLabels = $derived(labels.map((label) => label.key));

	const hiddenCount = $derived(Math.max(0, labels.length - COLLAPSED_LABEL_COUNT));

	const shownLabels = $derived(
		expanded || hiddenCount === 0 ? labels : labels.slice(0, COLLAPSED_LABEL_COUNT)
	);

	let search = $state("");

	const query = $derived(search.toLowerCase());

	const matchingLabels = $derived(
		allLabels.filter((label) => label.name.toLowerCase().includes(query))
	);

	const canCreate = $derived(
		!!onCreateLabel &&
			search !== "" &&
			!allLabels.some((label) => label.name.toLowerCase() === query)
	);

	const createColor = $derived(labelColorFor(search));

	const labelOptions = $derived<SelectOption[]>([
		...matchingLabels.map((label) => ({ value: label.key, label: label.name })),
		...(canCreate ? [{ value: `${CREATE_PREFIX}${search}`, label: search, icon: Plus }] : []),
		...(settingsHref
			? [
					{
						value: EDIT_VALUE,
						label: "Edit labels",
						icon: Pencil,
						separator: !canCreate && matchingLabels.length > 0
					}
				]
			: [])
	]);

	const labelColors = $derived(new Map(allLabels.map((label) => [label.key, label.color])));

	const baseId = $props.id();
	const labelsHeadingId = `${baseId}-labels`;
</script>

{#snippet sectionHeading(id: string, title: string)}
	<h3 {id} class="pr-xs typography-paragraph-small font-medium text-foreground-muted">
		{title}
	</h3>
{/snippet}

{#snippet labelBall(label: Label)}
	<ColorBall color={label.color} size="small" />
	<span class="truncate typography-paragraph-regular">{label.name}</span>
{/snippet}

{#snippet editLabels(face?: Snippet)}
	<Combobox
		multiple
		checkboxes
		freezePosition
		align="left"
		options={labelOptions}
		values={pickedLabels}
		onSearch={(value) => (search = value)}
		onToggle={(key) => {
			if (key === EDIT_VALUE) {
				if (settingsHref) void goto(resolve(settingsHref as "/"));
				return;
			}
			if (key.startsWith(CREATE_PREFIX)) {
				onCreateLabel?.(key.slice(CREATE_PREFIX.length), createColor);
				return;
			}
			onLabelsChange?.(
				pickedLabels.includes(key)
					? pickedLabels.filter((picked) => picked !== key)
					: [...pickedLabels, key]
			);
		}}
		label="Labels"
		placeholder="Select labels"
		searchPlaceholder="Filter labels…"
		emptyLabel="No labels"
		class="w-auto"
	>
		{#snippet trigger({ props })}
			{#if face}
				<button
					{...props}
					data-testid="editLabels"
					type="button"
					class="group/labels flex max-w-full min-w-0 cursor-pointer rounded-md text-left text-foreground-default focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-border-focus"
					aria-label={`Edit labels: ${labels.map((label) => label.name).join(", ")}`}
				>
					{@render face()}
				</button>
			{:else if labels.length === 0}
				<Button
					{...props}
					data-testid="editLabels"
					variant="ghost"
					size="small"
					icon={TagPlus}>Add label</Button
				>
			{:else}
				<Button
					{...props}
					data-testid="editLabels"
					variant="ghost"
					size="small"
					icon={TagPlus}
					aria-label="Edit labels"
				/>
			{/if}
		{/snippet}
		{#snippet option(choice)}
			{#if choice.value.startsWith(CREATE_PREFIX)}
				<span class="shrink-0">Create</span>
				<ColorBall color={createColor} size="small" />
				<span class="min-w-0 flex-1 truncate">"{choice.label}"</span>
			{:else if choice.value === EDIT_VALUE}
				<span class="min-w-0 flex-1 truncate">{choice.label}</span>
			{:else}
				<ColorBall color={labelColors.get(choice.value) ?? ""} size="small" />
				<span class="min-w-0 flex-1 truncate">{choice.label ?? choice.value}</span>
			{/if}
		{/snippet}
	</Combobox>
{/snippet}

{#snippet chip(label: Label)}
	{#if label.href}
		<Link href={label.href} variant="inherit" class={labelChip({ link: true })}>
			{@render labelBall(label)}
		</Link>
	{:else}
		<span class={labelChip()}>{@render labelBall(label)}</span>
	{/if}
{/snippet}

{#if summarize}
	<div class="@container flex min-w-0 flex-1 items-center">
		{#if canEdit && labels.length > 0}
			<LabelSummary {labels} addable>
				{#snippet trigger(face)}
					{@render editLabels(face)}
				{/snippet}
			</LabelSummary>
		{:else if canEdit}
			{@render editLabels()}
		{:else if labels.length > 0}
			<LabelSummary {labels}>
				{#snippet trigger(face)}
					<span data-fit="dots" class="flex min-w-0">
						<Dropdown
							label="Labels"
							class="max-w-full min-w-0"
							triggerClass="group/labels max-w-full min-w-0 text-foreground-default"
						>
							{#snippet trigger()}
								{@render face()}
							{/snippet}
							{#each labels as label (label.key)}
								<DropdownItem href={label.href}>
									<ColorBall color={label.color} />
									<span class="min-w-0 truncate">{label.name}</span>
								</DropdownItem>
							{/each}
						</Dropdown>
					</span>
				{/snippet}
			</LabelSummary>
		{/if}
	</div>
{:else}
	<div class="flex w-full flex-col items-start gap-md">
		<section class="flex w-full flex-col items-start gap-xs">
			{@render sectionHeading(labelsHeadingId, "Labels")}
			<ul aria-labelledby={labelsHeadingId} class="flex w-full flex-wrap items-center gap-xs">
				{#each shownLabels as label (label.key)}
					<li class="flex max-w-full">{@render chip(label)}</li>
				{:else}
					{#if !canEdit}
						<li class="flex py-0.75 typography-paragraph-small text-foreground-muted">
							No labels
						</li>
					{/if}
				{/each}
				{#if hiddenCount > 0 && !expanded}
					<li class="flex">
						<Button
							variant="ghost"
							size="small"
							onclick={() => (expanded = true)}
							icon={TagPlus}
						>
							Display all {labels.length}
							{plural(labels.length, "label", "labels")}
						</Button>
					</li>
				{/if}
				{#if canEdit}
					<li class="flex">{@render editLabels()}</li>
				{/if}
			</ul>
		</section>
	</div>
{/if}