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.

16.9 KB
Raw Blame
import { createSignal, createMemo, For, Show, type Accessor } from 'solid-js';
import X from 'lucide-solid/icons/x';
import CheckSquare from 'lucide-solid/icons/check-square';
import XSquare from 'lucide-solid/icons/x-square';
import { formControl } from '@/components/shared/Form';
import { SearchField } from '@/components/shared/SearchField';
import { FilterButtonGroup, type FilterOption } from '@/components/shared/FilterButtonGroup';
import { Button } from '@/components/shared/Button';
import { InlineNotice } from '@/components/shared/InlineNotice';
import { StatusDot } from '@/components/shared/StatusDot';
import { useResources } from '@/hooks/useResources';
import type { Resource, ResourceType } from '@/types/resource';
import { getPreferredInfrastructureDisplayName } from '@/utils/resourceIdentity';
import {
  getResourcePickerEmptyState,
  getResourcePickerTypeFilterLabel,
  matchesReportableResourceTypeFilter,
  REPORTABLE_RESOURCE_TYPES,
  RESOURCE_PICKER_TYPE_FILTERS,
  reportableResourceTypeSortOrder,
  type ResourcePickerTypeFilter as TypeFilter,
} from '@/utils/reportableResourceTypes';
import { showWarning } from '@/utils/toast';
import { getResourceTypePresentation } from '@/utils/resourceTypePresentation';
import { getSimpleStatusIndicator } from '@/utils/status';
import { PlatformWindowedList } from '@/features/platformPage/PlatformWindowedList';

const DEFAULT_MAX_SELECTION = 50;

export interface SelectedResource {
  id: string;
  type: ResourceType;
  name: string;
}

interface ResourcePickerProps {
  maxSelection?: number | null;
  resourceTypes?: ResourceType[];
  selected: Accessor<SelectedResource[]>;
  onSelectionChange: (items: SelectedResource[]) => void;
}

