import { useState } from 'react' import { formatINR, fromRupees } from '@sims/domain' import { Badge, Button, DataTable, EmptyState, Field, Notice, PageHeader, Toolbar } from '@sims/ui' import { addPrice, createModule, getModules, getPrices, patchModule, role, KIND_LABEL, type Kind, type Module, } from '../api' import { useData } from './Clients' const ALL_KINDS: Kind[] = ['one_time', 'monthly', 'yearly', 'usage'] const today = () => new Date().toISOString().slice(0, 10) /** Textarea ↔ string[]: one "what's included" bullet per row, blanks dropped. */ const splitLines = (s: string): string[] => s.split('\n').map((x) => x.trim()).filter((x) => x !== '') /** Module catalog + dated price book. Owner edits; staff read-only. */ export function Modules() { const isOwner = role() === 'owner' const modules = useData(getModules, []) const [creating, setCreating] = useState(false) const [selected, setSelected] = useState() return (
setCreating((v) => !v)}>New module : read-only (staff)} /> {creating && isOwner && ( { setCreating(false); modules.reload() }} /> )} {modules.error !== undefined && {modules.error}} {modules.data === undefined ? Loading… : modules.data.length === 0 ? No modules yet. : ( setSelected(modules.data![i])} rows={modules.data.map((m) => ({ code: m.code, name: m.name, sac: m.sac, kinds: m.allowedKinds.map((k) => KIND_LABEL[k]).join(' · '), multi: m.multiSubscription ? yes : '—', active: m.active ? active : inactive, }))} /> )} {selected !== undefined && ( <> modules.reload()} /> )}
) } /** Per-module "what's included" lines — prefilled into the composer, printed under the quote line. */ function QuoteContent(props: { module: Module; isOwner: boolean; onSaved: () => void }) { const [text, setText] = useState(props.module.quoteContent.join('\n')) const [saved, setSaved] = useState(false) const [error, setError] = useState() const save = () => { setError(undefined) patchModule(props.module.id, { quoteContent: splitLines(text) }) .then(() => { setSaved(true); props.onSaved() }) .catch((e: Error) => setError(e.message)) } return ( <>

Quote content — {props.module.name}

{props.isOwner ? (