Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

15.7 KB
Raw Blame
'use client';

import { isRecord } from '@lobechat/utils/object';
import {
  ActionIcon,
  Button,
  createStaticStyles,
  cssVar,
  Flexbox,
  Icon,
  Input,
  Text,
  TextArea,
} from '@lobehub/ui';
import { BracesIcon, CheckIcon, PencilIcon, PlusIcon, Trash2Icon, XIcon } from 'lucide-react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useIMECompositionEvent } from '@/hooks/useIMECompositionEvent';
import { densityVars } from '@/styles/densityVars';

import { describeError } from './errorMessage';

const styles = createStaticStyles(({ css }) => ({
  /** Railway's variables list: a framed block with a rule between rows. */
  list: css`
    overflow: hidden;
    border: 1px solid ${cssVar.colorBorderSecondary};
    border-radius: ${cssVar.borderRadiusLG};
  `,
  row: css`
    padding-block: 8px;
    padding-inline: ${densityVars.contentGap};

    & + & {
      border-block-start: 1px solid ${cssVar.colorBorderSecondary};
    }
  `,
  mono: css`
    font-family: ${cssVar.fontFamilyCode};
    font-size: 13px;
    word-break: break-all;
  `,
  /**
   * Dashed, the way Railway frames "no variables": the outline says a list
   * belongs here, and the dashes say it has not been filled in, without the
   * weight a solid box would give an absence.
   */
  empty: css`
    padding-block: 32px;
    padding-inline: ${densityVars.cardPadding};
    border: 1px dashed ${cssVar.colorBorder};
    border-radius: ${cssVar.borderRadiusLG};
  `,
  editor: css`
    font-family: ${cssVar.fontFamilyCode};
    font-size: ${cssVar.fontSizeSM};
  `,
}));

type Entry = [string, string];

/**
 * Parse `.env`-style text: one `KEY=value` per line, blank lines and `#`
 * comments skipped, an optional `export ` prefix and a matching pair of quotes
 * around the value dropped. Quoted values keep literal backslashes; JSON
 * escaping belongs to the separate JSON editor format. Later duplicate keys win,
 * as they would when a shell sources the file.
 *
 * @returns the entries, or the 1-based number of the first line that is not a
 *   `KEY=value` pair
 */
export const parseDotenv = (text: string): { entries: Entry[] } | { line: number } => {
  const entries = new Map<string, string>();
  const lines = text.split(/\r?\n/);
  for (const [index, raw] of lines.entries()) {
    const line = raw.trim();
    if (!line || line.startsWith('#')) continue;
    const match = line.match(/^(?:export\s+)?([A-Za-z_]\w*)\s*=(.*)$/);
    if (!match) return { line: index + 1 };
    let value = match[2].trim();
    if (value.length >= 2 && (value[0] === '"' || value[0] === "'") && value.at(-1) === value[0]) {
      value = value.slice(1, -1);
    }
    entries.set(match[1], value);
  }
  return { entries: [...entries.entries()] };
};

/** JSON is unambiguous for editing escapes; .env imports retain literal semantics. */
export const formatRawVariables = (entries: Entry[]) =>
  JSON.stringify(Object.fromEntries(entries), null, 2);