export function ResourcePicker(props: ResourcePickerProps) {
  const { resources, loading, error, refetch } = useResources(undefined, { retainReadError: true });
  const [retrying, setRetrying] = createSignal(false);
  const inventoryLoading = () => loading() || retrying();
  const inventoryAvailable = () => !error() && !inventoryLoading();
  const retryResources = async () => {
    if (inventoryLoading()) return;
    setRetrying(true);
    try {
      await refetch();
    } catch {
      // The canonical hook retains the read error. Do not expose provider
      // details or turn a failed retry into an empty inventory.
    } finally {
      setRetrying(false);
    }
  };
  const [search, setSearch] = createSignal('');
  const [typeFilter, setTypeFilter] = createSignal<TypeFilter>('all');
  const [tagFilter, setTagFilter] = createSignal('');
  const maxSelection = () =>
    props.maxSelection === null
      ? Number.POSITIVE_INFINITY
      : (props.maxSelection ?? DEFAULT_MAX_SELECTION);

  // Filter to reportable resource types across infrastructure, workloads, storage, and recovery.
  const reportableResources = createMemo(() => {
    // A retained snapshot is not permission to choose resources after its
    // read failed. Preserve the caller's explicit selection, not stale rows.
    if (!inventoryAvailable()) return [];
    return resources().filter(
      (r) =>
        REPORTABLE_RESOURCE_TYPES.has(r.type) &&
        (!props.resourceTypes || props.resourceTypes.includes(r.type)),
    );
  });
  const typeFilterOptions = createMemo<FilterOption<TypeFilter>[]>(() =>
    RESOURCE_PICKER_TYPE_FILTERS.filter(
      (type) =>
        !props.resourceTypes ||
        type === 'all' ||
        reportableResources().some((resource) =>
          matchesReportableResourceTypeFilter(resource, type),
        ),
    ).map((type) => ({
      value: type,
      label: getResourcePickerTypeFilterLabel(type),
    })),
  );

  // Apply filters
  const filteredResources = createMemo(() => {
    let result = reportableResources();

    // Type filter
    result = result.filter((r) => matchesReportableResourceTypeFilter(r, typeFilter()));

    // Search filter (name or ID)
    const searchTerm = search().toLowerCase().trim();
    if (searchTerm) {
      result = result.filter((r) => {
        const name = getPreferredInfrastructureDisplayName(r).toLowerCase();
        const id = r.id.toLowerCase();
        return name.includes(searchTerm) || id.includes(searchTerm);
      });
    }

    // Tag filter
    const tag = tagFilter().toLowerCase().trim();
    if (tag) {
      result = result.filter((r) => r.tags && r.tags.some((t) => t.toLowerCase().includes(tag)));
    }

    // Sort by domain first, then by type, then alphabetical.
    result.sort((a, b) => {
      const aOrder = reportableResourceTypeSortOrder(a.type);
      const bOrder = reportableResourceTypeSortOrder(b.type);
      if (aOrder !== bOrder) return aOrder - bOrder;
      return getPreferredInfrastructureDisplayName(a).localeCompare(
        getPreferredInfrastructureDisplayName(b),
      );
    });

    return result;
  });

  const selectedIds = createMemo(() => new Set(props.selected().map((s) => s.id)));

  const isSelected = (id: string) => selectedIds().has(id);

  const toggleResource = (resource: Resource) => {
    if (!inventoryAvailable()) return;
    const current = props.selected();
    if (isSelected(resource.id)) {
      props.onSelectionChange(current.filter((s) => s.id !== resource.id));
    } else {
      if (current.length >= maxSelection()) {
        showWarning(`Maximum ${maxSelection()} resources can be selected`);
        return;
      }
      props.onSelectionChange([
        ...current,
        {
          id: resource.id,
          type: resource.type,
          name: getPreferredInfrastructureDisplayName(resource),
        },
      ]);
    }
  };

  const selectAllVisible = () => {
    if (!inventoryAvailable()) return;
    const current = props.selected();
    const currentIds = new Set(current.map((s) => s.id));
    const toAdd = filteredResources()
      .filter((r) => !currentIds.has(r.id))
      .map((r) => ({
        id: r.id,
        type: r.type,
        name: getPreferredInfrastructureDisplayName(r),
      }));

    const newSelection = [...current, ...toAdd];
    if (newSelection.length > maxSelection()) {
      showWarning(
        `Maximum ${maxSelection()} resources can be selected. Only ${maxSelection() - current.length} more can be added.`,
      );
      props.onSelectionChange([...current, ...toAdd.slice(0, maxSelection() - current.length)]);
      return;
    }
    props.onSelectionChange(newSelection);
  };

  const clearAll = () => {
    props.onSelectionChange([]);
  };

  const removeSelected = (id: string) => {
    props.onSelectionChange(props.selected().filter((s) => s.id !== id));
  };

  return (
    <div class="space-y-3">
      {/* Filter bar */}
      <div class="flex flex-col gap-3">
        <div class="flex flex-wrap gap-2">
          <SearchField
            class="min-w-[200px] flex-1"
            inputClass={formControl}
            value={search()}
            onChange={setSearch}
            placeholder="Search by name or ID..."
          />

          <SearchField
            class="w-full sm:w-48"
            inputClass={formControl}
            placeholder="Filter by tag..."
            title="Filter resources by tag"
            value={tagFilter()}
            onChange={setTagFilter}
          />
        </div>

        <FilterButtonGroup
          options={typeFilterOptions()}
          value={typeFilter()}
          onChange={setTypeFilter}
          ariaLabel="Resource type filter"
          variant="settings"
        />
      </div>

      {/* Resource list */}
      <div class="border border-border rounded-md overflow-hidden" aria-busy={inventoryLoading()}>
        <Show
          when={inventoryAvailable()}
          fallback={
            <Show
              when={error()}
              fallback={
                <div class="p-8 text-center text-sm text-muted" role="status">
                  Loading resources…
                </div>
              }
            >
              <InlineNotice role="alert" tone="warning" class="m-3">
                <div class="space-y-3">
                  <p class="font-medium">Resources could not be loaded.</p>
                  <p>Existing selections are unchanged. Retry loading to choose resources.</p>
                  <Button
                    variant="secondary"
                    size="settingsAction"
                    class="w-full sm:w-auto"
                    disabled={inventoryLoading()}
                    onClick={() => void retryResources()}
                  >
                    {inventoryLoading() ? 'Retrying resources…' : 'Retry loading resources'}
                  </Button>
                </div>
              </InlineNotice>
            </Show>
          }
        >
          <Show
            when={reportableResources().length > 0}
            fallback={
              <div class="p-8 text-center text-muted">
                <p class="text-sm">{getResourcePickerEmptyState(false).title}</p>
                <p class="text-xs mt-1 text-muted">
                  {getResourcePickerEmptyState(false).description}
                </p>
              </div>
            }
          >
            <Show
              when={filteredResources().length > 0}
              fallback={
                <div class="p-6 text-center text-muted text-sm">
                  {getResourcePickerEmptyState(true).title}
                </div>
              }
            >
              <div class="max-h-[300px] overflow-y-auto">
                <PlatformWindowedList
                  items={filteredResources}
                  estimatedItemHeight={56}
                  enableThreshold={16}
                  windowSize={20}
                >
                  {(resource) => {
                    const badge = getResourceTypePresentation(resource.type) || {
                      label: resource.type,
                      badgeClasses: 'bg-surface-alt text-base-content',
                    };
                    return (
                      <button
                        class={`w-full flex items-start sm:items-center gap-3 px-3 py-2 text-left text-base-content transition-colors border-b border-border last:border-b-0 ${
                          isSelected(resource.id)
                            ? 'bg-blue-50 dark:bg-blue-900/25'
                            : 'hover:bg-surface-hover'
                        }`}
                        aria-pressed={isSelected(resource.id)}
                        onClick={() => toggleResource(resource)}
                      >
                        {/* Checkbox */}
                        <div
                          class={`w-4 h-4 rounded border shrink-0 flex items-center justify-center ${
                            isSelected(resource.id)
                              ? 'bg-blue-600 border-blue-600'
                              : 'border-base-content/40'
                          }`}
                        >
                          <Show when={isSelected(resource.id)}>
                            <svg
                              class="w-3 h-3 text-white"
                              fill="none"
                              viewBox="0 0 24 24"
                              stroke="currentColor"
                              stroke-width="3"
                            >
                              <path
                                stroke-linecap="round"
                                stroke-linejoin="round"
                                d="M5 13l4 4L19 7"
                              />
                            </svg>
                          </Show>
                        </div>

                        {/* Status dot */}
                        <StatusDot
                          variant={getSimpleStatusIndicator(resource.status).variant}
                          size="sm"
                          ariaHidden
                        />

                        {/* Name and ID */}
                        <div class="flex-1 min-w-0">
                          <div
                            class="text-sm text-base-content sm:truncate wrap-break-word"
                            title={getPreferredInfrastructureDisplayName(resource)}
                          >
                            {getPreferredInfrastructureDisplayName(resource)}
                          </div>
                          <div class="text-xs text-muted sm:truncate break-all" title={resource.id}>
                            {resource.id}
                          </div>
                          <div class="mt-1 flex flex-wrap items-center gap-1 sm:hidden">
                            <span class={`text-xs px-2 py-0.5 rounded-full ${badge.badgeClasses}`}>
                              {badge.label}
                            </span>
                            <Show when={resource.tags && resource.tags.length > 0}>
                              <For each={resource.tags?.slice(0, 2)}>
                                {(tag) => (
                                  <span class="text-xs px-1.5 py-0.5 rounded-sm bg-surface-alt text-base-content">
                                    {tag}
                                  </span>
                                )}
                              </For>
                              <Show when={(resource.tags?.length ?? 0) > 2}>
                                <span class="text-xs text-muted">
                                  +{(resource.tags?.length ?? 0) - 2}
                                </span>
                              </Show>
                            </Show>
                          </div>
                        </div>

                        {/* Type badge */}
                        <span
                          class={`hidden sm:inline-flex text-xs px-2 py-0.5 rounded-full shrink-0 ${badge.badgeClasses}`}
                        >
                          {badge.label}
                        </span>

                        {/* Tags */}
                        <Show when={resource.tags && resource.tags.length > 0}>
                          <div class="hidden sm:flex gap-1 shrink-0">
                            <For each={resource.tags?.slice(0, 2)}>
                              {(tag) => (
                                <span class="text-xs px-1.5 py-0.5 rounded-sm bg-surface-alt text-base-content">
                                  {tag}
                                </span>
                              )}
                            </For>
                            <Show when={(resource.tags?.length ?? 0) > 2}>
                              <span class="text-xs text-muted">
                                +{(resource.tags?.length ?? 0) - 2}
                              </span>
                            </Show>
                          </div>
                        </Show>
                      </button>
                    );
                  }}
                </PlatformWindowedList>
              </div>
            </Show>
          </Show>
        </Show>
      </div>

      {/* Action bar */}
      <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-2">
        <div class="flex flex-col sm:flex-row gap-2">
          <Button
            variant="outline"
            size="settingsAction"
            class="w-full gap-1.5 text-muted sm:w-auto"
            disabled={!inventoryAvailable() || filteredResources().length === 0}
            onClick={selectAllVisible}
          >
            <CheckSquare size={14} />
            Select all visible ({filteredResources().length})
          </Button>
          <Show when={props.selected().length > 0}>
            <Button
              variant="dangerOutline"
              size="settingsAction"
              class="w-full gap-1.5 sm:w-auto"
              onClick={clearAll}
            >
              <XSquare size={14} />
              Clear all
            </Button>
          </Show>
        </div>
        <span class="text-xs sm:text-sm text-muted">
          {props.selected().length} selected
          <Show when={props.selected().length >= maxSelection()}>
            <span class="text-amber-600 dark:text-amber-400 ml-1">(max)</span>
          </Show>
        </span>
      </div>

      {/* Selected summary chips */}
      <Show when={props.selected().length > 0}>
        <div
          class="flex max-h-40 flex-wrap gap-1.5 overflow-y-auto py-1"
          role="group"
          aria-label="Selected resources"
        >
          <For each={props.selected()}>
            {(item) => (
              <span class="inline-flex max-w-full items-center gap-1 pl-2 pr-1 py-1 rounded-md bg-blue-50 dark:bg-blue-900/25 border border-blue-200 dark:border-blue-800 text-sm text-base-content">
                <span class="min-w-0 break-all">{item.name}</span>
                <Button
                  variant="ghost"
                  size="xs"
                  class="min-h-0 shrink-0 p-0.5 text-base-content"
                  onClick={() => removeSelected(item.id)}
                  aria-label={`Remove ${item.name}`}
                  title={`Remove ${item.name}`}
                >
                  <X size={12} />
                </Button>
              </span>
            )}
          </For>
        </div>
      </Show>
    </div>
  );
}