web/src/components/things/ChangelogTimeline.svelte

723 lines · 18900 bytes

  1<script lang="ts">
  2  import type {
  3    ChangelogEntry,
  4    ServiceChange,
  5    Rejection,
  6    ChangelogPr,
  7  } from '../../utils/fetch-changelog';
  8  import { slugify } from '@utils/fetch-data';
  9
 10  interface Props {
 11    entries?: ChangelogEntry[];
 12    rejections?: Rejection[];
 13  }
 14  const { entries = [], rejections = [] }: Props = $props();
 15
 16  type Filter = 'added' | 'removed' | 'modified' | 'rejected';
 17
 18  const filterDefs: { key: Filter; label: string; icon: string }[] = [
 19    { key: 'added', label: 'Additions', icon: '+' },
 20    { key: 'removed', label: 'Removals', icon: '−' },
 21    { key: 'modified', label: 'Amendments', icon: '✎' },
 22    { key: 'rejected', label: 'Rejections', icon: '✕' },
 23  ];
 24
 25  let on: Record<Filter, boolean> = $state({
 26    added: true,
 27    removed: true,
 28    modified: true,
 29    rejected: true,
 30  });
 31  let searchQuery = $state('');
 32
 33  const MAX_VISIBLE_CHANGES = 8;
 34  let expanded: Record<string, boolean> = $state({});
 35
 36  function toggle(key: Filter) {
 37    on = { ...on, [key]: !on[key] };
 38  }
 39
 40  function toggleExpanded(sha: string) {
 41    expanded = { ...expanded, [sha]: !expanded[sha] };
 42  }
 43
 44  type TimelineItem =
 45    | {
 46        kind: 'entry';
 47        date: string;
 48        sha: string;
 49        pr?: ChangelogPr | null;
 50        data: ChangelogEntry;
 51      }
 52    | {
 53        kind: 'rejection';
 54        date: string;
 55        sha: string;
 56        pr: ChangelogPr;
 57        data: Rejection;
 58      };
 59
 60  const svc = (e: ChangelogEntry) => {
 61    const s = e.changes?.services;
 62    return {
 63      added: s?.added || [],
 64      removed: s?.removed || [],
 65      modified: s?.modified || [],
 66      moved: s?.moved || [],
 67      renamed: s?.renamed || [],
 68    };
 69  };
 70  const sec = (e: ChangelogEntry) => {
 71    const s = e.changes?.sections;
 72    return {
 73      added: s?.added || [],
 74      removed: s?.removed || [],
 75      moved: s?.moved || [],
 76    };
 77  };
 78  const cat = (e: ChangelogEntry) => {
 79    const c = e.changes?.categories;
 80    return { added: c?.added || [], removed: c?.removed || [] };
 81  };
 82
 83  function matchesFilters(
 84    item: TimelineItem,
 85    f: Record<Filter, boolean>,
 86  ): boolean {
 87    if (item.kind === 'rejection') return f.rejected;
 88    const s = svc(item.data),
 89      sc = sec(item.data),
 90      ct = cat(item.data);
 91    return (
 92      (f.added &&
 93        (s.added.length > 0 || sc.added.length > 0 || ct.added.length > 0)) ||
 94      (f.removed &&
 95        (s.removed.length > 0 ||
 96          sc.removed.length > 0 ||
 97          ct.removed.length > 0)) ||
 98      (f.modified &&
 99        (s.modified.length > 0 ||
100          s.moved.length > 0 ||
101          s.renamed.length > 0 ||
102          sc.moved.length > 0))
103    );
104  }
105
106  function matchesSearch(item: TimelineItem, query: string): boolean {
107    if (!query) return true;
108    const q = query.toLowerCase();
109    if (item.kind === 'rejection') {
110      return (
111        item.data.title.toLowerCase().includes(q) ||
112        (item.pr.author?.toLowerCase().includes(q) ?? false)
113      );
114    }
115    const s = svc(item.data);
116    const all = [
117      ...s.added,
118      ...s.removed,
119      ...s.modified,
120      ...s.moved.flatMap((m) => [
121        { name: m.name, category: m.from.category, section: m.from.section },
122        { name: m.name, category: m.to.category, section: m.to.section },
123      ]),
124      ...s.renamed.flatMap((m) => [
125        {
126          name: m.previousName,
127          category: m.from.category,
128          section: m.from.section,
129        },
130        { name: m.name, category: m.to.category, section: m.to.section },
131      ]),
132    ];
133    return (
134      all.some(
135        (c) =>
136          c.name.toLowerCase().includes(q) ||
137          `${c.category} ${c.section}`.toLowerCase().includes(q),
138      ) ||
139      (item.pr?.author?.toLowerCase().includes(q) ?? false)
140    );
141  }
142
143  const pl = (n: number, word: string) => `${n} ${word}${n > 1 ? 's' : ''}`;
144
145  function summarize(entry: ChangelogEntry): string {
146    const s = svc(entry),
147      sc = sec(entry);
148    const parts: string[] = [];
149    if (s.added.length) parts.push(pl(s.added.length, 'addition'));
150    if (s.removed.length) parts.push(pl(s.removed.length, 'removal'));
151    if (s.modified.length) parts.push(pl(s.modified.length, 'amendment'));
152    if (s.moved.length) parts.push(pl(s.moved.length, 'move'));
153    if (s.renamed.length) parts.push(pl(s.renamed.length, 'rename'));
154    if (sc.added.length) parts.push(pl(sc.added.length, 'new section'));
155    if (sc.removed.length) parts.push(pl(sc.removed.length, 'section removal'));
156    if (sc.moved.length) parts.push(pl(sc.moved.length, 'section rename'));
157    return parts.join(', ') || 'Changes';
158  }
159
160  function serviceLink(s: ServiceChange, isRemoval: boolean): string {
161    const c = slugify(s.category),
162      sc = slugify(s.section);
163    return isRemoval ? `/${c}/${sc}/` : `/${c}/${sc}/${slugify(s.name)}/`;
164  }
165
166  type ChangeRow = {
167    badge: string;
168    cls: string;
169    name: string;
170    href: string;
171    path?: string;
172    fields?: string[];
173  };
174
175  function changeRows(e: ChangelogEntry): ChangeRow[] {
176    const s = svc(e),
177      sc = sec(e),
178      ct = cat(e);
179    return [
180      ...s.added.map((v) => ({
181        badge: 'Added',
182        cls: 'add',
183        name: v.name,
184        href: serviceLink(v, false),
185        path: `into ${v.category} › ${v.section}`,
186      })),
187      ...s.removed.map((v) => ({
188        badge: 'Removed',
189        cls: 'rem',
190        name: v.name,
191        href: serviceLink(v, true),
192        path: `from ${v.category} › ${v.section}`,
193      })),
194      ...s.modified.map((v) => ({
195        badge: 'Amended',
196        cls: 'mod',
197        name: v.name,
198        href: serviceLink(v, false),
199        path: `in ${v.category} › ${v.section}`,
200        fields: v.fields,
201      })),
202      ...s.moved.map((v) => ({
203        badge: 'Moved',
204        cls: 'mod',
205        name: v.name,
206        href: serviceLink(
207          { name: v.name, category: v.to.category, section: v.to.section },
208          false,
209        ),
210        path: `from ${v.from.category} › ${v.from.section} → ${v.to.category} › ${v.to.section}`,
211      })),
212      ...s.renamed.map((v) => {
213        const sameLoc =
214          v.from.category === v.to.category && v.from.section === v.to.section;
215        return {
216          badge: 'Renamed',
217          cls: 'mod',
218          name: `${v.previousName} → ${v.name}`,
219          href: serviceLink(
220            { name: v.name, category: v.to.category, section: v.to.section },
221            false,
222          ),
223          path: sameLoc
224            ? `in ${v.to.category} › ${v.to.section}`
225            : `from ${v.from.category} › ${v.from.section} → ${v.to.category} › ${v.to.section}`,
226        };
227      }),
228      ...sc.added.map((v) => ({
229        badge: 'New Section',
230        cls: 'add',
231        name: v.name,
232        href: `/${slugify(v.category)}/${slugify(v.name)}/`,
233      })),
234      ...sc.removed.map((v) => ({
235        badge: 'Section Removed',
236        cls: 'rem',
237        name: v.name,
238        href: '',
239      })),
240      ...sc.moved.map((v) => ({
241        badge: 'Section Renamed',
242        cls: 'mod',
243        name: `${v.from.section} → ${v.to.section}`,
244        href: `/${slugify(v.to.category)}/${slugify(v.to.section)}/`,
245        path:
246          v.from.category === v.to.category
247            ? `in ${v.to.category}`
248            : `from ${v.from.category} to ${v.to.category}`,
249      })),
250      ...ct.added.map((v) => ({
251        badge: 'New Category',
252        cls: 'add',
253        name: v,
254        href: '',
255      })),
256      ...ct.removed.map((v) => ({
257        badge: 'Category Removed',
258        cls: 'rem',
259        name: v,
260        href: '',
261      })),
262    ];
263  }
264
265  const allItems = $derived(
266    [
267      ...entries.map((e): TimelineItem => ({
268        kind: 'entry',
269        date: e.date,
270        sha: e.sha,
271        pr: e.pr,
272        data: e,
273      })),
274      ...rejections.map((r): TimelineItem => ({
275        kind: 'rejection',
276        date: r.date,
277        sha: `rej-${r.pr.number}`,
278        pr: r.pr,
279        data: r,
280      })),
281    ].sort((a, b) => b.date.localeCompare(a.date)),
282  );
283
284  const filtered = $derived(
285    allItems.filter(
286      (item) => matchesFilters(item, on) && matchesSearch(item, searchQuery),
287    ),
288  );
289
290  const grouped = $derived(
291    filtered.reduce<Record<string, TimelineItem[]>>((acc, item) => {
292      const d = new Date(item.date + 'T00:00:00Z');
293      const key = d.toLocaleDateString('en-US', {
294        year: 'numeric',
295        month: 'long',
296        timeZone: 'UTC',
297      });
298      (acc[key] ??= []).push(item);
299      return acc;
300    }, {}),
301  );
302
303  function formatDate(dateStr: string): string {
304    return new Date(dateStr + 'T00:00:00Z').toLocaleDateString('en-US', {
305      month: 'short',
306      day: 'numeric',
307      year: 'numeric',
308      timeZone: 'UTC',
309    });
310  }
311</script>
312
313<details class="controls">
314  <summary>Search &amp; Filter</summary>
315  <div class="controls-body">
316    <input
317      class="search"
318      type="search"
319      aria-label="Filter changes by service, category or user"
320      placeholder="Filter by service, category, user.."
321      bind:value={searchQuery}
322    />
323    <div class="filters">
324      {#each filterDefs as f (f.key)}
325        <button
326          class="pill {f.key}"
327          class:active={on[f.key]}
328          aria-pressed={on[f.key]}
329          onclick={() => toggle(f.key)}
330        >
331          <span class="icon">{f.icon}</span>{f.label}
332        </button>
333      {/each}
334    </div>
335  </div>
336</details>
337
338<p class="sr-only" role="status">
339  Showing {filtered.length} of {allItems.length} entries
340</p>
341
342{#if filtered.length === 0}
343  <p class="empty">No matching changes found.</p>
344{/if}
345
346{#each Object.entries(grouped) as [month, monthItems] (month)}
347  <h2 class="month-header">{month}</h2>
348  {#each monthItems as item (item.sha)}
349    <article class="entry">
350      <time class="entry-date">{formatDate(item.date)}</time>
351      <div class="entry-body">
352        <div class="summary">
353          {#if item.pr?.authorAvatar}
354            <a
355              href={`https://github.com/${item.pr.author}`}
356              target="_blank"
357              rel="noreferrer"
358              aria-hidden="true"
359              tabindex="-1"
360            >
361              <img
362                class="avatar"
363                src={item.pr.authorAvatar}
364                alt=""
365                width="20"
366                height="20"
367                loading="lazy"
368              />
369            </a>
370          {/if}
371          {#if item.pr?.author}
372            <span class="author"
373              ><a
374                href={`https://github.com/${item.pr.author}`}
375                target="_blank"
376                rel="noreferrer">@{item.pr.author}</a
377              ></span
378            >
379          {/if}
380          <span class="summary-text">
381            {item.kind === 'rejection'
382              ? 'Submission Reviewed'
383              : summarize(item.data)}
384          </span>
385          {#if item.pr}
386            <a
387              class="pr-link"
388              href={item.pr.url}
389              target="_blank"
390              rel="noreferrer">#{item.pr.number}</a
391            >
392          {/if}
393        </div>
394
395        <div class="changes">
396          {#if item.kind === 'rejection'}
397            <div class="change">
398              <span class="badge rej">Rejected</span>
399              <a
400                class="svc-name"
401                href={item.pr.url}
402                target="_blank"
403                rel="noreferrer">{item.data.title}</a
404              >
405              <span class="path">Not merged</span>
406            </div>
407          {:else}
408            {@const rows = changeRows(item.data)}
409            {@const overflow = rows.length - MAX_VISIBLE_CHANGES}
410            {@const isOpen = !!expanded[item.sha]}
411            {#each rows.slice(0, MAX_VISIBLE_CHANGES) as row, i (i)}
412              {@render change(row)}
413            {/each}
414            {#if overflow > 0}
415              <div
416                class="extra"
417                class:open={isOpen}
418                id={`changes-${item.sha}`}
419                inert={!isOpen}
420              >
421                <div>
422                  {#each rows.slice(MAX_VISIBLE_CHANGES) as row, i (i)}
423                    {@render change(row)}
424                  {/each}
425                </div>
426              </div>
427              <button
428                class="show-more"
429                aria-expanded={isOpen}
430                aria-controls={`changes-${item.sha}`}
431                onclick={() => toggleExpanded(item.sha)}
432              >
433                {isOpen ? 'Show less' : `+${overflow} more`}
434              </button>
435            {/if}
436          {/if}
437        </div>
438      </div>
439    </article>
440  {/each}
441{/each}
442
443{#snippet change(row: ChangeRow)}
444  <div class="change">
445    <span class="badge {row.cls}">{row.badge}</span>
446    {#if row.href}
447      <a class="svc-name" href={row.href}>{row.name}</a>
448    {:else}
449      <strong>{row.name}</strong>
450    {/if}
451    {#if row.fields}<span class="fields">updated {row.fields.join(', ')}</span
452      >{/if}
453    {#if row.path && !row.fields}<span class="path">{row.path}</span>{/if}
454  </div>
455{/snippet}
456
457<style lang="scss">
458  @use '../../styles/mixins' as *;
459  .controls {
460    margin-bottom: var(--space-md);
461
462    summary {
463      cursor: pointer;
464      width: fit-content;
465      font-family: var(--font-subtitle);
466      opacity: var(--opacity-muted);
467      &:hover {
468        opacity: 1;
469      }
470    }
471
472    .controls-body {
473      display: flex;
474      align-items: flex-end;
475      justify-content: space-between;
476      gap: var(--space-md);
477      margin-top: var(--space-sm);
478    }
479
480    .search {
481      width: 200px;
482      padding: 0.35rem 0.75rem;
483      border: var(--border-heavy);
484      border-radius: var(--curve-lg);
485      box-shadow: var(--shadow-xs);
486      background: var(--surface);
487      color: var(--foreground);
488      &:focus {
489        outline: none;
490        border-color: var(--accent);
491        box-shadow: 2px 2px 0 var(--accent);
492      }
493      &::placeholder {
494        opacity: var(--opacity-dim);
495      }
496    }
497
498    .filters {
499      display: flex;
500      gap: 0.3rem;
501      flex-wrap: wrap;
502    }
503
504    .pill {
505      display: flex;
506      align-items: center;
507      gap: var(--space-xs);
508      min-height: 24px;
509      padding: 0.15rem var(--space-sm);
510      border: 1px solid transparent;
511      border-radius: var(--curve-md);
512      background: var(--background);
513      color: var(--foreground);
514      cursor: pointer;
515      font-family: var(--font-subtitle);
516      font-size: var(--text-xs);
517      opacity: var(--opacity-dim);
518      transition: var(--transition-normal);
519
520      .icon {
521        font-size: var(--text-sm);
522        line-height: 1;
523      }
524      &.active {
525        opacity: 1;
526        border-color: currentColor;
527      }
528      &.added {
529        &.active {
530          color: var(--changelog-add);
531        }
532        &:hover {
533          background: color-mix(in srgb, var(--changelog-add) 10%, transparent);
534        }
535      }
536      &.removed {
537        &.active {
538          color: var(--changelog-rem);
539        }
540        &:hover {
541          background: color-mix(in srgb, var(--changelog-rem) 10%, transparent);
542        }
543      }
544      &.modified {
545        &.active {
546          color: var(--changelog-mod);
547        }
548        &:hover {
549          background: color-mix(in srgb, var(--changelog-mod) 10%, transparent);
550        }
551      }
552      &.rejected {
553        &.active {
554          color: var(--changelog-rej);
555        }
556        &:hover {
557          background: color-mix(in srgb, var(--changelog-rej) 10%, transparent);
558        }
559      }
560    }
561  }
562
563  .empty {
564    text-align: center;
565    opacity: var(--opacity-muted);
566    margin: var(--space-lg) 0;
567  }
568
569  .month-header {
570    font-size: var(--text-lg);
571    margin: 1.5rem 0 var(--space-sm) 0;
572    padding-bottom: 0.3rem;
573    border-bottom: 1px solid var(--accent-3);
574    color: var(--accent-3-text);
575    font-family: var(--font-subtitle);
576  }
577
578  .entry {
579    display: flex;
580    gap: var(--space-md);
581    padding: 0.6rem 0;
582    border-bottom: 1px solid var(--background-hr-color);
583    &:last-child {
584      border-bottom: none;
585    }
586
587    .entry-date {
588      min-width: 7rem;
589      opacity: 0.75;
590      padding-top: 0.15rem;
591      font-family: var(--font-subtitle);
592    }
593
594    .entry-body {
595      flex: 1;
596      min-width: 0;
597    }
598  }
599
600  .summary {
601    display: flex;
602    align-items: center;
603    gap: 0.4rem;
604    flex-wrap: wrap;
605    margin-bottom: 0.3rem;
606    font-size: var(--text-sm);
607    opacity: var(--opacity-subtle);
608    .author a {
609      color: var(--foreground);
610      &:hover {
611        color: var(--accent-text);
612      }
613    }
614    .avatar {
615      border-radius: 50%;
616    }
617    .pr-link {
618      padding: 0 0.4rem;
619      border-radius: var(--curve-sm);
620      background: var(--accent-3);
621      color: var(--bright);
622      text-decoration: none;
623      font-family: var(--font-subtitle);
624      &:hover {
625        opacity: 0.85;
626      }
627    }
628  }
629
630  .changes {
631    display: flex;
632    flex-direction: column;
633    gap: 0.2rem;
634    .change {
635      display: flex;
636      align-items: baseline;
637      gap: 0.4rem;
638      flex-wrap: wrap;
639    }
640  }
641
642  .extra {
643    display: grid;
644    grid-template-rows: 0fr;
645    transition: var(--transition-normal);
646    &.open {
647      grid-template-rows: 1fr;
648    }
649    > div {
650      display: flex;
651      flex-direction: column;
652      gap: 0.2rem;
653      overflow: hidden;
654    }
655  }
656
657  .show-more {
658    align-self: flex-start;
659    min-height: 24px;
660    margin-top: 0.2rem;
661    padding: 0.1rem var(--space-sm);
662    border: 1px solid currentColor;
663    border-radius: var(--curve-md);
664    background: none;
665    color: var(--accent-3-text);
666    cursor: pointer;
667    font-family: var(--font-subtitle);
668    font-size: var(--text-xs);
669    transition: var(--transition-normal);
670    &:hover {
671      background: color-mix(in srgb, var(--accent-3) 15%, transparent);
672    }
673  }
674
675  .badge {
676    @include changelog-badge;
677    &.add {
678      background: color-mix(in srgb, var(--changelog-add) 33%, transparent);
679      color: var(--changelog-add);
680    }
681    &.rem {
682      background: color-mix(in srgb, var(--changelog-rej) 33%, transparent);
683      color: var(--changelog-rej);
684    }
685    &.mod {
686      background: color-mix(in srgb, var(--changelog-mod) 33%, transparent);
687      color: var(--changelog-mod);
688    }
689    &.rej {
690      background: color-mix(in srgb, var(--changelog-rem) 33%, transparent);
691      color: var(--changelog-rem);
692    }
693  }
694
695  .svc-name {
696    font-weight: 500;
697    color: var(--foreground);
698    text-decoration: none;
699  }
700  a.svc-name:hover {
701    color: var(--accent-text);
702    text-decoration: underline;
703  }
704
705  .path,
706  .fields {
707    font-size: var(--text-sm);
708    opacity: var(--opacity-muted);
709  }
710  .fields {
711    font-style: italic;
712  }
713
714  @media (max-width: 768px) {
715    .entry {
716      flex-direction: column;
717      gap: 0.2rem;
718      .entry-date {
719        min-width: unset;
720      }
721    }
722  }
723</style>