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.

13.2 KB
Raw Blame
import { Component, Match, Show, Switch } from 'solid-js';
import LockIcon from 'lucide-solid/icons/lock';
import { showTooltip, hideTooltip } from '@/components/shared/Tooltip';
import { ActionReviewDialog } from '@/features/actions/ActionReviewDialog';
import {
  getContainerUpdateBadgeTooltip,
  getContainerUpdateCurrentTooltip,
  getContainerUpdateErrorTooltip,
  getContainerUpdateUncheckableLabel,
  getContainerUpdateUncheckableTooltip,
  getUpdateButtonClass,
  getUpdateIconTooltip,
  hasContainerUpdate,
  hasContainerUpdateCurrent,
  hasContainerUpdateError,
  isContainerUpdateUncheckable,
  type ContainerUpdateBadgeProps,
  type UpdateButtonProps,
  type UpdateIconProps,
} from './containerUpdateBadgeModel';
import { useContainerUpdateButtonState } from './useContainerUpdateButtonState';

export type {
  ContainerUpdateBadgeProps,
  UpdateButtonProps,
  UpdateIconProps,
  UpdateState,
} from './containerUpdateBadgeModel';

const UpdateArrowIcon: Component<{ class?: string }> = (props) => (
  <svg class={props.class} fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
    <path
      stroke-linecap="round"
      stroke-linejoin="round"
      d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"
    />
  </svg>
);

const ErrorIndicatorIcon: Component<{ class?: string }> = (props) => (
  <svg class={props.class} fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
    <path
      stroke-linecap="round"
      stroke-linejoin="round"
      d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
    />
  </svg>
);

const SpinnerIcon: Component<{ class?: string }> = (props) => (
  <svg class={props.class} fill="none" viewBox="0 0 24 24">
    <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
    <path
      class="opacity-75"
      fill="currentColor"
      d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
    />
  </svg>
);

const CheckIcon: Component<{ class?: string }> = (props) => (
  <svg class={props.class} fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
    <path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
  </svg>
);

const XIcon: Component<{ class?: string }> = (props) => (
  <svg class={props.class} fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
    <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
  </svg>
);

/**
 * ContainerUpdateBadge displays Docker image update check results.
 * Update-available states use a blue color scheme to differentiate from health/status badges.
 */
