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(); 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) => ( start(choice)} /> ); return ( {running.map((ts) => ( {(ts.description ?? "") .split("\n") .filter((line) => line.trim()) .slice(-8) .map((line, i) => ( ))} launchCommand({ name: "add-note", type: LaunchType.UserInitiated, context: { timesheetId: ts.id }, }) } /> launchCommand({ name: "stop-activity", type: LaunchType.UserInitiated, context: { timesheetId: ts.id }, }) } /> ))} {favorites.length > 0 && ( {favorites.map((c) => startItem(c, "fav"))} )} {recent.length > 0 && ( {recent.map((c) => startItem(c, "rec"))} )} {byCustomer.map(([customer, choices]) => ( {choices.map((c) => startItem(c, "all", `${c.project.name} › ${c.title}`))} ))} launchCommand({ name: "start-activity", type: LaunchType.UserInitiated })} /> launchCommand({ name: "manage-timesheets", type: LaunchType.UserInitiated })} /> open(kimaiUrl("/"))} /> ); }