~james/core

core/web/src/lib/components/shell/Topbar.svelte -rw-r--r-- 13.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<script module lang="ts">
	import { tv, type VariantProps } from "tailwind-variants";

	export const topbar = tv({
		slots: {
			nav: "h-10 w-full px-lg",
			row: "flex h-full items-center justify-between gap-md",
			brand: "flex shrink-0 items-center gap-xs",
			logo: "flex shrink-0 items-center gap-xs no-underline hover:no-underline",
			wordmark: "hidden sm:inline-flex",
			mark: "inline-flex sm:hidden",
			actions: "flex items-center",
			action: "flex items-center",
			divider: "hidden h-6 w-px self-center rounded-md bg-background-inset md:block",
			link: "flex items-center text-foreground-default transition-colors hover:text-foreground-muted",
			identity: "flex items-center gap-2xs",
			ball: "flex size-8 items-center justify-center rounded-full border border-border-default bg-background-inset text-foreground-subtle md:size-5",
			bar: "hidden h-4 w-24 rounded-md bg-background-inset md:block",
			switchHeader:
				"border-b-0 px-xs py-1.5 typography-paragraph-small text-foreground-muted",
			addBall:
				"flex size-5.25 shrink-0 items-center justify-center rounded-full bg-background-muted text-foreground-default",
			sheet: "flex flex-col [&>hr]:my-1",
			panes: "-mx-md grid overflow-clip transition-[height] duration-250 ease-out-strong motion-reduce:transition-none",
			pane: "col-start-1 row-start-1 self-start px-md"
		},
		variants: {
			variant: {
				marketing: {
					nav: "mx-auto h-14 max-w-page rounded-2xl border border-border-default bg-background-default/80 px-sm shadow-small backdrop-blur-md",
					actions: "gap-xs"
				},
				app: {
					nav: "border-b border-border-default bg-background-default",
					actions: "gap-xs"
				}
			}
		}
	});

	export type TopbarVariants = VariantProps<typeof topbar>;
</script>

<script lang="ts">
	import { profileIdentifier } from "#lib/api/identity.ts";
	import { resolve } from "$app/paths";
	import type { ResolvedPathname } from "$app/types";
	import Button from "#lib/components/ui/Button.svelte";
	import Dropdown from "#lib/components/ui/Dropdown.svelte";
	import DropdownItem, { dropdownItem } from "#lib/components/ui/DropdownItem.svelte";
	import Modal from "#lib/components/ui/Modal.svelte";
	import { mobile } from "#lib/mobile.svelte.ts";
	import DropdownSubmenu from "#lib/components/ui/DropdownSubmenu.svelte";
	import DropdownGroup from "#lib/components/ui/DropdownGroup.svelte";
	import ArrowRight from "$icon/arrow-right";
	import BookMarked from "$icon/book-marked";
	import BookBookmark from "$icon/book-bookmark";
	import Building2 from "$icon/building-2";
	import Download from "$icon/download";
	import Cog from "$icon/cog";
	import Info from "$icon/info";
	import LineSquiggle from "$icon/line-squiggle";
	import LogOut from "$icon/log-out";
	import Plus from "$icon/plus";
	import Search from "$icon/search";
	import UserIcon from "$icon/user";
	import Spinner from "#lib/components/ui/Spinner.svelte";
	import Logo from "#lib/components/ui/Logo.svelte";
	import User from "#lib/components/ui/User.svelte";
	import TopbarSearch from "#lib/components/shell/TopbarSearch.svelte";
	import NotificationBell from "#lib/components/notifications/NotificationBell.svelte";
	import { isHandle } from "@atcute/lexicons/syntax";
	import type { AuthAccount, CurrentUser } from "#lib/auth.svelte.ts";
	import Separator from "#lib/components/ui/Separator.svelte";
	import { setContext, type Snippet } from "svelte";
	import { quintOut } from "svelte/easing";
	import { prefersReducedMotion } from "svelte/motion";
	import { fly } from "svelte/transition";
	import ChevronLeft from "$icon/chevron-left";
	import ChevronRight from "$icon/chevron-right";
	import { buttonIcon } from "#lib/components/ui/Button.svelte";
	import { siteLinkSections, type SiteLink } from "#lib/components/shell/siteLinks.ts";
	import StatusDot from "#lib/components/StatusDot.svelte";

	interface Props {
		variant: NonNullable<TopbarVariants["variant"]>;
		onSignOut?: () => void | Promise<void>;
		onSwitchAccount?: (did: AuthAccount["did"]) => void | Promise<void>;
		user: CurrentUser | null;
		accounts?: readonly AuthAccount[];
		loginHref?: string;
		loading?: boolean;
	}

	let {
		variant,
		onSignOut,
		onSwitchAccount,
		user,
		accounts = [],
		loginHref = "/login",
		loading = false
	}: Props = $props();

	const appPath = (path: string) => resolve(path as "/");

	const siteLinkHref = (link: SiteLink) =>
		link.internal ? appPath(link.href) : (link.href as ResolvedPathname);

	let newSheetOpen = $state(false);
	let searchSheetOpen = $state(false);
	let profileSheetOpen = $state(false);
	let aboutOpen = $state(false);
	let mainHeight = $state<number>();
	let aboutHeight = $state<number>();
	const paneHeight = $derived(
		aboutOpen ? (aboutHeight ?? mainHeight) : (mainHeight ?? aboutHeight)
	);

	setContext("dropdown-close", () => {
		newSheetOpen = false;
		profileSheetOpen = false;
	});

	const slide = (x: string) => ({
		x: prefersReducedMotion.current ? 0 : x,
		duration: 250,
		easing: quintOut
	});

	const openProfileSheet = () => {
		aboutOpen = false;
		profileSheetOpen = true;
	};

	const others = $derived(accounts.filter((account) => account.did !== user?.did));

	const style = $derived(topbar({ variant }));
