Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

13.0 KB
Raw Blame
import { Show, For, createUniqueId } from 'solid-js';
import type { UIEmailConfig } from '@/features/alerts/types';
import {
  ALERT_EMAIL_FROM_ADDRESS_LABEL,
  ALERT_EMAIL_FROM_ADDRESS_PLACEHOLDER,
  ALERT_EMAIL_MANUAL_CONFIGURATION_LABEL,
  ALERT_EMAIL_PASSWORD_LABEL,
  ALERT_EMAIL_PASSWORD_PLACEHOLDER,
  ALERT_EMAIL_PROVIDER_LABEL,
  ALERT_EMAIL_RATE_LIMIT_LABEL,
  ALERT_EMAIL_RATE_LIMIT_SUFFIX,
  ALERT_EMAIL_REAPPLY_DEFAULTS_LABEL,
  ALERT_EMAIL_RECIPIENTS_LABEL,
  ALERT_EMAIL_RETRY_POLICY_HELP,
  ALERT_EMAIL_SECURITY_LABEL,
  ALERT_EMAIL_SECURITY_NONE_LABEL,
  ALERT_EMAIL_SECURITY_STARTTLS_LABEL,
  ALERT_EMAIL_SECURITY_TLS_LABEL,
  ALERT_EMAIL_SMTP_PORT_LABEL,
  ALERT_EMAIL_SMTP_PORT_PLACEHOLDER,
  ALERT_EMAIL_SMTP_SERVER_LABEL,
  ALERT_EMAIL_SMTP_SERVER_PLACEHOLDER,
  ALERT_EMAIL_USERNAME_LABEL,
  getAlertEmailAdvancedToggleLabel,
  getAlertEmailProviderOptionLabel,
  getAlertEmailRecipientsPlaceholder,
  getAlertEmailSetupInstructionsToggleLabel,
  getAlertEmailTestButtonLabel,
  getAlertEmailUsernamePlaceholder,
} from '@/utils/alertEmailPresentation';
import { formField, labelClass, controlClass, formHelpText } from '@/components/shared/Form';
import {
  useEmailProviderSelectState,
  type EmailProviderSelectStateProps,
} from './useEmailProviderSelectState';
import { FormSelect } from '@/components/shared/FormSelect';
import { FormTextarea } from '@/components/shared/FormTextarea';
import { TagInput } from '@/components/shared/TagInput';
import { DestinationSeveritySelect } from './DestinationSeveritySelect';

interface EmailProviderSelectProps extends EmailProviderSelectStateProps {
  config: UIEmailConfig;
  onTest: () => void;
  testing?: boolean;
}

