Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

13.8 KB
Raw Blame
import { type ComponentType } from 'react'
import { useTranslation } from '../../../i18n'
import { useIsDark } from '../../../hooks/useIsDark'
import {
  Puzzle, ListChecks, Wallet, FileText, CalendarDays, Globe, Briefcase, Image, Terminal, Link2, Compass, BookOpen,
  Sparkles, Luggage, Plane, Bookmark, Users, Mountain, CarFront, Radar,
} from 'lucide-react'
import DawarichIcon from '../../../components/shared/DawarichIcon'
import AirTrailIcon from '../../../components/shared/AirTrailIcon'
import { DOCUMENT_PROVIDER_ICONS } from '../../../components/shared/documentProviderIconMap'
import MToggle from '../../components/MToggle'
import {
  type Addon,
  type CollabFeatures,
  type ProviderOption,
  COLLAB_SUB_FEATURES,
  getAddonLabel,
} from '../../../components/Admin/addons/addonModel'
import { useAddonManager } from '../../../components/Admin/addons/useAddonManager'
import AiSettingsDialog from '../../../components/Admin/addons/AiAdminConfig'
import { AI_ADDON_ICONS, type AiAddonId } from '../../../components/Admin/addons/aiAddons'
import { useAiAddonTile } from '../../../components/Admin/addons/useAiAddonTile'
import { ImmichIcon, SynologyIcon } from '../../../components/Admin/addons/PhotoProviderIcons'
import { MAdminCard, MAdminOptionRow, MAdminSetupRow } from './MAdminUi'
import MAdminPaddleRegionsPanel from './MAdminPaddleRegionsPanel'
import MMailIngestAdminCard from './MMailIngestAdminCard'
import type { FlightLiveOption } from '../../../components/Admin/AddonManager'
import MHintLabel from '../../components/MHintLabel'
import WandererMark from '../../../components/Tours/wanderer/WandererMark'
import MSkeleton from '../../components/MSkeleton'

// Keys are the `icon` column from the addons table; anything unknown falls back to
// Puzzle. Users/Sparkles cover collab and llm_parsing, as on the desktop.
const ICON_MAP = {
  ListChecks, Wallet, FileText, CalendarDays, Puzzle, Globe, Briefcase, Image, Terminal, Link2, Compass, BookOpen, Plane, Bookmark, Users, Sparkles,
  Mountain,
  // Road trip was seeded as 'Route'; the car says what it is better than a line does.
  Route: CarFront,
  Dawarich: DawarichIcon,
}

const PROVIDER_ICONS: Record<string, ComponentType<{ size?: number }>> = {
  immich: ImmichIcon,
  synologyphotos: SynologyIcon,
  ...DOCUMENT_PROVIDER_ICONS,
}

interface AddonIconProps {
  name: string
  size?: number
  /** A switched-off addon greys its icon out, brand marks included. */
  enabled?: boolean
}

function AddonIcon({ name, size = 18, enabled = true }: AddonIconProps) {
  if (name === 'Dawarich') return <DawarichIcon fill muted={!enabled} />
  // 'Plane' is the icon string airtrail was seeded with, and INSERT OR IGNORE
  // means every existing install still carries it — so the brand is keyed on
  // that rather than on a new name no row would ever have.
  if (name === 'Plane') return <AirTrailIcon fill muted={!enabled} />
  const Icon = ICON_MAP[name] || Puzzle
  return <Icon size={size} />
}

