import type { Storage } from '@/types/api'; import { getAllFilterOptionLabel } from '@/components/shared/filterOptionPresentation'; import { getSourcePlatformLabel, normalizeSourcePlatformKey, sourcePlatformScopeMatchesFilter, } from '@/utils/sourcePlatforms'; import { titleCaseDelimitedLabel } from '@/utils/textPresentation'; export type StorageSourceTone = 'slate' | 'orange' | 'indigo' | 'rose' | 'teal' | 'cyan' | 'blue'; export interface StorageSourceOption { key: string; label: string; tone: StorageSourceTone; } const ALL_STORAGE_SOURCE_OPTION: StorageSourceOption = { key: 'all', label: getAllFilterOptionLabel('sources'), tone: 'slate', }; const STORAGE_SOURCE_TONES: Record = { 'proxmox-pve': 'orange', 'proxmox-pbs': 'indigo', 'proxmox-pmg': 'rose', ceph: 'teal', truenas: 'blue', kubernetes: 'cyan', }; const CEPH_TYPES = new Set(['ceph', 'cephfs', 'rbd']); const PROXMOX_STORAGE_TYPES = new Set([ 'dir', 'nfs', 'cifs', 'lvm', 'lvmthin', 'zfspool', 'zfs', 'iscsi', 'glusterfs', 'btrfs', ]); const STORAGE_SOURCE_ORDER = [ 'proxmox-pve', 'proxmox-pbs', 'ceph', 'truenas', 'kubernetes', 'proxmox-pmg', ]; const slugifySource = (value: string): string => value .trim() .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); export const normalizeStorageSourceKey = (value: string | null | undefined): string => { const normalized = slugifySource(value || ''); const canonicalPlatform = normalizeSourcePlatformKey(normalized); if (canonicalPlatform) return canonicalPlatform; switch (normalized) { case 'all': return 'all'; case 'ceph': case 'cephfs': case 'rbd': return 'ceph'; default: return normalized; } }; export const storageSourceMatchesFilter = ( source: string | null | undefined, filter: string | null | undefined, ): boolean => { const sourceKey = normalizeStorageSourceKey(source); const filterKey = normalizeStorageSourceKey(filter); return sourcePlatformScopeMatchesFilter(sourceKey, filterKey); }; export const resolveStorageSourceKey = (storage: Storage): string => { // Prefer the explicit canonical platform tag when the storage adapter // already declared it (TrueNAS, VMware, Hyper-V, etc. set // `storage.platform` to the canonical platform key). Falling through to // raw `storage.type` here would otherwise collapse a vSAN/NFS/VMFS // datastore to its on-disk technology and miss every platform-scoped // filter (`forcedSourceFilter=vmware-vsphere` against a vSAN row, etc.). const platform = normalizeStorageSourceKey(storage.platform || ''); if ( platform === 'truenas' || platform === 'vmware-vsphere' || platform === 'proxmox-pbs' || platform === 'proxmox-pmg' || platform === 'kubernetes' || platform === 'microsoft-hyperv' ) { return platform; } const type = normalizeStorageSourceKey(storage.type); if ( type === 'proxmox-pbs' || type === 'ceph' || type === 'kubernetes' || type === 'proxmox-pmg' ) { return type; } if (CEPH_TYPES.has(type)) { return 'ceph'; } if (PROXMOX_STORAGE_TYPES.has(type) || type === '' || type === 'storage') { return 'proxmox-pve'; } if (type.includes('k8s') || type.includes('kubernetes') || type.includes('csi')) { return 'kubernetes'; } return type || 'proxmox-pve'; }; export const getStorageSourceOption = (value: string | null | undefined): StorageSourceOption => { const key = normalizeStorageSourceKey(value); if (key === 'all') { return ALL_STORAGE_SOURCE_OPTION; } return { key, label: (key === 'ceph' ? 'Ceph' : getSourcePlatformLabel(key)) || titleCaseDelimitedLabel(key) || 'Other', tone: STORAGE_SOURCE_TONES[key] || 'slate', }; }; export const orderStorageSourceKeys = (keys: Iterable): string[] => Array.from(new Set(Array.from(keys).map((key) => normalizeStorageSourceKey(key)))).sort( (a, b) => { const indexA = STORAGE_SOURCE_ORDER.indexOf(a); const indexB = STORAGE_SOURCE_ORDER.indexOf(b); if (indexA !== -1 || indexB !== -1) { if (indexA === -1) return 1; if (indexB === -1) return -1; return indexA - indexB; } return a.localeCompare(b); }, ); export const buildStorageSourceOptionsFromKeys = ( keys: Iterable, ): StorageSourceOption[] => { const orderedKeys = orderStorageSourceKeys(keys).filter((key) => key !== 'all'); return [ALL_STORAGE_SOURCE_OPTION, ...orderedKeys.map((key) => getStorageSourceOption(key))]; };