/** @jsxImportSource @atom.io/spa */ import type { SpaElement } from "@atom.io/spa" import { EditorIcon } from "./EditorIcon.tsx" import css from "./EventView.module.css" import { compactOutcomePath, historyEventPreview, historyGraphId, historyIdentity, historyParams, historyValue, historyWriteCount, type RecordedEvent, timelineEventKind, timelineEventTitle, } from "./history-event.ts" import { describeOutcomeValue, historySnapshotShape } from "./history-value.ts" import { HistoryWrite } from "./HistoryWrite.tsx" import { OutcomeValue } from "./OutcomeValue.tsx" import { type EditorTools, useExpanded } from "./store.ts" import { timelineEventSummary } from "./timeline-event-summary.ts" import { useTimelineOutcomeLink } from "./timeline-outcome-link.ts" export interface EcologyEventInteraction { owner: string hovered: string | undefined selected: string | undefined viewOutcome?(owner: string, outcome: string): boolean } const displayValue = (value: unknown, limit?: number) => historyValue(value, limit) ?? `[opaque]` export function EventView(props: { tools: EditorTools event: RecordedEvent index: number depth?: number ecology?: EcologyEventInteraction | undefined compact?: boolean | undefined timeline?: { key: string; applied: boolean; redoNext: boolean } | undefined timelineChild?: boolean | undefined initialOpen?: boolean }): SpaElement { const event = props.event const token = `token` in event ? event.token : undefined const depth = props.depth ?? 0 const timelineEvent = props.timeline !== undefined const timelineDetails = timelineEvent || props.timelineChild === true const compact = timelineEvent || props.compact === true const identity = historyIdentity(event) const owner = props.ecology?.owner const graphId = owner ? historyGraphId(owner, identity) : undefined const standaloneTimelineEvent = timelineEvent && !owner const outcome = event.type === `transaction_outcome` ? event : undefined const outcomeLink = timelineEvent && outcome ? useTimelineOutcomeLink({ tools: props.tools, event: outcome, timelineKey: props.timeline!.key, navigate: props.ecology?.viewOutcome, }) : undefined const topOutcome = !timelineEvent && compact && owner !== undefined const name = timelineEvent ? timelineEventTitle(event, props.index) : topOutcome ? `Outcome ${props.index + 1}` : token?.key const writeCount = compact && `subEvents` in event ? historyWriteCount(event.subEvents) : undefined const summary = timelineEvent ? timelineEventSummary(event, writeCount) : undefined const summaryText = typeof summary === `object` ? `${summary.label ? `${summary.label}: ` : ``}${summary.before} to ${summary.after}` : summary const description = () => outcomeLink ? undefined : [props.timeline?.redoNext ? `Redo next` : undefined, summaryText] .filter(Boolean) .join(`. `) || undefined const timelineTail = () => (summary !== undefined || props.timeline?.redoNext) && ( {props.timeline?.redoNext && Redo next} {summary !== undefined && ( {typeof summary === `string` ? ( summary ) : ( <> {summary.label && ( {summary.label} )} {summary.before} {summary.after} )} )} ) const lifecycle = timelineDetails && (event.type === `atom_creation` || event.type === `atom_disposal`) ? event : undefined const headingName = timelineEvent ? name! : topOutcome ? `outcome ${props.index + 1}` : `event` const selected = () => graphId ? props.ecology!.selected === graphId : undefined const label = () => ( <> {name} {timelineEvent ? !outcomeLink && ( {timelineEventKind(event)} ) : !(compact && outcome) && ( {compact ? event.type.replaceAll(`_`, ` `) : event.type} )} {timelineEvent && !outcomeLink && timelineTail()} ) const outcomeKind = () => { if (!outcomeLink) return const kind = timelineEventKind(event) const source = outcomeLink.source() return source ? ( ) : ( {kind} ) } const shownOutcome = () => outcomeLink?.open() && outcomeLink.source() const originalOutcome = () => { const source = shownOutcome() return ( source && ( ) ) } const descendants = `subEvents` in event ? event.subEvents : [] const [getOpen, toggle] = useExpanded( props.tools, timelineEvent ? [`timeline-event`, props.timeline!.key, identity] : compact ? compactOutcomePath(identity) : [`event`, identity], props.initialOpen ?? !compact, ) const nested = () => depth < 31 ? descendants.map((child, index) => ( )) : `Nested events omitted` const impact = () => ( {nested()} ) const value = (caption: string, data: unknown, wrap?: boolean) => ( ) const lifecycleDetails = () => (

{timelineEvent ? ( Value ) : ( <> {token?.key} {timelineEventKind(event)} )} {lifecycle && `value` in lifecycle && value(`recorded value`, lifecycle.value, true)}

) const args = (params: readonly unknown[], full = false) => historyParams(params, full, (data) => describeOutcomeValue(data, full).text) const hasMetadata = outcome && (outcome.params.length > 0 || outcome.output !== undefined) const compactOutcome = ({ params, output }: NonNullable) => ( <> {descendants.length > 0 && impact()} {hasMetadata && (

{params.length > 0 && ( Args args(params, true)} wrap={writeCount === 0} /> )} {output !== undefined && ( Result {value( `result`, output, historySnapshotShape(output) !== undefined, )} )}

)} ) const expandable = !(timelineEvent && lifecycle && !(`value` in lifecycle)) && !( compact && !timelineEvent && outcome && !hasMetadata && descendants.length === 0 ) const details = () => { if (outcome) { if (compact) return compactOutcome(outcome) return ( <>

params: {historyParams(outcome.params)}

output: {displayValue(outcome.output)}

{impact()} ) } if (lifecycle) return lifecycleDetails() if (`update` in event) return timelineEvent ? ( ) : (

{displayValue(event.update.oldValue)} {displayValue(event.update.newValue)}

) return descendants.length ? ( impact() ) : ( {historyEventPreview(event)} ) } return ( {compact && !timelineEvent && `update` in event ? ( ) : lifecycle && !timelineEvent ? ( lifecycleDetails() ) : ( <> {owner ? ( ) : ( !standaloneTimelineEvent && label() )} {outcomeKind()} {outcomeLink && timelineTail()} {!timelineEvent && writeCount !== undefined && ( {`${writeCount} ${writeCount === 1 ? `write` : `writes`}`} )} {timelineEvent ? (getOpen() || shownOutcome()) && ( {getOpen() && details()} {originalOutcome()} ) : getOpen() && details()} )} ) }