/** @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}
{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
{params.length > 0 && (
Args
params: {historyParams(outcome.params)}
output: {displayValue(outcome.output)}
{displayValue(event.update.oldValue)}
{historyEventPreview(event)}
)
}
return (