export function EmailProviderSelect(props: EmailProviderSelectProps) {
  const state = useEmailProviderSelectState(props);
  const fieldIdPrefix = `alert-email-${createUniqueId()}`;
  const fieldIds = {
    smtpServer: `${fieldIdPrefix}-smtp-server`,
    smtpPort: `${fieldIdPrefix}-smtp-port`,
    fromAddress: `${fieldIdPrefix}-from-address`,
    username: `${fieldIdPrefix}-username`,
    password: `${fieldIdPrefix}-password`,
    recipients: `${fieldIdPrefix}-recipients`,
    rateLimit: `${fieldIdPrefix}-rate-limit`,
    tagMode: `${fieldIdPrefix}-tag-mode`,
    minimumSeverity: `${fieldIdPrefix}-minimum-severity`,
  };
  const instructionBoxClass =
    'mt-2 rounded-sm border border-blue-200 bg-blue-50 px-3 py-2 text-xs leading-relaxed text-blue-900 dark:border-blue-700 dark:bg-blue-900/25 dark:text-blue-200';

  return (
    <div class="space-y-4 text-sm overflow-hidden">
      <div class={formField}>
        <div class="flex w-full flex-wrap items-end gap-2 sm:flex-nowrap">
          <FormSelect
            id="alert-email-provider-select"
            label={ALERT_EMAIL_PROVIDER_LABEL}
            value={props.config.provider}
            onChange={(e) => state.handleProviderChange(e.currentTarget.value)}
            selectBaseClass={controlClass('px-2 py-1.5')}
            selectClass="sm:w-auto sm:min-w-[180px]"
          >
            <option value="">{ALERT_EMAIL_MANUAL_CONFIGURATION_LABEL}</option>
            <For each={state.providers()}>
              {(provider) => (
                <option value={provider.name}>{getAlertEmailProviderOptionLabel(provider)}</option>
              )}
            </For>
          </FormSelect>
          <Show when={props.config.provider}>
            <button
              type="button"
              onClick={() => {
                const provider = state.currentProvider();
                if (provider) state.applyProviderDefaults(provider);
              }}
              class="min-h-11 text-xs font-medium text-blue-600 hover:underline sm:min-h-0 dark:text-blue-400"
            >
              {ALERT_EMAIL_REAPPLY_DEFAULTS_LABEL}
            </button>
          </Show>
        </div>
      </div>

      <Show when={state.currentProvider()}>
        <div class="sm:hidden w-full">
          <button
            type="button"
            onClick={state.toggleShowInstructions}
            class="min-h-11 text-xs font-medium text-blue-600 hover:underline sm:min-h-0 dark:text-blue-300"
          >
            {getAlertEmailSetupInstructionsToggleLabel(state.showInstructions())}
          </button>
          <Show when={state.showInstructions()}>
            <div class={instructionBoxClass}>{state.currentProvider()!.instructions}</div>
          </Show>
        </div>
        <div class="hidden w-full sm:block">
          <div class={instructionBoxClass}>{state.currentProvider()!.instructions}</div>
        </div>
      </Show>

      <div class="grid w-full gap-3 sm:grid-cols-2">
        <div class={formField}>
          <label for={fieldIds.smtpServer} class={labelClass()}>
            {ALERT_EMAIL_SMTP_SERVER_LABEL}
          </label>
          <input
            id={fieldIds.smtpServer}
            type="text"
            value={props.config.server}
            onInput={(e) => props.onChange({ ...props.config, server: e.currentTarget.value })}
            placeholder={ALERT_EMAIL_SMTP_SERVER_PLACEHOLDER}
            class={controlClass('px-2 py-1.5')}
          />
        </div>

        <div class={formField}>
          <label for={fieldIds.smtpPort} class={labelClass()}>
            {ALERT_EMAIL_SMTP_PORT_LABEL}
          </label>
          <input
            id={fieldIds.smtpPort}
            type="number"
            min="1"
            max="65535"
            value={props.config.port || ''}
            onInput={(e) => {
              const value = e.currentTarget.value;
              // Allow empty field while typing, parse as number when valid
              const port = value === '' ? 0 : parseInt(value, 10);
              props.onChange({ ...props.config, port: isNaN(port) ? 0 : port });
            }}
            onBlur={(e) => {
              // Apply default on blur if empty or invalid
              const value = parseInt(e.currentTarget.value, 10);
              if (!value || isNaN(value)) {
                props.onChange({ ...props.config, port: 587 });
              }
            }}
            placeholder={ALERT_EMAIL_SMTP_PORT_PLACEHOLDER}
            class={controlClass('px-2 py-1.5')}
          />
        </div>

        <div class={`${formField} sm:col-span-2`}>
          <label for={fieldIds.fromAddress} class={labelClass()}>
            {ALERT_EMAIL_FROM_ADDRESS_LABEL}
          </label>
          <input
            id={fieldIds.fromAddress}
            type="email"
            value={props.config.from}
            onInput={(e) => props.onChange({ ...props.config, from: e.currentTarget.value })}
            placeholder={ALERT_EMAIL_FROM_ADDRESS_PLACEHOLDER}
            class={controlClass('px-2 py-1.5')}
          />
        </div>

        <div class={formField}>
          <label for={fieldIds.username} class={labelClass()}>
            {ALERT_EMAIL_USERNAME_LABEL}
          </label>
          <input
            id={fieldIds.username}
            type="text"
            value={props.config.username}
            onInput={(e) => props.onChange({ ...props.config, username: e.currentTarget.value })}
            placeholder={getAlertEmailUsernamePlaceholder(props.config.provider)}
            class={controlClass('px-2 py-1.5')}
            autocomplete="off"
          />
        </div>

        <div class={formField}>
          <label for={fieldIds.password} class={labelClass()}>
            {ALERT_EMAIL_PASSWORD_LABEL}
          </label>
          <input
            id={fieldIds.password}
            type="password"
            value={props.config.password}
            onInput={(e) => props.onChange({ ...props.config, password: e.currentTarget.value })}
            placeholder={ALERT_EMAIL_PASSWORD_PLACEHOLDER}
            class={controlClass('px-2 py-1.5')}
            autocomplete="new-password"
          />
        </div>
      </div>

      <FormTextarea
        id={fieldIds.recipients}
        label={ALERT_EMAIL_RECIPIENTS_LABEL}
        value={props.config.to.join('\n')}
        onInput={(e) => {
          // Keep raw lines while editing; trimming/filtering on every keystroke
          // eats empty trailing lines and typed whitespace.
          props.onChange({ ...props.config, to: e.currentTarget.value.split('\n') });
        }}
        onKeyDown={(e) => {
          if (e.key === 'Enter') {
            e.stopPropagation();
          }
        }}
        rows={3}
        textareaBaseClass={controlClass('px-2 py-1.5 font-mono leading-snug')}
        placeholder={getAlertEmailRecipientsPlaceholder(props.config.from)}
      />

      <div class="space-y-2 border-t border-border pt-3">
        <DestinationSeveritySelect
          id={fieldIds.minimumSeverity}
          value={props.config.minimumSeverity ?? 'all'}
          compact
          onChange={(minimumSeverity) => props.onChange({ ...props.config, minimumSeverity })}
        />
      </div>

      <div class="space-y-2 border-t border-border pt-3">
        <div>
          <div class={labelClass()}>Resource tag routing</div>
          <p class={formHelpText + ' mt-1'}>
            Leave empty to receive all alerts. Add tags to deliver only alerts from matching
            resources.
          </p>
        </div>
        <TagInput
          tags={props.config.tagFilter ?? []}
          label="Resource tag routing"
          onChange={(tagFilter) => props.onChange({ ...props.config, tagFilter })}
          placeholder="Add a resource tag"
        />
        <Show when={(props.config.tagFilter?.length ?? 0) > 1}>
          <FormSelect
            id={fieldIds.tagMode}
            label="Tag matching"
            value={props.config.tagFilterMode === 'any' ? 'any' : 'all'}
            onChange={(event) =>
              props.onChange({
                ...props.config,
                tagFilterMode: event.currentTarget.value === 'any' ? 'any' : 'all',
              })
            }
            selectBaseClass={controlClass('px-2 py-1.5')}
          >
            <option value="all">Match all tags</option>
            <option value="any">Match any tag</option>
          </FormSelect>
        </Show>
      </div>

      <div class="border-t border-border pt-3">
        <button
          type="button"
          onClick={state.toggleShowAdvanced}
          class="min-h-11 text-xs font-semibold uppercase tracking-wide transition-colors hover:text-muted sm:min-h-0"
        >
          {getAlertEmailAdvancedToggleLabel(state.showAdvanced())}
        </button>

        <Show when={state.showAdvanced()}>
          <div class="mt-3 space-y-3 text-xs text-base-content">
            <div class="grid gap-3 sm:grid-cols-2">
              <FormSelect
                id="alert-email-security-select"
                label={ALERT_EMAIL_SECURITY_LABEL}
                value={props.config.tls ? 'tls' : props.config.startTLS ? 'starttls' : 'none'}
                onChange={(e) => {
                  const value = e.currentTarget.value;
                  props.onChange({
                    ...props.config,
                    tls: value === 'tls',
                    startTLS: value === 'starttls',
                  });
                }}
                fieldBaseClass="flex"
                fieldClass="flex-row items-center gap-2"
                labelClass="text-xs uppercase tracking-[0.08em]"
                selectBaseClass={controlClass('px-2 py-1 text-sm')}
                selectClass="min-w-[120px]"
              >
                <option value="none">{ALERT_EMAIL_SECURITY_NONE_LABEL}</option>
                <option value="starttls">{ALERT_EMAIL_SECURITY_STARTTLS_LABEL}</option>
                <option value="tls">{ALERT_EMAIL_SECURITY_TLS_LABEL}</option>
              </FormSelect>
              <div class="flex w-full flex-wrap items-center gap-2 sm:flex-nowrap">
                <label
                  for={fieldIds.rateLimit}
                  class={labelClass('text-xs uppercase tracking-[0.08em]')}
                >
                  {ALERT_EMAIL_RATE_LIMIT_LABEL}
                </label>
                <input
                  id={fieldIds.rateLimit}
                  type="number"
                  min="1"
                  value={props.config.rateLimit || 60}
                  onInput={(e) =>
                    props.onChange({ ...props.config, rateLimit: parseInt(e.currentTarget.value) })
                  }
                  class={`${controlClass('px-2 py-1 text-sm')} w-20`}
                />
                <span class={formHelpText}>{ALERT_EMAIL_RATE_LIMIT_SUFFIX}</span>
              </div>
            </div>

            <p class={formHelpText}>{ALERT_EMAIL_RETRY_POLICY_HELP}</p>
          </div>
        </Show>
      </div>

      <div class="flex justify-end pt-2">
        <button
          type="button"
          onClick={props.onTest}
          disabled={props.testing || !props.config.enabled}
          class="min-h-11 rounded-sm border border-blue-500 px-3 py-1.5 text-xs font-medium text-blue-600 transition-colors hover:bg-blue-50 disabled:opacity-50 disabled:cursor-not-allowed sm:min-h-0 dark:border-blue-400 dark:text-blue-300 dark:hover:bg-blue-900/25"
        >
          {getAlertEmailTestButtonLabel(Boolean(props.testing))}
        </button>
      </div>
    </div>
  );
}