first commit
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
This commit is contained in:
commit
6fc0a01c56
51 changed files with 11319 additions and 0 deletions
467
src/Settings.tsx
Normal file
467
src/Settings.tsx
Normal file
|
|
@ -0,0 +1,467 @@
|
|||
import { useState } from "react";
|
||||
import {
|
||||
Download,
|
||||
Check,
|
||||
LogOut,
|
||||
ShieldCheck,
|
||||
Smartphone,
|
||||
Cloud,
|
||||
RefreshCw,
|
||||
Sun,
|
||||
Moon,
|
||||
Monitor,
|
||||
ArrowUpRight,
|
||||
} from "lucide-react";
|
||||
import type { JournalRecord, Metric, Settings as SettingsData } from "./types";
|
||||
import { metrics } from "./types";
|
||||
import { downloadExport, offsetDay, today } from "./utils";
|
||||
import { logout, saveRecord, sync, useSyncStatus } from "./store";
|
||||
import { accentPresets, DEFAULT_ACCENT } from "../shared/theme.js";
|
||||
|
||||
export function Settings({
|
||||
settings,
|
||||
records,
|
||||
pending,
|
||||
lastSync,
|
||||
notify,
|
||||
install,
|
||||
update,
|
||||
}: {
|
||||
settings: SettingsData;
|
||||
records: JournalRecord[];
|
||||
pending: number;
|
||||
lastSync: string | null;
|
||||
notify: (text: string) => void;
|
||||
install: (() => void) | null;
|
||||
update: (() => void) | null;
|
||||
}) {
|
||||
const [draft, setDraft] = useState(settings);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [format, setFormat] = useState(settings.exportFormat);
|
||||
const [range, setRange] = useState(settings.exportDays);
|
||||
const [start, setStart] = useState(
|
||||
offsetDay(
|
||||
today(),
|
||||
1 - Number(settings.exportDays === "all" ? 30 : settings.exportDays),
|
||||
),
|
||||
);
|
||||
const [end, setEnd] = useState(today());
|
||||
const [selected, setSelected] = useState<Metric[]>(
|
||||
settings.exportMetrics ||
|
||||
metrics.filter((m) => settings.goals[m.key].enabled).map((m) => m.key),
|
||||
);
|
||||
const [includeWeight, setIncludeWeight] = useState(
|
||||
settings.exportWeight ?? true,
|
||||
);
|
||||
const [error, setError] = useState("");
|
||||
const status = useSyncStatus();
|
||||
const effectiveStart =
|
||||
range === "all"
|
||||
? records.reduce((value, r) => {
|
||||
const date = (r.data as { date?: string }).date;
|
||||
return date && date < value ? date : value;
|
||||
}, today())
|
||||
: start;
|
||||
const effectiveEnd =
|
||||
range === "all"
|
||||
? records.reduce((value, r) => {
|
||||
const date = !r.deleted && (r.data as { date?: string }).date;
|
||||
return date && date > value ? date : value;
|
||||
}, today())
|
||||
: end;
|
||||
async function save(event: React.FormEvent) {
|
||||
event.preventDefault();
|
||||
setBusy(true);
|
||||
setError("");
|
||||
try {
|
||||
await saveRecord(
|
||||
"settings",
|
||||
{
|
||||
...draft,
|
||||
exportFormat: format,
|
||||
exportDays: range,
|
||||
exportMetrics: selected,
|
||||
exportWeight: includeWeight,
|
||||
},
|
||||
"settings",
|
||||
);
|
||||
notify("Your preferences are saved");
|
||||
} catch {
|
||||
setError("Could not save. Check that each goal is a positive number.");
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<div className="page-heading">
|
||||
<div>
|
||||
<span className="eyebrow">MAKE ROOM FOR YOU</span>
|
||||
<h1>
|
||||
Your way<span>.</span>
|
||||
</h1>
|
||||
<p>A journal that fits your everyday.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="settings-grid">
|
||||
<form className="card settings-card" onSubmit={save}>
|
||||
<div className="card-title">
|
||||
<div>
|
||||
<span className="eyebrow">FOCUS ON WHAT MATTERS</span>
|
||||
<h2>Daily goals</h2>
|
||||
</div>
|
||||
<span className="tiny-badge">PERSONAL</span>
|
||||
</div>
|
||||
<p className="muted">
|
||||
Choose what you track. Turning a goal off keeps your past nutrition
|
||||
data.
|
||||
</p>
|
||||
<div className="goal-head">
|
||||
<span>Track</span>
|
||||
<span>Daily target</span>
|
||||
</div>
|
||||
{metrics.map((m) => (
|
||||
<div className="goal-row" key={m.key}>
|
||||
<label className="toggle-label">
|
||||
<input
|
||||
className="switch"
|
||||
type="checkbox"
|
||||
checked={draft.goals[m.key].enabled}
|
||||
onChange={(e) =>
|
||||
setDraft({
|
||||
...draft,
|
||||
goals: {
|
||||
...draft.goals,
|
||||
[m.key]: {
|
||||
...draft.goals[m.key],
|
||||
enabled: e.target.checked,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
/>
|
||||
<span>{m.label}</span>
|
||||
</label>
|
||||
<label className="unit-input goal-input">
|
||||
<input
|
||||
aria-label={`${m.label} daily goal`}
|
||||
type="number"
|
||||
inputMode="decimal"
|
||||
step="any"
|
||||
min="0.1"
|
||||
max="100000"
|
||||
required
|
||||
value={draft.goals[m.key].target}
|
||||
onChange={(e) =>
|
||||
setDraft({
|
||||
...draft,
|
||||
goals: {
|
||||
...draft.goals,
|
||||
[m.key]: {
|
||||
...draft.goals[m.key],
|
||||
target:
|
||||
e.target.value === ""
|
||||
? ("" as unknown as number)
|
||||
: +e.target.value,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
/>
|
||||
<span>{m.unit}</span>
|
||||
</label>
|
||||
</div>
|
||||
))}
|
||||
<div className="settings-section">
|
||||
<h3>Look & feel</h3>
|
||||
<div className="segmented theme-control">
|
||||
{(
|
||||
[
|
||||
{ key: "light", icon: Sun },
|
||||
{ key: "dark", icon: Moon },
|
||||
{ key: "system", icon: Monitor },
|
||||
] as const
|
||||
).map(({ key, icon: Icon }) => (
|
||||
<button
|
||||
key={key}
|
||||
type="button"
|
||||
className={draft.theme === key ? "active" : ""}
|
||||
onClick={() => setDraft({ ...draft, theme: key })}
|
||||
>
|
||||
<Icon size={16} />
|
||||
{key[0].toUpperCase() + key.slice(1)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div className="accent-preference">
|
||||
<span className="field-caption">ACCENT COLOR</span>
|
||||
<div className="accent-swatches">
|
||||
{accentPresets.map((preset) => (
|
||||
<button
|
||||
type="button"
|
||||
key={preset.color}
|
||||
aria-label={`${preset.name} accent`}
|
||||
aria-pressed={
|
||||
(draft.accentColor || DEFAULT_ACCENT).toLowerCase() ===
|
||||
preset.color
|
||||
}
|
||||
onClick={() =>
|
||||
setDraft({ ...draft, accentColor: preset.color })
|
||||
}
|
||||
>
|
||||
<span style={{ background: preset.color }} />
|
||||
{(draft.accentColor || DEFAULT_ACCENT).toLowerCase() ===
|
||||
preset.color && <Check size={15} />}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<label className="custom-accent">
|
||||
<span>Custom color</span>
|
||||
<code>
|
||||
{(draft.accentColor || DEFAULT_ACCENT).toUpperCase()}
|
||||
</code>
|
||||
<input
|
||||
aria-label="Custom accent color"
|
||||
type="color"
|
||||
value={draft.accentColor || DEFAULT_ACCENT}
|
||||
onChange={(e) =>
|
||||
setDraft({ ...draft, accentColor: e.target.value })
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div className="preference-row">
|
||||
<label htmlFor="weight-unit">Weight unit</label>
|
||||
<select
|
||||
id="weight-unit"
|
||||
value={draft.unit}
|
||||
onChange={(e) =>
|
||||
setDraft({ ...draft, unit: e.target.value as "kg" | "lb" })
|
||||
}
|
||||
>
|
||||
<option value="lb">Pounds (lb)</option>
|
||||
<option value="kg">Kilograms (kg)</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
{error && (
|
||||
<p role="alert" className="error">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<button className="primary wide" disabled={busy}>
|
||||
<Check size={18} />
|
||||
Save preferences
|
||||
</button>
|
||||
</form>
|
||||
<div className="settings-stack">
|
||||
<section className="card settings-card">
|
||||
<div className="card-title">
|
||||
<div>
|
||||
<span className="eyebrow">YOUR DATA, TO GO</span>
|
||||
<h2>Export your journal</h2>
|
||||
</div>
|
||||
<Download size={23} className="muted" />
|
||||
</div>
|
||||
<p className="muted">
|
||||
A clear, portable record for your own analysis or a conversation
|
||||
with an LLM.
|
||||
</p>
|
||||
<div className="form-row">
|
||||
<label>
|
||||
Format
|
||||
<select
|
||||
value={format}
|
||||
onChange={(e) => setFormat(e.target.value as typeof format)}
|
||||
>
|
||||
<option value="markdown">Markdown · LLM friendly</option>
|
||||
<option value="csv">CSV · spreadsheet</option>
|
||||
<option value="json">JSON · structured</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
Date range
|
||||
<select
|
||||
value={range}
|
||||
onChange={(e) => {
|
||||
const value = e.target.value as typeof range;
|
||||
setRange(value);
|
||||
if (value !== "all") {
|
||||
setStart(offsetDay(today(), 1 - +value));
|
||||
setEnd(today());
|
||||
}
|
||||
}}
|
||||
>
|
||||
<option value="7">Last 7 days</option>
|
||||
<option value="30">Last 30 days</option>
|
||||
<option value="90">Last 90 days</option>
|
||||
<option value="all">All time</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
{range !== "all" && (
|
||||
<div className="form-row">
|
||||
<label>
|
||||
From
|
||||
<input
|
||||
type="date"
|
||||
required
|
||||
value={start}
|
||||
max={end}
|
||||
onChange={(e) => setStart(e.target.value)}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Through
|
||||
<input
|
||||
type="date"
|
||||
required
|
||||
value={end}
|
||||
min={start}
|
||||
onChange={(e) => setEnd(e.target.value)}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
)}
|
||||
<span className="field-caption">INCLUDE IN EXPORT</span>
|
||||
<div className="export-options">
|
||||
{metrics.map((m) => (
|
||||
<label className="check-row" key={m.key}>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.includes(m.key)}
|
||||
onChange={(e) =>
|
||||
setSelected(
|
||||
e.target.checked
|
||||
? [...selected, m.key]
|
||||
: selected.filter((v) => v !== m.key),
|
||||
)
|
||||
}
|
||||
/>
|
||||
{m.label}
|
||||
</label>
|
||||
))}
|
||||
<label className="check-row">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={includeWeight}
|
||||
onChange={(e) => setIncludeWeight(e.target.checked)}
|
||||
/>
|
||||
Weight
|
||||
</label>
|
||||
</div>
|
||||
<button
|
||||
className="secondary wide"
|
||||
disabled={
|
||||
!effectiveStart ||
|
||||
!effectiveEnd ||
|
||||
effectiveStart > effectiveEnd ||
|
||||
(!selected.length && !includeWeight)
|
||||
}
|
||||
onClick={() => {
|
||||
downloadExport(
|
||||
records,
|
||||
settings,
|
||||
effectiveStart,
|
||||
effectiveEnd,
|
||||
selected,
|
||||
includeWeight,
|
||||
format,
|
||||
pending,
|
||||
);
|
||||
notify("Export downloaded");
|
||||
}}
|
||||
>
|
||||
<Download size={17} />
|
||||
Download export
|
||||
</button>
|
||||
<p className="small muted">
|
||||
Includes locally saved changes. No data is sent to an LLM.
|
||||
</p>
|
||||
</section>
|
||||
<section className="card settings-card">
|
||||
<div className="card-title">
|
||||
<div>
|
||||
<span className="eyebrow">READY WHEN YOU ARE</span>
|
||||
<h2>On your devices</h2>
|
||||
</div>
|
||||
<Smartphone size={22} className="muted" />
|
||||
</div>
|
||||
<div className="status-detail">
|
||||
<Cloud size={22} />
|
||||
<div>
|
||||
<strong>
|
||||
{pending
|
||||
? `${pending} change${pending === 1 ? "" : "s"} waiting to sync`
|
||||
: "Your journal is saved"}
|
||||
<span className="status-dot" />
|
||||
</strong>
|
||||
<small>
|
||||
{lastSync
|
||||
? `Last synced ${new Date(lastSync).toLocaleString()}`
|
||||
: "Your first sync will prepare your offline journal."}
|
||||
</small>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
className="secondary wide"
|
||||
disabled={status.busy || !status.online}
|
||||
onClick={() => void sync()}
|
||||
>
|
||||
<RefreshCw size={16} className={status.busy ? "spin" : ""} />
|
||||
{status.busy ? "Syncing…" : "Sync now"}
|
||||
</button>
|
||||
<p className="small muted">
|
||||
Your synced journal and new entries stay on this device for
|
||||
offline use. Open Calio while online before heading out; sync
|
||||
resumes when you return to the app.
|
||||
</p>
|
||||
{install ? (
|
||||
<button className="text-button" onClick={install}>
|
||||
Install Calio
|
||||
<ArrowUpRight size={16} />
|
||||
</button>
|
||||
) : (
|
||||
<p className="install-tip">
|
||||
<Smartphone size={18} />
|
||||
<span>
|
||||
On iPhone: open in Safari, tap Share, then{" "}
|
||||
<strong>Add to Home Screen</strong>. On Android: use your
|
||||
browser’s Install app menu.
|
||||
</span>
|
||||
</p>
|
||||
)}
|
||||
{update && (
|
||||
<button className="primary wide" onClick={update}>
|
||||
Update Calio
|
||||
<ArrowUpRight size={17} />
|
||||
</button>
|
||||
)}
|
||||
</section>
|
||||
<section className="account-row">
|
||||
<div>
|
||||
<ShieldCheck size={20} />
|
||||
<span>
|
||||
Your space. Your data.
|
||||
<small>Calio 0.1.0 · A personal nutrition journal</small>
|
||||
</span>
|
||||
</div>
|
||||
<button
|
||||
className="text-button"
|
||||
onClick={async () => {
|
||||
try {
|
||||
await logout();
|
||||
} catch (err) {
|
||||
notify((err as Error).message);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<LogOut size={16} />
|
||||
Sign out
|
||||
</button>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue