|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { Text, Box, useApp } from 'ink'; |
| 3 | +import { getConfigValue } from '../utils/config.js'; |
| 4 | +import { getBaseUrl, getDashboardUrl as resolveDashboardUrl } from '../utils/baseUrl.js'; |
| 5 | + |
| 6 | +export default function GetDashboardUrl() { |
| 7 | + const { exit } = useApp(); |
| 8 | + const [state, setState] = useState({ status: 'loading' }); |
| 9 | + |
| 10 | + const envValue = process.env.CODEANT_DASHBOARD_URL; |
| 11 | + const configValue = getConfigValue('dashboardUrl'); |
| 12 | + const source = envValue ? 'env' : configValue ? 'config' : 'upstream (queried from API base URL)'; |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + (async () => { |
| 16 | + try { |
| 17 | + const url = await resolveDashboardUrl(); |
| 18 | + setState({ status: 'resolved', url }); |
| 19 | + } catch (err) { |
| 20 | + setState({ status: 'error', message: err.message }); |
| 21 | + } |
| 22 | + exit(); |
| 23 | + })(); |
| 24 | + }, []); |
| 25 | + |
| 26 | + if (state.status === 'loading') { |
| 27 | + return React.createElement( |
| 28 | + Box, |
| 29 | + { flexDirection: 'column', padding: 1 }, |
| 30 | + React.createElement(Text, null, 'Querying dashboard URL...') |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + if (state.status === 'error') { |
| 35 | + return React.createElement( |
| 36 | + Box, |
| 37 | + { flexDirection: 'column', padding: 1 }, |
| 38 | + React.createElement(Text, { color: 'red' }, '✗ ', state.message), |
| 39 | + React.createElement(Text, { color: 'gray' }, 'Base URL: ', getBaseUrl()) |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + return React.createElement( |
| 44 | + Box, |
| 45 | + { flexDirection: 'column', padding: 1 }, |
| 46 | + React.createElement(Text, { bold: true }, 'Dashboard URL: ', state.url), |
| 47 | + React.createElement(Text, { color: 'gray' }, 'Source: ', source) |
| 48 | + ); |
| 49 | +} |
0 commit comments