Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

5.9 KB
Raw Blame
// Workspace settings: everything about the workspace, one section at a time (#workspace/<section>): General (its name
// and the rest of the business card, recordings and the call export, LIN-837), the team, phone numbers, billing
// (Linedesk Cloud: owners and admins, with or without a bill) and leaving it.
// Reached by G S (G T opens Team), the menu and ⌘K. Your own notifications are in Account; business hours and
// webhooks in the assistant's settings; API keys and Zapier in Integrations (old #settings, #team and
// #workspace/developers links are rewritten, shell/useRoute.ts).

import type { MessageKey } from '@linedesk/i18n'
import { useT } from '@linedesk/i18n/react'
import { useMemo } from 'react'
import type { PhoneSetupStatus } from '../auth.ts'
import { TeamSection } from '../team/TeamPage.tsx'
import { useTeam } from '../team/team-api.ts'
import { type PageSection, SettingsLayout, useSection } from '../ui/SettingsLayout.tsx'
import { AgentSetupStrip } from './AgentChange.tsx'
import { Billing } from './Billing.tsx'
import { DangerZone } from './DangerZone.tsx'
import { PhoneNumbers } from './PhoneNumbers.tsx'
import { WorkspaceGeneral } from './WorkspaceGeneral.tsx'

type SectionId = 'general' | 'team' | 'phone' | 'billing' | 'danger'
const SECTION_IDS: readonly SectionId[] = ['general', 'team', 'phone', 'billing', 'danger']

/**
 * Which sections this person sees: Billing only on an install that bills, for owners and admins (`billing`). Members
 * see General without its Export calls line (call:export).
 */
export function shownSectionIds(_canEdit: boolean, billing = false): SectionId[] {
  return SECTION_IDS.filter(id => id !== 'billing' || billing)
}

/** The page's sections in the active language (shownSectionIds); also listed on Settings (SettingsHome). */
export function useWorkspaceSections(canEdit: boolean, billing: boolean): readonly PageSection<SectionId>[] {
  const t = useT()
  return useMemo(
    () =>
      shownSectionIds(canEdit, billing).map(id => ({
        id,
        // i18n-dynamic: settings.workspace.nav.*
        label: t(`settings.workspace.nav.${id}.label` as MessageKey),
        description: t(`settings.workspace.nav.${id}.description` as MessageKey),
      })),
    [t, canEdit, billing],
  )
}

/**
 * What the Danger zone offers this person: the owner can't leave (they hand the workspace over or delete it), everyone
 * else can only leave (deleting is the owner's). The general words while the role isn't known yet.
 */
function dangerDescription(canEdit: boolean, role: string | undefined): MessageKey {
  if (!canEdit || role === 'admin' || role === 'member') return 'settings.workspace.nav.danger.leave'
  if (role === 'owner') return 'settings.workspace.nav.danger.owner'
  return 'settings.workspace.nav.danger.description'
}

type Props = {
  /** Owners and admins in their own session (settingsAccess, readOnly.ts). */
  canEdit: boolean
  /** Owners and admins, also during support access: what the Danger zone offers (dangerDescription). */
  canManageKeys: boolean
  /** Owners and admins: the strip to finish the setup until go-live, then "Edit with AI" and "Step by step" (AgentChange.tsx). */
  agentChange?: boolean
  phoneSetup: PhoneSetupStatus | null
  isInstanceAdmin: boolean
  /**
   * Linedesk Cloud (workspaceBillingView): the Billing section. On a bill, the bill's page for owners and admins
   * (`owner`, `billing:manage`; the server shows this workspace's usage alone to whoever does not manage the bill) or
   * the usage alone (`usage`); without a bill, "not on a bill" for owners and admins (`none`). Off billing and for
   * members it is not there at all.
   */
  billing?: 'owner' | 'usage' | 'none' | null
  /** In this client workspace through a white-label partner's team: Billing says it is on their partner plan. */
  viaPartner?: boolean
  /** On a bill: what each extra number costs a month, said where a number is added; null otherwise. */
  extraNumberCents?: number | null
  workspaceName: string
  /** After a rename here: the menu and the workspace switcher show the new name at once (App, renamedWorkspace). */
  onRenamed: (name: string) => void
  toast: (text: string) => void
}

export function WorkspaceSettings({
  canEdit,
  canManageKeys,
  agentChange = false,
  phoneSetup,
  isInstanceAdmin,
  billing = null,
  viaPartner = false,
  extraNumberCents = null,
  workspaceName,
  onRenamed,
  toast,
}: Props) {
  const t = useT()
  const team = useTeam().data
  const role = team?.members.find(m => m.userId === team.me)?.role
  const sections = useWorkspaceSections(canEdit, !!billing)
  // What the role offers, also during support access (the section itself then says it is read-only).
  const danger = t(dangerDescription(canManageKeys, role))
  const shown = useMemo(
    () => sections.map(s => (s.id === 'danger' ? { ...s, description: danger } : s)),
    [sections, danger],
  )
  const current = useSection('workspace', shown)
  return (
    <div className="page">
      <SettingsLayout
        page="workspace"
        title={t('settings.workspace.title')}
        context={<span className="sl-context-text">{workspaceName}</span>}
        banner={agentChange && <AgentSetupStrip target={{ kind: 'workspace' }} name={workspaceName} />}
        sections={shown}
        current={current}
      >
        {current === 'general' && (
          <WorkspaceGeneral canEdit={canEdit} name={workspaceName} onRenamed={onRenamed} toast={toast} />
        )}
        {current === 'team' && <TeamSection toast={toast} />}
        {current === 'phone' && (
          <PhoneNumbers
            canEdit={canEdit}
            status={phoneSetup}
            isInstanceAdmin={isInstanceAdmin}
            extraNumberCents={extraNumberCents}
          />
        )}
        {current === 'billing' && billing && <Billing toast={toast} view={billing} viaPartner={viaPartner} />}
        {current === 'danger' && <DangerZone workspaceName={workspaceName} isInstanceAdmin={isInstanceAdmin} />}
      </SettingsLayout>
    </div>
  )
}