Open History to see the last commit
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;
12}
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 {
48 kind: "thread",
49 capabilities: { details: "tooltip", expanded: "none", defaultDraftView: "compact" },
50 },
51 {
52 kind: "environment",
53 capabilities: { details: "tooltip", expanded: "none", defaultDraftView: "compact" },
54 },
55] as const satisfies ReadonlyArray<ContextPresentationDefinition>;
56
57function buildDefinitionRegistry(
58 definitions: ReadonlyArray<ContextPresentationDefinition>,
59): ReadonlyMap<KnownComposerContextKind, ContextPresentationDefinition> {
60 const registry = new Map<KnownComposerContextKind, ContextPresentationDefinition>();
61 for (const definition of definitions) {
62 if (registry.has(definition.kind)) {
63 throw new Error(`Duplicate context presentation definition: ${definition.kind}`);
64 }
65 registry.set(definition.kind, definition);
66 }
67 for (const kind of COMPOSER_CONTEXT_KINDS) {
68 if (!registry.has(kind)) {
69 throw new Error(`Missing context presentation definition: ${kind}`);
70 }
71 }
72 return registry;
73}
74
75const CONTEXT_PRESENTATION_DEFINITIONS = buildDefinitionRegistry(DEFINITIONS);
76
77function contextPresentationDefinition(
78 kind: KnownComposerContextKind,
79): ContextPresentationDefinition {
80 return CONTEXT_PRESENTATION_DEFINITIONS.get(kind)!;
81}
82
83export interface ContextPresentationHandler<TRecord, TRenderContext, TResult> {
84 kind: KnownComposerContextKind;
85 canRender?: (record: TRecord, context: TRenderContext) => boolean;
86 render: (
87 record: TRecord,
88 context: TRenderContext,
89 definition: ContextPresentationDefinition,
90 ) => TResult;
91}
92
93export interface ContextPresentationRegistry<TRecord, TRenderContext, TResult> {
94 definition(kind: KnownComposerContextKind): ContextPresentationDefinition;
95 render(kind: string, record: TRecord | undefined, context: TRenderContext): TResult;
96}
97
98export function createContextPresentationRegistry<TRecord, TRenderContext, TResult>(options: {
99 handlers: ReadonlyArray<ContextPresentationHandler<TRecord, TRenderContext, TResult>>;
100 requiredKinds?: ReadonlyArray<KnownComposerContextKind>;
101 fallback: (kind: string, record: TRecord | undefined, context: TRenderContext) => TResult;
102}): ContextPresentationRegistry<TRecord, TRenderContext, TResult> {
103 const handlers = new Map<
104 KnownComposerContextKind,
105 ContextPresentationHandler<TRecord, TRenderContext, TResult>
106 >();
107 for (const handler of options.handlers) {
108 if (handlers.has(handler.kind)) {
109 throw new Error(`Duplicate context presentation handler: ${handler.kind}`);
110 }
111 handlers.set(handler.kind, handler);
112 }
113 for (const kind of options.requiredKinds ?? []) {
114 if (!handlers.has(kind)) {
115 throw new Error(`Missing context presentation handler: ${kind}`);
116 }
117 }
118
119 return {
120 definition: contextPresentationDefinition,
121 render(kind, record, context) {
122 const handler = handlers.get(kind as KnownComposerContextKind);
123 if (!handler || record === undefined || handler.canRender?.(record, context) === false) {
124 return options.fallback(kind, record, context);
125 }
126 return handler.render(record, context, contextPresentationDefinition(handler.kind));
127 },
128 };
129}