Files
kimai-raycast/src/menu-bar.tsx
T
Jannik MeierandClaude Opus 5.5 9fe73950fd Add commands for manual timesheets and notes
- Manuelles Timesheet: create a timesheet with date, begin, end, break,
  activity (search, favorites/recent first), description and tags
- Notiz hinzufügen: append notes to a running activity without stopping
- Extract the activity choice dropdown shared with Tätigkeit starten

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 15:00:16 +01:00

141 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Color, Icon, launchCommand, LaunchType, MenuBarExtra, open } from "@raycast/api";
import { useMemo } from "react";
import { startTimesheet } from "./lib/actions";
import { kimaiUrl } from "./lib/api";
import { useActive, useCatalog, useStartOptions } from "./lib/hooks";
import { ActivityChoice, colorIcon, elapsedSeconds, formatDuration, formatTime, timesheetSubtitle } from "./lib/utils";
function truncate(s: string, max = 28) {
return s.length > max ? `${s.slice(0, max - 1)}…` : s;
}
export default function MenuBar() {
const { data: catalog, isLoading: catalogLoading } = useCatalog();
const { data: active, isLoading: activeLoading, revalidate: revalidateActive } = useActive();
const { favorites, recent, isLoading: optionsLoading, revalidate: revalidateOptions } = useStartOptions(catalog);
const running = active ?? [];
const now = Date.now();
const total = running.reduce((sum, ts) => sum + elapsedSeconds(ts, now), 0);
const title =
running.length === 0
? undefined
: running.length === 1
? `${truncate(running[0].activity.name, 18)} ${formatDuration(total)}`
: `${running.length}× ${formatDuration(total)}`;
// all activities grouped by customer, for the "Alle Tätigkeiten" submenu
const byCustomer = useMemo(() => {
const groups = new Map<string, ActivityChoice[]>();
for (const c of catalog?.choices ?? []) {
const name = c.customer?.name ?? "Ohne Kunde";
groups.set(name, [...(groups.get(name) ?? []), c]);
}
return [...groups.entries()].sort(([a], [b]) => a.localeCompare(b, "de"));
}, [catalog]);
async function start(choice: ActivityChoice) {
await startTimesheet(choice.key, { timezone: catalog?.timezone });
revalidateActive();
revalidateOptions();
}
const isRunning = (choice: ActivityChoice) =>
running.some((ts) => ts.project.id === choice.project.id && ts.activity.id === choice.activity.id);
const startItem = (choice: ActivityChoice, prefix: string, title = choice.title) => (
<MenuBarExtra.Item
key={`${prefix}-${choice.key}`}
icon={colorIcon(choice.color, isRunning(choice) ? Icon.Play : Icon.CircleFilled)}
title={title}
subtitle={prefix === "all" ? undefined : choice.subtitle}
tooltip={`${choice.subtitle} › ${choice.title} starten`}
onAction={() => start(choice)}
/>
);
return (
<MenuBarExtra
icon={{ source: Icon.Clock, tintColor: running.length ? Color.Green : undefined }}
title={title}
tooltip="Kimai"
isLoading={catalogLoading || activeLoading || optionsLoading}
>
<MenuBarExtra.Section
title={running.length ? `Laufend (${formatDuration(total)} h)` : "Keine laufende Tätigkeit"}
>
{running.map((ts) => (
<MenuBarExtra.Submenu
key={ts.id}
icon={colorIcon(ts.activity["color-safe"], Icon.Play)}
title={`${ts.activity.name} ${formatDuration(elapsedSeconds(ts, now))}`}
>
<MenuBarExtra.Item title={timesheetSubtitle(ts)} subtitle={`seit ${formatTime(ts.begin)}`} />
{(ts.description ?? "")
.split("\n")
.filter((line) => line.trim())
.slice(-8)
.map((line, i) => (
<MenuBarExtra.Item key={i} title={truncate(line.trim(), 60)} icon={Icon.Text} />
))}
<MenuBarExtra.Separator />
<MenuBarExtra.Item
title="Notiz hinzufügen…"
icon={Icon.Pencil}
onAction={() =>
launchCommand({
name: "add-note",
type: LaunchType.UserInitiated,
context: { timesheetId: ts.id },
})
}
/>
<MenuBarExtra.Item
title="Stoppen…"
icon={Icon.Stop}
onAction={() =>
launchCommand({
name: "stop-activity",
type: LaunchType.UserInitiated,
context: { timesheetId: ts.id },
})
}
/>
</MenuBarExtra.Submenu>
))}
</MenuBarExtra.Section>
{favorites.length > 0 && (
<MenuBarExtra.Section title="Favoriten starten">
{favorites.map((c) => startItem(c, "fav"))}
</MenuBarExtra.Section>
)}
{recent.length > 0 && (
<MenuBarExtra.Section title="Zuletzt verwendet">{recent.map((c) => startItem(c, "rec"))}</MenuBarExtra.Section>
)}
<MenuBarExtra.Section>
<MenuBarExtra.Submenu title="Alle Tätigkeiten" icon={Icon.List}>
{byCustomer.map(([customer, choices]) => (
<MenuBarExtra.Submenu key={customer} title={customer} icon={Icon.Building}>
{choices.map((c) => startItem(c, "all", `${c.project.name} › ${c.title}`))}
</MenuBarExtra.Submenu>
))}
</MenuBarExtra.Submenu>
<MenuBarExtra.Item
title="Tätigkeit starten…"
icon={Icon.Play}
onAction={() => launchCommand({ name: "start-activity", type: LaunchType.UserInitiated })}
/>
<MenuBarExtra.Item
title="Timesheets verwalten…"
icon={Icon.Calendar}
onAction={() => launchCommand({ name: "manage-timesheets", type: LaunchType.UserInitiated })}
/>
<MenuBarExtra.Item title="Kimai öffnen" icon={Icon.Globe} onAction={() => open(kimaiUrl("/"))} />
</MenuBarExtra.Section>
</MenuBarExtra>
);
}