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.

15.4 KB
Raw Blame
import { Accessor, Component, Show } from 'solid-js';
import { Card } from '@/components/shared/Card';
import { Dialog } from '@/components/shared/Dialog';
import { SectionHeader } from '@/components/shared/SectionHeader';
import { controlClass, formField, formHelpText, labelClass } from '@/components/shared/Form';
import { getAPITokenManagementLocationMessage } from '@/utils/apiTokenPresentation';
import { ExternalTextLink } from '@/components/shared/ExternalTextLink';
import { getShippedDocUrl } from '@/utils/docsLinks';

interface BackupTransferDialogsProps {
  exportPassphrase: Accessor<string>;
  setExportPassphrase: (value: string) => void;
  exportPassphraseConfirmation: Accessor<string>;
  setExportPassphraseConfirmation: (value: string) => void;
  importPassphrase: Accessor<string>;
  setImportPassphrase: (value: string) => void;
  importFile: Accessor<File | null>;
  setImportFile: (file: File | null) => void;
  showExportDialog: Accessor<boolean>;
  showImportDialog: Accessor<boolean>;
  showApiTokenModal: Accessor<boolean>;
  transferOrgID: Accessor<string>;
  isTransferring: Accessor<boolean>;
  transferError: Accessor<string>;
  transferNotice: Accessor<string>;
  apiTokenInput: Accessor<string>;
  apiTokenError: Accessor<string>;
  setApiTokenInput: (value: string) => void;
  handleExport: () => void;
  handleImport: () => void;
  closeExportDialog: () => void;
  closeImportDialog: () => void;
  closeApiTokenModal: () => void;
  handleApiTokenAuthenticate: () => void;
}

