Native-feel polish: autosaving settings, stacked sheets, desktop type scale
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 28s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 28s
- Settings apply as they change; theme/accent preview instantly and a background sync no longer discards in-progress edits - Food journal stays open beneath edit/add/delete sheets - Toasts render in the top layer above open sheets, with an error tone - Follow the new day when the PWA resumes after midnight - Add-food sheet: day/time row, one-tap meal picker, blank nutrient fields - Library cards gain a Log action; compact rows on phones - Desktop type scale raised from 7-12px to 10-15px (desktop.css) - Hover styles limited to hover-capable devices; touch press dimming and no text selection on app chrome - Sheets slide opaque on the UIKit curve; drag-to-dismiss tracks 1:1, supports flicks and departs from the dragged position - Removed calorie bubbles fade out; no progress dot at 0 kcal - Friendly network/proxy error messages; fresh outbox check before update Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3c0d6560f1
commit
c3b9376508
16 changed files with 1043 additions and 290 deletions
366
src/Settings.tsx
366
src/Settings.tsx
|
|
@ -1,4 +1,4 @@
|
|||
import { useState } from "react";
|
||||
import { useEffect, useRef, useState, type InputHTMLAttributes } from "react";
|
||||
import {
|
||||
Download,
|
||||
Check,
|
||||
|
|
@ -16,9 +16,86 @@ 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";
|
||||
import {
|
||||
accentPalette,
|
||||
accentPresets,
|
||||
DEFAULT_ACCENT,
|
||||
} from "../shared/theme.js";
|
||||
import { useTilt } from "./useTilt";
|
||||
|
||||
// Edits a number locally and commits once it is valid: after a short pause, on
|
||||
// blur, or on Enter. External changes (sync) apply only while it is not focused.
|
||||
function NumberField({
|
||||
value,
|
||||
min,
|
||||
max,
|
||||
exclusiveMin = false,
|
||||
onCommit,
|
||||
...props
|
||||
}: {
|
||||
value: number;
|
||||
min: number;
|
||||
max: number;
|
||||
exclusiveMin?: boolean;
|
||||
onCommit: (value: number) => void;
|
||||
} & Omit<
|
||||
InputHTMLAttributes<HTMLInputElement>,
|
||||
"value" | "min" | "max" | "onChange"
|
||||
>) {
|
||||
const [text, setText] = useState(String(value));
|
||||
const focused = useRef(false);
|
||||
const timer = useRef(0);
|
||||
const latest = useRef({ text, value, onCommit });
|
||||
latest.current = { text, value, onCommit };
|
||||
useEffect(() => {
|
||||
if (!focused.current) setText(String(value));
|
||||
}, [value]);
|
||||
const valid = (raw: string) => {
|
||||
const n = Number(raw);
|
||||
return (
|
||||
raw.trim() !== "" &&
|
||||
Number.isFinite(n) &&
|
||||
(exclusiveMin ? n > min : n >= min) &&
|
||||
n <= max
|
||||
);
|
||||
};
|
||||
const commit = (final: boolean) => {
|
||||
clearTimeout(timer.current);
|
||||
const { text: raw, value: current, onCommit: save } = latest.current;
|
||||
if (valid(raw)) {
|
||||
if (Number(raw) !== current) save(Number(raw));
|
||||
if (final) setText(String(Number(raw)));
|
||||
} else if (final) setText(String(current));
|
||||
};
|
||||
useEffect(() => () => commit(false), []);
|
||||
return (
|
||||
<input
|
||||
{...props}
|
||||
type="number"
|
||||
min={exclusiveMin ? undefined : min}
|
||||
max={max}
|
||||
value={text}
|
||||
aria-invalid={!valid(text) || undefined}
|
||||
onFocus={(event) => {
|
||||
focused.current = true;
|
||||
event.currentTarget.select();
|
||||
}}
|
||||
onChange={(event) => {
|
||||
setText(event.target.value);
|
||||
clearTimeout(timer.current);
|
||||
timer.current = window.setTimeout(() => commit(false), 700);
|
||||
}}
|
||||
onBlur={() => {
|
||||
focused.current = false;
|
||||
commit(true);
|
||||
}}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === "Enter") event.currentTarget.blur();
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export function Settings({
|
||||
settings,
|
||||
records,
|
||||
|
|
@ -32,18 +109,29 @@ export function Settings({
|
|||
records: JournalRecord[];
|
||||
pending: number;
|
||||
lastSync: string | null;
|
||||
notify: (text: string) => void;
|
||||
notify: (text: string, error?: boolean) => void;
|
||||
install: (() => void) | null;
|
||||
update: (() => void) | null;
|
||||
}) {
|
||||
const [draft, setDraft] = useState({
|
||||
...settings,
|
||||
minimumAverageCalories:
|
||||
settings.minimumAverageCalories ??
|
||||
Math.round(settings.goals.calories.target / 2),
|
||||
});
|
||||
const tilt = useTilt();
|
||||
const [busy, setBusy] = useState(false);
|
||||
// Latest settings, including saves still being written, so quick successive
|
||||
// changes build on each other instead of on a stale render.
|
||||
const current = useRef(settings);
|
||||
const received = useRef(settings);
|
||||
if (received.current !== settings) {
|
||||
received.current = settings;
|
||||
current.current = settings;
|
||||
}
|
||||
const cutoff =
|
||||
settings.minimumAverageCalories ??
|
||||
Math.round(settings.goals.calories.target / 2);
|
||||
const [accentPreview, setAccentPreview] = useState<string | null>(null);
|
||||
const accentTimer = useRef(0);
|
||||
const accent = (
|
||||
accentPreview ||
|
||||
settings.accentColor ||
|
||||
DEFAULT_ACCENT
|
||||
).toLowerCase();
|
||||
const [format, setFormat] = useState(settings.exportFormat);
|
||||
const [range, setRange] = useState(settings.exportDays);
|
||||
const [start, setStart] = useState(
|
||||
|
|
@ -62,10 +150,43 @@ export function Settings({
|
|||
);
|
||||
const [error, setError] = useState("");
|
||||
const status = useSyncStatus();
|
||||
// While dragging the custom color, preview it before the debounced save.
|
||||
useEffect(() => {
|
||||
if (!accentPreview) return;
|
||||
const dark = document.documentElement.dataset.theme === "dark";
|
||||
for (const [key, value] of Object.entries(
|
||||
accentPalette(accentPreview, dark),
|
||||
))
|
||||
document.documentElement.style.setProperty(key, value);
|
||||
}, [accentPreview]);
|
||||
// Preferences apply as soon as they change, like iOS Settings. The averages
|
||||
// cutoff is stored in kcal so later calorie-goal changes do not move it.
|
||||
function persist(change: Partial<SettingsData>) {
|
||||
const base = current.current;
|
||||
const next = {
|
||||
...base,
|
||||
minimumAverageCalories:
|
||||
base.minimumAverageCalories ??
|
||||
Math.round(base.goals.calories.target / 2),
|
||||
...change,
|
||||
};
|
||||
current.current = next;
|
||||
setError("");
|
||||
saveRecord("settings", next, "settings").catch(() =>
|
||||
setError("Could not save that change. Please try again."),
|
||||
);
|
||||
}
|
||||
const setGoal = (key: Metric, change: Partial<SettingsData["goals"][Metric]>) =>
|
||||
persist({
|
||||
goals: {
|
||||
...current.current.goals,
|
||||
[key]: { ...current.current.goals[key], ...change },
|
||||
},
|
||||
});
|
||||
const effectiveStart =
|
||||
range === "all"
|
||||
? records.reduce((value, r) => {
|
||||
const date = (r.data as { date?: string }).date;
|
||||
const date = !r.deleted && (r.data as { date?: string }).date;
|
||||
return date && date < value ? date : value;
|
||||
}, today())
|
||||
: start;
|
||||
|
|
@ -76,29 +197,6 @@ export function Settings({
|
|||
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">
|
||||
|
|
@ -107,11 +205,11 @@ export function Settings({
|
|||
<h1>
|
||||
Settings<span>.</span>
|
||||
</h1>
|
||||
<p>Goals, appearance and exports.</p>
|
||||
<p>Goals, appearance and exports. Changes save automatically.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="settings-grid">
|
||||
<form className="card settings-card" onSubmit={save}>
|
||||
<section className="card settings-card">
|
||||
<div className="card-title">
|
||||
<div>
|
||||
<span className="eyebrow">FOCUS ON WHAT MATTERS</span>
|
||||
|
|
@ -133,47 +231,22 @@ export function Settings({
|
|||
<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,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
role="switch"
|
||||
checked={settings.goals[m.key].enabled}
|
||||
onChange={(e) => setGoal(m.key, { enabled: e.target.checked })}
|
||||
/>
|
||||
<span>{m.label}</span>
|
||||
</label>
|
||||
<label className="unit-input goal-input">
|
||||
<input
|
||||
<NumberField
|
||||
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,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
min={0}
|
||||
exclusiveMin
|
||||
max={100000}
|
||||
value={settings.goals[m.key].target}
|
||||
onCommit={(target) => setGoal(m.key, { target })}
|
||||
/>
|
||||
<span>{m.unit}</span>
|
||||
</label>
|
||||
|
|
@ -184,23 +257,15 @@ export function Settings({
|
|||
<label className="average-cutoff">
|
||||
Minimum calories for averages
|
||||
<span className="unit-input">
|
||||
<input
|
||||
<NumberField
|
||||
aria-label="Minimum calories for averages"
|
||||
type="number"
|
||||
inputMode="numeric"
|
||||
min="0"
|
||||
max="100000"
|
||||
min={0}
|
||||
max={100000}
|
||||
step="1"
|
||||
required
|
||||
value={draft.minimumAverageCalories}
|
||||
onChange={(event) =>
|
||||
setDraft({
|
||||
...draft,
|
||||
minimumAverageCalories:
|
||||
event.target.value === ""
|
||||
? ("" as unknown as number)
|
||||
: +event.target.value,
|
||||
})
|
||||
value={cutoff}
|
||||
onCommit={(value) =>
|
||||
persist({ minimumAverageCalories: Math.round(value) })
|
||||
}
|
||||
/>
|
||||
<span>kcal</span>
|
||||
|
|
@ -215,11 +280,70 @@ export function Settings({
|
|||
</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"
|
||||
aria-pressed={settings.theme === key}
|
||||
className={settings.theme === key ? "active" : ""}
|
||||
onClick={() => persist({ 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={accent === preset.color}
|
||||
onClick={() => {
|
||||
setAccentPreview(null);
|
||||
persist({ accentColor: preset.color });
|
||||
}}
|
||||
>
|
||||
<span style={{ background: preset.color }} />
|
||||
{accent === preset.color && <Check size={15} />}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<label className="custom-accent">
|
||||
<span>Custom color</span>
|
||||
<code>{accent.toUpperCase()}</code>
|
||||
<input
|
||||
aria-label="Custom accent color"
|
||||
type="color"
|
||||
value={accent}
|
||||
onChange={(e) => {
|
||||
const color = e.target.value;
|
||||
setAccentPreview(color);
|
||||
clearTimeout(accentTimer.current);
|
||||
accentTimer.current = window.setTimeout(() => {
|
||||
persist({ accentColor: color });
|
||||
setAccentPreview(null);
|
||||
}, 350);
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div className="preference-row" data-tilt-control>
|
||||
<label className="toggle-label">
|
||||
<input
|
||||
className="switch"
|
||||
type="checkbox"
|
||||
role="switch"
|
||||
checked={tilt.enabled}
|
||||
onChange={() => void tilt.toggleTilt()}
|
||||
/>
|
||||
|
|
@ -246,69 +370,13 @@ export function Settings({
|
|||
Allow motion
|
||||
</button>
|
||||
)}
|
||||
<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}
|
||||
value={settings.unit}
|
||||
onChange={(e) =>
|
||||
setDraft({ ...draft, unit: e.target.value as "kg" | "lb" })
|
||||
persist({ unit: e.target.value as "kg" | "lb" })
|
||||
}
|
||||
>
|
||||
<option value="lb">Pounds (lb)</option>
|
||||
|
|
@ -321,11 +389,7 @@ export function Settings({
|
|||
{error}
|
||||
</p>
|
||||
)}
|
||||
<button className="primary wide" disabled={busy}>
|
||||
<Check size={18} />
|
||||
Save preferences
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
<div className="settings-stack">
|
||||
<section className="card settings-card">
|
||||
<div className="card-title">
|
||||
|
|
@ -441,6 +505,12 @@ export function Settings({
|
|||
format,
|
||||
pending,
|
||||
);
|
||||
persist({
|
||||
exportFormat: format,
|
||||
exportDays: range,
|
||||
exportMetrics: selected,
|
||||
exportWeight: includeWeight,
|
||||
});
|
||||
notify("Export downloaded");
|
||||
}}
|
||||
>
|
||||
|
|
@ -524,7 +594,7 @@ export function Settings({
|
|||
try {
|
||||
await logout();
|
||||
} catch (err) {
|
||||
notify((err as Error).message);
|
||||
notify((err as Error).message, true);
|
||||
}
|
||||
}}
|
||||
>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue