import React from 'react'
import { Box, Newline, Text, useInput } from 'ink'
import { getTheme } from '@utils/theme'
import { Select } from './CustomSelect/select'
import { render } from 'ink'
import { writeFileSync } from 'fs'
import { ConfigParseError } from '@utils/errors'
import { useExitOnCtrlCD } from '@hooks/useExitOnCtrlCD'
interface InvalidConfigHandlerProps {
error: ConfigParseError
}
interface InvalidConfigDialogProps {
filePath: string
errorDescription: string
onExit: () => void
onReset: () => void
}
/**
* Dialog shown when the Kode config file contains invalid JSON
*/
function InvalidConfigDialog({
filePath,
errorDescription,
onExit,
onReset,
}: InvalidConfigDialogProps): React.ReactNode {
const theme = getTheme()
// Handle escape key
useInput((_, key) => {
if (key.escape) {
onExit()
}
})
const exitState = useExitOnCtrlCD(() => process.exit(0))
// Handler for Select onChange
const handleSelect = (value: string) => {
if (value === 'exit') {
onExit()
} else {
onReset()
}
}
return (
<>
Configuration Error
The configuration file at {filePath} contains
invalid JSON.
{errorDescription}
Choose an option:
{exitState.pending ? (
Press {exitState.keyName} again to exit
) : (
)}
>
)
}
export function showInvalidConfigDialog({
error,
}: InvalidConfigHandlerProps): Promise {
return new Promise(resolve => {
render(
{
resolve()
process.exit(1)
}}
onReset={() => {
writeFileSync(
error.filePath,
JSON.stringify(error.defaultConfig, null, 2),
)
resolve()
process.exit(0)
}}
/>,
{ exitOnCtrlC: false },
)
})
}