Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(web): created dashboard for `stats.txt` files
Elijah Potterfb5a775eb359
diff --git a/packages/harper.js/src/Linter.ts b/packages/harper.js/src/Linter.tsindex 5002c7155..5727a3b0b 100644--- a/packages/harper.js/src/Linter.ts+++ b/packages/harper.js/src/Linter.ts@@ -1,4 +1,5 @@import type { Dialect, Lint, Suggestion } from 'harper-wasm';+import type Summary from './Summary';import type { BinaryModule } from './binary';import type { LintConfig, LintOptions } from './main';@@ -81,8 +82,8 @@ export default interface Linter {/** Summarize the linter's usage statistics.* You may optionally pass in a start and/or end time.*- * If so, the summary with only include data from a _after_ the start time but _before_ the end time. */- summarizeStats(start?: bigint, end?: bigint): Promise<any>;+ * If so, the summary with only include data from _after_ the start time but _before_ the end time. */+ summarizeStats(start?: bigint, end?: bigint): Promise<Summary>;/** Generate a statistics log file you can save to permanent storage. */generateStatsFile(): Promise<string>;diff --git a/packages/harper.js/src/Summary.ts b/packages/harper.js/src/Summary.tsnew file mode 100644index 000000000..b32f450c8--- /dev/null+++ b/packages/harper.js/src/Summary.ts@@ -0,0 +1,21 @@+/**+ * Represents the summary of linting results.+ */+export default interface Summary {+ /**+ * An object mapping each lint type to its count.+ * Example: `{ "Spelling": 4, "Capitalization": 1 }`+ */+ lint_counts: Record<string, number>;++ /**+ * The total number of fixes applied.+ */+ total_applied: number;++ /**+ * An object mapping misspelled words to their occurrence counts.+ * Example: `{ "mispelled": 1, "mispell": 1, "thigs": 2 }`+ */+ misspelled: Record<string, number>;+}diff --git a/packages/harper.js/src/main.ts b/packages/harper.js/src/main.tsindex ad38d5d94..7ff15d1b3 100644--- a/packages/harper.js/src/main.ts+++ b/packages/harper.js/src/main.ts@@ -13,7 +13,7 @@ export {binaryInlined,isSerializedRequest,} from './binary';-+export type { default as Summary } from './Summary';/** A linting rule configuration dependent on upstream Harper's available rules.* This is a record, since you shouldn't hard-code the existence of any particular rules and should generalize based on this struct. */export type LintConfig = Record<string, boolean | undefined>;diff --git a/packages/web/package.json b/packages/web/package.jsonindex e507908d8..86838c4b5 100644--- a/packages/web/package.json+++ b/packages/web/package.json@@ -23,8 +23,8 @@"svelte-check": "^4.1.5","tailwindcss": "^3.3.3","tslib": "catalog:",- "vite": "^6.1.0","typescript": "catalog:",+ "vite": "^6.1.0","vite-plugin-pwa": "^0.21.1","vite-plugin-top-level-await": "^1.5.0","vite-plugin-wasm": "^3.4.1"@@ -33,6 +33,7 @@"dependencies": {"@sveltepress/theme-default": "^5.0.7","@sveltepress/vite": "^1.1.5",+ "chart.js": "^4.4.8","harper.js": "workspace:*","lodash-es": "^4.17.21","reveal.js": "^5.1.0",diff --git a/packages/web/src/lib/LintChart.svelte b/packages/web/src/lib/LintChart.sveltenew file mode 100644index 000000000..734a7cdac--- /dev/null+++ b/packages/web/src/lib/LintChart.svelte@@ -0,0 +1,113 @@+<script lang="ts">+import lintKindColor from '$lib/lintKindColor';+import Chart from 'chart.js/auto';+import { onMount } from 'svelte';++// Receive lint counts from the parent component+export let lintCounts: Record<string, number> = {};++let chartCanvas: HTMLCanvasElement;+let lintChart: Chart | null = null;++// Update the chart data with new lint counts+function updateChart() {+ if (lintChart) {+ lintChart.data.labels = Object.keys(lintCounts);+ lintChart.data.datasets[0].data = Object.values(lintCounts);+ lintChart.update();+ }+}++onMount(() => {+ // Create a new Chart.js bar chart on mount+ lintChart = new Chart(chartCanvas, {+ type: 'bar',+ data: {+ labels: Object.keys(lintCounts),+ datasets: [+ {+ label: 'Number of Corrections Applied',+ data: Object.values(lintCounts),+ backgroundColor: Object.keys(lintCounts).map(lintKindColor),+ borderColor: 'rgba(80, 80, 80, 1)',+ borderWidth: 2,+ borderRadius: 6, // Rounded corners+ barPercentage: 0.6, // Thicker bars+ },+ ],+ },+ options: {+ responsive: true,+ maintainAspectRatio: false,+ plugins: {+ title: {+ display: true,+ text: 'Most Common Kinds of Corrections',+ color: '#444', // Dark gray text+ font: {+ size: 18,+ weight: 'bold',+ },+ },+ legend: {+ display: false,+ },+ },+ scales: {+ x: {+ grid: {+ color: '#ddd',+ },+ ticks: {+ color: '#333',+ font: {+ size: 14,+ },+ },+ },+ y: {+ beginAtZero: true,+ grid: {+ color: '#ddd',+ },+ ticks: {+ stepSize: 1,+ color: '#333',+ font: {+ size: 14,+ },+ },+ },+ },+ },+ });+});++// Whenever lintCounts changes, update the chart+$: if (lintChart) {+ updateChart();+}+</script>++<style>+ /* Wrap the chart in a container to control layout and background */+ .chart-container {+ background: #f9f9f9; /* Subtle off-white background */+ border: 1px solid #ccc; /* Light gray border */+ border-radius: 8px; /* Rounded corners */+ padding: 1rem;+ width: 100%;+ max-width: 700px; /* Adjust as needed */+ height: 400px; /* Fixed height for the chart area */+ margin: 0 auto; /* Center horizontally */+ }++ canvas {+ width: 100%;+ height: 100%;+ }+</style>++<div class="chart-container">+ <canvas bind:this={chartCanvas}></canvas>+</div>diff --git a/packages/web/src/routes/stats/+page.svelte b/packages/web/src/routes/stats/+page.sveltenew file mode 100644index 000000000..28a6d1c49--- /dev/null+++ b/packages/web/src/routes/stats/+page.svelte@@ -0,0 +1,68 @@+<script lang="ts">+import LintChart from '$lib/LintChart.svelte';+import {+ Table,+ TableBody,+ TableBodyCell,+ TableBodyRow,+ TableHead,+ TableHeadCell,+} from 'flowbite-svelte';+import { Fileupload } from 'flowbite-svelte';+import { type Summary, WorkerLinter, binary } from 'harper.js';++let linter = new WorkerLinter({ binary: binary });+let files = $state<FileList | undefined>();+let summary: Summary | undefined = $state();++$effect(() => {+ (async () => {+ if (files && files.length >= 1) {+ let file = files.item(0);++ let t = await file?.text();++ if (!t) {+ throw new Error('Unable to get text content.');+ }++ await linter.importStatsFile(t);+ summary = await linter.summarizeStats();+ }+ })();+});+</script>++{#if summary}+ <h1>Harper Statistics</h1>++ {#if summary.lint_counts}+ <h2>Most Common Kinds of Corrections</h2>+ <LintChart lintCounts={summary.lint_counts}/>+ {/if}++ {#if summary.total_applied}+ <p>In total, {summary.total_applied} corrections were applied.</p>+ {/if}++ {#if summary.misspelled}+ <h2>Most Misspelled Words</h2>+ <Table>+ <TableHead>+ <TableHeadCell>Word</TableHeadCell>+ <TableHeadCell># of Times Misspelled</TableHeadCell>+ </TableHead>+ <TableBody>+ {#each Object.entries(summary.misspelled) as [word, count]}+ <TableBodyRow>+ <TableBodyCell>{word}</TableBodyCell>+ <TableBodyCell>{count}</TableBodyCell>+ </TableBodyRow>+ {/each}+ </TableBody>+ </Table>+ {/if}+{:else}+ <p> Upload your `stats.txt` file to start reflecting on your authorship. </p>+ <Fileupload bind:files={files}/>+{/if}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 4b1d0b02d..241f72795 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -195,6 +195,9 @@ importers:'@sveltepress/vite':specifier: ^1.1.5version: 1.2.0(@sveltejs/[email protected](@sveltejs/[email protected]([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected])))([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected])))(@sveltejs/[email protected]([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected])))([email protected])([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))+ chart.js:+ specifier: ^4.4.8+ version: 4.4.8harper.js:specifier: workspace:*version: link:../harper.js@@ -1836,6 +1839,9 @@ packages:'@keyv/[email protected]':resolution: {integrity: sha512-qnEovoOp5Np2JDGonIDL6Ayihw0RhnRh6vxPuHo4RDn1UOzwEo4AeIfpL6UGIrsceWrCMiVPgwRjbHu4vYFc3g==}+ '@kurkle/[email protected]':+ resolution: {integrity: sha512-M5UknZPHRu3DEDWoipU6sE8PdkZ6Z/S+v4dD+Ke8IaNlpdSQah50lz1KtcFBa2vsdOnwbbnxJwVM4wty6udA5w==}+'@leichtgewicht/[email protected]':resolution: {integrity: sha512-Vo+PSpZG2/fmgmiNzYK9qWRh8h/CHrwD0mo1h1DzL4yzHNSfWYujGTYsWGreD000gcgmZ7K4Ys6Tx9TxtsKdDw==}@@ -4457,6 +4463,10 @@ packages:resolution: {integrity: sha512-iBZ4F4wRbyORVsu0jPV7gXkOsGYjGHPmAyv+HiHG8gi5PtC9KI2j1+v8/tlibRvjoWX027ypmG/n0HtO5t7unw==}+ resolution: {integrity: sha512-IkGZlVpXP+83QpMm4uxEiGqSI7jFizwVtF3+n5Pc3k7sMO+tkd0qxh2OzLhenM0K80xtmAONWGBn082EiBQSDA==}+ engines: {pnpm: '>=8'}+resolution: {integrity: sha512-7UNH6QDgPHImF5TmmYH0mSpbnU5KhUT8Z+27hpW/fJM8VWnZpz6INh/olHmVPqFOUm3+lES4R8LwBLHrh9XJqg==}engines: {node: ^18.0.0 || ^20.0.0 || >=22.0.0}@@ -12778,6 +12788,8 @@ snapshots:dependencies:buffer: 6.0.3+ '@kurkle/[email protected]': {}+'@leichtgewicht/[email protected]': {}'@lezer/[email protected]': {}@@ -16556,6 +16568,10 @@ snapshots:+ dependencies:+ '@kurkle/color': 0.3.4+dependencies:'@types/js-yaml': 4.0.9