Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

1.7 KB
Raw Blame
<script lang="ts">
import { GutterCenter, Isolate } from 'components';
import BarChart from './BarChart.svelte';

type Props = {
	data: Record<string, number>;
	/** The data from the previous period, used to show deltas. */
	prevPeriodData?: Record<string, number>;
	title: string;
	links: Record<string, string>;
	/** If there is a page giving more information about a specific key. */
	keyLinkGenerator?: (key: string) => string;
	keyLabel: string;
};

let { data, prevPeriodData, title, links, keyLabel, keyLinkGenerator }: Props = $props();

let counts = $derived(data);
let prevPeriodCounts = $derived(prevPeriodData);

let entries = $derived(Object.entries(counts).toSorted(([_a, a], [_b, b]) => b - a));

let deltas = $derived(
	Object.fromEntries(
		entries.map(([key, val]) => {
			let prevVal = prevPeriodCounts ? (prevPeriodCounts[key] ?? 0) : 0;
			return [key, val - prevVal];
		}),
	),
);
</script>

<Isolate>
  <h1>{title}</h1>

  <div class="flex flex-row [&>a]:px-4">
    {#each Object.entries(links) as [name, href]}
      <a href={href}>{name}</a>
    {/each}
  </div>

  <GutterCenter >
    <BarChart data={counts} title={title}/>
    {#if prevPeriodCounts != null}
      <BarChart data={deltas} title="Change" />
    {/if}
    
    <table>
    	<thead>
    		<tr>
    			<th>{keyLabel}</th>
    			<th>Value</th>
    		</tr>
    	</thead>
    	<tbody>
        {#each entries as [key, value] }
    		  <tr>
    		  	<th scope="row">
              {#if keyLinkGenerator != null}
                <a href={keyLinkGenerator(key)}>
                  {key}
                </a>
              {:else}
                {key}
              {/if}
              </th>
    		  	<td>{value}</td>
    		  </tr>
        {/each}
    	</tbody>
    </table>
  </GutterCenter>
</Isolate>