</script>

<nav class={style.nav()} aria-label="main">
	<div class={style.row()}>
		<div class={style.brand()}>
			<a href={resolve("/")} class={style.logo()}>
				<span class={style.wordmark()}>
					<Logo wordmark class="h-6" />
				</span>
				<span class={style.mark()}>
					<img src="/logos/dolly.svg" alt="tangled" class="size-6" />
				</span>
			</a>
		</div>

		<div class={style.actions()}>
			{#if variant === "marketing"}
				<Button variant="ghost" href={resolve("/login")}>Sign in</Button>

				<Button
					variant="primary"
					href={resolve("/signup")}
					icon={ArrowRight}
					iconSide="right"
				>
					Join now
				</Button>
			{:else}
				{#if user || loading}
					<div class={style.action({ class: "order-3 hidden sm:block md:order-1" })}>
						<TopbarSearch />
					</div>

					<div class={style.action({ class: "order-1 md:order-3" })}>
						{#snippet newItems()}
							<DropdownItem href={resolve("/repo/new")} icon={BookBookmark}
								>New repository</DropdownItem
							>
							<DropdownItem href={appPath("/repo/new?import=github")} icon={Download}
								>Import repository</DropdownItem
							>
							<Separator />
							<DropdownItem href={resolve("/strings/new")} icon={LineSquiggle}
								>New string</DropdownItem
							>
							<Separator />
							<DropdownItem href={resolve("/organizations/new")} icon={Building2}
								>New organization</DropdownItem
							>
						{/snippet}
						{#if mobile.current}
							<Button
								size="small"
								aria-label="New"
								aria-haspopup="dialog"
								aria-expanded={newSheetOpen}
								onclick={() => (newSheetOpen = true)}
							>
								<Plus class="size-3.5" aria-hidden="true" />
							</Button>
							<Modal drawer bind:open={newSheetOpen}>
								<div class={style.sheet()}>{@render newItems()}</div>
							</Modal>
						{:else}
							<Dropdown
								size="small"
								id="new-menu"
								group="topbar"
								variant="button"
								align="right"
								label="New"
							>
								{#snippet trigger()}
									<Plus class="size-3.5" aria-hidden="true" />
									<span class="hidden md:inline" aria-hidden="true">New</span>
								{/snippet}
								{@render newItems()}
							</Dropdown>
						{/if}
					</div>

					<div class={style.action({ class: "order-2 md:order-4" })}>
						<NotificationBell />
					</div>

					<div class="order-3 md:hidden">
						<Button
							variant="ghost"
							icon={Search}
							size="small"
							aria-label="Search"
							aria-haspopup="dialog"
							aria-expanded={searchSheetOpen}
							onclick={() => (searchSheetOpen = true)}
						/>
						<Modal drawer class="h-full" bind:open={searchSheetOpen}>
							{#if searchSheetOpen}
								<TopbarSearch sheet onnavigate={() => (searchSheetOpen = false)} />
							{/if}
						</Modal>
					</div>

					<div class={style.divider({ class: "md:order-2" })}></div>
				{/if}

				<div class={style.action({ class: "order-4 md:order-5" })}>
					{#if loading}
						<div class={style.identity()} role="status" aria-label="Signing in">
							<span class={style.ball()}>
								<Spinner class="size-4" />
							</span>
							<span class={style.bar()} aria-hidden="true"></span>
						</div>
					{:else if user}
						{#snippet aboutLinks()}
							{#each siteLinkSections as section (section.title)}
								<DropdownGroup label={section.title} icon={section.icon}>
									{#each section.links as link (link.href)}
										<DropdownItem
											href={siteLinkHref(link)}
											target={link.newTab ? "_blank" : undefined}
											rel={link.internal
												? undefined
												: "external noopener noreferrer"}
											>{link.label}{#if link.status}<StatusDot
												/>{/if}</DropdownItem
										>
									{/each}
								</DropdownGroup>
							{/each}
						{/snippet}
						{#snippet profileItems(current: CurrentUser, about: Snippet)}
							{@const actor = profileIdentifier(
								isHandle(current.handle) ? current.handle : undefined,
								current.did
							)}
							{#if others.length > 0}
								<div class={style.switchHeader()}>Switch account</div>
								{#each others as account (account.did)}
									<DropdownItem onclick={() => onSwitchAccount?.(account.did)}>
										<User did={account.did} handle={account.handle} />
									</DropdownItem>
								{/each}
							{/if}
							<DropdownItem data-testid="addAccount" href={appPath(loginHref)}>
								<span class={style.addBall()}>
									<Plus class="size-3" aria-hidden="true" />
								</span>
								Add account
							</DropdownItem>
							<Separator />
							<DropdownItem href={appPath(`/${actor}`)} icon={UserIcon}
								>Profile</DropdownItem
							>
							<DropdownItem href={appPath(`/${actor}?tab=repos`)} icon={BookMarked}>
								Repositories
							</DropdownItem>
							<DropdownItem
								href={appPath(`/${actor}?tab=strings`)}
								icon={LineSquiggle}
							>
								Strings
							</DropdownItem>
							<DropdownItem href={resolve("/settings/organizations")} icon={Building2}
								>Organizations</DropdownItem
							>
							<DropdownItem href={resolve("/settings")} icon={Cog}
								>Settings</DropdownItem
							>
							{@render about()}
							<DropdownItem danger icon={LogOut} onclick={onSignOut}
								>Sign out</DropdownItem
							>
						{/snippet}
						{#snippet aboutSubmenu()}
							<DropdownSubmenu label="About" icon={Info}
								>{@render aboutLinks()}</DropdownSubmenu
							>
						{/snippet}
						{#snippet aboutDrillIn()}
							<Button
								variant="ghost"
								icon={Info}
								class={dropdownItem({ class: "[&>span]:w-full" })}
								onclick={() => (aboutOpen = true)}
							>
								<span class="grow">About</span>
								<ChevronRight class={buttonIcon()} aria-hidden="true" />
							</Button>
						{/snippet}
						{#if mobile.current}
							<Button
								variant="ghost"
								size="small"
								class="w-7 px-0"
								aria-label="Account"
								aria-haspopup="dialog"
								aria-expanded={profileSheetOpen}
								onclick={openProfileSheet}
							>
								<User
									did={user.did}
									handle={user.handle}
									handleClass="hidden"
									loading="eager"
								/>
							</Button>
							<Modal drawer bind:open={profileSheetOpen}>
								<div
									class={style.panes()}
									style:height={paneHeight === undefined
										? undefined
										: `${paneHeight}px`}
								>
									{#if aboutOpen}
										<div
											class={style.sheet({ class: style.pane() })}
											transition:fly={slide("100%")}
											bind:offsetHeight={aboutHeight}
										>
											<Button
												variant="ghost"
												icon={ChevronLeft}
												class={dropdownItem()}
												onclick={() => (aboutOpen = false)}>About</Button
											>
											{@render aboutLinks()}
										</div>
									{:else}
										<div
											class={style.sheet({ class: style.pane() })}
											transition:fly={slide("-100%")}
											bind:offsetHeight={mainHeight}
										>
											{@render profileItems(user, aboutDrillIn)}
										</div>
									{/if}
								</div>
							</Modal>
						{:else}
							<Dropdown
								id="profile-menu"
								group="topbar"
								align="right"
								buttonVariant="ghost"
								triggerClass="max-md:w-7 max-md:px-0 md:pl-2xs"
								variant="button"
								size="small"
							>
								{#snippet trigger()}
									<User
										did={user.did}
										handle={user.handle}
										handleClass="hidden md:inline"
										loading="eager"
									/>
								{/snippet}
								{@render profileItems(user, aboutSubmenu)}
							</Dropdown>
						{/if}
					{:else}
						<div class={style.identity({ class: "gap-xs" })}>
							<Button
								data-testid="login"
								variant="ghost"
								size="small"
								href={appPath(loginHref)}>Sign in</Button
							>
							<Button variant="primary" size="small" href={resolve("/signup")}>
								Join now
							</Button>
						</div>
					{/if}
				</div>
			{/if}
		</div>
	</div>
</nav>