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.

5.8 KB
Raw Blame
import { Component, Show, For, createMemo, createSignal } from 'solid-js';
import { apiFetch } from '@/utils/apiClient';
import { Dialog } from '@/components/shared/Dialog';
import { FormTextarea } from '@/components/shared/FormTextarea';
import { getSourcePlatformBadge } from '@/components/shared/sourcePlatformBadges';
import { getSourcePlatformLabel } from '@/utils/sourcePlatforms';
import { showError, showSuccess } from '@/utils/toast';
import XIcon from 'lucide-solid/icons/x';

interface ReportMergeModalProps {
  isOpen: boolean;
  resourceId: string;
  resourceName: string;
  sources: string[];
  onClose: () => void;
  onReported?: () => void;
}

export const ReportMergeModal: Component<ReportMergeModalProps> = (props) => {
  const [notes, setNotes] = createSignal('');
  const [isSubmitting, setIsSubmitting] = createSignal(false);
  const [error, setError] = createSignal<string | null>(null);

  const sourceBadges = createMemo(() =>
    props.sources.map((source) => {
      const badge = getSourcePlatformBadge(source);
      return {
        label: badge?.label || getSourcePlatformLabel(source),
        classes:
          badge?.classes ||
          'inline-flex items-center rounded-sm px-2 py-0.5 text-[10px] font-medium whitespace-nowrap bg-surface-alt text-base-content',
      };
    }),
  );
  const dialogTitleId = createMemo(
    () => `report-merge-title-${props.resourceId.replace(/[^a-zA-Z0-9_-]/g, '-')}`,
  );
  const dialogDescriptionId = createMemo(
    () => `report-merge-description-${props.resourceId.replace(/[^a-zA-Z0-9_-]/g, '-')}`,
  );

  const handleSubmit = async () => {
    if (isSubmitting()) return;
    setIsSubmitting(true);
    setError(null);
    try {
      const response = await apiFetch(
        `/api/resources/${encodeURIComponent(props.resourceId)}/report-merge`,
        {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            sources: props.sources,
            notes: notes().trim() || undefined,
          }),
        },
      );

      if (!response.ok) {
        const message = await response.text().catch(() => '');
        throw new Error(message || 'Failed to report merge');
      }

      showSuccess('Resource split applied');
      props.onReported?.();
      props.onClose();
      setNotes('');
    } catch (err) {
      const message = err instanceof Error ? err.message : 'Failed to report merge';
      setError(message);
      showError('Unable to report merge', message);
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <Dialog
      isOpen={props.isOpen}
      onClose={props.onClose}
      panelClass="max-w-lg"
      ariaLabelledBy={dialogTitleId()}
      ariaDescribedBy={dialogDescriptionId()}
    >
      <div class="flex items-start justify-between border-b border-border px-5 py-4">
        <div>
          <h3 id={dialogTitleId()} class="text-lg font-semibold text-base-content">
            Split Merged Resource
          </h3>
          <p id={dialogDescriptionId()} class="mt-1 text-xs text-muted">
            Use this when two systems were combined incorrectly. Pulse will keep them separate on
            future refreshes.
          </p>
        </div>
        <button
          type="button"
          onClick={props.onClose}
          class="rounded-md p-1.5 transition-colors hover:text-base-content hover:bg-surface-hover"
          aria-label="Close"
        >
          <XIcon class="h-5 w-5" />
        </button>
      </div>

      <div class="flex-1 overflow-y-auto space-y-4 px-5 py-4 text-sm text-base-content">
        <div>
          <div class="text-xs font-medium uppercase tracking-wide text-muted">Resource</div>
          <div class="mt-1 font-medium">{props.resourceName}</div>
          <div class="mt-0.5 text-xs text-muted">{props.resourceId}</div>
        </div>

        <div>
          <div class="text-xs font-medium uppercase tracking-wide text-muted">Merged Sources</div>
          <div class="mt-2 flex flex-wrap gap-2">
            <For each={sourceBadges()}>
              {(badge) => <span class={badge.classes}>{badge.label}</span>}
            </For>
          </div>
        </div>

        <FormTextarea
          label="Notes (optional)"
          labelClass="text-xs font-medium uppercase tracking-wide text-muted"
          fieldBaseClass="space-y-2"
          value={notes()}
          onInput={(event) => setNotes(event.currentTarget.value)}
          rows={3}
          textareaBaseClass="w-full rounded-md border border-border bg-surface px-3 py-2 text-sm shadow-xs focus:border-blue-500 focus:outline-hidden focus:ring-1 focus:ring-blue-500"
          placeholder="Example: Agent running on a different host with same hostname."
        />

        <Show when={error()}>
          <div
            role="alert"
            aria-live="assertive"
            class="rounded-md border border-red-200 bg-red-50 px-3 py-2 text-xs text-red-700 dark:border-red-900/25 dark:bg-red-900/25 dark:text-red-200"
          >
            {error()}
          </div>
        </Show>
      </div>

      <div class="flex items-center justify-end gap-2 border-t border-border bg-surface-hover px-5 py-3">
        <button
          type="button"
          onClick={props.onClose}
          class="rounded-md px-3 py-2 text-xs font-medium transition-colors hover:text-base-content"
          disabled={isSubmitting()}
        >
          Cancel
        </button>
        <button
          type="button"
          onClick={handleSubmit}
          disabled={isSubmitting() || props.sources.length < 2}
          class="rounded-md bg-blue-600 px-3 py-2 text-xs font-semibold text-white transition-colors hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-60"
        >
          {isSubmitting() ? 'Submitting...' : 'Split Resource'}
        </button>
      </div>
    </Dialog>
  );
};

export default ReportMergeModal;