export const BackupTransferDialogs: Component<BackupTransferDialogsProps> = (props) => {
  return (
    <>
      <Show when={props.showExportDialog()}>
        <Dialog
          isOpen={true}
          onClose={props.closeExportDialog}
          panelClass="max-w-md"
          ariaLabel="Export configuration"
        >
          <Card padding="lg" class="max-w-md w-full min-h-0 overflow-y-auto">
            <SectionHeader title="Export configuration" size="md" class="mb-4" />
            <p class="mb-4 text-sm text-muted break-all">
              Workspace ID: <strong class="text-base-content">{props.transferOrgID()}</strong>
            </p>
            <Show when={props.transferError() && !props.showApiTokenModal()}>
              <p role="alert" class="mb-4 text-sm text-red-700 dark:text-red-300">
                {props.transferError()}
              </p>
            </Show>
            <Show when={props.transferNotice() && !props.showApiTokenModal()}>
              <p role="status" class="mb-4 text-sm text-muted">
                {props.transferNotice()}
              </p>
            </Show>
            <Show when={props.isTransferring()}>
              <p role="status" class="mb-4 text-sm text-muted">
                Exporting configuration. Keep this dialog open while Pulse waits for a response.
              </p>
            </Show>

            <div class="space-y-4">
              <div class={formField}>
                <label for="backup-export-passphrase" class={labelClass()}>
                  Backup passphrase
                </label>
                <input
                  id="backup-export-passphrase"
                  disabled={props.isTransferring()}
                  type="password"
                  autocomplete="new-password"
                  aria-describedby="backup-export-passphrase-help"
                  value={props.exportPassphrase()}
                  onInput={(event) => props.setExportPassphrase(event.currentTarget.value)}
                  placeholder="Choose a strong, unique passphrase"
                  class={controlClass()}
                />
                <p id="backup-export-passphrase-help" class={`${formHelpText} mt-1`}>
                  Use at least 12 characters, different from your Pulse or SSO login password. This
                  encrypts the backup. It does not verify your login. Store it separately from the
                  file. Pulse cannot recover a forgotten passphrase.
                </p>
              </div>

              <div class={formField}>
                <label for="backup-export-passphrase-confirmation" class={labelClass()}>
                  Confirm backup passphrase
                </label>
                <input
                  id="backup-export-passphrase-confirmation"
                  disabled={props.isTransferring()}
                  type="password"
                  autocomplete="new-password"
                  aria-describedby="backup-export-confirmation-help"
                  value={props.exportPassphraseConfirmation()}
                  onInput={(event) =>
                    props.setExportPassphraseConfirmation(event.currentTarget.value)
                  }
                  placeholder="Re-enter the backup passphrase"
                  class={controlClass()}
                />
                <p id="backup-export-confirmation-help" class={`${formHelpText} mt-1`}>
                  Enter it again to catch typing mistakes before creating the file.
                </p>
              </div>

              <div class="bg-amber-50 dark:bg-amber-900/25 border border-amber-200 dark:border-amber-800 rounded-md p-3">
                <div class="flex gap-2">
                  <svg
                    class="w-4 h-4 text-amber-600 dark:text-amber-400 shrink-0 mt-0.5"
                    fill="none"
                    stroke="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
                    />
                  </svg>
                  <div class="text-xs text-amber-700 dark:text-amber-300">
                    <strong>Configuration only:</strong> Includes Proxmox connections, SSO settings
                    and API-token records, but not history, TrueNAS/vSphere connections or agent
                    enrolment state. Local login credentials and sessions are not included. Check
                    the{' '}
                    <ExternalTextLink
                      href={getShippedDocUrl('MIGRATION.md')}
                      variant="inlineInherit"
                    >
                      migration guide
                    </ExternalTextLink>{' '}
                    before relying on this for recovery. Keep your backup passphrase for restoring.
                  </div>
                </div>
              </div>

              <div class="flex justify-end space-x-3">
                <button
                  type="button"
                  onClick={props.closeExportDialog}
                  disabled={props.isTransferring()}
                  class="px-4 py-2 border border-border text-base-content rounded-md hover:bg-surface-hover"
                >
                  Cancel
                </button>
                <button
                  type="button"
                  onClick={props.handleExport}
                  disabled={
                    props.isTransferring() ||
                    props.exportPassphrase().length < 12 ||
                    !props.exportPassphraseConfirmation()
                  }
                  class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed"
                >
                  Export
                </button>
              </div>
            </div>
          </Card>
        </Dialog>
      </Show>

      <Show when={props.showApiTokenModal()}>
        <Dialog
          isOpen={true}
          onClose={props.closeApiTokenModal}
          panelClass="max-w-md"
          ariaLabel="API token required"
        >
          <Card padding="lg" class="max-w-md w-full min-h-0 overflow-y-auto">
            <SectionHeader title="API token required" size="md" class="mb-4" />
            <Show when={props.apiTokenError() || props.transferError()}>
              <p role="alert" class="mb-4 text-sm text-red-700 dark:text-red-300">
                {props.apiTokenError() || props.transferError()}
              </p>
            </Show>

            <div class="space-y-4">
              <p class="text-sm text-muted">
                This Pulse instance requires an API token for export/import operations. Please enter
                a token authorised for this workspace. Saving it does not repeat the transfer.
                Review the workspace and submit again yourself.
              </p>

              <div class={formField}>
                <label for="backup-api-token" class={labelClass()}>
                  API Token
                </label>
                <input
                  id="backup-api-token"
                  type="password"
                  value={props.apiTokenInput()}
                  onInput={(event) => props.setApiTokenInput(event.currentTarget.value)}
                  placeholder="Enter API token"
                  class={controlClass()}
                />
              </div>

              <div class="text-xs text-muted rounded-sm p-2">
                <p class="font-semibold mb-1">{getAPITokenManagementLocationMessage()}</p>
                <p>
                  Tokens are managed in the UI and stored in <code>api_tokens.json</code>.
                </p>
              </div>
            </div>

            <div class="flex justify-end space-x-2 mt-6">
              <button
                type="button"
                onClick={props.closeApiTokenModal}
                class="px-4 py-2 border border-border text-base-content rounded-md hover:bg-surface-hover"
              >
                Cancel
              </button>
              <button
                type="button"
                onClick={props.handleApiTokenAuthenticate}
                disabled={!props.apiTokenInput()}
                class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed"
              >
                Authenticate
              </button>
            </div>
          </Card>
        </Dialog>
      </Show>

      <Show when={props.showImportDialog()}>
        <Dialog
          isOpen={true}
          onClose={props.closeImportDialog}
          panelClass="max-w-md"
          ariaLabel="Import configuration"
        >
          <Card padding="lg" class="max-w-md w-full min-h-0 overflow-y-auto">
            <SectionHeader title="Import configuration" size="md" class="mb-4" />
            <p class="mb-4 text-sm text-muted break-all">
              Workspace ID: <strong class="text-base-content">{props.transferOrgID()}</strong>
            </p>
            <Show when={props.transferError() && !props.showApiTokenModal()}>
              <p role="alert" class="mb-4 text-sm text-red-700 dark:text-red-300">
                {props.transferError()}
              </p>
            </Show>
            <Show when={props.transferNotice() && !props.showApiTokenModal()}>
              <p role="status" class="mb-4 text-sm text-muted">
                {props.transferNotice()}
              </p>
            </Show>
            <Show when={props.isTransferring()}>
              <p role="status" class="mb-4 text-sm text-muted">
                Importing configuration. Keep this dialog open while Pulse waits for a response.
              </p>
            </Show>

            <div class="space-y-4">
              <div class={formField}>
                <label for="backup-import-file" class={labelClass()}>
                  Configuration File
                </label>
                <input
                  id="backup-import-file"
                  disabled={props.isTransferring()}
                  type="file"
                  accept=".json"
                  aria-describedby="backup-import-file-help"
                  onChange={(event) => {
                    const file = event.currentTarget.files?.[0] ?? null;
                    props.setImportFile(file);
                  }}
                  class={controlClass('cursor-pointer')}
                />
                <p id="backup-import-file-help" class={`${formHelpText} mt-1`}>
                  Files up to 2 MiB are read locally. Import requests, including the password, must
                  fit the server’s 1 MiB limit.
                </p>
              </div>

              <div class={formField}>
                <label for="backup-import-passphrase" class={labelClass()}>
                  Backup passphrase
                </label>
                <input
                  id="backup-import-passphrase"
                  disabled={props.isTransferring()}
                  type="password"
                  autocomplete="off"
                  aria-describedby="backup-import-passphrase-help"
                  value={props.importPassphrase()}
                  onInput={(event) => props.setImportPassphrase(event.currentTarget.value)}
                  placeholder="Enter the passphrase used when creating this backup"
                  class={controlClass()}
                />
                <p id="backup-import-passphrase-help" class={`${formHelpText} mt-1`}>
                  Use the original passphrase for this file. For older backups, this may be the
                  login password you chose when exporting, not your current login password.
                </p>
              </div>

              <div class="bg-yellow-50 dark:bg-yellow-900/25 border border-yellow-200 dark:border-yellow-800 rounded-sm p-3">
                <div class="space-y-2 text-xs text-yellow-700 dark:text-yellow-300">
                  <p>
                    <strong>Warning:</strong> Import replaces the included settings and API-token
                    records, not the whole installation. Back up the destination first. History and
                    excluded settings are not restored. Check the{' '}
                    <ExternalTextLink
                      href={getShippedDocUrl('MIGRATION.md')}
                      variant="inlineInherit"
                    >
                      migration guide
                    </ExternalTextLink>{' '}
                    before retiring the old server.
                  </p>
                  <p>
                    <strong>Agent migration:</strong> API-token records are included, but agent
                    inventory, enrolment state, profiles and assignments are not. Import cannot
                    change the Pulse URL stored on remote agents. Preserve each agent's identity and
                    private credential, retarget if the address changes, and verify fresh admission.
                  </p>
                </div>
              </div>

              <div class="flex justify-end space-x-3">
                <button
                  type="button"
                  onClick={props.closeImportDialog}
                  disabled={props.isTransferring()}
                  class="px-4 py-2 border border-border text-base-content rounded-md hover:bg-surface-hover"
                >
                  Cancel
                </button>
                <button
                  type="button"
                  onClick={props.handleImport}
                  disabled={
                    props.isTransferring() || !props.importPassphrase() || !props.importFile()
                  }
                  class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed"
                >
                  Import
                </button>
              </div>
            </div>
          </Card>
        </Dialog>
      </Show>
    </>
  );
};