| 1 | import React, { useMemo, useState } from "react"; |
| 2 | |
| 3 | import { useService } from "../hooks/useService.js"; |
| 4 | import { services } from "../services/index.js"; |
| 5 | import type { UpdateServiceState } from "../services/types.js"; |
| 6 | import { SERVICE_NAMES } from "../services/types.js"; |
| 7 | |
| 8 | import { Selector, SelectorOption } from "./Selector.js"; |
| 9 | |
| 10 | interface UpdateSelectorProps { |
| 11 | onCancel: () => void; |
| 12 | } |
| 13 | |
| 14 | interface UpdateOption extends SelectorOption { |
| 15 | action: "run" | "toggle-auto" | "back"; |
| 16 | } |
| 17 | |
| 18 | export const UpdateSelector: React.FC<UpdateSelectorProps> = ({ onCancel }) => { |
| 19 | const [selectedIndex, setSelectedIndex] = useState(0); |
| 20 | const updateServiceState = useService<UpdateServiceState>( |
| 21 | SERVICE_NAMES.UPDATE, |
| 22 | ); |
| 23 | |
| 24 | const updateState = updateServiceState.value; |
| 25 | |
| 26 | const isWorking = |
| 27 | updateServiceState.state === "loading" || |
| 28 | updateState?.status === "checking" || |
| 29 | updateState?.status === "updating"; |
| 30 | |
| 31 | const error: string | null = useMemo(() => { |
| 32 | if (updateServiceState.state === "error") { |
| 33 | return updateServiceState.error?.message || "Update service error"; |
| 34 | } |
| 35 | if (updateState?.status === "error") { |
| 36 | return updateState.message || "Update failed"; |
| 37 | } |
| 38 | return null; |
| 39 | }, [updateServiceState.state, updateServiceState.error, updateState]); |
| 40 | |
| 41 | const loadingMessage = useMemo(() => { |
| 42 | if (!updateState) return "Preparing update..."; |
| 43 | return updateState.message || "Working..."; |
| 44 | }, [updateState]); |
| 45 | |
| 46 | const options: UpdateOption[] = useMemo(() => { |
| 47 | const runLabel = updateState?.latestVersion |
| 48 | ? `Run update to v${updateState.latestVersion}` |
| 49 | : "Run update"; |
| 50 | |
| 51 | const autoUpdateLabel = updateState?.autoUpdate |
| 52 | ? "Turn off auto-updates" |
| 53 | : "Turn on auto-updates"; |
| 54 | |
| 55 | return [ |
| 56 | { id: "run-update", name: runLabel, action: "run" }, |
| 57 | { id: "toggle-auto", name: autoUpdateLabel, action: "toggle-auto" }, |
| 58 | { id: "back", name: "Back", action: "back" }, |
| 59 | ]; |
| 60 | }, [updateState?.latestVersion, updateState?.autoUpdate]); |
| 61 | |
| 62 | const handleSelect = async (option: UpdateOption) => { |
| 63 | switch (option.action) { |
| 64 | case "run": |
| 65 | // Perform a manual update (not auto-update) |
| 66 | await services.updateService.performUpdate(false); |
| 67 | return; // Keep selector open to show status changes |
| 68 | case "toggle-auto": |
| 69 | // Toggle auto-update setting |
| 70 | const newValue = !updateState?.autoUpdate; |
| 71 | await services.updateService.setAutoUpdate(newValue); |
| 72 | return; // Keep selector open to show updated label |
| 73 | case "back": |
| 74 | onCancel(); |
| 75 | return; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | return ( |
| 80 | <Selector |
| 81 | title="Update Continue CLI" |
| 82 | options={options} |
| 83 | selectedIndex={selectedIndex} |
| 84 | loading={!!isWorking} |
| 85 | error={error} |
| 86 | loadingMessage={loadingMessage} |
| 87 | currentId={null} |
| 88 | onSelect={handleSelect} |
| 89 | onCancel={onCancel} |
| 90 | onNavigate={setSelectedIndex} |
| 91 | /> |
| 92 | ); |
| 93 | }; |