|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { RpcApi } from "@/app/store/wshclientapi"; |
| 5 | +import { TabRpcClient } from "@/app/store/wshrpcutil"; |
| 6 | +import { isWindows } from "@/util/platformutil"; |
| 7 | +import { atom, type Atom, type PrimitiveAtom } from "jotai"; |
| 8 | +import { globalStore } from "./jotaiStore"; |
| 9 | + |
| 10 | +class ConnectionsModel { |
| 11 | + private static instance: ConnectionsModel; |
| 12 | + gitBashPathAtom: PrimitiveAtom<string> = atom("") as PrimitiveAtom<string>; |
| 13 | + hasGitBashAtom: Atom<boolean>; |
| 14 | + |
| 15 | + private constructor() { |
| 16 | + this.hasGitBashAtom = atom((get) => { |
| 17 | + if (!isWindows()) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + const path = get(this.gitBashPathAtom); |
| 21 | + return path !== ""; |
| 22 | + }); |
| 23 | + this.loadGitBashPath(); |
| 24 | + } |
| 25 | + |
| 26 | + static getInstance(): ConnectionsModel { |
| 27 | + if (!ConnectionsModel.instance) { |
| 28 | + ConnectionsModel.instance = new ConnectionsModel(); |
| 29 | + } |
| 30 | + return ConnectionsModel.instance; |
| 31 | + } |
| 32 | + |
| 33 | + async loadGitBashPath(rescan: boolean = false): Promise<void> { |
| 34 | + if (!isWindows()) { |
| 35 | + return; |
| 36 | + } |
| 37 | + try { |
| 38 | + const path = await RpcApi.FindGitBashCommand(TabRpcClient, rescan, { timeout: 2000 }); |
| 39 | + globalStore.set(this.gitBashPathAtom, path); |
| 40 | + } catch (error) { |
| 41 | + console.error("Failed to find git bash path:", error); |
| 42 | + globalStore.set(this.gitBashPathAtom, ""); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + getGitBashPath(): string { |
| 47 | + return globalStore.get(this.gitBashPathAtom); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export { ConnectionsModel }; |
0 commit comments