Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382<script lang="ts"> import type { Action } from "svelte/action"; import { untrack, type Component } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; import { afterNavigate, goto } from "$app/navigation"; import { page } from "$app/state"; import { resolve } from "$app/paths"; import { MediaQuery } from "svelte/reactivity"; import BookPlus from "$icon/book-plus"; import CircleDot from "$icon/circle-dot"; import GitCommit from "$icon/git-commit-horizontal"; import GitPullRequest from "$icon/git-pull-request"; import Button from "#lib/components/ui/Button.svelte"; import EmptyState from "#lib/components/ui/EmptyState.svelte"; import ActivityMonthPanel from "#lib/components/profile/ActivityMonthPanel.svelte"; import { formatCount, plural } from "#lib/format.ts"; import { formatMonth, monthStart } from "#lib/components/profile/activityMonths.ts"; import { remToPx } from "#lib/rem.ts"; import { ACTIVITY_METRICS, layoutActivityChart, type ActivityMetric } from "#lib/components/profile/profileActivityChart.ts"; import type { ProfileActivityData, ProfileActivityMonth } from "#lib/components/profile/types.ts";
type Icon = Component<SvelteHTMLElements["svg"]>;
let { activity }: { activity: ProfileActivityData } = $props();
const uid = $props.id(); const panelId = `${uid}-month`;
const MONTH_PARAM = "month"; const SECTION_PARAM = "section"; const MONTH_COUNT = 6; const CURRENT_MONTH = MONTH_COUNT - 1;
const emptyMonth = (monthsAgo: number): ProfileActivityMonth => ({ monthsAgo, commits: 0, commitRepoCount: 0, commitRepos: [], repos: [], issues: [], pulls: [] }); const months = $derived( Array.from({ length: MONTH_COUNT }, (_, index) => { const monthsAgo = CURRENT_MONTH - index; return ( activity.months.find((month) => month.monthsAgo === monthsAgo) ?? emptyMonth(monthsAgo) ); }) ); const monthName = (monthsAgo: number) => formatMonth(monthsAgo, { month: "long" }); const monthKey = (monthsAgo: number) => monthStart(monthsAgo).toISOString().slice(0, 7); const metrics: { key: ActivityMetric; label: string; singular: string; icon: Icon }[] = [ { key: "pulls", label: "pull requests", singular: "pull request", icon: GitPullRequest }, { key: "commits", label: "commits", singular: "commit", icon: GitCommit }, { key: "repos", label: "new repositories", singular: "new repository", icon: BookPlus }, { key: "issues", label: "issues", singular: "issue", icon: CircleDot } ]; const valueOf = (month: ProfileActivityMonth, key: ActivityMetric) => key === "commits" ? month.commits : month[key].length; const nameOf = (value: number, metric: (typeof metrics)[number]) => plural(value, metric.singular, metric.label); const values = $derived( Object.fromEntries( ACTIVITY_METRICS.map((metric) => [ metric, months.map((month) => valueOf(month, metric)) ]) ) as Record<ActivityMetric, number[]> );
let availableWidth = $state(982); let measured = $state(false); let fontScale = $state(1); const layout = $derived(layoutActivityChart(values, availableWidth, fontScale)); const viewport: Action<HTMLDivElement> = (node) => { fontScale = remToPx(1) / 16; let followsCurrent = true; let frame = 0; const alignCurrent = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(() => { if (followsCurrent) node.scrollLeft = node.scrollWidth - node.clientWidth; }); }; const onScroll = () => { followsCurrent = node.scrollWidth - node.clientWidth - node.scrollLeft <= 1; }; const observer = new ResizeObserver(([entry]) => { availableWidth = Math.floor(entry.contentRect.width); measured = true; alignCurrent(); }); observer.observe(node); node.addEventListener("scroll", onScroll, { passive: true }); alignCurrent(); return { destroy() { cancelAnimationFrame(frame); observer.disconnect(); node.removeEventListener("scroll", onScroll); } }; };
const currentUrl = () => page.shallow?.url ?? page.url; const fromUrl = () => { const params = currentUrl().searchParams; const index = months.findIndex( (month) => monthKey(month.monthsAgo) === params.get(MONTH_PARAM) ); if (index === -1) return { selected: null, expanded: {} }; return { selected: index, expanded: Object.fromEntries( params .getAll(SECTION_PARAM) .filter((key) => ACTIVITY_METRICS.includes(key as ActivityMetric)) .map((key) => [key, true]) ) }; }; const initial = fromUrl(); let selected = $state<number | null>(initial.selected); const activeMonth = $derived(selected ?? CURRENT_MONTH); let shown = $state<number | null>(initial.selected); let focus = $state<{ metric: ActivityMetric | null } | null>(null); const select = (monthIndex: number) => { selected = selected === monthIndex ? null : monthIndex; if (selected === null) return; shown = selected; focus = { metric: null }; }; let expanded = $state<Partial<Record<ActivityMetric, boolean>>>(initial.expanded); afterNavigate((navigation) => { if (navigation.type === "enter" || (navigation.shallow && navigation.type !== "popstate")) return; const next = fromUrl(); selected = next.selected; shown = next.selected; expanded = next.expanded; focus = null; }); $effect(() => { const month = selected === null ? null : monthKey(CURRENT_MONTH - selected); const sections = month === null ? [] : ACTIVITY_METRICS.filter((metric) => expanded[metric]); untrack(() => { const current = currentUrl(); const url = new URL(current.href); url.searchParams.delete(MONTH_PARAM); url.searchParams.delete(SECTION_PARAM); if (month !== null) url.searchParams.set(MONTH_PARAM, month); for (const section of sections) url.searchParams.append(SECTION_PARAM, section); if (url.href !== current.href) void goto(resolve((url.pathname + url.search + url.hash) as "/"), { shallow: true, replace: true, state: page.state }); }); }); const openPoint = (monthIndex: number, metric: ActivityMetric) => { if (selected === monthIndex && expanded[metric]) { expanded[metric] = false; return; } selected = monthIndex; shown = monthIndex; expanded = { [metric]: true }; focus = { metric }; }; const order = $derived( shown === null ? [...ACTIVITY_METRICS] : ACTIVITY_METRICS.toSorted( (a, b) => layout.points[a][shown!].y - layout.points[b][shown!].y ) );
let hoveredMetric = $state<ActivityMetric | null>(null);
const narrow = new MediaQuery("width < 48rem", false); const newestFirst = $derived(months.map((month, index) => ({ month, index })).toReversed());</script>
<section class="space-y-xs"> <h2 class="px-xs typography-paragraph-regular font-medium">Activity</h2> {#if activity.months.length === 0} <EmptyState message="This user does not have any activity yet." /> {:else} <ol class="flex flex-col overflow-hidden rounded-md border border-border-default md:hidden"> {#each newestFirst as { month, index } (month.monthsAgo)} <li class="relative flex flex-col gap-3xs border-border-default p-xs not-first:border-t" > <button type="button" class="absolute inset-0 cursor-pointer transition-[background-color] duration-150 hover:bg-background-subtle" aria-label={`${monthName(month.monthsAgo)} activity`} aria-haspopup="dialog" onclick={() => select(index)} ></button> <span class="pointer-events-none relative px-xs py-2xs typography-paragraph-regular font-medium" aria-hidden="true">{monthName(month.monthsAgo)}</span > <ul class="pointer-events-none relative flex flex-col"> {#each metrics as metric (metric.key)} {@const Icon = metric.icon} {@const value = valueOf(month, metric.key)} <li class="flex"> {#if value > 0} <Button variant="ghost" class="pointer-events-auto min-h-0 justify-start gap-xs px-xs py-2xs text-left" aria-haspopup="dialog" onclick={() => openPoint(index, metric.key)} > <Icon class="size-3.5 shrink-0" aria-hidden="true" /> {formatCount(value)} {nameOf(value, metric)} </Button> {:else} <span class="flex items-center gap-xs px-xs py-2xs typography-paragraph-regular text-foreground-subtle" > <Icon class="size-3.5 shrink-0" aria-hidden="true" /> {formatCount(value)} {nameOf(value, metric)} </span> {/if} </li> {/each} </ul> </li> {/each} </ol> <div class="max-md:hidden"> <div use:viewport class="rounded-md border border-border-default transition-[border-radius] duration-250 ease-out-strong {selected === null ? '' : 'rounded-b-none border-b-transparent bg-background-subtle'} {measured ? 'overflow-x-auto' : 'overflow-hidden'}" > <div class="relative bg-background-default" style={`width:${layout.width}px;height:${layout.height}px`} > <div class="absolute top-0 right-0 h-full bg-background-subtle transition-opacity duration-150 ease-out {activeMonth === CURRENT_MONTH ? 'opacity-100' : 'opacity-0'}" style={`width:${layout.columns.at(-1)!.width}px`} ></div> <svg class="pointer-events-none absolute inset-0 z-1 size-full" viewBox={`0 0 ${layout.width} ${layout.height}`} fill="none" aria-hidden="true" > {#each metrics as metric (metric.key)} <path d={layout.paths[metric.key]} class="stroke-foreground-subtle transition-[stroke-opacity] duration-200 ease-out" stroke-width="1" stroke-opacity={hoveredMetric === null ? 0.25 : hoveredMetric === metric.key ? 0.9 : 0.08} data-line={metric.key} /> {/each} </svg>
{#each months as month, monthIndex (month.monthsAgo)} {@const column = layout.columns[monthIndex]} {@const active = activeMonth === monthIndex} <div class="group/month contents"> <button type="button" class="absolute top-0 h-full cursor-pointer transition-[background-color] duration-150 select-none group-hover/month:bg-background-subtle {selected === monthIndex ? 'bg-background-subtle' : ''} {selected !== null && selected !== monthIndex ? 'after:absolute after:inset-x-0 after:bottom-0 after:h-px after:bg-border-default' : ''} {column.divider ? 'border-l border-border-default' : ''}" style={`left:${column.left}px;width:${column.width}px`} aria-label={`${monthName(month.monthsAgo)} activity`} aria-pressed={selected === monthIndex} aria-controls={panelId} onclick={() => select(monthIndex)} > <span class="absolute bottom-2 left-2 px-xs py-3xs typography-paragraph-regular transition-[color] duration-150 {active ? 'text-foreground-default' : 'text-foreground-muted'}" > {monthName(month.monthsAgo)} </span> </button>
{#each metrics as metric (metric.key)} {@const Icon = metric.icon} {@const point = layout.points[metric.key][monthIndex]} {@const current = monthIndex === CURRENT_MONTH} {@const dimmed = hoveredMetric !== null && hoveredMetric !== metric.key} {@const interactive = point.value > 0} <svelte:element this={interactive ? "button" : "div"} {...interactive ? { type: "button", "aria-label": `${formatCount(point.value)} ${nameOf(point.value, metric)} in ${monthName(month.monthsAgo)}`, "aria-expanded": selected === monthIndex && Boolean(expanded[metric.key]), "aria-controls": panelId, onclick: () => openPoint(monthIndex, metric.key) } : { role: "presentation" }} class={[ "absolute z-10 flex h-7 -translate-y-1/2 items-center rounded-md px-2xs py-3xs typography-paragraph-regular whitespace-nowrap transition-[background-color,box-shadow,color] duration-150 ease-out select-none hover:z-20 focus-visible:z-20 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-border-focus", active ? "bg-background-subtle shadow-[0_0_var(--spacing-xs)_var(--spacing-2xs)_var(--color-background-subtle)]" : "bg-background-default shadow-[0_0_var(--spacing-xs)_var(--spacing-2xs)_var(--color-background-default)] group-hover/month:bg-background-subtle group-hover/month:shadow-[0_0_var(--spacing-xs)_var(--spacing-2xs)_var(--color-background-subtle)]", dimmed ? "text-foreground-subtle" : "text-foreground-default", interactive && "cursor-pointer" ]} style={`left:${point.x}px;top:${point.y}px`} data-metric={metric.key} data-month={monthIndex} onpointerenter={() => (hoveredMetric = metric.key)} onpointerleave={() => (hoveredMetric = null)} > <Icon class="size-3.5 shrink-0" aria-hidden="true" /> <span class="pl-xs whitespace-pre" >{formatCount(point.value)}{current ? ` ${nameOf(point.value, metric)}` : ""}</span > </svelte:element> {/each} </div> {/each} </div> </div> <ActivityMonthPanel id={panelId} month={shown === null ? null : months[shown]} open={selected !== null} order={narrow.current ? [...ACTIVITY_METRICS] : order} bind:expanded {focus} sheet={narrow.current} onclose={() => (selected = null)} /> </div> {#if activity.truncated} <p class="px-xs typography-paragraph-small text-foreground-subtle"> Some older activity is not shown. </p> {/if} {/if}</section>