1
'use client';
2
3
-import { useState, useMemo } from 'react';
3
+import { useState, useMemo, useEffect, useCallback, type ReactNode } from 'react';
4
import Link from 'next/link';
5
import { ExternalLink, Search, Copy, Check, ChevronDown, ChevronRight, ArrowLeft } from 'lucide-react';
6
import { TypeTable } from 'fumadocs-ui/components/type-table';
40
function formatType(param: ParameterSchema): string {
41
let typeStr = param.type || 'string';
42
43
+ // Show array item type
44
+ if (typeStr === 'array' && param.items) {
45
+ const itemType = param.items.type || 'unknown';
46
+ typeStr = `array<${itemType}>`;
47
+ }
48
+
49
// Include enum values in type display
50
if (param.enum && param.enum.length > 0) {
51
const enumValues = param.enum.map(v => `"${v}"`).join(' | ');
55
return typeStr;
56
}
57
52
-// Convert parameter schema to TypeTable format
58
+// Get children from a param (object properties or array item properties)
59
+function getChildren(param: ParameterSchema): Record<string, ParameterSchema> | null {
60
+ const props = param.properties || param.items?.properties;
61
+ if (!props || typeof props !== 'object') return null;
62
+
63
+ const requiredList: string[] = param.requiredFields || param.items?.requiredFields || param.items?.required as string[] || [];
64
+ const result: Record<string, ParameterSchema> = {};
65
+ for (const [key, value] of Object.entries(props)) {
66
+ if (typeof value === 'object' && value !== null) {
67
+ result[key] = {
68
+ ...(value as ParameterSchema),
69
+ required: Array.isArray(requiredList) ? requiredList.includes(key) : false,
70
+ };
71
+ }
72
+ }
73
+ return Object.keys(result).length > 0 ? result : null;
74
+}
75
+
76
+// Build a ReactNode description that includes text + nested TypeTable for children
77
+function buildDescription(param: ParameterSchema): ReactNode {
78
+ const children = getChildren(param);
79
+ if (!children) return param.description || undefined;
80
+
81
+ return (
82
+ <div className="space-y-2">
83
+ {param.description && <p>{param.description}</p>}
84
+ <TypeTable type={paramsToTypeTable(children)} />
85
+ </div>
86
+ );
87
+}
88
+
89
+// Convert parameter schema to TypeTable format, recursively nesting child TypeTables in descriptions
90
function paramsToTypeTable(params: Record<string, ParameterSchema>): Record<string, {
91
type: string;
55
- description?: string;
92
+ description?: ReactNode;
93
default?: string;
94
required?: boolean;
95
}> {
59
- const result: Record<string, {
60
- type: string;
61
- description?: string;
62
- default?: string;
63
- required?: boolean;
64
- }> = {};
65
-
96
+ const result: Record<string, { type: string; description?: ReactNode; default?: string; required?: boolean }> = {};
97
for (const [name, param] of Object.entries(params)) {
98
result[name] = {
99
type: formatType(param),
69
- description: param.description || undefined,
100
+ description: buildDescription(param),
101
default: formatDefault(param.default),
102
required: param.required,
103
};
104
}
74
-
105
return result;
106
}
107
108
+// Process raw API schema into ParameterSchema records
109
+function processApiSchema(schema: any): Record<string, ParameterSchema> | undefined {
110
+ if (!schema) return undefined;
111
+ const props = schema.properties || schema;
112
+ const required = schema.required || [];
113
+ if (!props || typeof props !== 'object') return undefined;
114
+
115
+ const result: Record<string, ParameterSchema> = {};
116
+ for (const [key, value] of Object.entries(props)) {
117
+ if (typeof value === 'object' && value !== null) {
118
+ const param = value as any;
119
+ result[key] = {
120
+ type: param.type,
121
+ description: param.description,
122
+ default: param.default,
123
+ example: param.example,
124
+ enum: param.enum,
125
+ required: required.includes(key),
126
+ ...(param.properties ? { properties: param.properties } : {}),
127
+ ...(param.required ? { requiredFields: param.required } : {}),
128
+ ...(param.items ? { items: param.items } : {}),
129
+ };
130
+ }
131
+ }
132
+ return Object.keys(result).length > 0 ? result : undefined;
133
+}
134
+
135
// Check if item is a Tool with parameters
136
function isTool(item: Tool | Trigger): item is Tool {
137
return 'input_parameters' in item || 'output_parameters' in item;
142
return 'config' in item || 'payload' in item || 'type' in item;
143
}
144
88
-function ToolItem({ item }: { item: Tool | Trigger }) {
145
+function ToolItem({ item, toolkitVersion }: { item: Tool | Trigger; toolkitVersion?: string | null }) {
146
const [expanded, setExpanded] = useState(false);
147
const [copied, setCopied] = useState(false);
148
+ const [detailedParams, setDetailedParams] = useState<{
149
+ input?: Record<string, ParameterSchema>;
150
+ output?: Record<string, ParameterSchema>;
151
+ } | null>(null);
152
+ const [loading, setLoading] = useState(false);
153
154
const copySlug = (e: React.MouseEvent) => {
155
e.stopPropagation();
161
const tool = isTool(item) ? item : null;
162
const trigger = isTrigger(item) ? item : null;
163
102
- const hasInputParams = tool?.input_parameters && Object.keys(tool.input_parameters).length > 0;
103
- const hasOutputParams = tool?.output_parameters && Object.keys(tool.output_parameters).length > 0;
164
+ // Fetch detailed schema when a tool is expanded
165
+ const fetchDetailedSchema = useCallback(async () => {
166
+ if (!tool || detailedParams || loading) return;
167
+ setLoading(true);
168
+ try {
169
+ const versionParam = toolkitVersion ? `?version=${encodeURIComponent(toolkitVersion)}` : '';
170
+ const res = await fetch(`/api/tools/${item.slug}${versionParam}`);
171
+ if (res.ok) {
172
+ const data = await res.json();
173
+ setDetailedParams({
174
+ input: processApiSchema(data.input_parameters),
175
+ output: processApiSchema(data.output_parameters),
176
+ });
177
+ }
178
+ } catch {
179
+ // silently fall back to basic params
180
+ } finally {
181
+ setLoading(false);
182
+ }
183
+ }, [tool, item.slug, toolkitVersion, detailedParams, loading]);
184
+
185
+ useEffect(() => {
186
+ if (expanded && tool) {
187
+ fetchDetailedSchema();
188
+ }
189
+ }, [expanded, tool, fetchDetailedSchema]);
190
+
191
+ const inputParams = detailedParams?.input || tool?.input_parameters;
192
+ const outputParams = detailedParams?.output || tool?.output_parameters;
193
+
194
+ const hasInputParams = inputParams && Object.keys(inputParams).length > 0;
195
+ const hasOutputParams = outputParams && Object.keys(outputParams).length > 0;
196
const hasConfig = trigger?.config && Object.keys(trigger.config).length > 0;
197
const hasPayload = trigger?.payload && Object.keys(trigger.payload).length > 0;
198
236
{hasInputParams && (
237
<div className="space-y-2">
238
<h4 className="text-xs font-semibold uppercase tracking-wide text-fd-muted-foreground">Input Parameters</h4>
147
- <TypeTable type={paramsToTypeTable(tool.input_parameters!)} />
239
+ <TypeTable type={paramsToTypeTable(inputParams!)} />
240
</div>
241
)}
242
243
{hasOutputParams && (
244
<div className="space-y-2">
245
<h4 className="text-xs font-semibold uppercase tracking-wide text-fd-muted-foreground">Output</h4>
154
- <TypeTable type={paramsToTypeTable(tool.output_parameters!)} />
246
+ <TypeTable type={paramsToTypeTable(outputParams!)} />
247
</div>
248
)}
249
433
{activeTab === 'tools' && (
434
filteredTools.length > 0 ? (
435
filteredTools.map((tool) => (
344
- <ToolItem key={tool.slug} item={tool} />
436
+ <ToolItem key={tool.slug} item={tool} toolkitVersion={toolkit.version} />
437
))
438
) : (
439
<p className="px-4 py-8 text-center text-sm text-fd-muted-foreground">
444
{activeTab === 'triggers' && (
445
filteredTriggers.length > 0 ? (
446
filteredTriggers.map((trigger) => (
355
- <ToolItem key={trigger.slug} item={trigger} />
447
+ <ToolItem key={trigger.slug} item={trigger} toolkitVersion={toolkit.version} />
448
))
449
) : (
450
<p className="px-4 py-8 text-center text-sm text-fd-muted-foreground">