Spaces:
Sleeping
Sleeping
File size: 15,011 Bytes
c6253b2 9af2b22 c6253b2 9af2b22 c6253b2 9af2b22 c6253b2 fb29daa c6253b2 9af2b22 c6253b2 9af2b22 c6253b2 fb29daa c6253b2 9af2b22 c6253b2 fb29daa c6253b2 fb29daa c6253b2 9af2b22 c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 fb29daa c6253b2 | 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | /** Block detail + thick feedback sheet for schedule blocks. */
import { useEffect, useState } from "preact/hooks";
import {
addPlanBlock,
deletePlanBlock,
getScheduleTemplates,
postBlockFeedback,
type ScheduledBlock,
type ScheduleTemplate,
type TaskKind,
} from "../api";
import { Button } from "./Button";
import { Pressable } from "./Pressable";
import { Sheet } from "./Sheet";
import { useToast } from "./Toast";
const KIND_OPTIONS: { value: TaskKind; label: string }[] = [
{ value: "earn_ship", label: "Work / earn" },
{ value: "admin_spain", label: "Trip / admin" },
{ value: "body_care", label: "Body or room" },
{ value: "move_out", label: "Move-out step" },
{ value: "food_out", label: "Food" },
{ value: "stabilize", label: "Stabilize / reset" },
{ value: "explore", label: "Try something new" },
{ value: "restore_fun", label: "Fun recharge" },
{ value: "boundary", label: "Boundary practice" },
{ value: "sleep_window", label: "Sleep wind-down" },
{ value: "other", label: "Other" },
];
function addMinutes(hhmm: string, mins: number): string {
const [h, m] = hhmm.split(":").map(Number);
const total = h * 60 + m + mins;
const nh = Math.floor(((total % (24 * 60)) + 24 * 60) % (24 * 60) / 60);
const nm = ((total % (24 * 60)) + 24 * 60) % (24 * 60) % 60;
return `${String(nh).padStart(2, "0")}:${String(nm).padStart(2, "0")}`;
}
type Props = {
open: boolean;
day: string;
block: ScheduledBlock | null;
mode: "view" | "create";
onClose: () => void;
onChanged: () => void;
};
export function BlockSheet({ open, day, block, mode, onClose, onChanged }: Props) {
const toast = useToast();
const [did, setDid] = useState<"done" | "partial" | "skipped">("done");
const [actualMin, setActualMin] = useState(30);
const [quality, setQuality] = useState(3);
const [fun, setFun] = useState(3);
const [energy, setEnergy] = useState(0);
const [wouldRepeat, setWouldRepeat] = useState<"yes" | "no" | "maybe" | "">("");
const [skipReason, setSkipReason] = useState("");
const [note, setNote] = useState("");
const [money, setMoney] = useState("");
const [emotions, setEmotions] = useState<string[]>([]);
const [fseEvent, setFseEvent] = useState("");
const [intensity, setIntensity] = useState(5);
const [showThick, setShowThick] = useState(false);
const [busy, setBusy] = useState(false);
const [templates, setTemplates] = useState<ScheduleTemplate[]>([]);
const [title, setTitle] = useState("New block");
const [kind, setKind] = useState<TaskKind>("other");
const [start, setStart] = useState("09:00");
const [end, setEnd] = useState("09:30");
const [priority, setPriority] = useState<"P0" | "P1" | "P2">("P2");
const [intent, setIntent] = useState<ScheduledBlock["intent"]>("duty");
useEffect(() => {
if (!open) return;
if (mode === "create") {
void getScheduleTemplates()
.then((r) => setTemplates(r.items || []))
.catch(() => setTemplates([]));
}
if (mode === "view" && block) {
setActualMin(block.feedback?.actual_min ?? block.planned_min ?? 30);
setDid((block.feedback?.did as typeof did) || (block.status === "skipped" ? "skipped" : "done"));
setQuality(block.feedback?.quality ?? 3);
setFun(block.feedback?.fun ?? 3);
setEnergy(block.feedback?.energy_after ?? 0);
setWouldRepeat((block.feedback?.would_repeat as typeof wouldRepeat) || "");
setSkipReason(block.feedback?.skip_reason || "");
setNote(block.feedback?.note || "");
setMoney(block.feedback?.money_amount != null ? String(block.feedback.money_amount) : "");
setEmotions(block.feedback?.emotions || []);
setFseEvent(block.feedback?.fse_event || "");
setIntensity(block.feedback?.intensity ?? 5);
setShowThick(false);
}
if (mode === "create") {
setTitle("New block");
setKind("other");
setStart("09:00");
setEnd("09:30");
setPriority("P2");
setIntent("duty");
}
}, [open, mode, block?.id]);
const applyTemplate = (tmpl: ScheduleTemplate) => {
setTitle(tmpl.title);
setKind(tmpl.kind);
setPriority((tmpl.priority as "P0" | "P1" | "P2") || "P2");
setIntent(
(tmpl.intent as ScheduledBlock["intent"]) ||
(tmpl.kind === "explore"
? "explore"
: tmpl.kind === "restore_fun"
? "restore_fun"
: "duty"),
);
setEnd(addMinutes(start, tmpl.default_min || 30));
};
const saveFeedback = async () => {
if (!block) return;
setBusy(true);
try {
await postBlockFeedback(day, block.id, {
did,
actual_min: did === "skipped" ? null : actualMin,
quality: did === "skipped" || !showThick ? null : quality,
fun: did === "skipped" || !showThick ? null : fun,
energy_after: showThick ? energy : null,
money_amount: money ? Number(money) : null,
would_repeat: wouldRepeat || null,
skip_reason: did === "skipped" ? skipReason || "other" : null,
note,
emotions,
fse_event: fseEvent,
intensity: did === "skipped" ? null : intensity,
});
toast.show("Saved");
onChanged();
onClose();
} catch (e) {
toast.show(e instanceof Error ? e.message : "Save failed", "error");
} finally {
setBusy(false);
}
};
const createBlock = async () => {
setBusy(true);
try {
await addPlanBlock(day, {
title,
kind,
start,
end,
priority,
intent:
kind === "explore"
? "explore"
: kind === "restore_fun"
? "restore_fun"
: kind === "stabilize"
? "measure"
: intent,
locked: priority === "P0",
});
toast.show("Block added");
onChanged();
onClose();
} catch (e) {
toast.show(e instanceof Error ? e.message : "Could not add", "error");
} finally {
setBusy(false);
}
};
const removeBlock = async () => {
if (!block || !confirm("Remove this block?")) return;
setBusy(true);
try {
await deletePlanBlock(day, block.id);
toast.show("Removed");
onChanged();
onClose();
} catch (e) {
toast.show(e instanceof Error ? e.message : "Delete failed", "error");
} finally {
setBusy(false);
}
};
return (
<Sheet
open={open}
tall
title={mode === "create" ? "Add block" : block?.title || "Block"}
onClose={onClose}
>
{mode === "create" ? (
<div class="stack">
{templates.length > 0 && (
<>
<span class="field-label">From template</span>
<div class="choice-list">
{templates.slice(0, 8).map((tmpl) => (
<Pressable
key={tmpl.id}
className="choice-item"
onClick={() => applyTemplate(tmpl)}
>
{tmpl.title}
</Pressable>
))}
</div>
</>
)}
<label class="field-label">
Title
<input class="field-input" value={title} onInput={(e) => setTitle((e.target as HTMLInputElement).value)} />
</label>
<span class="field-label">Type</span>
<div class="choice-list">
{KIND_OPTIONS.map((opt) => (
<Pressable
key={opt.value}
className="choice-item"
ariaPressed={kind === opt.value}
onClick={() => setKind(opt.value)}
>
{opt.label}
</Pressable>
))}
</div>
<div class="plan-time-row">
<label class="field-label">
Start
<input class="field-input" type="time" value={start} onInput={(e) => setStart((e.target as HTMLInputElement).value)} />
</label>
<label class="field-label">
End
<input class="field-input" type="time" value={end} onInput={(e) => setEnd((e.target as HTMLInputElement).value)} />
</label>
</div>
<span class="field-label">Priority</span>
<div class="segment-row">
{(["P0", "P1", "P2"] as const).map((p) => (
<Pressable key={p} className="segment" ariaPressed={priority === p} onClick={() => setPriority(p)}>
{p === "P0" ? "Locked" : p === "P1" ? "Important" : "Flex"}
</Pressable>
))}
</div>
<div class="resolve-sticky">
<Button className="btn-large" disabled={busy} onClick={createBlock}>
{busy ? "Saving…" : "Add block"}
</Button>
</div>
</div>
) : block ? (
<div class="stack">
<p class="meta-line">
{block.start}–{block.end} · {block.label || block.kind} ·{" "}
{block.priority === "P0" ? "Locked" : block.priority}
{block.locked ? " · locked" : ""}
</p>
<span class="section-title">How did it go?</span>
<div class="segment-row">
{([
["done", "Done"],
["partial", "Partial"],
["skipped", "Skipped"],
] as const).map(([value, label]) => (
<Pressable
key={value}
className="segment"
ariaPressed={did === value}
onClick={() => setDid(value)}
>
{label}
</Pressable>
))}
</div>
{did !== "skipped" && (
<label class="field-label">
Actual minutes
<input
class="field-input"
type="number"
min={0}
max={24 * 60}
value={actualMin}
onInput={(e) => setActualMin(Number((e.target as HTMLInputElement).value))}
/>
</label>
)}
<label class="field-label">
Comment
<textarea
class="field-textarea"
value={note}
onInput={(e) => setNote((e.target as HTMLTextAreaElement).value)}
placeholder="What happened?"
/>
</label>
<span class="field-label">Emotions</span>
<div class="chip-row" role="group">
{["calm", "shame", "urge", "anxiety", "anger", "lonely", "tired", "hope"].map((em) => (
<Pressable
key={em}
className="chip"
ariaPressed={emotions.includes(em)}
onClick={() =>
setEmotions((cur) =>
cur.includes(em) ? cur.filter((x) => x !== em) : [...cur, em],
)
}
>
{em}
</Pressable>
))}
</div>
<label class="field-label">
FSE event (short phrase)
<input
class="field-input"
value={fseEvent}
onInput={(e) => setFseEvent((e.target as HTMLInputElement).value)}
placeholder="What spiked?"
/>
</label>
{did !== "skipped" && (
<label class="field-label">
Intensity · {intensity}
<input
type="range"
min={1}
max={10}
value={intensity}
onInput={(e) => setIntensity(Number((e.target as HTMLInputElement).value))}
/>
</label>
)}
{did === "skipped" && (
<>
<span class="field-label">Why skipped?</span>
<div class="segment-row">
{["time", "fear", "urge", "boring", "fse", "other"].map((v) => (
<Pressable
key={v}
className="segment"
ariaPressed={skipReason === v}
onClick={() => setSkipReason(v)}
>
{v}
</Pressable>
))}
</div>
</>
)}
<Pressable className="btn btn-plain" onClick={() => setShowThick((v) => !v)}>
{showThick ? "Hide quality / fun / energy" : "More: quality, fun, energy, money"}
</Pressable>
{showThick && did !== "skipped" && (
<>
<span class="field-label">Quality</span>
<div class="segment-row">
{[1, 2, 3, 4, 5].map((n) => (
<Pressable key={n} className="segment" ariaPressed={quality === n} onClick={() => setQuality(n)}>
{n}
</Pressable>
))}
</div>
<span class="field-label">Fun</span>
<div class="segment-row">
{[1, 2, 3, 4, 5].map((n) => (
<Pressable key={n} className="segment" ariaPressed={fun === n} onClick={() => setFun(n)}>
{n}
</Pressable>
))}
</div>
<span class="field-label">Energy after</span>
<div class="segment-row">
{([-2, -1, 0, 1, 2] as const).map((v) => (
<Pressable
key={String(v)}
className="segment"
ariaPressed={energy === v}
onClick={() => setEnergy(v)}
>
{v > 0 ? `+${v}` : String(v)}
</Pressable>
))}
</div>
<span class="field-label">Would repeat?</span>
<div class="segment-row">
{(["yes", "maybe", "no"] as const).map((v) => (
<Pressable
key={v}
className="segment"
ariaPressed={wouldRepeat === v}
onClick={() => setWouldRepeat(v)}
>
{v}
</Pressable>
))}
</div>
<label class="field-label">
Money (optional)
<input
class="field-input"
type="number"
value={money}
onInput={(e) => setMoney((e.target as HTMLInputElement).value)}
/>
</label>
</>
)}
<div class="resolve-sticky">
<Button className="btn-large" disabled={busy} onClick={saveFeedback}>
{busy ? "Saving…" : "Save how it went"}
</Button>
<Button className="btn-secondary" disabled={busy} onClick={removeBlock}>
Remove block
</Button>
</div>
</div>
) : null}
</Sheet>
);
}
|