Elffuss BraidHub

kikocis/continue public

main · 5.3 KiBRawPermalink
1import { useCallback, useEffect, useState } from "react";
2
3import type { PermissionMode } from "../../permissions/types.js";
4import { modeService } from "../../services/ModeService.js";
5import { getGitBranch, getGitRemoteUrl, isGitRepo } from "../../util/git.js";
6import type { ConfigOption, ModelOption } from "../types/selectorTypes.js";
7
8import { useConfigSelector } from "./useConfigSelector.js";
9import { useModelSelector } from "./useModelSelector.js";
10
11// Helper function to get repo info for responsive display
12export function getRepoInfo(remoteUrl?: string): {
13 repoName: string;
14 branchName: string | null;
15 fullPath: string;
16 isGitRepo: boolean;
17} {
18 let url = remoteUrl ?? "";
19 const isGit = isGitRepo();
20 let branchName: string | null = null;
21
22 if (!url && isGit) {
23 const gitUrl = getGitRemoteUrl();
24 if (gitUrl) {
25 url = gitUrl;
26 }
27 }
28
29 if (!url) {
30 url = process.cwd();
31 }
32
33 // Get branch info if we're in a git repo
34 if (isGit) {
35 branchName = getGitBranch();
36 }
37
38 // Clean up the URL
39 const cleanUrl = url
40 .replace(/\.git$/, "")
41 .replace(/^(https|http):\/\/.*?\//, "");
42
43 return {
44 repoName: cleanUrl,
45 branchName,
46 fullPath: url,
47 isGitRepo: isGit,
48 };
49}
50
51// Helper function to get responsive repo URL text based on available width
52export function getResponsiveRepoText(
53 remoteUrl?: string,
54 availableWidth: number = 0,
55): string {
56 const repoInfo = getRepoInfo(remoteUrl);
57
58 // If no available width provided, show nothing
59 if (availableWidth <= 0) {
60 return "";
61 }
62
63 // Calculate the minimum space needed for different display options
64 const branchSeparator = " ⊦";
65 const branchText = repoInfo.branchName || "";
66 const repoText = repoInfo.repoName;
67
68 // Priority order for display:
69 // 1. repo + branch (if everything fits)
70 // 2. branch only (preferred when both fit individually)
71 // 3. repo only (fallback when branch doesn't fit)
72 // 4. nothing if neither fit
73
74 if (repoInfo.isGitRepo && repoInfo.branchName) {
75 const fullText = `${repoText}${branchSeparator}${branchText}`;
76 const branchOnlyText = branchText;
77 const repoOnlyText = repoText;
78
79 // If full text fits, use it
80 if (fullText.length <= availableWidth) {
81 return fullText;
82 }
83
84 // Prefer branch if it fits (more immediately useful)
85 if (branchOnlyText.length <= availableWidth) {
86 return branchOnlyText;
87 }
88
89 // If branch doesn't fit but repo does, use repo
90 if (repoOnlyText.length <= availableWidth) {
91 return repoOnlyText;
92 }
93 }
94
95 // No branch or not in git repo
96 if (repoText.length <= availableWidth) {
97 return repoText;
98 }
99
100 return "";
101}
102
103// Helper function to get repo URL text (legacy compatibility)
104export function getRepoUrlText(remoteUrl?: string): string {
105 return getResponsiveRepoText(remoteUrl);
106}
107
108// Custom hook for intro message
109export function useIntroMessage(
110 isRemoteMode: boolean,
111 _services: any,
112 _allServicesReady: boolean,
113) {
114 const [showIntroMessage, setShowIntroMessage] = useState(false);
115
116 useEffect(() => {
117 const shouldShow = !isRemoteMode;
118
119 setShowIntroMessage(shouldShow);
120 }, [isRemoteMode]);
121
122 return [showIntroMessage, setShowIntroMessage] as const;
123}
124
125// Custom hook for login handling
126export function useLoginHandlers(
127 navigateTo: any,
128 navState: any,
129 closeCurrentScreen: () => void,
130) {
131 const handleLoginPrompt = useCallback(
132 (promptText: string): Promise<string> => {
133 return new Promise((resolve) => {
134 navigateTo("login", { text: promptText, resolve });
135 });
136 },
137 [navigateTo],
138 );
139
140 const handleLoginTokenSubmit = useCallback(
141 (token: string) => {
142 if (navState.screenData?.resolve) {
143 navState.screenData.resolve(token);
144 closeCurrentScreen();
145 }
146 },
147 [navState.screenData, closeCurrentScreen],
148 );
149
150 return { handleLoginPrompt, handleLoginTokenSubmit };
151}
152
153// Custom hook for mode tracking
154export function useCurrentMode() {
155 const [currentMode, setCurrentMode] = useState<PermissionMode>(
156 modeService.getCurrentMode(),
157 );
158
159 useEffect(() => {
160 const handleModeChange = (newMode: PermissionMode) => {
161 setCurrentMode(newMode);
162 };
163
164 modeService.on("modeChanged", handleModeChange);
165 return () => {
166 modeService.off("modeChanged", handleModeChange);
167 };
168 }, []);
169
170 return currentMode;
171}
172
173// Custom hook to combine all selector logic
174export function useSelectors(
175 configPath: string | undefined,
176 setChatHistory: React.Dispatch<React.SetStateAction<any[]>>,
177 handleClear: () => void,
178): {
179 handleConfigSelect: (config: ConfigOption) => Promise<void>;
180 handleModelSelect: (model: ModelOption) => Promise<void>;
181} {
182 const { handleConfigSelect } = useConfigSelector({
183 configPath,
184 onMessage: (message) => {
185 // Convert message to ChatHistoryItem format
186 setChatHistory((prev) => [
187 ...prev,
188 {
189 message: { role: message.role, content: message.content },
190 contextItems: [],
191 },
192 ]);
193 },
194 handleClear,
195 });
196
197 const { handleModelSelect } = useModelSelector({
198 onMessage: (message) => {
199 // Convert message to ChatHistoryItem format
200 setChatHistory((prev) => [
201 ...prev,
202 {
203 message: { role: "system", content: message.content },
204 contextItems: [],
205 },
206 ]);
207 },
208 });
209
210 return {
211 handleConfigSelect,
212 handleModelSelect,
213 };
214}