Spaces:
Sleeping
Sleeping
File size: 6,555 Bytes
57ed4c2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | /** Home “Move now” checklist → Daily + optional Log prefill. */
import { useState } from "preact/hooks";
import {
ApiError,
getDaily,
putDaily,
type DailyUpsert,
} from "../api";
import { isoDate } from "../dates";
import { navigate } from "../router";
import { Button } from "./Button";
import { Pressable } from "./Pressable";
import { Sheet } from "./Sheet";
import { useToast } from "./Toast";
const EMPTY: DailyUpsert = {
primary_brick: "none",
brick_done: false,
corn_sessions: 0,
delay_ok: true,
daydream: "none",
rerun: "clean",
court: "closed",
stayed_indoors_all_day: false,
left_room: false,
left_home: false,
movement_minutes: 0,
interrupt_walk_minutes: 0,
fantasy_walk_minutes: 0,
headphones_on_walk: false,
music_cinematic_on_walk: false,
proof_brick_done: false,
proof_brick_kind: null,
fantasy_minutes_scheduled: 0,
fantasy_minutes_unplanned: 0,
note: "",
};
type Props = {
open: boolean;
onClose: () => void;
indoorsStreak?: number;
};
export function MoveNowSheet({ open, onClose, indoorsStreak = 0 }: Props) {
const toast = useToast();
const [leftBed, setLeftBed] = useState(false);
const [walletHome, setWalletHome] = useState(true);
const [walkKind, setWalkKind] = useState<"interrupt" | "fantasy">("interrupt");
const [minutes, setMinutes] = useState(20);
const [busy, setBusy] = useState(false);
const save = async (alsoLog: boolean) => {
setBusy(true);
const today = isoDate(new Date());
try {
let base: DailyUpsert = { ...EMPTY };
try {
const row = await getDaily(today);
base = {
primary_brick: row.primary_brick,
brick_done: row.brick_done,
corn_sessions: row.corn_sessions,
delay_ok: row.delay_ok,
daydream: row.daydream,
rerun: row.rerun,
court: row.court,
stayed_indoors_all_day: false,
left_room: leftBed || Boolean(row.left_room),
left_home: Boolean(row.left_home) || minutes >= 15,
movement_minutes: row.movement_minutes ?? 0,
interrupt_walk_minutes: row.interrupt_walk_minutes ?? 0,
fantasy_walk_minutes: row.fantasy_walk_minutes ?? 0,
headphones_on_walk:
walkKind === "interrupt" ? false : Boolean(row.headphones_on_walk),
music_cinematic_on_walk:
walkKind === "interrupt" ? false : Boolean(row.music_cinematic_on_walk),
proof_brick_done: true,
proof_brick_kind: "interrupt_walk",
fantasy_minutes_scheduled: row.fantasy_minutes_scheduled ?? 0,
fantasy_minutes_unplanned: row.fantasy_minutes_unplanned ?? 0,
note: row.note || "",
};
} catch {
base = {
...EMPTY,
left_room: leftBed,
left_home: minutes >= 15,
proof_brick_done: true,
proof_brick_kind: "interrupt_walk",
headphones_on_walk: walkKind !== "interrupt",
};
}
if (walkKind === "interrupt") {
base.interrupt_walk_minutes = Math.max(
base.interrupt_walk_minutes || 0,
minutes,
);
base.headphones_on_walk = false;
base.music_cinematic_on_walk = false;
} else {
base.fantasy_walk_minutes = Math.max(
base.fantasy_walk_minutes || 0,
minutes,
);
}
base.movement_minutes =
(base.interrupt_walk_minutes || 0) + (base.fantasy_walk_minutes || 0);
base.stayed_indoors_all_day = false;
await putDaily(today, base);
toast.show("Movement saved on Daily");
onClose();
if (alsoLog) {
const happened = encodeURIComponent(
walkKind === "interrupt"
? `Interrupt walk ${minutes} min — wallet home, first 15 no headphones`
: `Music / fantasy walk ${minutes} min — leisure, not medicine`,
);
navigate(`/log?happened=${happened}&remedy=${encodeURIComponent("Interrupt walk")}`);
}
} catch (err) {
toast.show(
err instanceof ApiError ? err.message : "Could not save movement",
"error",
);
} finally {
setBusy(false);
}
};
return (
<Sheet open={open} title="Move now" onClose={onClose}>
<div class="stack">
<p class="muted">
Preview, not proof. Interrupt walk: no headphones, eyes outside.
{indoorsStreak >= 2
? ` ${indoorsStreak} days indoors — 20 min out, wallet home, first 15 min no headphones.`
: ""}
</p>
<label class="toggle-row">
<input
type="checkbox"
checked={leftBed}
onChange={(e) => setLeftBed((e.target as HTMLInputElement).checked)}
/>
<span>Left bed / room</span>
</label>
<label class="toggle-row">
<input
type="checkbox"
checked={walletHome}
onChange={(e) => setWalletHome((e.target as HTMLInputElement).checked)}
/>
<span>Wallet stays home (no spend needed)</span>
</label>
<span class="field-label">Walk type</span>
<div class="chip-row" role="group" aria-label="Walk type">
<Pressable
className="chip"
ariaPressed={walkKind === "interrupt"}
onClick={() => setWalkKind("interrupt")}
>
Interrupt (medicine)
</Pressable>
<Pressable
className="chip"
ariaPressed={walkKind === "fantasy"}
onClick={() => setWalkKind("fantasy")}
>
Music walk (leisure)
</Pressable>
</div>
{walkKind === "interrupt" ? (
<p class="muted">Interrupt walk: no headphones, eyes outside.</p>
) : (
<p class="muted">Music walk: leisure, not medicine.</p>
)}
<label class="field-label" for="move-min">
Minutes · {minutes}
</label>
<input
id="move-min"
type="range"
min={5}
max={60}
step={5}
value={minutes}
onInput={(e) => setMinutes(Number((e.target as HTMLInputElement).value))}
/>
<Button disabled={busy || !leftBed} onClick={() => void save(false)}>
Save to Daily
</Button>
<Button
variant="secondary"
disabled={busy || !leftBed}
onClick={() => void save(true)}
>
Save + open Log
</Button>
</div>
</Sheet>
);
}
|