|
| 1 | +<script lang="ts"> |
| 2 | + import { LayerCake, Svg } from 'layercake'; |
| 3 | + import { |
| 4 | + GROUP_NO_SENDER_BUCKET_KEY, |
| 5 | + type GroupSenderRow |
| 6 | + } from '$lib/services/evaultService'; |
| 7 | + import GroupDonutArcs from './GroupDonutArcs.svelte'; |
| 8 | +
|
| 9 | + interface Props { |
| 10 | + senderRows: GroupSenderRow[]; |
| 11 | + /** Group route param; used for per-sender message list links. */ |
| 12 | + groupEvaultId: string; |
| 13 | + } |
| 14 | + let { senderRows, groupEvaultId }: Props = $props(); |
| 15 | +
|
| 16 | + /** When false (default), system / no-sender bucket is omitted from the donut and legend. */ |
| 17 | + let showSystemNoSender = $state(false); |
| 18 | +
|
| 19 | + function isSystemNoSenderRow(r: GroupSenderRow): boolean { |
| 20 | + return ( |
| 21 | + r.bucketKey === GROUP_NO_SENDER_BUCKET_KEY || |
| 22 | + r.ename === '—' || |
| 23 | + r.displayName.trim().toLowerCase() === 'system / no sender' |
| 24 | + ); |
| 25 | + } |
| 26 | +
|
| 27 | + type DonutSlice = { |
| 28 | + id: string; |
| 29 | + label: string; |
| 30 | + sub: string; |
| 31 | + value: number; |
| 32 | + color: string; |
| 33 | + bucketKey: string; |
| 34 | + evaultPageId: string | null; |
| 35 | + }; |
| 36 | +
|
| 37 | + const PALETTE = [ |
| 38 | + '#2563eb', |
| 39 | + '#7c3aed', |
| 40 | + '#0d9488', |
| 41 | + '#059669', |
| 42 | + '#d97706', |
| 43 | + '#dc2626', |
| 44 | + '#db2777', |
| 45 | + '#4f46e5', |
| 46 | + '#ea580c', |
| 47 | + '#0891b2' |
| 48 | + ]; |
| 49 | +
|
| 50 | + const slices = $derived.by((): DonutSlice[] => { |
| 51 | + const rows = senderRows.filter((r) => { |
| 52 | + if (!showSystemNoSender && isSystemNoSenderRow(r)) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + return r.messageCount > 0; |
| 56 | + }); |
| 57 | + return rows.map((r, i) => ({ |
| 58 | + id: `${i}-${r.bucketKey}`, |
| 59 | + label: r.displayName, |
| 60 | + sub: r.ename === '—' ? 'No sender' : r.ename, |
| 61 | + value: r.messageCount, |
| 62 | + color: PALETTE[i % PALETTE.length], |
| 63 | + bucketKey: r.bucketKey, |
| 64 | + evaultPageId: r.evaultPageId |
| 65 | + })); |
| 66 | + }); |
| 67 | +
|
| 68 | + const totalMessages = $derived(slices.reduce((s, x) => s + x.value, 0)); |
| 69 | +
|
| 70 | + const messagesListHref = (bucketKey: string) => |
| 71 | + `/groups/${encodeURIComponent(groupEvaultId)}/messages?bucket=${encodeURIComponent(bucketKey)}`; |
| 72 | +</script> |
| 73 | + |
| 74 | +<div class="rounded-lg border border-gray-200 bg-white p-5 shadow-sm"> |
| 75 | + <h2 class="text-lg font-semibold text-gray-900">Contribution by sender</h2> |
| 76 | + <p class="mt-1 text-sm text-gray-500"> |
| 77 | + Share of scanned messages per sender (same counts as the table below) |
| 78 | + </p> |
| 79 | + |
| 80 | + {#if senderRows.some((r) => isSystemNoSenderRow(r) && r.messageCount > 0)} |
| 81 | + <label |
| 82 | + class="mt-4 flex cursor-pointer items-center gap-2 text-sm text-gray-700 select-none" |
| 83 | + > |
| 84 | + <input |
| 85 | + type="checkbox" |
| 86 | + bind:checked={showSystemNoSender} |
| 87 | + class="h-4 w-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500" |
| 88 | + /> |
| 89 | + <span>Show system / no sender messages in chart</span> |
| 90 | + </label> |
| 91 | + {/if} |
| 92 | + |
| 93 | + {#if slices.length === 0} |
| 94 | + <p class="mt-6 text-center text-sm text-gray-500">No sender data to chart.</p> |
| 95 | + {:else} |
| 96 | + <div class="mt-6 flex flex-col gap-6 lg:flex-row lg:items-center lg:gap-10"> |
| 97 | + <div |
| 98 | + class="layercake-donut h-[min(22rem,55vw)] min-h-[220px] w-full max-w-[22rem] shrink-0" |
| 99 | + > |
| 100 | + <LayerCake |
| 101 | + ssr={true} |
| 102 | + data={slices} |
| 103 | + x="id" |
| 104 | + y="value" |
| 105 | + xDomain={slices.map((s) => s.id)} |
| 106 | + yDomain={[0, Math.max(...slices.map((s) => s.value), 1)]} |
| 107 | + padding={{ top: 8, right: 8, bottom: 8, left: 8 }} |
| 108 | + > |
| 109 | + <Svg label="Donut chart of messages per sender"> |
| 110 | + <GroupDonutArcs {groupEvaultId} /> |
| 111 | + </Svg> |
| 112 | + </LayerCake> |
| 113 | + </div> |
| 114 | + |
| 115 | + <ul class="min-w-0 flex-1 space-y-2 text-sm"> |
| 116 | + {#each slices as s (s.id)} |
| 117 | + {@const pct = |
| 118 | + totalMessages > 0 ? Math.round((s.value / totalMessages) * 1000) / 10 : 0} |
| 119 | + <li class="flex items-start gap-3"> |
| 120 | + <span |
| 121 | + class="mt-1.5 h-3 w-3 shrink-0 rounded-sm ring-1 ring-gray-200" |
| 122 | + style:background-color={s.color} |
| 123 | + aria-hidden="true" |
| 124 | + ></span> |
| 125 | + <div class="min-w-0 flex-1"> |
| 126 | + <div class="font-medium text-gray-900"> |
| 127 | + {#if s.evaultPageId} |
| 128 | + <a |
| 129 | + href="/evaults/{encodeURIComponent(s.evaultPageId)}" |
| 130 | + class="text-blue-600 hover:underline">{s.label}</a> |
| 131 | + {:else} |
| 132 | + {s.label} |
| 133 | + {/if} |
| 134 | + </div> |
| 135 | + <div class="truncate font-mono text-xs text-gray-500">{s.sub}</div> |
| 136 | + </div> |
| 137 | + <div class="shrink-0 text-right text-gray-700 tabular-nums"> |
| 138 | + <a href={messagesListHref(s.bucketKey)} class="text-blue-600 hover:underline" |
| 139 | + >{s.value}</a> |
| 140 | + <span class="text-gray-400">({pct}%)</span> |
| 141 | + </div> |
| 142 | + </li> |
| 143 | + {/each} |
| 144 | + </ul> |
| 145 | + </div> |
| 146 | + {/if} |
| 147 | +</div> |
| 148 | + |
| 149 | +<style> |
| 150 | + .layercake-donut :global(.layercake-container) { |
| 151 | + width: 100%; |
| 152 | + height: 100%; |
| 153 | + } |
| 154 | +</style> |
0 commit comments