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.

8.1 KB
Raw Blame
import { createSignal, createEffect, onCleanup, Signal } from 'solid-js';

const LOCAL_STORAGE_SYNC_EVENT = 'pulse-localstorage-sync';

type LocalStorageSyncDetail = {
  key: string;
  value: string | null;
};

function broadcastLocalStorageChange(key: string, value: string | null): void {
  if (typeof window === 'undefined') return;
  try {
    window.dispatchEvent(
      new CustomEvent<LocalStorageSyncDetail>(LOCAL_STORAGE_SYNC_EVENT, {
        detail: { key, value },
      }),
    );
  } catch {
    // Ignore event dispatch errors
  }
}

/**
 * Creates a signal that syncs with localStorage
 * @param key - The localStorage key
 * @param defaultValue - Default value if nothing in localStorage
 * @param parse - Optional parser function for complex types
 * @param stringify - Optional stringify function for complex types
 */
function createLocalStorageSignal<T>(
  key: string,
  defaultValue: T,
  parse?: (value: string) => T,
  stringify?: (value: T) => string,
): Signal<T> {
  // Get initial value from localStorage
  const stored = localStorage.getItem(key);
  const initialValue =
    stored !== null ? (parse ? parse(stored) : (stored as unknown as T)) : defaultValue;

  const [value, setValue] = createSignal<T>(initialValue);

  // Keep multiple instances in sync (and reflect updates performed elsewhere in the same tab).
  if (typeof window !== 'undefined') {
    const applyRaw = (raw: string | null) => {
      const next = raw !== null ? (parse ? parse(raw) : (raw as unknown as T)) : defaultValue;
      if (Object.is(next, value())) return;
      setValue(() => next);
    };

    const handleStorage = (e: StorageEvent) => {
      if (e.storageArea !== window.localStorage) return;
      if (e.key !== key) return;
      applyRaw(e.newValue);
    };

    const handleCustom = (e: Event) => {
      const evt = e as CustomEvent<LocalStorageSyncDetail>;
      if (!evt.detail || evt.detail.key !== key) return;
      applyRaw(evt.detail.value);
    };

    window.addEventListener('storage', handleStorage);
    window.addEventListener(LOCAL_STORAGE_SYNC_EVENT, handleCustom);
    onCleanup(() => {
      window.removeEventListener('storage', handleStorage);
      window.removeEventListener(LOCAL_STORAGE_SYNC_EVENT, handleCustom);
    });
  }

  // Sync to localStorage on changes
  createEffect(() => {
    const val = value();
    if (val === null || val === undefined) {
      localStorage.removeItem(key);
      broadcastLocalStorageChange(key, null);
    } else {
      const raw = stringify ? stringify(val) : String(val);
      localStorage.setItem(key, raw);
      broadcastLocalStorageChange(key, raw);
    }
  });

  return [value, setValue];
}

/**
 * Creates a boolean signal that syncs with localStorage
 * @param key - The localStorage key
 * @param defaultValue - Default value if nothing in localStorage
 */
export function createLocalStorageBooleanSignal(
  key: string,
  defaultValue: boolean = false,
): Signal<boolean> {
  return createLocalStorageSignal(
    key,
    defaultValue,
    (val) => val === 'true',
    (val) => String(val),
  );
}

/**
 * Creates a number signal that syncs with localStorage
 * @param key - The localStorage key
 * @param defaultValue - Default value if nothing in localStorage
 */
export function createLocalStorageNumberSignal(
  key: string,
  defaultValue: number = 0,
): Signal<number> {
  return createLocalStorageSignal(
    key,
    defaultValue,
    (val) => {
      const parsed = Number(val);
      return Number.isFinite(parsed) ? parsed : defaultValue;
    },
    (val) => String(val),
  );
}

/**
 * Creates a string signal that syncs with localStorage
 * @param key - The localStorage key
 * @param defaultValue - Default value if nothing in localStorage
 */
export function createLocalStorageStringSignal(
  key: string,
  defaultValue: string = '',
): Signal<string> {
  return createLocalStorageSignal(
    key,
    defaultValue,
    (val) => String(val),
    (val) => String(val),
  );
}

export type LowPriorityNoticeOwner = 'github-star' | 'release-update';

export const SESSION_STORAGE_KEYS = {
  LOW_PRIORITY_NOTICE_OWNER: 'pulse-low-priority-notice-owner',
  // Demo mode signs the visitor in once per browser tab. The value is
  // 'attempted' after the login page has tried, or 'suppressed' after an
  // explicit sign-out, so a visitor who signed out lands on the form.
  DEMO_AUTO_LOGIN: 'pulse-demo-auto-login',
} as const;

/**
 * Reserve the single low-priority app-shell notice slot for this browser
 * session. Informational and promotional prompts use this boundary so a user
 * never has to clear a sequence of unrelated notices after opening Pulse.
 */