export const ContainerUpdateBadge: Component<ContainerUpdateBadgeProps> = (props) => {
  return (
    <Show
      when={
        hasContainerUpdate(props.updateStatus) ||
        hasContainerUpdateError(props.updateStatus) ||
        isContainerUpdateUncheckable(props.updateStatus) ||
        (props.showCurrent && hasContainerUpdateCurrent(props.updateStatus))
      }
    >
      <Switch>
        <Match when={hasContainerUpdate(props.updateStatus)}>
          <span
            class="inline-flex items-center gap-1 rounded-full px-1.5 py-0.5 text-xs font-medium bg-blue-100 text-blue-700 dark:bg-blue-900/25 dark:text-blue-300 cursor-help"
            onMouseEnter={(e) => {
              const rect = e.currentTarget.getBoundingClientRect();
              showTooltip(
                getContainerUpdateBadgeTooltip(props.updateStatus),
                rect.left + rect.width / 2,
                rect.top,
                {
                  align: 'center',
                  direction: 'up',
                },
              );
            }}
            onMouseLeave={() => hideTooltip()}
          >
            <UpdateArrowIcon class="w-3 h-3" />
            <Show when={!props.compact}>
              <span>Update</span>
            </Show>
          </span>
        </Match>
        <Match when={isContainerUpdateUncheckable(props.updateStatus)}>
          <span
            class="inline-flex items-center justify-center gap-1 rounded-full bg-surface-alt px-1.5 py-0.5 text-[11px] font-medium text-muted cursor-help"
            aria-label={getContainerUpdateUncheckableTooltip(props.updateStatus)}
            onMouseEnter={(e) => {
              const rect = e.currentTarget.getBoundingClientRect();
              showTooltip(
                getContainerUpdateUncheckableTooltip(props.updateStatus),
                rect.left + rect.width / 2,
                rect.top,
                {
                  align: 'center',
                  direction: 'up',
                },
              );
            }}
            onMouseLeave={() => hideTooltip()}
          >
            <span>{getContainerUpdateUncheckableLabel(props.updateStatus)}</span>
          </span>
        </Match>
        <Match when={hasContainerUpdateError(props.updateStatus)}>
          <span
            class="inline-flex items-center gap-1 rounded-full px-1.5 py-0.5 text-xs font-medium bg-surface-alt text-muted cursor-help"
            aria-label={getContainerUpdateErrorTooltip(props.updateStatus)}
            onMouseEnter={(e) => {
              const rect = e.currentTarget.getBoundingClientRect();
              showTooltip(
                getContainerUpdateErrorTooltip(props.updateStatus),
                rect.left + rect.width / 2,
                rect.top,
                {
                  align: 'center',
                  direction: 'up',
                },
              );
            }}
            onMouseLeave={() => hideTooltip()}
          >
            <ErrorIndicatorIcon class="w-3 h-3" />
            <span>Check failed</span>
          </span>
        </Match>
        <Match when={props.showCurrent && hasContainerUpdateCurrent(props.updateStatus)}>
          <span
            class="inline-flex items-center justify-center gap-1 rounded-full bg-surface-alt px-1.5 py-0.5 text-[11px] font-medium text-muted cursor-help"
            aria-label={getContainerUpdateCurrentTooltip(props.updateStatus)}
            onMouseEnter={(e) => {
              const rect = e.currentTarget.getBoundingClientRect();
              showTooltip(
                getContainerUpdateCurrentTooltip(props.updateStatus),
                rect.left + rect.width / 2,
                rect.top,
                {
                  align: 'center',
                  direction: 'up',
                },
              );
            }}
            onMouseLeave={() => hideTooltip()}
          >
            <Show when={!props.compact}>
              <CheckIcon class="w-3 h-3" />
            </Show>
            <span>Current</span>
          </span>
        </Match>
      </Switch>
    </Show>
  );
};

/**
 * Compact version of UpdateBadge - just an icon with no text.
 * Use this in table cells where space is limited.
 */
export const UpdateIcon: Component<UpdateIconProps> = (props) => {
  return (
    <Show when={hasContainerUpdate(props.updateStatus)}>
      <span
        class="inline-flex items-center justify-center w-5 h-5 rounded-full bg-blue-100 text-blue-700 dark:bg-blue-900/25 dark:text-blue-300 cursor-help"
        onMouseEnter={(e) => {
          const rect = e.currentTarget.getBoundingClientRect();
          showTooltip(
            getUpdateIconTooltip(props.updateStatus),
            rect.left + rect.width / 2,
            rect.top,
            {
              align: 'center',
              direction: 'up',
            },
          );
        }}
        onMouseLeave={() => hideTooltip()}
      >
        <UpdateArrowIcon class="w-3 h-3" />
      </span>
    </Show>
  );
};

/**
 * UpdateButton displays a clickable button to trigger container updates.
 * Uses a persistent store to maintain state across WebSocket refreshes.
 *
 * If the server has disabled Docker update actions (via PULSE_DISABLE_DOCKER_UPDATE_ACTIONS
 * or the Settings UI), this component will render a read-only UpdateBadge instead,
 * allowing users to see that updates are available without being able to trigger them.
 *
 * While system settings are loading, the button displays in a disabled/loading state
 * to prevent premature clicks before the server configuration is known.
 */
const UPDATE_TOUCH_TARGET_CLASS = ' max-sm:min-h-11 max-sm:px-3';

