Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452<script lang="ts"> import type { Action } from "svelte/action"; import type { Component } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; import { SvelteDate } from "svelte/reactivity"; import { Tween } from "svelte/motion"; import { linear } from "svelte/easing"; 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 EmptyState from "#lib/components/ui/EmptyState.svelte"; import ActivityMonthPanel from "#lib/components/profile/ActivityMonthPanel.svelte"; import { formatCount, plural } from "#lib/format.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 shimmerId = `${uid}-shimmer`; const glowId = `${uid}-glow`; const panelId = `${uid}-month`;
const emptyMonth = (monthsAgo: number): ProfileActivityMonth => ({ monthsAgo, commits: 0, commitRepos: [], repos: [], issues: [], pulls: [] }); const months = $derived( Array.from({ length: 6 }, (_, index) => { const monthsAgo = 5 - index; return ( activity.months.find((month) => month.monthsAgo === monthsAgo) ?? emptyMonth(monthsAgo) ); }) ); const monthName = (monthsAgo: number) => { const date = new SvelteDate(); date.setUTCDate(1); date.setUTCMonth(date.getUTCMonth() - monthsAgo); return new Intl.DateTimeFormat("en", { month: "long", timeZone: "UTC" }).format(date); }; 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 focused = $state<number | null>(null); let measured = $state(false); const layout = $derived(layoutActivityChart(values, availableWidth)); const viewport: Action<HTMLDivElement> = (node) => { let followsCurrent = true; let frame = 0; const alignCurrent = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(() => { if (followsCurrent) node.scrollLeft = node.scrollWidth - node.clientWidth; findFocused(); }); }; const findFocused = () => { const scrollable = node.scrollWidth - node.clientWidth; if (scrollable <= 1) { focused = null; return; } if (node.scrollLeft <= 1) { focused = 0; return; } const middle = node.scrollLeft + node.clientWidth / 2; const index = layout.columns.findIndex( (column) => middle >= column.left && middle < column.left + column.width ); focused = index < 0 ? null : Math.min(index, layout.columns.length - 2); }; const onScroll = () => { followsCurrent = node.scrollWidth - node.clientWidth - node.scrollLeft <= 1; findFocused(); }; 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); } }; };
let selected = $state<number | null>(null); let shown = $state<number | null>(null); const select = (monthIndex: number) => { selected = selected === monthIndex ? null : monthIndex; if (selected !== null) shown = selected; }; let focus = $state<{ metric: ActivityMetric } | null>(null); const openPoint = (monthIndex: number, metric: ActivityMetric) => { selected = monthIndex; shown = monthIndex; focus = { metric }; };
let pointerInside = $state(false); let shiftHeld = $state(false); let hoveredMetric = $state<ActivityMetric | null>(null); let activeMonth = $state<number | null>(null); const SWEEP_BAND = 56; const SWEEP_SPEED = 0.5; const SWEEP_OPACITY = 0.25; const sweep = new Tween(0, { easing: linear }); let sweepOrigin = $state(0); const startSweep = (x: number) => { const reach = Math.max(x, layout.width - x) + SWEEP_BAND; sweepOrigin = x; if (matchMedia("(prefers-reduced-motion: reduce)").matches) { sweep.set(reach, { duration: 0 }); return; } sweep.set(0, { duration: 0 }); sweep.set(reach, { duration: reach / SWEEP_SPEED }); }; const sweepStops = $derived.by(() => { const span = layout.width + SWEEP_BAND * 4; const offset = (x: number) => (x + SWEEP_BAND * 2) / span; const left = sweepOrigin - sweep.current; const right = sweepOrigin + sweep.current; return [ [left - SWEEP_BAND, false], [left, true], [Math.min(left + SWEEP_BAND, sweepOrigin), false], [Math.max(right - SWEEP_BAND, sweepOrigin), false], [right, true], [right + SWEEP_BAND, false] ].map(([x, bright]) => ({ offset: offset(x as number), bright: bright as boolean })); }); const hover = ( metric: ActivityMetric | null, monthIndex: number | null = null, origin?: number ) => { const entersPoint = monthIndex !== null && monthIndex !== activeMonth && sweep.current === sweep.target; if (metric && origin !== undefined && (metric !== hoveredMetric || entersPoint)) startSweep(origin); hoveredMetric = metric; activeMonth = monthIndex; }; const leave = (event: PointerEvent) => { const next = event.relatedTarget instanceof Element ? event.relatedTarget : null; if (next?.closest(`[data-metric="${hoveredMetric}"], [data-line="${hoveredMetric}"]`)) return; hover(null); }; const trackPointer = (event: PointerEvent) => { pointerInside = true; shiftHeld = event.shiftKey; }; const trackShift = (event: KeyboardEvent) => { shiftHeld = event.shiftKey; };</script>
<svelte:window onkeydown={trackShift} onkeyup={trackShift} onblur={() => (shiftHeld = false)} />
<section class="space-y-xs [--metric-commits:var(--color-indigo-500)] [--metric-issues:var(--color-green-500)] [--metric-pulls:var(--color-purple-500)] [--metric-repos:var(--color-orange-500)] [--sweep-mix:black_15%] dark:[--sweep-mix:white_55%]"> <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} <div> <div use:viewport class="rounded-md border border-border-default transition-[border-radius] duration-250 ease-out-strong {selected === null ? '' : 'rounded-b-none'} {measured ? 'overflow-x-auto' : 'overflow-hidden'}" > <div class="relative bg-background-default" style={`width:${layout.width}px;height:${layout.height}px`} role="presentation" onpointermove={trackPointer} onpointerleave={() => (pointerInside = false)} > <div class="absolute top-0 right-0 h-full bg-background-subtle" 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" > <defs> {#if hoveredMetric} {@const color = `var(--metric-${hoveredMetric})`} <linearGradient id={shimmerId} gradientUnits="userSpaceOnUse" x1={-SWEEP_BAND * 2} y1="0" x2={layout.width + SWEEP_BAND * 2} y2="0" > {#each sweepStops as stop, index (index)} <stop offset={stop.offset} style={`stop-color:${stop.bright ? `color-mix(in oklch, ${color}, color-mix(in oklch, ${color}, var(--sweep-mix)) ${SWEEP_OPACITY * 100}%)` : color}`} /> {/each} </linearGradient> <linearGradient id={`${shimmerId}-mask`} gradientUnits="userSpaceOnUse" x1={-SWEEP_BAND * 2} y1="0" x2={layout.width + SWEEP_BAND * 2} y2="0" > {#each sweepStops as stop, index (index)} <stop offset={stop.offset} stop-color={stop.bright ? "white" : "black"} /> {/each} </linearGradient> <mask id={`${shimmerId}-band`} maskUnits="userSpaceOnUse" x="0" y="0" width={layout.width} height={layout.height} > <rect width={layout.width} height={layout.height} fill={`url(#${shimmerId}-mask)`} /> </mask> {/if} <filter id={glowId} filterUnits="userSpaceOnUse" x="0" y="0" width={layout.width} height={layout.height} > <feGaussianBlur stdDeviation="3" /> </filter> </defs> {#each metrics as metric (metric.key)} <path d={layout.paths[metric.key]} class="transition-[stroke-opacity] duration-200 ease-out" style={`stroke:var(--metric-${metric.key})`} stroke-width="4" stroke-opacity={hoveredMetric === metric.key ? 0.1 : 0} filter={`url(#${glowId})`} /> <path d={layout.paths[metric.key]} class="transition-[stroke-opacity] duration-200 ease-out" style={`stroke:${hoveredMetric === metric.key ? `url(#${shimmerId})` : `var(--metric-${metric.key})`}`} stroke-width="1" stroke-opacity={hoveredMetric === null || hoveredMetric === metric.key ? 1 : 0.08} /> {#if hoveredMetric === metric.key} <path d={layout.paths[metric.key]} style={`stroke:color-mix(in oklch, var(--metric-${metric.key}), var(--sweep-mix))`} stroke-width="3" stroke-opacity={SWEEP_OPACITY} stroke-linecap="round" mask={`url(#${shimmerId}-band)`} /> {/if} {/each} {#each metrics as metric (metric.key)} <path d={layout.paths[metric.key]} class="pointer-events-[stroke]" stroke="transparent" stroke-width="12" data-line={metric.key} role="presentation" onpointerenter={(event) => hover( metric.key, null, event.clientX - event.currentTarget.ownerSVGElement!.getBoundingClientRect() .left )} onpointerleave={leave} /> {/each} </svg>
{#each months as month, monthIndex (month.monthsAgo)} {@const column = layout.columns[monthIndex]} <button type="button" class="ease absolute top-0 h-full cursor-pointer transition-[background-color] duration-150 hover:bg-background-subtle {selected === monthIndex ? 'bg-background-subtle' : ''} {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="ease absolute bottom-2 left-2 rounded-md px-xs py-3xs typography-paragraph-regular transition-[color] duration-150 {monthIndex === 5 || selected === monthIndex ? 'border border-border-subtle bg-background-default 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 === 5} {@const lineHovered = !current && hoveredMetric === metric.key} {@const active = lineHovered && activeMonth === monthIndex} {@const revealed = current || active || focused === monthIndex || (pointerInside && shiftHeld)} <div class="absolute z-10 flex h-7 {revealed ? '' : 'pointer-events-none'} -translate-x-[calc(var(--spacing-2xs)+(--spacing(1.75)))] -translate-y-1/2 cursor-pointer items-center rounded-md px-2xs py-3xs typography-paragraph-regular transition-[color,opacity] duration-150 ease-out hover:z-20 {current ? 'bg-background-subtle text-foreground-default shadow-[0_0_var(--spacing-xs)_var(--spacing-2xs)_var(--color-background-subtle)]' : `select-none ${hoveredMetric && !lineHovered ? 'opacity-20' : ''} ${active ? 'text-foreground-on-emphasis' : revealed ? 'text-foreground-default' : 'text-foreground-muted'}`}" style={`left:${point.x}px;top:${point.y}px;--metric:var(--metric-${metric.key});--metric-ink:color-mix(in oklab, var(--metric) 85%, black)`} data-metric={metric.key} data-month={monthIndex} role="presentation" onpointerenter={() => hover(metric.key, monthIndex, point.x)} onpointerleave={leave} onclick={() => openPoint(monthIndex, metric.key)} > {#if !current} <span class="absolute top-1/2 left-2 size-1.5 -translate-y-1/2 rounded-full bg-(--metric) transition-opacity duration-200 ease-out {revealed ? 'opacity-0' : 'opacity-100'}" ></span> <span class="absolute inset-y-0 -right-1.5 -left-1 rounded-full border {active ? 'border-(--metric-ink) bg-(--metric) opacity-100 transition-[clip-path,background-color,border-color] duration-200 ease-out-strong [clip-path:inset(0_round_9999px)] motion-reduce:transition-[background-color,border-color]' : revealed ? 'border-transparent bg-background-default opacity-100 transition-opacity duration-300 ease-out [clip-path:inset(0_round_9999px)]' : 'border-(--metric) bg-(--metric) opacity-0 [clip-path:inset(calc(50%-0.1875rem)_calc(100%-1.125rem)_calc(50%-0.1875rem)_0.75rem_round_9999px)] [transition:opacity_200ms_ease-out,clip-path_0s_200ms,background-color_0s_200ms,border-color_0s_200ms]'}" ></span> {/if} <span class="hit-area-2 pointer-events-auto grid shrink-0 place-items-center" > <Icon class="size-3.5 transition-[scale,opacity] motion-reduce:transition-opacity {active ? 'scale-100 opacity-100 duration-200 ease-out-strong' : revealed ? 'scale-100 opacity-100 duration-300 ease-out' : 'scale-90 opacity-0 duration-200 ease-out'}" aria-hidden="true" /> </span> <span class="relative flex items-center pl-xs whitespace-nowrap transition-opacity ease-out {revealed ? 'opacity-100' : 'opacity-0'} {revealed && !active ? 'duration-300' : 'duration-200'}" > {formatCount(point.value)} {#if current} <span class="whitespace-pre"> {` ${nameOf(point.value, metric)}`} </span> {/if} </span> </div> {/each} {/each} </div> </div> <ActivityMonthPanel id={panelId} month={shown === null ? null : months[shown]} open={selected !== null} {focus} /> </div> {#if activity.truncated} <p class="px-xs typography-paragraph-small text-foreground-subtle"> Some older activity is not shown. </p> {/if} {/if}</section>