export default function MAdminAddonManager({ bagTrackingEnabled, onToggleBagTracking, collabFeatures, onToggleCollabFeature, flightLive, mailImport = false }: {
  bagTrackingEnabled?: boolean
  onToggleBagTracking?: () => void
  collabFeatures?: CollabFeatures
  onToggleCollabFeature?: (key: string) => void
  /** Live flight status, a row of the trip group (desktop parity). */
  flightLive?: FlightLiveOption
  /** Booking mail import, a row of the integration group with its setup sheet; off on a managed install. */
  mailImport?: boolean
}) {
  const { t } = useTranslation()
  const dark = useIsDark()
  const {
    addons, loading, handleToggle, tripAddons, globalAddons, integrationAddons, aiAddons, providerOptions, documentProviderOptions,
    toursWanderer,
    wandererOffered,
    toggleToursWanderer,
    mapSwitches,
  } = useAddonManager()

  if (loading) {
    return (
      <MSkeleton variant="rows" count={5} />
    )
  }

  return (
    <div className="space-y-3">
      {/* Header */}
      <MAdminCard>
        <div className="text-[0.875rem] font-extrabold text-m-ink">{t('admin.addons.title')}</div>
        {/* One run of text with the wordmark inside it, so a line break falls between words and never around the logo. */}
        <p className="mt-[2px] font-geist text-[length:calc(10px*var(--fs-scale-caption,1))] leading-relaxed text-m-muted">
          {t('admin.addons.subtitleBefore')}{' '}
          <img src={dark ? '/text-light.svg' : '/text-dark.svg'} alt="TREK" className="inline h-[9px] w-auto align-baseline opacity-70" />{' '}
          {t('admin.addons.subtitleAfter')}
        </p>
      </MAdminCard>

      {addons.length === 0 ? (
        <MAdminCard>
          <p className="py-6 text-center font-geist text-[0.6875rem] text-m-faint">
            {t('admin.addons.noAddons')}
          </p>
        </MAdminCard>
      ) : (
        <>
          {/* Trip Addons */}
          {(tripAddons.length > 0 || flightLive) && (
            <MAdminCard>
              <MGroupHead icon={Briefcase} label={t('admin.addons.type.trip')} hint={t('admin.addons.tripHint')} />
              {tripAddons.map((addon, i) => (
                <div key={addon.id}>
                  <MAddonRow addon={addon} onToggle={handleToggle} t={t} first={i === 0} />
                  {addon.id === 'packing' && addon.enabled && onToggleBagTracking && (
                    <MSubRow
                      icon={Luggage}
                      title={t('admin.bagTracking.title')}
                      subtitle={t('admin.bagTracking.subtitle')}
                      enabled={!!bagTrackingEnabled}
                      onToggle={onToggleBagTracking}
                    />
                  )}
                  {addon.id === 'documents' && addon.enabled && <MProviderShelf options={documentProviderOptions} />}
                  {addon.id === 'tours' && addon.enabled && (
                    <>
                      {wandererOffered && (
                        <MSubRow
                          providerIcon={WandererMark}
                          title={t('admin.toursWanderer.title')}
                          subtitle={t('admin.toursWanderer.subtitle')}
                          enabled={toursWanderer}
                          onToggle={() => void toggleToursWanderer()}
                        />
                      )}
                      {mapSwitches.map(({ key, Icon, titleKey, subtitleKey, enabled, toggle }) => (
                        <MSubRow
                          key={key}
                          icon={Icon}
                          title={t(titleKey)}
                          subtitle={t(subtitleKey)}
                          enabled={enabled}
                          onToggle={() => void toggle()}
                        />
                      ))}
                      <MAdminPaddleRegionsPanel />
                    </>
                  )}
                  {addon.id === 'collab' && addon.enabled && collabFeatures && onToggleCollabFeature && (
                    <>
                      {COLLAB_SUB_FEATURES.map(feat => (
                        <MSubRow
                          key={feat.key}
                          icon={feat.icon}
                          title={t(feat.titleKey)}
                          subtitle={t(feat.subtitleKey)}
                          enabled={collabFeatures[feat.key]}
                          onToggle={() => onToggleCollabFeature(feat.key)}
                        />
                      ))}
                    </>
                  )}
                </div>
              ))}
              {flightLive && <MFlightLiveRow option={flightLive} first={tripAddons.length === 0} />}
            </MAdminCard>
          )}

          {/* Global Addons */}
          {globalAddons.length > 0 && (
            <MAdminCard>
              <MGroupHead icon={Globe} label={t('admin.addons.type.global')} hint={t('admin.addons.globalHint')} />
              {globalAddons.map((addon, i) => (
                <div key={addon.id}>
                  <MAddonRow addon={addon} onToggle={handleToggle} t={t} first={i === 0} />
                  {/* Memories providers as sub-items under Journey addon — only while it is on */}
                  {addon.id === 'journey' && addon.enabled && <MProviderShelf options={providerOptions} />}
                </div>
              ))}
            </MAdminCard>
          )}

          {/* Integration Addons, headed by the AI ones as one group, as on the desktop */}
          {(integrationAddons.length > 0 || aiAddons.length > 0 || mailImport) && (
            <MAdminCard>
              <MGroupHead icon={Link2} label={t('admin.addons.type.integration')} hint={t('admin.addons.integrationHint')} />
              {aiAddons.length > 0 && <MAiAddons addons={aiAddons} onToggle={handleToggle} />}
              {integrationAddons.map((addon, i) => (
                <MAddonRow key={addon.id} addon={addon} onToggle={handleToggle} t={t} first={i === 0 && aiAddons.length === 0} />
              ))}
              {mailImport && <MMailIngestAdminCard first={integrationAddons.length === 0 && aiAddons.length === 0} />}
            </MAdminCard>
          )}
        </>
      )}
    </div>
  )
}

/** Group subheader: small icon + uppercase label, matches the desktop section band. */
function MGroupHead({ icon: Icon, label, hint }: { icon: typeof Briefcase; label: string; hint: string }) {
  return (
    <div className="mb-1">
      <div className="flex items-start gap-2">
        <Icon size={13} className="mt-[2px] flex-none text-m-muted" />
        <MHintLabel
          label={label}
          hint={hint}
          labelClassName="font-geist text-[length:calc(10px*var(--fs-scale-caption,1))] font-bold uppercase tracking-[0.06em] text-m-muted"
        />
      </div>
    </div>
  )
}