export function reserveLowPriorityNoticeSession(owner: LowPriorityNoticeOwner): boolean {
  if (typeof sessionStorage === 'undefined') return true;
  try {
    const existingOwner = sessionStorage.getItem(SESSION_STORAGE_KEYS.LOW_PRIORITY_NOTICE_OWNER);
    if (existingOwner) return existingOwner === owner;
    sessionStorage.setItem(SESSION_STORAGE_KEYS.LOW_PRIORITY_NOTICE_OWNER, owner);
    return true;
  } catch {
    // Storage-disabled browsers still get the notice's own persistence rules.
    return true;
  }
}

/**
 * Creates a number signal that syncs with localStorage
 * @param key - The localStorage key
 * @param defaultValue - Default value if nothing in localStorage
 */
// Storage keys used throughout the application
export const STORAGE_KEYS = {
  // Authentication
  AUTH: 'pulse_auth',
  AUTH_USER: 'pulse_auth_user',
  REMEMBERED_LOGIN_USERNAME: 'pulse_remembered_login_username',
  ORG_ID: 'pulse_org_id',
  SETUP_CREDENTIALS: 'pulse_setup_credentials',
  SETUP_HANDOFF: 'pulse_setup_handoff',

  // UI preferences
  THEME_PREFERENCE: 'pulseThemePreference',
  LOCALE_PREFERENCE: 'pulseLocalePreference',
  SIDEBAR_COLLAPSED: 'sidebarCollapsed',
  FULL_WIDTH_MODE: 'fullWidthMode',

  // Metadata
  GUEST_METADATA: 'pulseGuestMetadata',
  DOCKER_METADATA: 'pulseDockerMetadata',

  // Updates
  UPDATES: 'pulse-updates',
  WHATS_NEW_LAST_SEEN: 'pulseWhatsNewLastSeen',

  // Alert settings
  ALERT_HISTORY_TIME_FILTER: 'alertHistoryTimeFilter',
  ALERT_HISTORY_SEVERITY_FILTER: 'alertHistorySeverityFilter',

  // Recovery settings
  RECOVERY_SEARCH_HISTORY: 'backupsSearchHistory',

  // Workloads settings
  WORKLOADS_SEARCH_HISTORY: 'workloadsSearchHistory',
  WORKLOADS_METRIC_DISPLAY_MODE: 'workloadsMetricDisplayMode',
  WORKLOADS_METRIC_HOVER_MODE: 'workloadsMetricHoverMode',
  WORKLOADS_METRIC_HISTORY_RANGE: 'workloadsMetricHistoryRange',
  WORKLOADS_HISTORY_HINT_SEEN: 'workloadsHistoryHintSeenV4',
  WORKLOADS_MEMORY_DISPLAY_BASIS: 'workloadsMemoryDisplayBasis',
  AGENTS_SEARCH_HISTORY: 'agentsSearchHistory',

  // Machines settings
  MACHINES_SEARCH_HISTORY: 'machinesSearchHistory',

  // Storage search
  STORAGE_SEARCH_HISTORY: 'storageSearchHistory',

  // Alerts search
  ALERTS_SEARCH_HISTORY: 'alertsSearchHistory',

  // Temperature display
  TEMPERATURE_UNIT: 'temperatureUnit', // 'celsius' | 'fahrenheit'

  // Column visibility
  WORKLOADS_HIDDEN_COLUMNS: 'workloadsHiddenColumns',
  WORKLOADS_COLUMN_WIDTHS: 'workloadsColumnWidths',
  RECOVERY_HIDDEN_COLUMNS: 'backupsHiddenColumns',
  STORAGE_HIDDEN_COLUMNS: 'storageHiddenColumns',

  // Resources search
  RESOURCES_SEARCH_HISTORY: 'resourcesSearchHistory',

  // Feature discovery
  DISMISSED_FEATURE_TIPS: 'pulse-dismissed-feature-tips',
  DEBUG_MODE: 'pulse_debug_mode',

  // GitHub star prompt
  GITHUB_STAR_DISMISSED: 'pulse-github-star-dismissed',
  GITHUB_STAR_PROMPT_SHOWN: 'pulse-github-star-prompt-shown',
  GITHUB_STAR_ACTIVE_DAYS: 'pulse-github-star-active-days',
  GITHUB_STAR_LAST_ACTIVE_DATE: 'pulse-github-star-last-active-date',

  // Billing / migration guidance
  AGENT_MIGRATION_NOTICE_DISMISSED: 'pulse-agent-migration-notice-dismissed',

  // Audit log
  AUDIT_AUTO_VERIFY: 'pulse-audit-auto-verify',
  AUDIT_AUTO_VERIFY_LIMIT: 'pulse-audit-auto-verify-limit',
  AUDIT_PAGE_SIZE: 'pulse-audit-page-size',
  AUDIT_PAGE_OFFSET: 'pulse-audit-page-offset',
  AUDIT_VERIFICATION_FILTER: 'pulse-audit-verification-filter',
  AUDIT_EVENT_FILTER: 'pulse-audit-event-filter',
  AUDIT_USER_FILTER: 'pulse-audit-user-filter',
  AUDIT_SUCCESS_FILTER: 'pulse-audit-success-filter',
} as const;