gensx
Version:
`GenSX command line tools.
71 lines • 3.15 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text, useApp } from "ink";
import { useEffect, useState } from "react";
import { ErrorMessage } from "../../components/ErrorMessage.js";
import { LoadingSpinner } from "../../components/LoadingSpinner.js";
import { useProjectName } from "../../hooks/useProjectName.js";
import { getAuth } from "../../utils/config.js";
import { validateAndSelectEnvironment } from "../../utils/env-config.js";
function useSelectEnvironment(envName, initialProjectName) {
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [environmentName, setEnvironmentName] = useState(null);
const [success, setSuccess] = useState(false);
const { exit } = useApp();
const { loading: projectLoading, error: projectError, projectName, } = useProjectName(initialProjectName);
useEffect(() => {
let mounted = true;
async function selectEnvironmentFlow() {
if (!projectName)
return;
try {
// Check authentication first
const authConfig = await getAuth();
if (!authConfig) {
throw new Error("Not authenticated. Please run 'gensx login' first.");
}
const success = await validateAndSelectEnvironment(projectName, envName);
if (!success) {
throw new Error(`Environment ${envName} does not exist in project ${projectName}`);
}
if (mounted) {
setEnvironmentName(envName);
setSuccess(true);
setLoading(false);
}
}
catch (err) {
if (mounted) {
const error = err instanceof Error ? err : new Error(String(err));
setError(error);
setLoading(false);
setTimeout(() => {
exit();
}, 100);
}
}
}
void selectEnvironmentFlow();
return () => {
mounted = false;
};
}, [envName, projectName, exit]);
return {
loading: loading || projectLoading,
error: error ?? projectError,
projectName,
environmentName,
success,
};
}
export function SelectEnvironmentUI({ environmentName, projectName: initialProjectName, }) {
const { loading, error, projectName, environmentName: selectedEnv, } = useSelectEnvironment(environmentName, initialProjectName);
if (error) {
return _jsx(ErrorMessage, { message: error.message });
}
if (loading || !projectName) {
return _jsx(LoadingSpinner, {});
}
return (_jsx(Box, { flexDirection: "column", gap: 1, children: _jsxs(Text, { children: [_jsx(Text, { color: "green", children: "\u2714" }), " Environment", " ", _jsx(Text, { color: "green", children: selectedEnv }), " is now active for project", " ", _jsx(Text, { color: "cyan", children: projectName })] }) }));
}
//# sourceMappingURL=select.js.map