import { useCallback, useMemo, useState } from 'react'; import type { Tokens } from '../gen/Tokens'; import { useActiveEnvironmentVariables } from '../hooks/useActiveEnvironmentVariables'; import { useRenderTemplate } from '../hooks/useRenderTemplate'; import { useTemplateTokensToString } from '../hooks/useTemplateTokensToString'; import { Button } from './core/Button'; import { InlineCode } from './core/InlineCode'; import { Select } from './core/Select'; import { VStack } from './core/Stacks'; interface Props { initialTokens: Tokens; hide: () => void; onChange: (rawTag: string) => void; } export function TemplateVariableDialog({ hide, onChange, initialTokens }: Props) { const variables = useActiveEnvironmentVariables(); const [selectedVariableName, setSelectedVariableName] = useState(() => { return initialTokens.tokens[0]?.type === 'tag' && initialTokens.tokens[0]?.val.type === 'var' ? initialTokens.tokens[0]?.val.name : ''; // Should never happen }); const tokens: Tokens = useMemo(() => { const selectedVariable = variables.find((v) => v.name === selectedVariableName); return { tokens: [ { type: 'tag', val: { type: 'var', name: selectedVariable?.name ?? '', }, }, ], }; }, [selectedVariableName, variables]); const tagText = useTemplateTokensToString(tokens); const handleDone = useCallback(async () => { if (tagText.data != null) { onChange(tagText.data); } hide(); }, [hide, onChange, tagText.data]); const rendered = useRenderTemplate(tagText.data ?? ''); return (