Elffuss BraidHub

kikocis/continue public

9d5865e0e05b · 2.2 KiBRawPermalink
1import { Text } from "ink";
2import React, { useEffect, useMemo, useState } from "react";
3
4import { compareVersions, getLatestVersion, getVersion } from "../version.js";
5
6import { useTerminalSize } from "./hooks/useTerminalSize.js";
7
8interface UpdateNotificationProps {
9 isRemoteMode?: boolean;
10}
11
12const UpdateNotification: React.FC<UpdateNotificationProps> = ({
13 isRemoteMode = false,
14}) => {
15 const [updateAvailable, setUpdateAvailable] = useState(false);
16 const [latestVersion, setLatestVersion] = useState<string>("");
17 const [currentVersion] = useState(getVersion());
18 const { columns } = useTerminalSize();
19
20 useEffect(() => {
21 // Skip update check in test environment
22 if (process.env.NODE_ENV === "test") {
23 return;
24 }
25
26 // Skip update check for development versions
27 if (currentVersion.endsWith("-dev")) {
28 return;
29 }
30
31 const abortController = new AbortController();
32
33 const checkForUpdate = async () => {
34 try {
35 const latest = await getLatestVersion(abortController.signal);
36 if (latest) {
37 setLatestVersion(latest);
38 const comparison = compareVersions(currentVersion, latest);
39 setUpdateAvailable(comparison === "older");
40 }
41 } catch (error) {
42 // Silently fail - we don't want to interrupt the user experience
43 if (error instanceof Error && error.name !== "AbortError") {
44 console.debug("Failed to check for updates:", error);
45 }
46 }
47 };
48
49 // Check for updates but don't block
50 checkForUpdate();
51
52 // Cleanup function to abort the request when component unmounts
53 return () => {
54 abortController.abort();
55 };
56 }, [currentVersion]);
57
58 const text = useMemo(() => {
59 if (columns < 75) {
60 return `v${latestVersion} available`;
61 }
62 return `Update available: v${latestVersion} (npm i -g @continuedev/cli)`;
63 }, [columns, latestVersion]);
64
65 if (!updateAvailable) {
66 if (isRemoteMode) {
67 return <Text color="cyan">◉ Remote Mode</Text>;
68 }
69 return (
70 <Text wrap="truncate-end" color="gray">
71 ● Continue CLI
72 </Text>
73 );
74 }
75
76 return <Text color="dim">{text}</Text>;
77};
78
79export { UpdateNotification };