interface MAddonRowProps {
  addon: Addon
  onToggle: (addon: Addon) => void
  t: (key: string) => string
  first?: boolean
}

/**
 * Addon row: icon tile, name with its description behind the info mark, MToggle.
 * No type badge: every row stands under its type's heading already.
 */
function MAddonRow({ addon, onToggle, t, first }: MAddonRowProps) {
  const label = getAddonLabel(t, addon)
  return (
    <MAdminOptionRow
      icon={<AddonIcon name={addon.icon} size={18} enabled={addon.enabled} />}
      title={label.name}
      hint={label.description}
      first={first}
      trailing={<MToggle checked={addon.enabled} ariaLabel={label.name} onChange={() => onToggle(addon)} />}
    />
  )
}

/** Live flight status as a trip row, locked with a word when the server environment holds it. */
function MFlightLiveRow({ option, first }: { option: FlightLiveOption; first: boolean }) {
  const { t } = useTranslation()
  const hint = option.managed ? t('flightLive.adminHintManaged') : t('flightLive.adminHint')
  return (
    <MAdminOptionRow
      icon={<Radar size={18} />}
      title={t('flightLive.adminTitle')}
      hint={option.locked ? `${hint} ${t('flightLive.envLocked')}` : hint}
      first={first}
      trailing={<MToggle checked={option.on} ariaLabel={t('flightLive.adminToggle')} disabled={option.locked} onChange={option.onToggle} />}
    />
  )
}

/** A provider shelf under its addon: Journey's photo providers, Documents' document providers. */
function MProviderShelf({ options }: { options: ProviderOption[] }) {
  return (
    <>
      {options.map(provider => (
        <MSubRow
          key={provider.key}
          providerIcon={PROVIDER_ICONS[provider.key]}
          title={provider.label}
          subtitle={provider.description}
          enabled={provider.enabled}
          onToggle={provider.toggle}
        />
      ))}
    </>
  )
}

interface MSubRowProps {
  icon?: typeof Luggage
  providerIcon?: ComponentType<{ size?: number }>
  title: string
  subtitle: string
  enabled: boolean
  onToggle: () => void
}

/** Indented sub-feature row (bag tracking, collab features, memory providers). */
function MSubRow({ icon: Icon, providerIcon: ProviderIcon, title, subtitle, enabled, onToggle }: MSubRowProps) {
  return (
    <div className="flex items-center gap-3 border-t border-[color:var(--m-rowbr)] py-[9px] ps-[50px]">
      <span className="flex-none text-m-faint">
        {Icon ? <Icon size={14} /> : ProviderIcon ? <ProviderIcon size={14} /> : null}
      </span>
      <div className="min-w-0 flex-1">
        <MHintLabel label={title} hint={subtitle} labelClassName="text-[length:calc(12px*var(--fs-scale-body,1))] font-bold text-m-ink" />
      </div>
      <MToggle checked={enabled} ariaLabel={title} onChange={onToggle} />
    </div>
  )
}

/** The AI addons as one group: a head row, their switches, and the button to the settings dialog. */
function MAiAddons({ addons, onToggle }: { addons: Addon[]; onToggle: (addon: Addon) => void }) {
  const { t } = useTranslation()
  const tile = useAiAddonTile(addons)
  return (
    <div>
      <div className="flex items-center gap-3 py-[11px]">
        <span className="flex h-[38px] w-[38px] flex-none items-center justify-center rounded-[11px] bg-[color:var(--m-ic)] text-m-ink">
          <Sparkles size={18} />
        </span>
        <div className="min-w-0 flex-1">
          <MHintLabel label={t('admin.addons.ai.name')} hint={t('admin.addons.ai.description')} />
        </div>
        <span className="flex-none rounded-full bg-[color:var(--m-ic)] px-[7px] py-[2px] font-geist text-[0.5625rem] font-bold text-m-muted">
          {tile.enabledCount}/{addons.length}
        </span>
      </div>
      {addons.map(addon => {
        const label = getAddonLabel(t, addon)
        return (
          <MSubRow
            key={addon.id}
            icon={AI_ADDON_ICONS[addon.id as AiAddonId]}
            title={label.name}
            subtitle={label.description}
            enabled={addon.enabled}
            onToggle={() => onToggle(addon)}
          />
        )
      })}
      {tile.canSetUp && <MAdminSetupRow label={t('admin.addons.ai.setup')} onOpen={tile.openDialog} />}
      {tile.dialogOpen && <AiSettingsDialog addons={addons} onClose={tile.closeDialog} />}
    </div>
  )
}