Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727<script lang="ts"> import { resolve } from '$app/paths'; import { enhance } from '$app/forms'; import { CHART_DAYS, MAX_TARGET, WEEKDAY_NAMES, WEEK_ORDER, targetMet, writingDaysBetween, type ChartDay, type Progress } from '#lib/goal.ts'; import { dayOfWeek, formatWords, localDay, parseDay } from '#lib/words.ts';
/** * A goal belongs to one writer in one project, because `writing_day` counts * each writer's own words; a co-author in the same project has their own panel. * * Everything here is optional: a writer with no target sees today's words * alone, and the streak stays off until it is asked for. */ let { projectId, progress, failed, onclose, onretry, onchanged }: { projectId: string; /** Null until the first read lands. */ progress: Progress | null; /** The last progress read did not arrive, so the panel offers another. */ failed: boolean; onclose: () => void; onretry: () => void; /** A goal was saved, so the readout and the chart are out of date. */ onchanged: () => void; } = $props();
/* * Forms name the workspace route rather than posting to `?/goal`: a relative * action resolves against the current page, and the workspace URL carries a * scene. */ const goalAction = $derived(`${resolve('/p/[id]', { id: projectId })}?/goal`);
let target = $state(0); let daysOff = $state<number[]>([]); let streak = $state(false); let deadlineTarget = $state(0); let deadlineDay = $state(''); let initializedProject = $state<string | null>(null);
$effect(() => { if (initializedProject === projectId || !progress) return; target = progress.goal.target; daysOff = [...progress.goal.daysOff]; streak = progress.goal.streak; deadlineTarget = progress.goal.deadline?.target ?? 0; deadlineDay = progress.goal.deadline?.day ?? ''; initializedProject = projectId; });
let saving = $state(false); let message = $state<string | null>(null);
/** * The tenth above the tallest value is headroom: a target line drawn on the * plot's top edge reads as a border, not as a datum. */ const ceiling = $derived( Math.max(progress?.goal.target ?? 0, ...(progress?.recent ?? []).map((day) => day.words), 1) * 1.1 );
const targetLine = $derived( progress && progress.goal.target > 0 ? (progress.goal.target / ceiling) * 100 : null );
const recentTotal = $derived((progress?.recent ?? []).reduce((sum, day) => sum + day.words, 0));
const met = $derived(progress ? targetMet(progress.goal, progress.today) : false);
/** Both fields are needed before a deadline means anything. */ const deadlineValid = $derived(deadlineTarget > 0 && parseDay(deadlineDay) !== null);
/** An existing deadline keeps its count, so the preview starts from what is there. */ const deadlineRemaining = $derived( Math.max(0, deadlineTarget - (progress?.deadline?.added ?? 0)) );
/** The pace the fields would ask for, before the writer saves them. */ const deadlinePace = $derived.by(() => { if (!progress || !deadlineValid) return null;
const day = parseDay(deadlineDay);
if (!day) return null;
const writingDays = writingDaysBetween(progress.day, day, daysOff);
return writingDays > 0 && deadlineRemaining > 0 ? Math.ceil(deadlineRemaining / writingDays) : null; });
/** One decimal is finer than a 48px plot can show, and keeps the attribute short. */ function height(day: ChartDay): string { return `${((day.words / ceiling) * 100).toFixed(1)}%`; }
function describe(day: ChartDay): string { const words = day.words === 1 ? '1 word' : `${formatWords(day.words)} words`; const off = day.off ? ', day off' : '';
if (progress && day.day === progress.day) return `Today, ${words}${off}`;
// Noon UTC formatted in UTC, so the weekday cannot shift with the reader's // clock; the day string carries no time. const named = new Date(`${day.day}T12:00:00Z`).toLocaleDateString('en-GB', { weekday: 'long', day: 'numeric', month: 'long', timeZone: 'UTC' });
return `${named}, ${words}${off}`; }
/** A short date for the readout, fixed to UTC so the day cannot shift. */ function formatDeadline(day: string): string { return new Date(`${day}T12:00:00Z`).toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' }); }
function toggleDay(day: number, on: boolean) { daysOff = on ? [...daysOff.filter((value) => value !== day), day].sort((a, b) => a - b) : daysOff.filter((value) => value !== day); }</script>
<aside class="progress" aria-label="Progress"> <header class="head"> <h2>Progress</h2> <button type="button" class="close tap-area" onclick={onclose} aria-label="Close progress" >×</button > </header>
{#if failed} <p class="empty" role="alert"> Progress could not be loaded. <button class="btn btn-sm retry" type="button" onclick={onretry}>Try again</button> </p> {:else if !progress} <p class="empty" role="status">Reading your progress…</p> {:else} <dl class="readout"> <div> <dt class="label">Today</dt> <dd class="tnum"> {#if progress.goal.target > 0} {formatWords(progress.today)} / {formatWords(progress.goal.target)} {#if met}<span class="met">met</span>{/if} {:else} {formatWords(progress.today)} {/if} </dd> </div> {#if progress.deadline} <div class="deadline-readout"> <dt class="label">Deadline</dt> <dd class="tnum"> <span> {formatWords(progress.deadline.added)} / {formatWords(progress.deadline.target)} {#if progress.deadline.met} <span class="met">met</span> {:else if progress.deadline.missed} <span class="missed">passed</span> {/if} </span> <span class="deadline-note"> {#if progress.deadline.met} Met by {formatDeadline(progress.deadline.day)} {:else if progress.deadline.missed} Passed {formatDeadline(progress.deadline.day)} · {formatWords(progress.deadline.remaining)} short {:else if progress.deadline.pace === null} No writing days left before {formatDeadline(progress.deadline.day)} {:else} {progress.deadline.daysLeft} {progress.deadline.daysLeft === 1 ? 'writing day' : 'writing days'} left · {formatWords(progress.deadline.pace)}/day {/if} </span> </dd> </div> {/if} <div> <dt class="label">Last {CHART_DAYS} days</dt> <dd class="tnum">{formatWords(recentTotal)}</dd> </div> {#if progress.goal.streak} <div> <dt class="label">Streak</dt> <dd class="tnum"> {progress.runs.current} {progress.runs.current === 1 ? 'day' : 'days'} {#if progress.runs.best > progress.runs.current} <span class="best">best {progress.runs.best}</span> {/if} </dd> </div> {/if} </dl>
<!-- A fortnight of nothing is not a chart: fourteen empty columns read as a broken gauge, so the panel says it in a sentence instead. --> {#if recentTotal === 0} <p class="empty chart-empty">Nothing recorded in the last {CHART_DAYS} days.</p> {:else} <div class="chart-frame"> <div class="plot"> {#if targetLine !== null} <span class="target" style={`bottom: ${targetLine}%`} aria-hidden="true"></span> {/if} <ol class="chart"> {#each progress.recent as day (day.day)} <li class="column" title={describe(day)}> <span class="bar" style={`height: ${height(day)}`} aria-hidden="true"></span> <span class="visually-hidden">{describe(day)}</span> </li> {/each} </ol> </div>
<div class="spine" aria-hidden="true"> {#each progress.recent as day (day.day)} <span class="rule" class:off={day.off} class:today={day.day === progress.day}></span> {/each} </div>
<div class="axis" aria-hidden="true"> {#each progress.recent as day (day.day)} <span class="tick" class:today={day.day === progress.day} >{WEEKDAY_NAMES[dayOfWeek(day.day)].slice(0, 2)}</span > {/each} </div> </div> {/if}
<form class="goal" method="POST" action={goalAction} use:enhance={({ formData }) => { formData.set('day', localDay());
saving = true; message = null;
return async ({ result }) => { saving = false;
if (result.type === 'success') { message = 'Saved.'; onchanged(); return; }
message = result.type === 'failure' ? String( (result.data as { message?: string } | undefined)?.message ?? 'The goal was not saved.' ) : 'The goal was not saved. Try again in a moment.'; }; }} > <input type="hidden" name="day" />
<div class="row"> <label class="field-label" for="goal-target">Words a day</label> <input class="field" id="goal-target" name="target" type="number" min="0" max={MAX_TARGET} step="1" inputmode="numeric" bind:value={target} /> <p class="field-hint">Zero turns the target off.</p> </div>
<fieldset class="row deadline"> <legend class="field-label">Deadline</legend> <label class="field-label" for="goal-deadline-target">Words to add</label> <input class="field" id="goal-deadline-target" name="deadlineTarget" type="number" min="0" max={MAX_TARGET} step="1" inputmode="numeric" bind:value={deadlineTarget} /> <label class="field-label" for="goal-deadline-day">By</label> <input class="field" id="goal-deadline-day" name="deadlineDay" type="date" bind:value={deadlineDay} /> <p class="field-hint"> {#if deadlinePace !== null} About {formatWords(deadlinePace)} words a writing day. {:else if deadlineValid && deadlineRemaining === 0} The target is already met. {:else if deadlineValid} No writing days left before that date. {:else} Set both fields, or leave both empty. Words count from today. {/if} </p> </fieldset>
<fieldset class="row days"> <legend class="field-label">Days off</legend> <div class="day-grid"> {#each WEEK_ORDER as day (day)} <label class="day"> <input class="check" type="checkbox" name="daysOff" value={day} checked={daysOff.includes(day)} onchange={(event) => toggleDay(day, event.currentTarget.checked)} /> <span>{WEEKDAY_NAMES[day].slice(0, 3)}</span> </label> {/each} </div> <p class="field-hint">A day off does not break a streak.</p> </fieldset>
<label class="switch" for="goal-streak"> <input class="check" id="goal-streak" name="streak" type="checkbox" bind:checked={streak} /> <span>Show a streak</span> </label>
<button class="btn save" type="submit" disabled={saving}> {saving ? 'Saving…' : 'Save'} </button>
{#if message} <p class="status" role="status">{message}</p> {/if} </form>
<section class="counts"> <h3 class="label">What counts</h3> <p> Words you add while writing in a scene, counted by the date on your own clock. Deleting words never takes them off the day, and a day with no new words is not counted. Pasted words count, because a save cannot tell a paste from typing. Notes, plot cards, imports and a co-author's words do not count. A deadline counts the same words, from the day you set it. </p> </section> {/if}</aside>
<style> .progress { display: flex; flex-direction: column; min-height: 0; height: 100%; background: var(--dr-surface-secondary); border-left: 2px solid var(--dr-border-color-container); overflow-y: auto; }
.head { display: flex; align-items: center; justify-content: space-between; padding: var(--dr-space-md); border-bottom: 1px solid var(--dr-border-color-container); position: sticky; top: 0; background: var(--dr-surface-secondary); z-index: 1; }
h2 { margin: 0; font-family: var(--dr-font-heading); font-weight: 700; font-size: var(--dr-text-sm); letter-spacing: 0.08em; text-transform: uppercase; color: var(--dr-heading); }
.close { border: 0; background: transparent; color: var(--dr-text-secondary); font-size: 1.1rem; line-height: 1; cursor: pointer; padding: 0 0.25rem; }
.close:hover { color: var(--dr-text-primary); }
.empty { margin: 0; padding: var(--dr-space-md); font-size: var(--dr-text-xs); line-height: 1.5; color: var(--dr-text-muted); }
.retry { margin-top: var(--dr-space-sm); }
/* The readout is a machine's status block: a label and a figure per line. */ .readout { display: grid; gap: var(--dr-space-sm); margin: 0; padding: var(--dr-space-md); border-bottom: 1px solid var(--dr-border-color-container); }
.readout > div { display: flex; align-items: baseline; justify-content: space-between; gap: var(--dr-space-sm); /* A long deadline note must wrap inside the panel rather than widen it. */ min-width: 0; }
.readout dt { flex: none; color: var(--dr-text-muted); }
.readout dd { min-width: 0; margin: 0; font-size: var(--dr-text-base); color: var(--dr-text-primary); text-align: right; }
.met, .best, .missed { margin-left: 0.375rem; font-family: var(--dr-font-mono); font-size: var(--dr-text-xs); letter-spacing: 0; text-transform: none; }
.met { color: var(--dr-success); }
.best { color: var(--dr-text-muted); }
.missed { color: var(--dr-danger-text); }
/* The deadline's figure sits above its pace, so a long note does not push the label out of line. */ .deadline-readout dd { display: grid; justify-items: end; gap: 0.125rem; }
.deadline-note { font-family: var(--dr-font-mono); font-size: var(--fw-text-2xs); color: var(--dr-text-muted); text-align: right; }
/* * The chart is a bar gauge, not a sparkline: every column is a day at the same * scale, so a tall bar and a short one are a fact and not a trend. * * Three rows share one grid of columns: the plot, the spine of ticks, and the * weekday initials. They align because all three are a flex row with the same * gap and the same `flex: 1` children, so no width is written twice. */ .chart-frame { padding: var(--dr-space-md) var(--dr-space-md) var(--dr-space-sm); border-bottom: 1px solid var(--dr-border-color-container); }
.chart-empty { border-bottom: 1px solid var(--dr-border-color-container); }
.plot { position: relative; height: 48px; }
.chart { display: flex; align-items: flex-end; gap: 3px; margin: 0; padding: 0; height: 100%; list-style: none; }
.column { display: flex; align-items: flex-end; flex: 1; min-width: 0; height: 100%; }
.bar { display: block; width: 100%; background: var(--dr-text-secondary); }
.spine, .axis { display: flex; gap: 3px; }
.spine { margin-top: 2px; }
/* The day itself. An off day leaves a gap, so the spine breaks where the writer rests, and today takes the one warm mark in this panel. */ .rule { display: block; flex: 1; height: 2px; background: var(--dr-border-color-container); }
.rule.off { background: transparent; }
.rule.today { background: var(--dr-accent); }
.axis { margin-top: 0.25rem; }
.tick { flex: 1; min-width: 0; font-family: var(--dr-font-mono); font-size: var(--fw-text-2xs); text-align: center; color: var(--dr-text-muted); }
.tick.today { color: var(--dr-text-primary); }
/* * The target is a reference line across the gauge, not a fill. It takes a text * role rather than a wall colour, because it carries a value: a container wall * is quiet on purpose, and a datum cannot be. */ .target { position: absolute; left: 0; right: 0; /* Above the bars, so the line crosses a day that beat the target rather than stopping at its edge. */ z-index: 1; border-top: 1px dashed var(--dr-text-muted); pointer-events: none; }
.goal { display: flex; flex-direction: column; gap: var(--dr-space-md); padding: var(--dr-space-md); border-bottom: 1px solid var(--dr-border-color-container); }
.row { margin: 0; padding: 0; border: 0; }
/* A target is a number the writer reads back against the readout above, so it is set in the readout's own face rather than the prose face. The deadline's two fields are readouts for the same reason. */ #goal-target, #goal-deadline-target, #goal-deadline-day { font-family: var(--dr-font-mono); font-size: var(--dr-text-sm); }
.deadline { display: grid; gap: 0.375rem; }
.deadline .field-label { margin-bottom: 0; }
.days { min-width: 0; }
/* Whole labels take touch input, without overlapping checkbox overlays. */ .day-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--dr-space-xs) var(--dr-space-sm); margin-top: 0.375rem; }
.day { display: flex; align-items: center; gap: 0.375rem; font-family: var(--dr-font-mono); font-size: var(--dr-text-xs); color: var(--dr-text-secondary); cursor: pointer; }
@media (pointer: coarse) { .day { min-width: 44px; min-height: 44px; }
.day .check::after { content: none; } }
.switch { display: flex; align-items: center; gap: 0.5rem; font-size: var(--dr-text-sm); color: var(--dr-text-secondary); cursor: pointer; }
.save { width: 100%; }
.status { margin: 0; font-size: var(--dr-text-xs); color: var(--dr-text-muted); }
.counts { padding: var(--dr-space-md); }
.counts h3 { margin: 0 0 var(--dr-space-sm); color: var(--dr-text-muted); }
.counts p { margin: 0; font-size: var(--dr-text-xs); line-height: 1.6; /* This paragraph is the one thing a writer has to read to trust the numbers above it, so it takes the secondary ink rather than the muted one. */ color: var(--dr-text-secondary); }</style>