This commit is contained in:
Jannik Meier committed 2026-11-05 14:16:57 +01:00
commit 913e5a87d2
18 files changed
+5139

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
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 ? <MenuBarExtra.Item title={truncate(ts.description, 60)} icon={Icon.Text} /> : null}
<MenuBarExtra.Separator />
<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>
);
}