export const UpdateButton: Component<UpdateButtonProps> = (props) => {
  const state = useContainerUpdateButtonState(props);
  const readOnlyUpdateExplanation = () => state.updateUnavailableExplanation();
  const showReadOnlyUpdateExplanation = (target: HTMLElement) => {
    const rect = target.getBoundingClientRect();
    showTooltip(readOnlyUpdateExplanation(), rect.left + rect.width / 2, rect.top, {
      align: 'center',
      direction: 'up',
    });
  };
  const shouldRenderReadOnlyStatus = () =>
    state.currentState() === 'idle' &&
    (hasContainerUpdateError(props.updateStatus) ||
      hasContainerUpdateCurrent(props.updateStatus) ||
      isContainerUpdateUncheckable(props.updateStatus) ||
      state.isUpdateUnavailable() ||
      (state.settingsLoaded() && state.shouldHideButton()));

  return (
    <Show when={state.hasUpdate()}>
      <Show
        when={shouldRenderReadOnlyStatus()}
        fallback={
          <div class="inline-flex items-center gap-1" data-prevent-toggle>
            <button
              type="button"
              class={`${getUpdateButtonClass(state.currentState(), state.isUpdateUnavailable())}${
                props.touchTarget ? UPDATE_TOUCH_TARGET_CLASS : ''
              }`}
              onClick={state.handleClick}
              onMouseDown={(e) => {
                e.stopPropagation();
              }}
              disabled={state.isButtonDisabled()}
              aria-label={state.buttonTooltip() || state.buttonLabel()}
              data-prevent-toggle
              onMouseEnter={(e) => {
                const rect = e.currentTarget.getBoundingClientRect();
                showTooltip(state.buttonTooltip(), rect.left + rect.width / 2, rect.top, {
                  align: 'center',
                  direction: 'up',
                });
              }}
              onMouseLeave={() => hideTooltip()}
            >
              <Show when={!state.settingsLoaded()}>
                <UpdateArrowIcon class="w-3 h-3 animate-pulse opacity-50" />
              </Show>
              <Show when={state.settingsLoaded()}>
                <Switch>
                  <Match when={state.currentState() === 'updating'}>
                    <SpinnerIcon class="w-3 h-3 animate-spin" />
                  </Match>
                  <Match
                    when={
                      state.currentState() === 'queued' || state.currentState() === 'inconclusive'
                    }
                  >
                    <ErrorIndicatorIcon class="w-3 h-3" />
                  </Match>
                  <Match when={state.currentState() === 'success'}>
                    <CheckIcon class="w-3 h-3" />
                  </Match>
                  <Match when={state.currentState() === 'error'}>
                    <XIcon class="w-3 h-3" />
                  </Match>
                  <Match when={state.currentState() === 'idle'}>
                    <UpdateArrowIcon class="w-3 h-3" />
                  </Match>
                </Switch>
              </Show>
              <Show when={!props.compact}>
                <span class={!state.settingsLoaded() ? 'opacity-50' : ''}>
                  {state.buttonLabel()}
                </span>
              </Show>
            </button>
            <ActionReviewDialog
              detail={state.reviewDetail()}
              onClose={state.handleReviewClosed}
              onChanged={state.handleReviewChanged}
            />
          </div>
        }
      >
        <Show
          when={hasContainerUpdate(props.updateStatus) && state.isUpdateUnavailable()}
          fallback={
            <ContainerUpdateBadge
              updateStatus={props.updateStatus}
              compact={props.compact}
              showCurrent={true}
            />
          }
        >
          <span
            class={`inline-flex items-center gap-1 whitespace-nowrap rounded-full bg-amber-100 px-2 py-0.5 text-xs font-medium text-amber-800 dark:bg-amber-900/25 dark:text-amber-200 cursor-help focus-visible:outline focus-visible:outline-2 focus-visible:outline-primary${
              props.touchTarget ? UPDATE_TOUCH_TARGET_CLASS : ''
            }`}
            tabindex="0"
            aria-label={readOnlyUpdateExplanation()}
            data-prevent-toggle
            onClick={(event) => {
              event.stopPropagation();
              showReadOnlyUpdateExplanation(event.currentTarget);
            }}
            onMouseEnter={(event) => showReadOnlyUpdateExplanation(event.currentTarget)}
            onMouseLeave={() => hideTooltip()}
            onFocus={(event) => showReadOnlyUpdateExplanation(event.currentTarget)}
            onBlur={() => hideTooltip()}
            onKeyDown={(event) => {
              if (event.key === 'Escape') hideTooltip();
            }}
          >
            <LockIcon class="h-3 w-3 shrink-0" aria-hidden="true" />
            {/* "Update" fits the Updates column at its narrowest; the lock,
                amber tone and explanation state that Pulse cannot apply it. */}
            <span>Update</span>
          </span>
        </Show>
      </Show>
    </Show>
  );
};

export default ContainerUpdateBadge;