| 1 | import { useCallback, useEffect, useState } from "react"; |
| 2 | |
| 3 | import type { PermissionMode } from "../../permissions/types.js"; |
| 4 | import { modeService } from "../../services/ModeService.js"; |
| 5 | import { getGitRemoteUrl, isGitRepo } from "../../util/git.js"; |
| 6 | import type { ConfigOption, ModelOption } from "../types/selectorTypes.js"; |
| 7 | |
| 8 | import { useConfigSelector } from "./useConfigSelector.js"; |
| 9 | import { useModelSelector } from "./useModelSelector.js"; |
| 10 | |
| 11 | // Helper function to get repo URL text |
| 12 | export function getRepoUrlText(remoteUrl?: string): string { |
| 13 | let url = remoteUrl ?? ""; |
| 14 | if (!url) { |
| 15 | const isGit = isGitRepo(); |
| 16 | if (isGit) { |
| 17 | const gitUrl = getGitRemoteUrl(); |
| 18 | if (gitUrl) { |
| 19 | url = gitUrl; |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | if (!url) { |
| 24 | url = process.cwd(); |
| 25 | } |
| 26 | |
| 27 | url = url.replace(/\.git$/, ""); |
| 28 | url = url.replace(/^(https|http):\/\/.*?\//, ""); |
| 29 | return url; |
| 30 | } |
| 31 | |
| 32 | // Custom hook for intro message |
| 33 | export function useIntroMessage( |
| 34 | isRemoteMode: boolean, |
| 35 | _services: any, |
| 36 | _allServicesReady: boolean, |
| 37 | ) { |
| 38 | const [showIntroMessage, setShowIntroMessage] = useState(false); |
| 39 | |
| 40 | useEffect(() => { |
| 41 | const shouldShow = !isRemoteMode; |
| 42 | |
| 43 | setShowIntroMessage(shouldShow); |
| 44 | }, [isRemoteMode]); |
| 45 | |
| 46 | return [showIntroMessage, setShowIntroMessage] as const; |
| 47 | } |
| 48 | |
| 49 | // Custom hook for login handling |
| 50 | export function useLoginHandlers( |
| 51 | navigateTo: any, |
| 52 | navState: any, |
| 53 | closeCurrentScreen: () => void, |
| 54 | ) { |
| 55 | const handleLoginPrompt = useCallback( |
| 56 | (promptText: string): Promise<string> => { |
| 57 | return new Promise((resolve) => { |
| 58 | navigateTo("login", { text: promptText, resolve }); |
| 59 | }); |
| 60 | }, |
| 61 | [navigateTo], |
| 62 | ); |
| 63 | |
| 64 | const handleLoginTokenSubmit = useCallback( |
| 65 | (token: string) => { |
| 66 | if (navState.screenData?.resolve) { |
| 67 | navState.screenData.resolve(token); |
| 68 | closeCurrentScreen(); |
| 69 | } |
| 70 | }, |
| 71 | [navState.screenData, closeCurrentScreen], |
| 72 | ); |
| 73 | |
| 74 | return { handleLoginPrompt, handleLoginTokenSubmit }; |
| 75 | } |
| 76 | |
| 77 | // Custom hook for mode tracking |
| 78 | export function useCurrentMode() { |
| 79 | const [currentMode, setCurrentMode] = useState<PermissionMode>( |
| 80 | modeService.getCurrentMode(), |
| 81 | ); |
| 82 | |
| 83 | useEffect(() => { |
| 84 | const handleModeChange = (newMode: PermissionMode) => { |
| 85 | setCurrentMode(newMode); |
| 86 | }; |
| 87 | |
| 88 | modeService.on("modeChanged", handleModeChange); |
| 89 | return () => { |
| 90 | modeService.off("modeChanged", handleModeChange); |
| 91 | }; |
| 92 | }, []); |
| 93 | |
| 94 | return currentMode; |
| 95 | } |
| 96 | |
| 97 | // Custom hook to combine all selector logic |
| 98 | export function useSelectors( |
| 99 | configPath: string | undefined, |
| 100 | setChatHistory: React.Dispatch<React.SetStateAction<any[]>>, |
| 101 | handleClear: () => void, |
| 102 | ): { |
| 103 | handleConfigSelect: (config: ConfigOption) => Promise<void>; |
| 104 | handleModelSelect: (model: ModelOption) => Promise<void>; |
| 105 | } { |
| 106 | const { handleConfigSelect } = useConfigSelector({ |
| 107 | configPath, |
| 108 | onMessage: (message) => { |
| 109 | // Convert message to ChatHistoryItem format |
| 110 | setChatHistory((prev) => [ |
| 111 | ...prev, |
| 112 | { |
| 113 | message: { role: message.role, content: message.content }, |
| 114 | contextItems: [], |
| 115 | }, |
| 116 | ]); |
| 117 | }, |
| 118 | handleClear, |
| 119 | }); |
| 120 | |
| 121 | const { handleModelSelect } = useModelSelector({ |
| 122 | onMessage: (message) => { |
| 123 | // Convert message to ChatHistoryItem format |
| 124 | setChatHistory((prev) => [ |
| 125 | ...prev, |
| 126 | { |
| 127 | message: { role: "system", content: message.content }, |
| 128 | contextItems: [], |
| 129 | }, |
| 130 | ]); |
| 131 | }, |
| 132 | }); |
| 133 | |
| 134 | return { |
| 135 | handleConfigSelect, |
| 136 | handleModelSelect, |
| 137 | }; |
| 138 | } |