|
| 1 | +import { Button } from "@opencode-ai/ui/button" |
| 2 | +import { Icon } from "@opencode-ai/ui/icon" |
| 3 | +import { Popover } from "@opencode-ai/ui/popover" |
| 4 | +import { Tooltip } from "@opencode-ai/ui/tooltip" |
| 5 | +import { useMutation, useQuery, useQueryClient } from "@tanstack/solid-query" |
| 6 | +import { For, Show, createMemo, createSignal } from "solid-js" |
| 7 | +import { useGlobalSDK } from "@/context/global-sdk" |
| 8 | +import { useLanguage } from "@/context/language" |
| 9 | +import { useSessionLayout } from "@/pages/session/session-layout" |
| 10 | + |
| 11 | +type ScheduleInfo = { |
| 12 | + id: string |
| 13 | + expression: string |
| 14 | + message: string |
| 15 | + nextRun: number | null |
| 16 | + lastRanAt: number | null |
| 17 | + lastRunStatus: "ran" | "skipped" | null |
| 18 | +} |
| 19 | + |
| 20 | +const SCHEDULE_QUERY_KEY = ["session", "schedules"] as const |
| 21 | + |
| 22 | +function formatRelativeTime(language: ReturnType<typeof useLanguage>, ts: number | null) { |
| 23 | + if (ts === null) return null |
| 24 | + return new Date(ts).toLocaleString(language.intl()) |
| 25 | +} |
| 26 | + |
| 27 | +export function SessionScheduleButton() { |
| 28 | + const language = useLanguage() |
| 29 | + const globalSDK = useGlobalSDK() |
| 30 | + const { params } = useSessionLayout() |
| 31 | + const queryClient = useQueryClient() |
| 32 | + const [shown, setShown] = createSignal(false) |
| 33 | + |
| 34 | + const sessionID = createMemo(() => params.id) |
| 35 | + |
| 36 | + const schedulesQuery = useQuery(() => ({ |
| 37 | + queryKey: [...SCHEDULE_QUERY_KEY, sessionID()], |
| 38 | + enabled: !!sessionID(), |
| 39 | + queryFn: async () => { |
| 40 | + const id = sessionID() |
| 41 | + if (!id) return [] as ScheduleInfo[] |
| 42 | + const result = await globalSDK.client.session.schedules({ sessionID: id }) |
| 43 | + return ((result?.data as ScheduleInfo[] | undefined) ?? []) as ScheduleInfo[] |
| 44 | + }, |
| 45 | + refetchInterval: 10_000, |
| 46 | + staleTime: 5_000, |
| 47 | + })) |
| 48 | + |
| 49 | + const deleteMutation = useMutation(() => ({ |
| 50 | + mutationFn: async (scheduleID: string) => { |
| 51 | + const id = sessionID() |
| 52 | + if (!id) return |
| 53 | + await globalSDK.client.session.deleteSchedule({ sessionID: id, scheduleID }) |
| 54 | + }, |
| 55 | + onSuccess: () => { |
| 56 | + queryClient.invalidateQueries({ queryKey: [...SCHEDULE_QUERY_KEY, sessionID()] }) |
| 57 | + }, |
| 58 | + })) |
| 59 | + |
| 60 | + const schedules = createMemo(() => schedulesQuery.data ?? []) |
| 61 | + const count = createMemo(() => schedules().length) |
| 62 | + |
| 63 | + return ( |
| 64 | + <Show when={sessionID() && count() > 0}> |
| 65 | + <Popover |
| 66 | + open={shown()} |
| 67 | + onOpenChange={setShown} |
| 68 | + triggerAs={Button} |
| 69 | + triggerProps={{ |
| 70 | + variant: "ghost", |
| 71 | + class: "titlebar-icon w-8 h-6 p-0 box-border", |
| 72 | + "aria-label": language.t("schedule.button.label", { count: count() }), |
| 73 | + style: { scale: 1 }, |
| 74 | + }} |
| 75 | + trigger={ |
| 76 | + <div class="relative flex items-center justify-center"> |
| 77 | + <Icon name="clock" size="small" /> |
| 78 | + <Show when={count() > 1}> |
| 79 | + <span class="absolute -top-1 -right-2 min-w-3.5 h-3.5 px-1 rounded-full bg-icon-info-base text-text-invert-strong text-10-strong leading-none flex items-center justify-center"> |
| 80 | + {count()} |
| 81 | + </span> |
| 82 | + </Show> |
| 83 | + </div> |
| 84 | + } |
| 85 | + class="[&_[data-slot=popover-body]]:p-0 w-[360px] max-w-[calc(100vw-40px)] bg-background-strong border border-border-weak-base shadow-lg rounded-xl" |
| 86 | + gutter={4} |
| 87 | + placement="bottom-end" |
| 88 | + > |
| 89 | + <Show when={shown()}> |
| 90 | + <div class="flex flex-col"> |
| 91 | + <div class="px-4 py-3 border-b border-border-weak-base"> |
| 92 | + <div class="text-13-strong text-text-strong">{language.t("schedule.popover.title")}</div> |
| 93 | + <div class="text-11-regular text-text-weak mt-0.5"> |
| 94 | + {language.t("schedule.popover.subtitle", { count: count() })} |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + <div class="max-h-[400px] overflow-y-auto"> |
| 98 | + <For each={schedules()}> |
| 99 | + {(item) => ( |
| 100 | + <ScheduleRow |
| 101 | + item={item} |
| 102 | + onDelete={() => deleteMutation.mutate(item.id)} |
| 103 | + deleting={deleteMutation.isPending && deleteMutation.variables === item.id} |
| 104 | + /> |
| 105 | + )} |
| 106 | + </For> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + </Show> |
| 110 | + </Popover> |
| 111 | + </Show> |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +function ScheduleRow(props: { item: ScheduleInfo; onDelete: () => void; deleting: boolean }) { |
| 116 | + const language = useLanguage() |
| 117 | + const lastRan = createMemo(() => formatRelativeTime(language, props.item.lastRanAt)) |
| 118 | + const nextRun = createMemo(() => formatRelativeTime(language, props.item.nextRun)) |
| 119 | + |
| 120 | + const tooltipContent = () => ( |
| 121 | + <div class="flex flex-col gap-1 text-11-regular"> |
| 122 | + <div class="flex items-center gap-2"> |
| 123 | + <span class="text-text-invert-base">{language.t("schedule.tooltip.last")}</span> |
| 124 | + <Show |
| 125 | + when={lastRan()} |
| 126 | + fallback={<span class="text-text-invert-weak">{language.t("schedule.tooltip.never")}</span>} |
| 127 | + > |
| 128 | + <span class="text-text-invert-strong">{lastRan()}</span> |
| 129 | + <Show when={props.item.lastRunStatus === "skipped"}> |
| 130 | + <span class="text-icon-warning-base">{language.t("schedule.tooltip.skipped")}</span> |
| 131 | + </Show> |
| 132 | + </Show> |
| 133 | + </div> |
| 134 | + <div class="flex items-center gap-2"> |
| 135 | + <span class="text-text-invert-base">{language.t("schedule.tooltip.next")}</span> |
| 136 | + <Show |
| 137 | + when={nextRun()} |
| 138 | + fallback={<span class="text-text-invert-weak">{language.t("schedule.tooltip.never")}</span>} |
| 139 | + > |
| 140 | + <span class="text-text-invert-strong">{nextRun()}</span> |
| 141 | + </Show> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + ) |
| 145 | + |
| 146 | + return ( |
| 147 | + <div class="group flex items-start gap-2 px-4 py-3 border-b border-border-weak-base last:border-b-0 hover:bg-background-base"> |
| 148 | + <div class="flex-1 min-w-0"> |
| 149 | + <div class="font-mono text-11-regular text-text-weak mb-0.5">{props.item.expression}</div> |
| 150 | + <div class="text-12-regular text-text-base truncate" title={props.item.message}> |
| 151 | + {props.item.message} |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + <Tooltip value={tooltipContent()} placement="left"> |
| 155 | + <Button |
| 156 | + type="button" |
| 157 | + variant="ghost" |
| 158 | + class="size-5 opacity-60 hover:opacity-100" |
| 159 | + aria-label={language.t("schedule.row.info")} |
| 160 | + > |
| 161 | + <Icon name="help" size="small" /> |
| 162 | + </Button> |
| 163 | + </Tooltip> |
| 164 | + <Button |
| 165 | + type="button" |
| 166 | + variant="ghost" |
| 167 | + class="size-5 opacity-0 group-hover:opacity-100 transition-opacity" |
| 168 | + aria-label={language.t("schedule.row.delete")} |
| 169 | + onClick={props.onDelete} |
| 170 | + disabled={props.deleting} |
| 171 | + > |
| 172 | + <Icon name="close" size="small" /> |
| 173 | + </Button> |
| 174 | + </div> |
| 175 | + ) |
| 176 | +} |
0 commit comments