import { Component, createMemo, createSignal, Show } from 'solid-js' import { query } from '../graphql' import { EnvVariable } from '../graphql/generated/schema' import { ADMIN_UPDATE_ENV_VARIABLE_MUTATION } from '../graphql/mutations' import formStyles from '../styles/Form.module.css' import Button from '../ui/Button' import Modal from '../ui/Modal' import TextPreview from '../ui/TextPreview' interface EnvVariableModalProps { isOpen: boolean variable: EnvVariable onClose: () => void onSave: () => void onValueChange?: (value: string) => void // FIXME: no need } const EnvVariableModal: Component = (props) => { const [value, setValue] = createSignal(props.variable.value) const [saving, setSaving] = createSignal(false) const [error, setError] = createSignal(null) const [showFormatted, setShowFormatted] = createSignal(false) // Определяем нужно ли использовать textarea const needsTextarea = createMemo(() => { const val = value() return ( val.length > 50 || val.includes('\n') || props.variable.type === 'json' || props.variable.key.includes('URL') || props.variable.key.includes('SECRET') ) }) // Форматируем JSON если возможно const formattedValue = createMemo(() => { if (props.variable.type === 'json' || (value().startsWith('{') && value().endsWith('}'))) { try { return JSON.stringify(JSON.parse(value()), null, 2) } catch { return value() } } return value() }) const handleSave = async () => { setSaving(true) setError(null) try { const result = await query<{ updateEnvVariable: boolean }>( `${location.origin}/graphql`, ADMIN_UPDATE_ENV_VARIABLE_MUTATION, { key: props.variable.key, value: value() } ) if (result?.updateEnvVariable) { props.onSave() } else { setError('Failed to update environment variable') } } catch (err) { setError(err instanceof Error ? err.message : 'Unknown error occurred') } finally { setSaving(false) } } const formatValue = () => { if (props.variable.type === 'json') { try { const formatted = JSON.stringify(JSON.parse(value()), null, 2) setValue(formatted) } catch (_e) { setError('Invalid JSON format') } } } return (
e.preventDefault()}>