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.6 KB
Raw Blame
import type {
  PortalMutationState,
  PortalAccountState,
  PortalAccountSummary,
  PortalAccountUIEntry,
  PortalBootstrapData,
  PortalLoginState,
  PortalShellState,
  PortalBillingFlowID,
  PortalBillingState,
  PortalQueryState,
  RefundState,
  BillingStatus,
  PortalAccessMember,
  VerificationFlowState,
} from './types';
import { preferredPortalShellSection } from './shell_section';

export function emptyStatus(): BillingStatus {
  return {
    visible: false,
    message: '',
    error: false,
  };
}

export function newVerificationFlowState(): VerificationFlowState {
  return {
    pendingEmail: '',
    request: createMutationState(),
    confirm: createMutationState(),
    step2Visible: false,
    status: emptyStatus(),
    result: null,
    emailValue: '',
    codeValue: '',
    checkboxChecked: false,
  };
}

export function createPortalLoginState(): PortalLoginState {
  return {
    emailValue: '',
    request: createMutationState(),
    success: false,
    successMessage: '',
  };
}

export function createPortalAccountState(): PortalAccountState {
  return {
    byAccountID: {},
  };
}

export function createPortalShellState(
  initialBootstrap: Pick<PortalBootstrapData, 'authenticated' | 'has_self_hosted_commercial' | 'accounts'>
): PortalShellState {
  return {
    activeSection: preferredPortalShellSection(initialBootstrap),
  };
}

export function createMutationState(): PortalMutationState {
  return {
    pending: false,
    error: '',
  };
}

export function createQueryState<T>(data: T): PortalQueryState<T> {
  return {
    status: 'idle',
    data: data,
    error: '',
  };
}

export function ensurePortalAccountUIEntry(accountState: PortalAccountState, accountID: string): PortalAccountUIEntry {
  if (!accountState.byAccountID[accountID]) {
    accountState.byAccountID[accountID] = {
      addWorkspaceOpen: false,
      createWorkspace: createMutationState(),
      selectedWorkspaceID: '',
      manageWorkspace: createMutationState(),
      accessVisible: false,
      activeAccessJob: '',
      accessQuery: createQueryState<PortalAccessMember[]>([]),
    };
  }
  return accountState.byAccountID[accountID];
}

function clonePortalAccessMembers(members: PortalAccessMember[]): PortalAccessMember[] {
  var cloned = [] as PortalAccessMember[];
  for (var i = 0; i < members.length; i += 1) {
    cloned.push({
      subject_id: members[i].subject_id,
      email: members[i].email,
      role: members[i].role,
      user_id: members[i].user_id,
      state: members[i].state,
      created_at: members[i].created_at,
    });
  }
  return cloned;
}

export function syncPortalAccountStateBootstrap(accountState: PortalAccountState, accounts: PortalAccountSummary[]): void {
  for (var i = 0; i < accounts.length; i += 1) {
    var account = accounts[i];
    var entry = ensurePortalAccountUIEntry(accountState, account.id);
    entry.accessQuery.status = 'ready';
    entry.accessQuery.error = '';
    entry.accessQuery.data = clonePortalAccessMembers(account.members || []);
  }
}

export function createPortalBillingState(): PortalBillingState {
  return {
    openBillingPanelID: '',
    upgradeFeatureKey: '',
    upgradeTrialRequested: false,
    upgradePortalHandoffID: '',
    upgradePortalHandoff: createQueryState(null),
    upgradePricing: createQueryState(null),
    upgradeCheckout: createMutationState(),
    flows: {
      manage: newVerificationFlowState(),
      retrieve: newVerificationFlowState(),
      export: newVerificationFlowState(),
      delete: newVerificationFlowState(),
    },
    refund: {
      emailValue: '',
      tokenValue: '',
      submit: createMutationState(),
      status: emptyStatus(),
    },
  };
}

export function syncLoginStateBootstrapEmail(loginState: PortalLoginState, email: string): void {
  if (!loginState.emailValue) {
    loginState.emailValue = email || '';
  }
}

export function syncBillingStateBootstrapEmail(billingState: PortalBillingState, email: string): void {
  if (!billingState.flows.manage.emailValue) billingState.flows.manage.emailValue = email || '';
  if (!billingState.flows.retrieve.emailValue) billingState.flows.retrieve.emailValue = email || '';
  if (!billingState.flows.export.emailValue) billingState.flows.export.emailValue = email || '';
  if (!billingState.flows.delete.emailValue) billingState.flows.delete.emailValue = email || '';
  if (!billingState.refund.emailValue) billingState.refund.emailValue = email || '';
}

export function setFlowStatus(billingState: PortalBillingState, flowID: PortalBillingFlowID, message: string, isError: boolean): void {
  billingState.flows[flowID].status = {
    visible: true,
    message,
    error: !!isError,
  };
}

export function clearFlowStatus(billingState: PortalBillingState, flowID: PortalBillingFlowID): void {
  billingState.flows[flowID].status = emptyStatus();
}

export function setRefundStatus(billingState: PortalBillingState, message: string, isError: boolean): void {
  billingState.refund.status = {
    visible: true,
    message,
    error: !!isError,
  };
}

export function toggleBillingPanelState(billingState: PortalBillingState, panelID: string): void {
  billingState.openBillingPanelID = billingState.openBillingPanelID === panelID ? '' : panelID;
}

export function resetVerificationFlowState(billingState: PortalBillingState, flowID: PortalBillingFlowID): void {
  var previous = billingState.flows[flowID];
  billingState.flows[flowID] = newVerificationFlowState();
  billingState.flows[flowID].emailValue = previous.emailValue;
}

export function updateBillingInputValue(billingState: PortalBillingState, inputKind: string, value: string): void {
  switch (inputKind) {
    case 'manage-email':
      billingState.flows.manage.emailValue = value;
      return;
    case 'manage-code':
      billingState.flows.manage.codeValue = value;
      return;
    case 'retrieve-email':
      billingState.flows.retrieve.emailValue = value;
      return;
    case 'retrieve-code':
      billingState.flows.retrieve.codeValue = value;
      return;
    case 'refund-email':
      billingState.refund.emailValue = value;
      return;
    case 'refund-token':
      billingState.refund.tokenValue = value;
      return;
    case 'data-export-email':
      billingState.flows.export.emailValue = value;
      return;
    case 'data-export-code':
      billingState.flows.export.codeValue = value;
      return;
    case 'data-delete-email':
      billingState.flows.delete.emailValue = value;
      return;
    case 'data-delete-code':
      billingState.flows.delete.codeValue = value;
      return;
    default:
      return;
  }
}

export function updateDeleteConfirmation(billingState: PortalBillingState, checked: boolean): void {
  billingState.flows.delete.checkboxChecked = checked;
}