Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

contextPresentationRegistry.ts

126 lines4.4 KB

1import { COMPOSER_CONTEXT_KINDS, type KnownComposerContextKind } from "@t3tools/contracts";
2​
3export interface ContextPresentationCapability {
4 details: "none" | "tooltip" | "popover";
5 expanded: "none" | "inline-block" | "modal";
6 defaultDraftView: "compact" | "expanded";
7}
8​
9export interface ContextPresentationDefinition {
10 kind: KnownComposerContextKind;
11 capabilities: ContextPresentationCapability;
13​
14const DEFINITIONS = [
15 {
16 kind: "image",
17 capabilities: { details: "tooltip", expanded: "modal", defaultDraftView: "compact" },
18 },
19 {
20 kind: "file",
21 capabilities: { details: "tooltip", expanded: "modal", defaultDraftView: "compact" },
22 },
23 {
24 kind: "terminal",
25 capabilities: { details: "popover", expanded: "none", defaultDraftView: "compact" },
26 },
27 {
28 kind: "element",
29 capabilities: { details: "popover", expanded: "none", defaultDraftView: "compact" },
30 },
31 {
32 kind: "preview-annotation",
33 capabilities: { details: "popover", expanded: "modal", defaultDraftView: "compact" },
34 },
35 {
36 kind: "review-comment",
37 capabilities: { details: "popover", expanded: "none", defaultDraftView: "compact" },
38 },
39 {
40 kind: "mention",
41 capabilities: { details: "none", expanded: "none", defaultDraftView: "compact" },
42 },
43 {
44 kind: "skill",
45 capabilities: { details: "tooltip", expanded: "none", defaultDraftView: "compact" },
46 },
47] as const satisfies ReadonlyArray<ContextPresentationDefinition>;
48​
49function buildDefinitionRegistry(
50 definitions: ReadonlyArray<ContextPresentationDefinition>,
51): ReadonlyMap<KnownComposerContextKind, ContextPresentationDefinition> {
52 const registry = new Map<KnownComposerContextKind, ContextPresentationDefinition>();
53 for (const definition of definitions) {
54 if (registry.has(definition.kind)) {
55 throw new Error(`Duplicate context presentation definition: ${definition.kind}`);
56 }
57 registry.set(definition.kind, definition);
58 }
59 for (const kind of COMPOSER_CONTEXT_KINDS) {
60 if (!registry.has(kind)) {
61 throw new Error(`Missing context presentation definition: ${kind}`);
62 }
63 }
64 return registry;
66​
67export const CONTEXT_PRESENTATION_DEFINITIONS = buildDefinitionRegistry(DEFINITIONS);
68​
69export function contextPresentationDefinition(
70 kind: KnownComposerContextKind,
71): ContextPresentationDefinition {
72 return CONTEXT_PRESENTATION_DEFINITIONS.get(kind)!;
74​
75export interface ContextPresentationHandler<TRecord, TRenderContext, TResult> {
76 kind: KnownComposerContextKind;
77 canRender?: (record: TRecord, context: TRenderContext) => boolean;
78 render: (
79 record: TRecord,
80 context: TRenderContext,
81 definition: ContextPresentationDefinition,
82 ) => TResult;
84​
85export interface ContextPresentationRegistry<TRecord, TRenderContext, TResult> {
86 definition(kind: KnownComposerContextKind): ContextPresentationDefinition;
87 render(kind: string, record: TRecord | undefined, context: TRenderContext): TResult;
89​
90/**
91 * Creates one checked dispatcher for a rendering surface. Definitions are shared; renderers stay
92 * surface-specific so web composer and transcript presentation can differ without drifting on
93 * capability semantics.
94 */
95export function createContextPresentationRegistry<TRecord, TRenderContext, TResult>(options: {
96 handlers: ReadonlyArray<ContextPresentationHandler<TRecord, TRenderContext, TResult>>;
97 requiredKinds?: ReadonlyArray<KnownComposerContextKind>;
98 fallback: (kind: string, record: TRecord | undefined, context: TRenderContext) => TResult;
99}): ContextPresentationRegistry<TRecord, TRenderContext, TResult> {
100 const handlers = new Map<
101 KnownComposerContextKind,
102 ContextPresentationHandler<TRecord, TRenderContext, TResult>
103 >();
104 for (const handler of options.handlers) {
105 if (handlers.has(handler.kind)) {
106 throw new Error(`Duplicate context presentation handler: ${handler.kind}`);
107 }
108 handlers.set(handler.kind, handler);
109 }
110 for (const kind of options.requiredKinds ?? []) {
111 if (!handlers.has(kind)) {
112 throw new Error(`Missing context presentation handler: ${kind}`);
113 }
114 }
115​
116 return {
117 definition: contextPresentationDefinition,
118 render(kind, record, context) {
119 const handler = handlers.get(kind as KnownComposerContextKind);
120 if (!handler || record === undefined || handler.canRender?.(record, context) === false) {
121 return options.fallback(kind, record, context);
122 }
123 return handler.render(record, context, contextPresentationDefinition(handler.kind));
124 },
125 };