|
1 | 1 | <script lang="ts"> |
2 | 2 | import { untrack } from 'svelte'; |
3 | 3 | import { invalidateAll } from '$app/navigation'; |
| 4 | + import { fetchTrendByCategory } from '$lib/api'; |
4 | 5 |
|
5 | 6 | let { |
6 | 7 | owner, |
7 | 8 | repo, |
8 | 9 | projectId, |
9 | 10 | badgeEnabled, |
| 11 | + defaultBranch, |
10 | 12 | onclose, |
11 | 13 | }: { |
12 | 14 | owner: string; |
13 | 15 | repo: string; |
14 | 16 | projectId: number; |
15 | 17 | badgeEnabled: number; |
| 18 | + defaultBranch: string; |
16 | 19 | onclose: () => void; |
17 | 20 | } = $props(); |
18 | 21 |
|
|
26 | 29 | ]; |
27 | 30 |
|
28 | 31 | let selectedMetric = $state('coverage'); |
| 32 | + let categories = $state<string[]>(['default']); |
| 33 | + let selectedCategory = $state('default'); |
29 | 34 | let localBadgeEnabled = $state(untrack(() => badgeEnabled)); |
30 | 35 | let toggling = $state(false); |
31 | 36 |
|
| 37 | + $effect(() => { |
| 38 | + const metric = selectedMetric; |
| 39 | + (async () => { |
| 40 | + let next = ['default']; |
| 41 | + try { |
| 42 | + const result = await fetchTrendByCategory(owner, repo, metric, defaultBranch, { limit: 1 }); |
| 43 | + if (result.categories.length > 0) { |
| 44 | + next = result.categories.map((c) => c.category); |
| 45 | + } |
| 46 | + } catch { |
| 47 | + // fall back to ['default'] |
| 48 | + } |
| 49 | + categories = next; |
| 50 | + if (!next.includes(selectedCategory)) { |
| 51 | + selectedCategory = next[0] ?? 'default'; |
| 52 | + } |
| 53 | + })(); |
| 54 | + }); |
| 55 | +
|
32 | 56 | const badgeEndpointUrl = $derived( |
33 | | - `${window.location.origin}/api/badge/${owner}/${repo}/${selectedMetric}.json`, |
| 57 | + `${window.location.origin}/api/badge/${owner}/${repo}/${selectedMetric}.json${ |
| 58 | + selectedCategory !== 'default' ? `?category=${encodeURIComponent(selectedCategory)}` : '' |
| 59 | + }`, |
34 | 60 | ); |
35 | 61 | const shieldsUrl = $derived( |
36 | 62 | `https://img.shields.io/endpoint?url=${encodeURIComponent(badgeEndpointUrl)}`, |
37 | 63 | ); |
38 | | - const markdownSnippet = $derived(``); |
| 64 | + const badgeAltText = $derived( |
| 65 | + `${selectedMetric}${selectedCategory !== 'default' ? ` (${selectedCategory})` : ''} badge`, |
| 66 | + ); |
| 67 | + const markdownSnippet = $derived(``); |
39 | 68 | const rstSnippet = $derived(`.. image:: ${shieldsUrl}`); |
40 | 69 |
|
41 | 70 | let copiedField: string | null = $state(null); |
|
155 | 184 | <option value={m.value}>{m.label}</option> |
156 | 185 | {/each} |
157 | 186 | </select> |
| 187 | + |
| 188 | + <label for="badge-category-select" class="metric-label">Category</label> |
| 189 | + <select id="badge-category-select" class="metric-select" bind:value={selectedCategory}> |
| 190 | + {#each categories as cat (cat)} |
| 191 | + <option value={cat}>{cat}</option> |
| 192 | + {/each} |
| 193 | + </select> |
158 | 194 | </div> |
159 | 195 |
|
160 | 196 | <div class="badge-preview"> |
161 | 197 | {#if localBadgeEnabled} |
162 | | - <img src={shieldsUrl} alt="{selectedMetric} status badge" /> |
| 198 | + <img src={shieldsUrl} alt="{badgeAltText} status badge" /> |
163 | 199 | {:else} |
164 | 200 | <span class="badge-placeholder">badge preview unavailable</span> |
165 | 201 | {/if} |
|
358 | 394 | display: flex; |
359 | 395 | align-items: center; |
360 | 396 | gap: 10px; |
| 397 | + flex-wrap: wrap; |
361 | 398 | } |
362 | 399 |
|
363 | 400 | .metric-label { |
|
0 commit comments