feat: track duration of subtask

This commit is contained in:
Jannik Meier committed 2026-11-06 14:39:46 +01:00
1 parent 9fe73950fd
commit d594207a2c
16 files changed
+718 -814

No files matched your search

+2 -193
View File
@@ -1,196 +1,5 @@
import { Action, ActionPanel, Form, Icon, popToRoot, showToast, Toast } from "@raycast/api";
import { useEffect, useMemo, useState } from "react";
import { api } from "./lib/api";
import { ChoiceDropdown, choiceKeyOf, firstChoiceValue } from "./lib/choice-dropdown";
import { useCatalog, useStartOptions } from "./lib/hooks";
import { formatDuration, kimaiDateAt, parseDuration, parseChoiceKey, parseTime, showError } from "./lib/utils";
import { TimesheetForm } from "./lib/timesheet-form";
export default function AddTimesheet() {
const { data: catalog, isLoading: catalogLoading } = useCatalog();
const { favorites, recent, isLoading: optionsLoading } = useStartOptions(catalog);
const all = useMemo(() => catalog?.choices ?? [], [catalog]);
const byKey = useMemo(() => new Map(all.map((c) => [c.key, c])), [all]);
const [date, setDate] = useState<Date | null>(new Date());
const [beginStr, setBeginStr] = useState("");
const [endStr, setEndStr] = useState("");
const [breakStr, setBreakStr] = useState("");
const [choice, setChoice] = useState("");
const [description, setDescription] = useState("");
const [tags, setTags] = useState("");
const [errors, setErrors] = useState<Record<string, string | undefined>>({});
const choiceKey = choiceKeyOf(choice);
const selected = byKey.get(choiceKey ?? "");
// preselect the first favorite (or recent) entry once loaded
useEffect(() => {
if (choice || optionsLoading || catalogLoading) return;
const first = firstChoiceValue(favorites, recent, all);
if (first) setChoice(first);
}, [favorites, recent, all, optionsLoading, catalogLoading]);
const clearError = (field: string) => setErrors((e) => ({ ...e, [field]: undefined }));
const begin = parseTime(beginStr);
const end = parseTime(endStr);
const breakSec = parseDuration(breakStr);
const duration =
begin !== undefined && end !== undefined && end > begin && breakSec !== undefined
? (end - begin) * 60 - breakSec
: undefined;
function onTimeChange(field: "begin" | "end", v: string) {
(field === "begin" ? setBeginStr : setEndStr)(v);
if (v.trim() !== "" && parseTime(v) === undefined) {
setErrors((e) => ({ ...e, [field]: "Format z. B. 8:30" }));
} else {
clearError(field);
}
}
function onBreakChange(v: string) {
setBreakStr(v);
if (parseDuration(v) === undefined) setErrors((e) => ({ ...e, break: "Format z. B. 0:30" }));
else clearError("break");
}
/** returns true when saved */
async function submit(): Promise<boolean> {
const errs: Record<string, string> = {};
if (!date) errs.date = "Pflichtfeld";
if (begin === undefined) errs.begin = beginStr.trim() ? "Format z. B. 8:30" : "Pflichtfeld";
if (end === undefined) errs.end = endStr.trim() ? "Format z. B. 17:00" : "Pflichtfeld";
else if (begin !== undefined && end <= begin) errs.end = "Bis muss nach Von liegen";
if (breakSec === undefined) errs.break = "Format z. B. 0:30";
else if (duration !== undefined && duration <= 0) errs.break = "Pause ist länger als die Arbeitszeit";
if (!choiceKey) errs.choice = "Bitte Tätigkeit wählen";
if (!description.trim()) errs.description = "Beschreibung ist Pflicht";
if (Object.keys(errs).length) {
setErrors(errs);
return false;
}
const toast = await showToast({ style: Toast.Style.Animated, title: "Speichere…" });
try {
// times are entered as wall-clock time of the Kimai user, so no timezone conversion
const ts = await api.create({
begin: kimaiDateAt(date!, begin!),
end: kimaiDateAt(date!, end!),
...parseChoiceKey(choiceKey!),
description,
tags: tags.trim() || undefined,
// without a value Kimai applies its own break rules
...(breakStr.trim() ? { break: formatDuration(breakSec ?? 0) } : {}),
});
toast.style = Toast.Style.Success;
toast.title = `Timesheet erstellt: ${ts.activity.name}`;
toast.message = formatDuration(ts.duration);
return true;
} catch (e) {
toast.hide();
await showError("Speichern fehlgeschlagen", e);
return false;
}
}
async function saveAndClose() {
if (await submit()) await popToRoot();
}
// keep date and assignment, continue where the last entry ended
async function saveAndNext() {
if (!(await submit())) return;
setBeginStr(endStr);
setEndStr("");
setBreakStr("");
setDescription("");
}
return (
<Form
isLoading={catalogLoading || optionsLoading}
navigationTitle="Manuelles Timesheet"
actions={
<ActionPanel>
<Action.SubmitForm title="Speichern" icon={Icon.Check} onSubmit={saveAndClose} />
<Action.SubmitForm
title="Speichern und weiteres erfassen"
icon={Icon.Plus}
shortcut={{ modifiers: ["cmd", "shift"], key: "enter" }}
onSubmit={saveAndNext}
/>
</ActionPanel>
}
>
<Form.DatePicker
id="date"
title="Datum"
type={Form.DatePicker.Type.Date}
value={date}
onChange={(v) => {
setDate(v);
clearError("date");
}}
error={errors.date}
/>
<Form.TextField
id="begin"
title="Von"
placeholder="z. B. 8:30"
value={beginStr}
onChange={(v) => onTimeChange("begin", v)}
error={errors.begin}
/>
<Form.TextField
id="end"
title="Bis"
placeholder="z. B. 17:00"
value={endStr}
onChange={(v) => onTimeChange("end", v)}
error={errors.end}
/>
<Form.TextField
id="break"
title="Pause"
placeholder="automatisch"
value={breakStr}
onChange={onBreakChange}
error={errors.break}
info="Leer lassen, damit Kimai die Pause nach seinen Regeln setzt"
/>
{duration !== undefined && duration > 0 && <Form.Description title="Dauer" text={formatDuration(duration)} />}
<Form.Separator />
<ChoiceDropdown
favorites={favorites}
recent={recent}
all={all}
value={choice}
onChange={(v) => {
setChoice(v);
clearError("choice");
}}
error={errors.choice}
/>
{selected && (
<>
<Form.Description title="Kunde" text={selected.customer?.name ?? "–"} />
<Form.Description title="Projekt" text={selected.project.name} />
</>
)}
<Form.Separator />
<Form.TextArea
id="description"
title="Beschreibung"
value={description}
onChange={(v) => {
setDescription(v);
clearError("description");
}}
error={errors.description}
/>
<Form.TextField id="tags" title="Tags" placeholder="kommagetrennt" value={tags} onChange={setTags} />
</Form>
);
return <TimesheetForm mode="create" isRoot />;
}