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.

6.4 KB
Raw Blame
import { AlertQueueActionFeedback } from '../AlertQueueActionFeedback';
import { Show } from 'solid-js';
import { hasFeature } from '@/stores/license';
import { AlertAppriseDestinationsSection } from '../AlertAppriseDestinationsSection';
import { AlertDeliveryHealthCard } from '../AlertDeliveryHealthCard';
import { AlertDeliveryLogCard } from '../AlertDeliveryLogCard';
import { AlertDeliveryPausedCard } from '../AlertDeliveryPausedCard';
import { AlertDeadManDestinationSection } from '../AlertDeadManDestinationSection';
import { AlertDestinationsLoadErrorCard } from '../AlertDestinationsLoadErrorCard';
import { AlertDestinationsLoadingState } from '../AlertDestinationsLoadingState';
import { AlertEmailDestinationsSection } from '../AlertEmailDestinationsSection';
import { AlertPushDestinationsSection } from '../AlertPushDestinationsSection';
import { AlertWebhookDestinationsSection } from '../AlertWebhookDestinationsSection';
import { useAlertDeliveryPausedReason } from '../useAlertDeliveryPausedReason';

import {
  useAlertDestinationsTabState,
  type AlertDestinationsTabStateProps,
} from '../useAlertDestinationsTabState';

export interface DestinationsTabProps extends AlertDestinationsTabStateProps {
  setHasUnsavedChanges: (value: boolean) => void;
  setEmailConfig: (config: ReturnType<AlertDestinationsTabStateProps['emailConfig']>) => void;
  deadManPingUrl: () => string;
  setDeadManPingUrl: (value: string) => void;
  pushMinimumSeverity: () => 'all' | 'critical';
  setPushMinimumSeverity: (value: 'all' | 'critical') => void;
}

export function DestinationsTab(props: DestinationsTabProps) {
  return (
    <Show
      when={!props.isLoadingDestinations() && !props.isRetrying()}
      fallback={<AlertDestinationsLoadingState />}
    >
      <Show
        when={!props.configLoadError()}
        fallback={
          <AlertDestinationsLoadErrorCard
            error={props.configLoadError() || ''}
            isRetrying={props.isRetrying()}
            onRetry={props.onRetryLoad}
          />
        }
      >
        <LoadedDestinationsTab {...props} />
      </Show>
    </Show>
  );
}

function LoadedDestinationsTab(props: DestinationsTabProps) {
  const state = useAlertDestinationsTabState(props);
  // Destinations configured here are inert while delivery is gated off, so the
  // pause has to be visible on this surface rather than only on the overview.
  const deliveryPaused = useAlertDeliveryPausedReason();
  const deliveryLog = () => (
    <AlertDeliveryLogCard
      log={state.deliveryLog()}
      unavailable={state.deliveryLogUnavailable()}
      refreshing={state.refreshingDeliveryLog()}
      onRefresh={() => void state.loadDeliveryLog()}
      webhooks={state.webhooks()}
      heldEvents={state.heldEvents()}
      heldEventsUnavailable={state.heldEventsUnavailable()}
      refreshingHeldEvents={state.refreshingHeldEvents()}
    />
  );

  return (
    <div class="flex w-full max-w-full flex-col gap-6 md:gap-8">
      <Show when={!state.isLoading()} fallback={<AlertDestinationsLoadingState />}>
        <Show when={deliveryPaused.pausedReason()}>
          {(reason) => (
            <AlertDeliveryPausedCard
              reason={reason()}
              activating={deliveryPaused.activating()}
              onActivate={() => void deliveryPaused.activate()}
            />
          )}
        </Show>

        <AlertQueueActionFeedback
          message={state.queueActionFeedback()}
          onClear={state.clearQueueActionFeedback}
        />
        <Show when={state.deliveryNeedsAttention()}>
          <AlertDeliveryHealthCard
            health={state.deliveryHealth()?.queue ?? null}
            unavailable={state.deliveryHealthUnavailable()}
            refreshing={state.refreshingDeliveryHealth()}
            onRefresh={() => void state.loadDeliveryHealth()}
            retryingFailures={state.retryingTerminalFailures()}
            dismissingFailures={state.dismissingTerminalFailures()}
            onRetryFailures={() => void state.retryTerminalFailures()}
            onDismissFailures={() => void state.dismissTerminalFailures()}
          />
          {/* Degraded delivery keeps its evidence beside the recovery controls. */}
          {deliveryLog()}
        </Show>

        <Show when={state.hasLoadError()}>
          <AlertDestinationsLoadErrorCard
            error={props.configLoadError() || state.webhookLoadError() || ''}
            isRetrying={props.isRetrying()}
            onRetry={state.handleRetry}
          />
        </Show>

        {/*
          Unless delivery is degraded, destinations lead: "where do my alerts
          go" is the page's job. Webhooks (Discord, Telegram, ntfy, Gotify,
          Teams, Slack) come first because they are what most installs use, and
          delivery activity follows the destinations.
        */}
        <Show when={!state.hasLoadError()}>
          <AlertWebhookDestinationsSection
            webhooks={state.webhooks()}
            addWebhook={state.addWebhook}
            updateWebhook={state.updateWebhook}
            deleteWebhook={state.deleteWebhook}
            testWebhook={state.testWebhook}
            testingWebhook={state.testingWebhook()}
          />

          <AlertEmailDestinationsSection
            config={props.emailConfig()}
            setConfig={props.setEmailConfig}
            setHasUnsavedChanges={props.setHasUnsavedChanges}
            onTest={state.testEmailConfig}
            testing={state.testingEmail()}
          />

          <AlertAppriseDestinationsSection
            config={state.appriseState()}
            updateApprise={state.updateApprise}
            setHasUnsavedChanges={props.setHasUnsavedChanges}
            onTest={state.testApprise}
            testing={state.testingApprise()}
          />
        </Show>

        <Show when={!state.deliveryNeedsAttention()}>{deliveryLog()}</Show>

        <Show when={!state.hasLoadError()}>
          <AlertDeadManDestinationSection
            pingUrl={props.deadManPingUrl}
            setPingUrl={props.setDeadManPingUrl}
            setHasUnsavedChanges={props.setHasUnsavedChanges}
          />

          <AlertPushDestinationsSection
            relayLicensed={hasFeature('relay')}
            minimumSeverity={props.pushMinimumSeverity()}
            onMinimumSeverityChange={(minimumSeverity) => {
              props.setPushMinimumSeverity(minimumSeverity);
              props.setHasUnsavedChanges(true);
            }}
          />
        </Show>
      </Show>
    </div>
  );
}