App.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. import { App as AntApp, ConfigProvider, Form, Input, Modal, Select } from 'antd';
  2. import { useEffect, useMemo, useState } from 'react';
  3. import { AddDomainDrawer } from '../components/domain/AddDomainDrawer';
  4. import { AdminLayout } from '../layouts/AdminLayout';
  5. import AdminPage from '../pages/Admin';
  6. import ApiTokens from '../pages/ApiTokens';
  7. import Dashboard from '../pages/Dashboard';
  8. import DnsApi from '../pages/DnsApi';
  9. import DomainDetail from '../pages/Domains/DomainDetail';
  10. import DomainsPage from '../pages/Domains';
  11. import Inbox from '../pages/Inbox';
  12. import PlaceholderPage from '../pages/PlaceholderPage';
  13. import SendingLogs from '../pages/SendingLogs';
  14. import Settings from '../pages/Settings';
  15. import SmtpCredentials from '../pages/SmtpCredentials';
  16. import Webhooks from '../pages/Webhooks';
  17. import { I18nProvider, useI18n } from './i18n/react';
  18. import { buildDnsApplyFeedback } from './domain-model.js';
  19. import { api } from './services/api';
  20. import './styles.css';
  21. import { mailhubTheme } from './theme';
  22. import type {
  23. AddDomainPayload,
  24. ApiToken,
  25. AppData,
  26. DnsCredential,
  27. Domain,
  28. DomainMode,
  29. DomainPatchPayload,
  30. InboundMailbox,
  31. InboundMessage,
  32. MailboxClientConfig,
  33. RuntimeConfig,
  34. SmtpCredential,
  35. SmtpRelay,
  36. SmtpRelayPayload,
  37. User,
  38. ViewKey
  39. } from './types';
  40. const emptyData: AppData = {
  41. me: null,
  42. config: null,
  43. domains: [],
  44. events: [],
  45. analytics: null,
  46. smtpCredential: null,
  47. smtpCredentials: [],
  48. smtpRelays: [],
  49. inboundMailboxes: [],
  50. inboundMessages: [],
  51. dnsCredentials: [],
  52. apiTokens: [],
  53. settings: null,
  54. users: []
  55. };
  56. const viewTitleKeys: Record<ViewKey, string> = {
  57. dashboard: 'nav.dashboard',
  58. domains: 'nav.domains',
  59. 'dns-api': 'nav.dnsApi',
  60. smtp: 'nav.smtp',
  61. inbox: 'nav.inbox',
  62. tokens: 'nav.tokens',
  63. logs: 'nav.logs',
  64. webhooks: 'nav.webhooks',
  65. admin: 'nav.admin',
  66. settings: 'nav.settings'
  67. };
  68. export default function App() {
  69. return (
  70. <ConfigProvider theme={mailhubTheme}>
  71. <AntApp>
  72. <I18nProvider>
  73. <MailHubConsole />
  74. </I18nProvider>
  75. </AntApp>
  76. </ConfigProvider>
  77. );
  78. }
  79. function MailHubConsole() {
  80. const { message } = AntApp.useApp();
  81. const { t } = useI18n();
  82. const [data, setData] = useState<AppData>(emptyData);
  83. const [activeView, setActiveView] = useState<ViewKey>('dashboard');
  84. const [domainMode, setDomainMode] = useState<DomainMode>('list');
  85. const [selectedDomainId, setSelectedDomainId] = useState<number | null>(null);
  86. const [initialDomainTab, setInitialDomainTab] = useState('overview');
  87. const [addOpen, setAddOpen] = useState(false);
  88. const [loading, setLoading] = useState(true);
  89. const [actionLoading, setActionLoading] = useState(false);
  90. const [testDomain, setTestDomain] = useState<Domain | null>(null);
  91. const [testForm] = Form.useForm();
  92. const selectedDomain = data.domains.find((domain) => domain.id === selectedDomainId) || data.domains[0] || null;
  93. useEffect(() => {
  94. void loadAll();
  95. }, []);
  96. async function loadAll() {
  97. setLoading(true);
  98. try {
  99. const me = await api.me();
  100. const [
  101. config,
  102. domains,
  103. events,
  104. inboundMailboxes,
  105. inboundMessages,
  106. analytics,
  107. smtpCredential,
  108. smtpCredentials,
  109. smtpRelays,
  110. dnsCredentials,
  111. apiTokens
  112. ] = await Promise.all([
  113. api.config(),
  114. api.domains(),
  115. api.events(),
  116. api.inboundMailboxes(),
  117. api.inboundMessages(),
  118. api.analytics(7),
  119. api.smtpCredential(),
  120. api.smtpCredentials(),
  121. api.smtpRelays(),
  122. api.dnsCredentials(),
  123. api.apiTokens()
  124. ]);
  125. let settings: RuntimeConfig | null = null;
  126. let users: User[] = [];
  127. if (me.user.role === 'admin') {
  128. const [settingsResult, usersResult] = await Promise.all([api.adminSettings(), api.adminUsers()]);
  129. settings = settingsResult.settings;
  130. users = usersResult.users;
  131. }
  132. setData({
  133. me: me.user,
  134. config,
  135. domains: domains.domains || [],
  136. events: events.events || [],
  137. inboundMailboxes: inboundMailboxes.mailboxes || [],
  138. inboundMessages: inboundMessages.messages || [],
  139. analytics: analytics.analytics || null,
  140. smtpCredential: smtpCredential.credential || null,
  141. smtpCredentials: smtpCredentials.credentials || [],
  142. smtpRelays: smtpRelays.relays || [],
  143. dnsCredentials: dnsCredentials.credentials || [],
  144. apiTokens: apiTokens.tokens || [],
  145. settings,
  146. users
  147. });
  148. setSelectedDomainId((current) => {
  149. if (current && domains.domains.some((domain) => domain.id === current)) return current;
  150. return domains.domains[0]?.id || null;
  151. });
  152. } catch (error) {
  153. const text = error instanceof Error ? error.message : t('common.error');
  154. message.error(text);
  155. if (/Authentication required/i.test(text)) window.location.href = '/login';
  156. } finally {
  157. setLoading(false);
  158. }
  159. }
  160. function replaceDomain(domain: Domain) {
  161. setData((current) => ({
  162. ...current,
  163. domains: current.domains.map((item) => item.id === domain.id ? domain : item)
  164. }));
  165. }
  166. async function runAction<T>(fn: () => Promise<T>, success?: string) {
  167. setActionLoading(true);
  168. try {
  169. const result = await fn();
  170. if (success) message.success(success);
  171. return result;
  172. } catch (error) {
  173. message.error(error instanceof Error ? error.message : t('common.error'));
  174. return null;
  175. } finally {
  176. setActionLoading(false);
  177. }
  178. }
  179. async function createDomain(values: AddDomainPayload) {
  180. const immediateCheck = Boolean(values.immediateCheck);
  181. const result = await runAction(async () => api.createDomain(values), t('actions.domainCreated'));
  182. if (!result?.domain) return;
  183. let nextDomain = result.domain;
  184. setData((current) => ({ ...current, domains: [nextDomain, ...current.domains] }));
  185. setSelectedDomainId(nextDomain.id);
  186. setActiveView('domains');
  187. setDomainMode('detail');
  188. setInitialDomainTab('dns');
  189. setAddOpen(false);
  190. if (immediateCheck) {
  191. const checked = await runAction(async () => api.checkDomain(nextDomain.id), t('actions.dnsCheckCompleted'));
  192. if (checked?.domain) {
  193. nextDomain = checked.domain;
  194. replaceDomain(nextDomain);
  195. }
  196. }
  197. }
  198. function viewDetail(domain: Domain, tab = 'overview') {
  199. setSelectedDomainId(domain.id);
  200. setInitialDomainTab(tab);
  201. setDomainMode('detail');
  202. setActiveView('domains');
  203. }
  204. async function checkDomain(domain: Domain) {
  205. const result = await runAction(async () => api.checkDomain(domain.id), t('actions.dnsCheckRefreshed'));
  206. if (result?.domain) replaceDomain(result.domain);
  207. }
  208. async function applyDns(domain: Domain) {
  209. const result = await runAction(async () => api.applyDns(domain.id));
  210. if (result) {
  211. const feedback = buildDnsApplyFeedback(result.apply, {
  212. completed: t('actions.dnsApplyCompleted'),
  213. partial: t('actions.dnsApplyPartial')
  214. });
  215. if (feedback.type === 'warning') {
  216. message.warning(feedback.message);
  217. } else {
  218. message.success(feedback.message);
  219. }
  220. }
  221. if (result?.domain) {
  222. replaceDomain(result.domain);
  223. setInitialDomainTab('dns');
  224. viewDetail(result.domain, 'dns');
  225. }
  226. }
  227. async function patchDomain(domain: Domain, values: DomainPatchPayload) {
  228. const result = await runAction(async () => api.patchDomain(domain.id, values), t('actions.domainSaved'));
  229. if (result?.domain) replaceDomain(result.domain);
  230. }
  231. async function deleteDomain(domain: Domain) {
  232. const result = await runAction(async () => api.deleteDomain(domain.id), t('actions.domainDeleted'));
  233. if (!result?.deleted) return;
  234. setData((current) => ({ ...current, domains: current.domains.filter((item) => item.id !== domain.id) }));
  235. if (selectedDomainId === domain.id) {
  236. setSelectedDomainId(null);
  237. setDomainMode('list');
  238. }
  239. }
  240. function openTestModal(domain: Domain) {
  241. setTestDomain(domain);
  242. testForm.setFieldsValue({
  243. from: `noreply@${domain.domain}`,
  244. subject: `MailHub test for ${domain.domain}`,
  245. text: `This is a MailHub test message from ${domain.domain}.`,
  246. html: `<p>This is a MailHub test message from ${domain.domain}.</p><p><a href="${window.location.origin}/login">Open MailHub</a></p>`,
  247. smtpRelayId: domain.smtpRelayId || undefined
  248. });
  249. }
  250. async function submitTestMail() {
  251. if (!testDomain) return;
  252. const values = await testForm.validateFields();
  253. await runAction(async () => api.sendTest(testDomain.id, values), t('actions.testMailQueued'));
  254. setTestDomain(null);
  255. const [events, analytics] = await Promise.all([api.events(), api.analytics(7)]);
  256. setData((current) => ({ ...current, events: events.events || [], analytics: analytics.analytics || current.analytics }));
  257. }
  258. async function loadSendEvent(id: number) {
  259. const result = await api.event(id);
  260. return result.event;
  261. }
  262. async function copy(value: string) {
  263. if (!value || value === '-') return;
  264. await navigator.clipboard.writeText(value);
  265. message.success(t('common.copied'));
  266. }
  267. async function saveDnsCredential(values: Record<string, unknown>, id?: number) {
  268. const result = await runAction(async () => api.saveDnsCredential(values, id), id ? t('actions.dnsApiUpdated') : t('actions.dnsApiCreated'));
  269. if (!result?.credential) return;
  270. setData((current) => ({
  271. ...current,
  272. dnsCredentials: id
  273. ? current.dnsCredentials.map((item) => item.id === id ? result.credential : item)
  274. : [result.credential, ...current.dnsCredentials]
  275. }));
  276. }
  277. async function testDnsCredential(credential: DnsCredential) {
  278. await runAction(async () => api.testDnsCredential(credential.id), `${credential.name} ${t('actions.dnsApiTestCompleted')}`);
  279. }
  280. async function deleteDnsCredential(credential: DnsCredential) {
  281. const result = await runAction(async () => api.deleteDnsCredential(credential.id), t('actions.dnsApiDeleted'));
  282. if (!result?.deleted) return;
  283. setData((current) => ({
  284. ...current,
  285. dnsCredentials: current.dnsCredentials.filter((item) => item.id !== credential.id),
  286. domains: current.domains.map((domain) => domain.dnsCredentialId === credential.id ? { ...domain, dnsCredentialId: null } : domain)
  287. }));
  288. }
  289. async function loadSmtpLoginCredential(id: number) {
  290. const result = await runAction(async () => api.smtpCredentialDetail(id));
  291. return result?.credential || null;
  292. }
  293. async function saveSmtpLoginCredential(values: { username: string; password?: string }, id?: number) {
  294. const result = await runAction(
  295. async () => api.saveSmtpLoginCredential(values, id),
  296. id ? t('actions.smtpUpdated') : t('actions.smtpCreated')
  297. );
  298. if (!result?.credential) return null;
  299. setData((current) => {
  300. const credentials = id
  301. ? current.smtpCredentials.map((item) => item.id === id ? result.credential : item)
  302. : [result.credential, ...current.smtpCredentials];
  303. return {
  304. ...current,
  305. smtpCredential: credentials[0] || null,
  306. smtpCredentials: credentials,
  307. config: current.config?.submission
  308. ? {
  309. ...current.config,
  310. submission: {
  311. ...current.config.submission,
  312. username: credentials[0]?.username || '',
  313. passwordSet: Boolean(credentials[0]?.passwordSet)
  314. }
  315. }
  316. : current.config
  317. };
  318. });
  319. return result.credential;
  320. }
  321. async function deleteSmtpLoginCredential(credential: SmtpCredential) {
  322. const credentialId = credential.id;
  323. if (!credentialId) return;
  324. const result = await runAction(async () => api.deleteSmtpCredential(credentialId), t('actions.smtpDeleted'));
  325. if (!result?.deleted) return;
  326. setData((current) => {
  327. const credentials = current.smtpCredentials.filter((item) => item.id !== credentialId);
  328. return {
  329. ...current,
  330. smtpCredential: credentials[0] || null,
  331. smtpCredentials: credentials,
  332. config: current.config?.submission
  333. ? {
  334. ...current.config,
  335. submission: {
  336. ...current.config.submission,
  337. username: credentials[0]?.username || '',
  338. passwordSet: Boolean(credentials[0]?.passwordSet)
  339. }
  340. }
  341. : current.config
  342. };
  343. });
  344. }
  345. async function loadSmtpRelay(id: number) {
  346. const result = await runAction(async () => api.smtpRelay(id));
  347. return result?.relay || null;
  348. }
  349. async function saveSmtpRelay(values: SmtpRelayPayload, id?: number) {
  350. const result = await runAction(
  351. async () => api.saveSmtpRelay(values, id),
  352. id ? t('actions.smtpRelayUpdated') : t('actions.smtpRelayCreated')
  353. );
  354. if (!result?.relay) return null;
  355. setData((current) => ({
  356. ...current,
  357. smtpRelays: id
  358. ? current.smtpRelays.map((item) => item.id === id ? result.relay : item)
  359. : [result.relay, ...current.smtpRelays]
  360. }));
  361. return result.relay;
  362. }
  363. async function deleteSmtpRelay(relay: SmtpRelay) {
  364. const result = await runAction(async () => api.deleteSmtpRelay(relay.id), t('actions.smtpRelayDeleted'));
  365. if (!result?.deleted) return;
  366. setData((current) => ({
  367. ...current,
  368. smtpRelays: current.smtpRelays.filter((item) => item.id !== relay.id),
  369. domains: current.domains.map((domain) => domain.smtpRelayId === relay.id ? { ...domain, smtpRelayId: null } : domain)
  370. }));
  371. }
  372. async function createInboundMailbox(values: {
  373. address: string;
  374. displayName?: string;
  375. password: string;
  376. aliases?: string;
  377. forwardTo?: string;
  378. keepForwarded?: boolean;
  379. quotaMb?: number | string | null;
  380. }): Promise<{ mailbox: InboundMailbox; clientConfig?: MailboxClientConfig } | null> {
  381. const result = await runAction(async () => api.createInboundMailbox(values), t('actions.inboundMailboxCreated'));
  382. if (!result?.mailbox) return null;
  383. setData((current) => ({
  384. ...current,
  385. inboundMailboxes: [result.mailbox, ...current.inboundMailboxes]
  386. }));
  387. return result;
  388. }
  389. async function loadInboundMessages(mailboxId?: number | null) {
  390. const result = await runAction(async () => api.inboundMessages(mailboxId));
  391. if (!result?.messages) return [];
  392. setData((current) => ({ ...current, inboundMessages: result.messages }));
  393. return result.messages;
  394. }
  395. async function loadInboundMessage(id: number): Promise<InboundMessage | null> {
  396. const result = await api.inboundMessage(id);
  397. let inboundMessage = result.message;
  398. if (inboundMessage && !inboundMessage.read) {
  399. const readResult = await api.markInboundMessageRead(id, true);
  400. inboundMessage = readResult.message || { ...inboundMessage, read: true };
  401. }
  402. if (inboundMessage) {
  403. setData((current) => {
  404. const previous = current.inboundMessages.find((item) => item.id === inboundMessage.id);
  405. const shouldDecrementUnread = Boolean(previous && !previous.read && inboundMessage.read);
  406. return {
  407. ...current,
  408. inboundMessages: current.inboundMessages.map((item) => (
  409. item.id === inboundMessage.id ? { ...item, ...inboundMessage } : item
  410. )),
  411. inboundMailboxes: current.inboundMailboxes.map((mailbox) => (
  412. mailbox.id === inboundMessage.mailboxId && shouldDecrementUnread
  413. ? { ...mailbox, unreadCount: Math.max(0, mailbox.unreadCount - 1) }
  414. : mailbox
  415. ))
  416. };
  417. });
  418. }
  419. return inboundMessage;
  420. }
  421. async function createApiToken(values: { name: string; scopes: string[]; expiresAt?: string | null }) {
  422. const result = await runAction(async () => api.createApiToken(values), t('tokens.createdSuccess'));
  423. if (!result?.token) return null;
  424. setData((current) => ({ ...current, apiTokens: [result.token, ...current.apiTokens] }));
  425. return result.token;
  426. }
  427. async function updateApiToken(token: ApiToken, values: { name: string; scopes: string[]; expiresAt?: string | null }) {
  428. const result = await runAction(async () => api.updateApiToken(token.id, values), t('tokens.updatedSuccess'));
  429. if (!result?.token) return;
  430. setData((current) => ({
  431. ...current,
  432. apiTokens: current.apiTokens.map((item) => item.id === result.token.id ? result.token : item)
  433. }));
  434. }
  435. async function revokeApiToken(token: ApiToken) {
  436. const result = await runAction(async () => api.deleteApiToken(token.id), t('tokens.revokedSuccess'));
  437. if (!result?.token) return;
  438. setData((current) => ({
  439. ...current,
  440. apiTokens: current.apiTokens.map((item) => item.id === result.token?.id ? result.token : item)
  441. }));
  442. }
  443. async function saveSettings(values: Partial<RuntimeConfig>) {
  444. const result = await runAction(async () => api.saveAdminSettings(values), t('actions.settingsSaved'));
  445. if (!result?.settings) return;
  446. setData((current) => ({ ...current, settings: result.settings, config: { ...current.config, ...result.settings } as RuntimeConfig }));
  447. }
  448. async function logout() {
  449. await api.logout().catch(() => null);
  450. window.location.href = '/login';
  451. }
  452. const breadcrumb = useMemo(() => {
  453. if (activeView === 'domains' && domainMode === 'detail' && selectedDomain) return [t('nav.domains'), selectedDomain.domain];
  454. return [t(viewTitleKeys[activeView])];
  455. }, [activeView, domainMode, selectedDomain, t]);
  456. const runtimeLine = data.config
  457. ? `${data.config.mailHostname} · ${data.config.sendingIp || t('common.unsetSendingIp')}`
  458. : t('common.loadingConfig');
  459. const content = renderContent();
  460. return (
  461. <>
  462. <AdminLayout
  463. activeView={activeView}
  464. breadcrumb={breadcrumb}
  465. user={data.me}
  466. runtimeLine={runtimeLine}
  467. loading={loading}
  468. onViewChange={(view) => {
  469. setActiveView(view);
  470. if (view === 'domains') setDomainMode('list');
  471. }}
  472. onRefresh={loadAll}
  473. onAddDomain={() => setAddOpen(true)}
  474. onLogout={logout}
  475. >
  476. {content}
  477. </AdminLayout>
  478. <AddDomainDrawer
  479. open={addOpen}
  480. loading={actionLoading}
  481. config={data.config}
  482. dnsCredentials={data.dnsCredentials}
  483. smtpRelays={data.smtpRelays}
  484. onClose={() => setAddOpen(false)}
  485. onSubmit={createDomain}
  486. />
  487. <Modal
  488. title={testDomain ? `${t('testMail.title')} · ${testDomain.domain}` : t('testMail.title')}
  489. open={Boolean(testDomain)}
  490. confirmLoading={actionLoading}
  491. onCancel={() => setTestDomain(null)}
  492. onOk={submitTestMail}
  493. >
  494. <Form form={testForm} layout="vertical">
  495. <Form.Item name="from" label="From" rules={[{ required: true, message: t('testMail.fromRequired') }]}>
  496. <Input />
  497. </Form.Item>
  498. <Form.Item name="to" label="To" rules={[{ required: true, message: t('testMail.toRequired') }]}>
  499. <Input placeholder="user@example.com" />
  500. </Form.Item>
  501. <Form.Item name="subject" label="Subject">
  502. <Input />
  503. </Form.Item>
  504. <Form.Item name="text" label="Text">
  505. <Input.TextArea rows={5} />
  506. </Form.Item>
  507. <Form.Item name="html" label="HTML">
  508. <Input.TextArea rows={5} />
  509. </Form.Item>
  510. <Form.Item name="smtpRelayId" label={t('smtpRelay.domainDefault')}>
  511. <Select
  512. allowClear
  513. placeholder={t('smtpRelay.useResolutionOrder')}
  514. options={data.smtpRelays.map((relay) => ({
  515. value: relay.id,
  516. label: relayLabel(relay, t)
  517. }))}
  518. />
  519. </Form.Item>
  520. </Form>
  521. </Modal>
  522. </>
  523. );
  524. function renderContent() {
  525. if (activeView === 'dashboard') {
  526. return (
  527. <Dashboard
  528. analytics={data.analytics}
  529. domains={data.domains}
  530. events={data.events}
  531. config={data.config}
  532. smtpCredential={data.smtpCredential}
  533. />
  534. );
  535. }
  536. if (activeView === 'domains') {
  537. if (domainMode === 'detail' && selectedDomain) {
  538. return (
  539. <DomainDetail
  540. key={selectedDomain.id}
  541. domain={selectedDomain}
  542. config={data.config}
  543. smtpCredential={data.smtpCredential}
  544. apiTokens={data.apiTokens}
  545. events={data.events}
  546. dnsCredentials={data.dnsCredentials}
  547. smtpRelays={data.smtpRelays}
  548. actionLoading={actionLoading}
  549. initialTab={initialDomainTab}
  550. onBack={() => setDomainMode('list')}
  551. onApplyDns={applyDns}
  552. onCheck={checkDomain}
  553. onSendTest={openTestModal}
  554. onPatchDomain={patchDomain}
  555. onCopy={copy}
  556. onDelete={deleteDomain}
  557. />
  558. );
  559. }
  560. return (
  561. <DomainsPage
  562. domains={data.domains}
  563. events={data.events}
  564. dnsCredentials={data.dnsCredentials}
  565. actionLoading={actionLoading}
  566. onViewDetail={viewDetail}
  567. onApplyDns={applyDns}
  568. onCheck={checkDomain}
  569. onSendTest={openTestModal}
  570. onDelete={deleteDomain}
  571. onAddDomain={() => setAddOpen(true)}
  572. />
  573. );
  574. }
  575. if (activeView === 'dns-api') {
  576. return (
  577. <DnsApi
  578. credentials={data.dnsCredentials}
  579. loading={actionLoading}
  580. onSave={saveDnsCredential}
  581. onTest={testDnsCredential}
  582. onDelete={deleteDnsCredential}
  583. />
  584. );
  585. }
  586. if (activeView === 'smtp') {
  587. return (
  588. <SmtpCredentials
  589. config={data.config}
  590. credential={data.smtpCredential}
  591. credentials={data.smtpCredentials}
  592. relays={data.smtpRelays}
  593. loading={actionLoading}
  594. onCopy={copy}
  595. onLoadCredential={loadSmtpLoginCredential}
  596. onSaveCredential={saveSmtpLoginCredential}
  597. onDeleteCredential={deleteSmtpLoginCredential}
  598. onLoadRelay={loadSmtpRelay}
  599. onSaveRelay={saveSmtpRelay}
  600. onDeleteRelay={deleteSmtpRelay}
  601. />
  602. );
  603. }
  604. if (activeView === 'inbox') {
  605. return (
  606. <Inbox
  607. config={data.config}
  608. domains={data.domains}
  609. mailboxes={data.inboundMailboxes}
  610. messages={data.inboundMessages}
  611. loading={actionLoading}
  612. onCreateMailbox={createInboundMailbox}
  613. onPatchDomain={patchDomain}
  614. onLoadMessages={loadInboundMessages}
  615. onLoadMessage={loadInboundMessage}
  616. onCopy={copy}
  617. onAddDomain={() => setAddOpen(true)}
  618. />
  619. );
  620. }
  621. if (activeView === 'tokens') {
  622. return (
  623. <ApiTokens
  624. tokens={data.apiTokens}
  625. config={data.config}
  626. loading={actionLoading}
  627. onCreate={createApiToken}
  628. onUpdate={updateApiToken}
  629. onRevoke={revokeApiToken}
  630. onCopy={copy}
  631. />
  632. );
  633. }
  634. if (activeView === 'logs') {
  635. return <SendingLogs events={data.events} domains={data.domains} onCopy={copy} onLoadEvent={loadSendEvent} />;
  636. }
  637. if (activeView === 'webhooks') {
  638. return <Webhooks domains={data.domains} onCopy={copy} />;
  639. }
  640. if (activeView === 'admin') {
  641. return <AdminPage me={data.me} />;
  642. }
  643. if (activeView === 'settings') {
  644. return (
  645. <Settings
  646. me={data.me}
  647. settings={data.settings}
  648. users={data.users}
  649. loading={actionLoading}
  650. onSave={saveSettings}
  651. />
  652. );
  653. }
  654. return <PlaceholderPage title={t(viewTitleKeys[activeView])} />;
  655. }
  656. }
  657. function relayLabel(relay: SmtpRelay, t: (key: string) => string) {
  658. return `${relay.name}${relay.isDefault ? ` · ${t('smtpRelay.default')}` : ''} · ${relay.host}:${relay.port}`;
  659. }