Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

4.2 KB
Raw Blame
'use client';

import { useState } from 'react';
import { toast } from 'sonner';
import { useTranslations } from 'next-intl';
import type {
  NotificationEncryption,
  NotificationSettings,
} from '@/lib/api/endpoints/notificationSettings';
import { useUpdateNotificationSettings } from '@/services/teams.service';
import type { EmailProvider } from '@/components/common/inputs/ProviderToggle';
import { toPositiveInt } from '@/lib/utils';

export interface EmailForm {
  provider: EmailProvider;
  setProvider: (v: EmailProvider) => void;
  enabled: boolean;
  setEnabled: (v: boolean) => void;
  host: string;
  setHost: (v: string) => void;
  port: string;
  setPort: (v: string) => void;
  encryption: NotificationEncryption;
  setEncryption: (v: NotificationEncryption) => void;
  username: string;
  setUsername: (v: string) => void;
  password: string;
  setPassword: (v: string) => void;
  timeout: string;
  setTimeout: (v: string) => void;
  apiKey: string;
  setApiKey: (v: string) => void;
  from: string;
  setFrom: (v: string) => void;
  settings: NotificationSettings;
  dirty: boolean;
  saving: boolean;
  save: () => Promise<void>;
}

// Form state for the Email notification provider tab (SMTP or Resend). Shared between
// the tab's Save button and the body fields, so it lives in a hook. Secrets start
// blank and are sent only when changed. The whole tab saves at once.
export function useEmailForm(teamId: number, settings: NotificationSettings): EmailForm {
  const t = useTranslations('teams.notifications');
  const update = useUpdateNotificationSettings(teamId);

  // The team's own provider wins when it configured one; otherwise it sends
  // through the instance provider, which is also the default for a new team.
  const initialProvider: EmailProvider = settings.smtp.enabled
    ? 'smtp'
    : settings.resend.enabled
      ? 'resend'
      : 'system';
  const initialEnabled =
    settings.smtp.enabled || settings.resend.enabled || settings.system.enabled;
  const initialPort = settings.smtp.port == null ? '' : String(settings.smtp.port);
  const initialTimeout = settings.smtp.timeout == null ? '' : String(settings.smtp.timeout);

  const [provider, setProvider] = useState<EmailProvider>(initialProvider);
  const [enabled, setEnabled] = useState(initialEnabled);
  const [host, setHost] = useState(settings.smtp.host);
  const [port, setPort] = useState(initialPort);
  const [encryption, setEncryption] = useState<NotificationEncryption>(settings.smtp.encryption);
  const [username, setUsername] = useState(settings.smtp.username);
  const [password, setPassword] = useState('');
  const [timeout, setTimeout] = useState(initialTimeout);
  const [apiKey, setApiKey] = useState('');
  const [from, setFrom] = useState(settings.from);

  const smtpDirty =
    host !== settings.smtp.host ||
    port !== initialPort ||
    encryption !== settings.smtp.encryption ||
    username !== settings.smtp.username ||
    timeout !== initialTimeout ||
    password.length > 0;
  const dirty =
    provider !== initialProvider ||
    enabled !== initialEnabled ||
    (provider !== 'system' && from !== settings.from) ||
    (provider === 'smtp' ? smtpDirty : provider === 'resend' ? apiKey.length > 0 : false);

  async function save() {
    await update.mutateAsync({
      system: { enabled: provider === 'system' && enabled },
      smtp: {
        enabled: provider === 'smtp' && enabled,
        host: host.trim(),
        port: toPositiveInt(port),
        encryption,
        username: username.trim(),
        ...(password.length > 0 ? { password } : {}),
        timeout: toPositiveInt(timeout),
      },
      resend: {
        enabled: provider === 'resend' && enabled,
        ...(apiKey.length > 0 ? { apiKey } : {}),
      },
      from: from.trim(),
    });
    setPassword('');
    setApiKey('');
    toast.success(t('emailSaved'));
  }

  return {
    provider,
    setProvider,
    enabled,
    setEnabled,
    host,
    setHost,
    port,
    setPort,
    encryption,
    setEncryption,
    username,
    setUsername,
    password,
    setPassword,
    timeout,
    setTimeout,
    apiKey,
    setApiKey,
    from,
    setFrom,
    settings,
    dirty,
    saving: update.isPending,
    save,
  };
}