export const getRawVariableFormat = (text: string) =>
  /^[{[]/.test(text.trimStart()) ? 'json' : 'dotenv';

export const parseJsonVariables = (
  text: string,
): { entries: Entry[] } | { error: 'jsonSyntax' | 'jsonValues' } => {
  let value: unknown;
  try {
    value = JSON.parse(text);
  } catch {
    return { error: 'jsonSyntax' };
  }
  if (!isRecord(value)) return { error: 'jsonValues' };
  const entries: Entry[] = [];
  for (const [key, entry] of Object.entries(value)) {
    if (typeof entry !== 'string') return { error: 'jsonValues' };
    entries.push([key, entry]);
  }
  return { entries };
};

export const parseRawVariables = (text: string) =>
  getRawVariableFormat(text) === 'json' ? parseJsonVariables(text) : parseDotenv(text);

/**
 * One variable, either as a row or open for editing. Key and value edit
 * together: a variable is one thing, and confirming half of it is not a state
 * anyone wants stored.
 */
const EnvRow = memo<{
  entry: Entry;
  onRemove: () => Promise<void>;
  onSave: (entry: Entry) => Promise<void>;
}>(({ entry, onRemove, onSave }) => {
  const { t } = useTranslation('setting');
  const { t: tCommon } = useTranslation('common');
  const [draft, setDraft] = useState<Entry | null>(null);
  const [busy, setBusy] = useState(false);
  /** Shown under the row being edited (or beside the row whose remove failed), not as a toast. */
  const [error, setError] = useState('');
  const { compositionProps, isComposingRef } = useIMECompositionEvent();

  const run = async (action: () => Promise<void>) => {
    setBusy(true);
    setError('');
    try {
      await action();
      setDraft(null);
    } catch (err) {
      console.error('[EnvironmentVariables] Save failed:', err);
      setError(describeError(err, t, t('environments.saveFailedRetry')));
    } finally {
      setBusy(false);
    }
  };

  if (draft) {
    return (
      <Flexbox horizontal align={'center'} className={styles.row} gap={8} wrap={'wrap'}>
        <Input
          autoFocus
          aria-invalid={!!error}
          data-invalid={error ? '' : undefined}
          placeholder={'NODE_ENV'}
          style={{ flex: 1 }}
          value={draft[0]}
          onChange={(event) => {
            setDraft([event.target.value, draft[1]]);
            setError('');
          }}
        />
        <Input
          aria-invalid={!!error}
          data-invalid={error ? '' : undefined}
          placeholder={'production'}
          style={{ flex: 1 }}
          value={draft[1]}
          onChange={(event) => {
            setDraft([draft[0], event.target.value]);
            setError('');
          }}
          onKeyDown={(event) => {
            if (event.key === 'Escape') setDraft(null);
            // Not the Enter that confirms an IME candidate: a value typed in
            // Chinese would otherwise save on the keystroke that picked it.
            if (event.key === 'Enter' && !isComposingRef.current && draft[0].trim()) {
              void run(() => onSave([draft[0].trim(), draft[1]]));
            }
          }}
          {...compositionProps}
        />
        <ActionIcon
          disabled={!draft[0].trim()}
          icon={CheckIcon}
          loading={busy}
          size={'small'}
          title={tCommon('save')}
          onClick={() => run(() => onSave([draft[0].trim(), draft[1]]))}
        />
        <ActionIcon
          disabled={busy}
          icon={XIcon}
          size={'small'}
          title={tCommon('cancel')}
          onClick={() => {
            setDraft(null);
            setError('');
          }}
        />
        {error && (
          <Text fontSize={12} role={'alert'} style={{ flexBasis: '100%' }} type={'danger'}>
            {error}
          </Text>
        )}
      </Flexbox>
    );
  }

  return (
    <Flexbox horizontal align={'center'} className={styles.row} gap={8} wrap={'wrap'}>
      <span className={styles.mono} style={{ flex: 1 }}>
        {entry[0]}
      </span>
      {/* Shown, not starred out: the hint above says these are stored in the
          clear and copied into every instance, and a mask would promise a
          secrecy the storage does not keep. */}
      <span className={styles.mono} style={{ color: cssVar.colorTextSecondary, flex: 1 }}>
        {entry[1]}
      </span>
      <ActionIcon
        icon={PencilIcon}
        size={'small'}
        title={t('environments.form.editEnv')}
        onClick={() => setDraft([entry[0], entry[1]])}
      />
      <ActionIcon
        icon={Trash2Icon}
        loading={busy}
        size={'small'}
        title={t('environments.form.removeEnv')}
        onClick={() => run(onRemove)}
      />
      {error && (
        <Text fontSize={12} role={'alert'} style={{ flexBasis: '100%' }} type={'danger'}>
          {error}
        </Text>
      )}
    </Flexbox>
  );
});

EnvRow.displayName = 'EnvironmentVariableRow';

interface EnvironmentVariablesProps {
  entries: Entry[];
  /** Writes the whole set; every change here is one specification save. */
  onSave: (entries: Entry[]) => Promise<void>;
}

/**
 * An environment's variables, laid out the way Railway lays out a service's.
 *
 * A heading that counts them, with the two ways in on the right: a new row for
 * one variable, or the raw editor for a whole `.env` at once. New rows open at
 * the top of the list so the field is where the button was, not below a list
 * that may be long.
 */
const EnvironmentVariables = memo<EnvironmentVariablesProps>(({ entries, onSave }) => {
  const { t } = useTranslation('setting');
  const [adding, setAdding] = useState<Entry | null>(null);
  const [raw, setRaw] = useState<string | null>(null);
  const [busy, setBusy] = useState(false);
  /**
   * Errors of the raw editor and of the new-variable row. Both surfaces stay open on failure, so
   * the reason renders under the textarea / inside the row instead of a corner toast.
   */
  const [rawError, setRawError] = useState('');
  const [addError, setAddError] = useState('');
  const { compositionProps, isComposingRef } = useIMECompositionEvent();

  const run = async (action: () => Promise<void>, setError: (message: string) => void) => {
    setBusy(true);
    setError('');
    try {
      await action();
    } catch (err) {
      console.error('[EnvironmentVariables] Save failed:', err);
      setError(describeError(err, t, t('environments.saveFailedRetry')));
    } finally {
      setBusy(false);
    }
  };

  const openRaw = () => {
    setAdding(null);
    setAddError('');
    setRawError('');
    setRaw(formatRawVariables(entries));
  };

  const saveRaw = () => {
    if (raw === null) return;
    const parsed = parseRawVariables(raw);
    if ('error' in parsed) {
      setRawError(t(`environments.form.${parsed.error}`));
      return;
    }
    if ('line' in parsed) {
      setRawError(t('environments.form.rawInvalid', { line: parsed.line }));
      return;
    }
    void run(async () => {
      await onSave(parsed.entries);
      setRaw(null);
    }, setRawError);
  };

  const addNew = () =>
    run(async () => {
      if (!adding) return;
      const key = adding[0].trim();
      await onSave([...entries.filter(([k]) => k !== key), [key, adding[1]]]);
      setAdding(null);
    }, setAddError);

  return (
    <Flexbox gap={12}>
      <Flexbox horizontal align={'center'} gap={8} justify={'space-between'}>
        {/* The section around this editor carries its title; only the count is said here. */}
        <Text fontSize={12} type={'secondary'}>
          {entries.length > 0 && t('environments.form.envCount', { count: entries.length })}
        </Text>
        <Flexbox horizontal align={'center'} gap={4}>
          {raw === null ? (
            <>
              <Button
                icon={<Icon icon={BracesIcon} />}
                size={'small'}
                type={'text'}
                onClick={openRaw}
              >
                {t('environments.form.rawEditor')}
              </Button>
              <Button
                disabled={adding !== null}
                icon={<Icon icon={PlusIcon} />}
                size={'small'}
                type={'primary'}
                onClick={() => {
                  setAddError('');
                  setAdding(['', '']);
                }}
              >
                {t('environments.form.addEnv')}
              </Button>
            </>
          ) : (
            <>
              <Button
                disabled={busy}
                size={'small'}
                onClick={() => {
                  setRaw(null);
                  setRawError('');
                }}
              >
                {t('cancel', { ns: 'common' })}
              </Button>
              <Button loading={busy} size={'small'} type={'primary'} onClick={saveRaw}>
                {t('save', { ns: 'common' })}
              </Button>
            </>
          )}
        </Flexbox>
      </Flexbox>

      {raw !== null ? (
        <Flexbox gap={6}>
          <Text fontSize={12} weight={500}>
            {t('environments.form.rawFormat', {
              format: getRawVariableFormat(raw) === 'json' ? 'JSON' : '.env',
            })}
          </Text>
          <TextArea
            autoFocus
            aria-invalid={!!rawError}
            aria-label={t('environments.form.rawEditor')}
            classNames={{ input: styles.editor }}
            data-invalid={rawError ? '' : undefined}
            placeholder={'{\n  "NODE_ENV": "production"\n}'}
            rows={Math.min(16, Math.max(6, raw.split('\n').length + 1))}
            value={raw}
            onChange={(event) => {
              setRaw(event.target.value);
              setRawError('');
            }}
          />
          {rawError && (
            <Text fontSize={12} role={'alert'} type={'danger'}>
              {rawError}
            </Text>
          )}
          <Text fontSize={12} type={'secondary'}>
            {t('environments.form.rawFormatsHint')}
          </Text>
        </Flexbox>
      ) : entries.length === 0 && !adding ? (
        <Flexbox align={'center'} className={styles.empty} gap={4}>
          <Text weight={500}>{t('environments.form.envEmpty')}</Text>
          <Flexbox horizontal align={'center'} gap={4}>
            <Text fontSize={12} type={'secondary'}>
              {t('environments.form.envEmptyHint')}
            </Text>
            <Button size={'small'} type={'link'} onClick={openRaw}>
              {t('environments.form.rawEditor')}
            </Button>
          </Flexbox>
        </Flexbox>
      ) : (
        <div className={styles.list}>
          {adding && (
            <Flexbox horizontal align={'center'} className={styles.row} gap={8} wrap={'wrap'}>
              <Input
                autoFocus
                aria-invalid={!!addError}
                data-invalid={addError ? '' : undefined}
                placeholder={'NODE_ENV'}
                style={{ flex: 1 }}
                value={adding[0]}
                onChange={(event) => {
                  setAdding([event.target.value, adding[1]]);
                  setAddError('');
                }}
              />
              <Input
                aria-invalid={!!addError}
                data-invalid={addError ? '' : undefined}
                placeholder={'production'}
                style={{ flex: 1 }}
                value={adding[1]}
                onChange={(event) => {
                  setAdding([adding[0], event.target.value]);
                  setAddError('');
                }}
                onKeyDown={(event) => {
                  if (event.key === 'Escape') setAdding(null);
                  if (event.key === 'Enter' && !isComposingRef.current && adding[0].trim()) {
                    void addNew();
                  }
                }}
                {...compositionProps}
              />
              <ActionIcon
                disabled={!adding[0].trim()}
                icon={CheckIcon}
                loading={busy}
                size={'small'}
                title={t('save', { ns: 'common' })}
                onClick={addNew}
              />
              <ActionIcon
                disabled={busy}
                icon={XIcon}
                size={'small'}
                title={t('cancel', { ns: 'common' })}
                onClick={() => {
                  setAdding(null);
                  setAddError('');
                }}
              />
              {addError && (
                <Text fontSize={12} role={'alert'} style={{ flexBasis: '100%' }} type={'danger'}>
                  {addError}
                </Text>
              )}
            </Flexbox>
          )}
          {entries.map(([key, value]) => (
            <EnvRow
              entry={[key, value]}
              key={key}
              onRemove={() => onSave(entries.filter(([k]) => k !== key))}
              onSave={(entry) => onSave(entries.map(([k, v]) => (k === key ? entry : [k, v])))}
            />
          ))}
        </div>
      )}
    </Flexbox>
  );
});

EnvironmentVariables.displayName = 'EnvironmentVariables';

export default EnvironmentVariables;