Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

2.7 KB
Raw Blame
import type { SigningMenuItem } from "@editor/utils/signingItems";
import type { IconName } from "@editor/ui/Icon";

export interface QuickNavIdentity {
  displayName: string;
  profilePictureUrl: string | null;
}

/** One row of the avatar menu: a settings page worth reaching in one click. */
export interface QuickNavAccountShortcut {
  id: string;
  label: string;
  icon: IconName;
  /** A `/settings/{section}` path, optionally with a `#anchor`. */
  to: string;
}

export interface QuickNavAccountAction {
  id: string;
  label: string;
  icon: IconName;
  onSelect: () => void;
}

export type QuickNavAccountHeader = "local" | "signedOut";

export interface QuickNavAccountMenu {
  shortcuts: QuickNavAccountShortcut[];
  /** Absent when there is no session to end (no login, or a guest). */
  signOut?: () => void;
  header?: QuickNavAccountHeader;
  actions?: QuickNavAccountAction[];
}

/** The last resolved account data, retained across view changes. */
export interface QuickNavAccount {
  accountId: string | null;
  isAnonymous: boolean;
  identity: QuickNavIdentity | null;
  signingBadge: number;
  signingItems: SigningMenuItem[];
  portalAccess: boolean;
}

/** Undefined fields retain their previous value; null, false and zero replace it. */
export type QuickNavAccountUpdate = Partial<QuickNavAccount>;

export const EMPTY_QUICK_NAV_ACCOUNT: QuickNavAccount = {
  accountId: null,
  isAnonymous: false,
  identity: null,
  signingBadge: 0,
  signingItems: [],
  portalAccess: false,
};

/** Clears the previous account before applying an update for a different account. */
export function updateQuickNavAccount(
  previous: QuickNavAccount,
  update: QuickNavAccountUpdate,
): QuickNavAccount {
  const accountId =
    update.accountId === undefined ? previous.accountId : update.accountId;
  const current =
    accountId === previous.accountId ? previous : EMPTY_QUICK_NAV_ACCOUNT;
  const next: QuickNavAccount = {
    accountId,
    isAnonymous: update.isAnonymous ?? current.isAnonymous,
    identity:
      update.identity === undefined ? current.identity : update.identity,
    signingBadge: update.signingBadge ?? current.signingBadge,
    signingItems: update.signingItems ?? current.signingItems,
    portalAccess: update.portalAccess ?? current.portalAccess,
  };

  const unchanged =
    next.accountId === previous.accountId &&
    next.isAnonymous === previous.isAnonymous &&
    next.signingBadge === previous.signingBadge &&
    next.signingItems === previous.signingItems &&
    next.portalAccess === previous.portalAccess &&
    next.identity?.displayName === previous.identity?.displayName &&
    next.identity?.profilePictureUrl === previous.identity?.profilePictureUrl;
  return unchanged ? previous : next;
}