diff --git a/crates/app/src/pages/components/details/export.tsx b/crates/app/src/pages/components/details/export.tsx index 2e912832a..f5c185480 100644 --- a/crates/app/src/pages/components/details/export.tsx +++ b/crates/app/src/pages/components/details/export.tsx @@ -5,7 +5,7 @@ import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue,} from "@/ import {useEffect, useState} from "react"; import {API} from "@/service"; import {useParams} from "react-router-dom"; -import {ComponentList, Export, Parameter, Typ,} from "@/types/component"; +import {Case, ComponentList, Export, Parameter, Typ,} from "@/types/component"; // ---------- Shadcn UI Tooltip Imports ---------- import {TooltipProvider} from "@/components/ui/tooltip"; @@ -96,9 +96,9 @@ function parseTypeForTooltip(typ: Typ | undefined): { }; } case "Variant": { - const cases = (typ.cases || []).map((c) => { + const cases = (typ.cases as Case[] || []).map((c) => { const parsed = parseTypeForTooltip(c.typ); - return `${c.charAt(0).toUpperCase() + c.slice(1)}(${ + return `${c.name.charAt(0).toUpperCase() + c.name.slice(1)}(${ parsed.full })`; }); @@ -108,7 +108,7 @@ function parseTypeForTooltip(typ: Typ | undefined): { }; } case "Enum": { - const cases = (typ.cases || []).map((c) => c.charAt(0).toUpperCase() + c.slice(1)); + const cases = (typ.cases as string[] || []).map((c) => c.charAt(0).toUpperCase() + c.slice(1)); return { short: "enum", full: `enum (\n ${cases.join(",\n ")}\n)`, @@ -220,7 +220,7 @@ export default function Exports() { const [versionList, setVersionList] = useState([]); const [versionChange, setVersionChange] = useState(0); const [result, setResult] = useState([]); - + const [functions, setFunctions] = useState([]); // const [functions, setFunctions] = useState([]); useEffect(() => { @@ -252,6 +252,7 @@ export default function Exports() { ); if (!componentDetails) { setResult([]); + setFunctions([]); return; } @@ -261,6 +262,7 @@ export default function Exports() { componentDetails.metadata?.exports || [] ); setResult(exportsResult); + setFunctions(exportsResult); // If you want to maintain a separate array of raw functions for searching: // const rawFunctions: ComponentExportFunction[] = calculateExportFunctions( @@ -276,15 +278,10 @@ export default function Exports() { const handleSearch = (e: React.ChangeEvent) => { const value = e.target.value.toLowerCase(); - // const searchResult = calculateExportFunctions( - // component.versions?.find( - // (data) => data.versionedComponentId?.version === versionChange - // )?.metadata?.exports || [] - // ).filter((fn: ComponentExportFunction) => - // fn.name.toLowerCase().includes(value) - // ); - - // setFunctions(searchResult); + const searchResult = functions.filter((fn: ExportResult) => + fn.function_name.toLowerCase().includes(value) + ); + setResult(searchResult); }; return ( diff --git a/crates/app/src/pages/components/details/plugin.tsx b/crates/app/src/pages/components/details/plugin.tsx index 3864613f0..44f54ed8b 100644 --- a/crates/app/src/pages/components/details/plugin.tsx +++ b/crates/app/src/pages/components/details/plugin.tsx @@ -1,334 +1,314 @@ -import { Search, Trash2 } from "lucide-react"; -import { Input } from "@/components/ui/input"; -import { - Table, - TableBody, - TableCell, - TableHead, - TableHeader, - TableRow, -} from "@/components/ui/table"; -import { - Select, - SelectContent, - SelectItem, - SelectTrigger, - SelectValue, -} from "@/components/ui/select"; -import { useEffect, useState } from "react"; -import { API } from "@/service"; -import { useParams } from "react-router-dom"; -import { - Component, - ComponentList, - InstalledPlugin, -} from "@/types/component.ts"; -import { toast } from "@/hooks/use-toast.ts"; +import {Search, Trash2} from "lucide-react"; +import {Input} from "@/components/ui/input"; +import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow,} from "@/components/ui/table"; +import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue,} from "@/components/ui/select"; +import {useEffect, useState} from "react"; +import {API} from "@/service"; +import {useParams} from "react-router-dom"; +import {Component, ComponentList, InstalledPlugin,} from "@/types/component.ts"; +import {toast} from "@/hooks/use-toast.ts"; import { Dialog, - DialogTrigger, + DialogClose, DialogContent, - DialogTitle, DialogDescription, - DialogClose, + DialogTitle, + DialogTrigger, } from "@/components/ui/dialog"; -import { Button } from "@/components/ui/button"; -import { Label } from "@/components/ui/label"; -import { Plugin, PluginList } from "@/types/plugin"; -import { object } from "zod"; +import {Button} from "@/components/ui/button"; +import {Label} from "@/components/ui/label"; export default function Plugins() { - const { componentId = "" } = useParams(); - const [component, setComponent] = useState( - {} as ComponentList - ); - const [plugins, setPlugins] = useState([]); - const [filteredPlugins, setFilteredPlugins] = useState([]); - const [versionList, setVersionList] = useState([]); - const [versionChange, setVersionChange] = useState(0); - const [isDialogOpen, setIsDialogOpen] = useState(false); - const [newPlugin, setNewPlugin] = useState({ - name: "", - priority: 1, - version: "", - }); - const [availabePlugin, setAvailabePlugin] = useState< - Record - >({}); - - // Fetch component versions & plugins on mount + const {componentId = ""} = useParams(); + const [component, setComponent] = useState( + {} as ComponentList + ); + const [plugins, setPlugins] = useState([]); + const [filteredPlugins, setFilteredPlugins] = useState([]); + const [versionList, setVersionList] = useState([]); + const [versionChange, setVersionChange] = useState(0); + const [isDialogOpen, setIsDialogOpen] = useState(false); + const [newPlugin, setNewPlugin] = useState({ + name: "", + priority: 1, + version: "", + }); + const [availabePlugin, setAvailabePlugin] = useState< + Record + >({}); - useEffect(() => { - const fetchPlugins = async () => { - try { - const plugins = await API.getPlugins(); - const pluginMap: Record = {}; + // Fetch component versions & plugins on mount - plugins.forEach(({ name, version }) => { - if (!pluginMap[name]) { - pluginMap[name] = []; - } - pluginMap[name].push(version); - }); - setAvailabePlugin(pluginMap); - } catch (error) { - toast({ - title: "Failed to fetch plugins", - description: "An error occurred while fetching the plugin list.", - variant: "destructive", - duration: 5000, - }); - } - }; + useEffect(() => { + const fetchPlugins = async () => { + try { + const plugins = await API.getPlugins(); + const pluginMap: Record = {}; - fetchPlugins(); - if (!componentId) return; - refreshComponent(); - }, []); + plugins.forEach(({name, version}) => { + if (!pluginMap[name]) { + pluginMap[name] = []; + } + pluginMap[name].push(version); + }); + setAvailabePlugin(pluginMap); + } catch (error) { + toast({ + title: "Failed to fetch plugins", + description: `An error occurred while fetching the plugin list. ${error}`, + variant: "destructive", + duration: 5000, + }); + } + }; - const refreshComponent = () => { - API.getComponentByIdAsKey().then((response) => { - setComponent(response[componentId]); - const data = response[componentId]; - const versionList = data.versionList || []; - setVersionList(versionList); - if (versionList.length > 0) { - handleVersionChange(versionList[versionList.length - 1]); - } - }); - }; - - const handleVersionChange = ( - version: number, - fetchedComponent?: Component - ) => { - setVersionChange(version); + fetchPlugins(); + if (!componentId) return; + refreshComponent(); + }, []); - const fetchComponent = fetchedComponent - ? Promise.resolve(fetchedComponent) - : API.getComponentByIdAndVersion(componentId, version); + const refreshComponent = () => { + API.getComponentByIdAsKey().then((response) => { + setComponent(response[componentId]); + const data = response[componentId]; + const versionList = data.versionList || []; + setVersionList(versionList); + if (versionList.length > 0) { + handleVersionChange(versionList[versionList.length - 1]); + } + }); + }; - fetchComponent.then((response) => { - setPlugins(response.installedPlugins || []); - setFilteredPlugins(response.installedPlugins || []); - }); - }; + const handleVersionChange = ( + version: number, + fetchedComponent?: Component + ) => { + setVersionChange(version); - // Handle search input - const handleSearch = (e: React.ChangeEvent) => { - const query = e.target.value.trim().toLowerCase(); - setFilteredPlugins( - plugins.filter((plugin) => plugin.name.toLowerCase().includes(query)) - ); - }; + const fetchComponent = fetchedComponent + ? Promise.resolve(fetchedComponent) + : API.getComponentByIdAndVersion(componentId, version); - // Handle delete action - const handleDeletePlugin = (pluginId: string) => { - const versionList = component.versionList || []; - const latestVersion = versionList[versionList.length - 1]; - if (latestVersion === versionChange) { - API.deletePluginToComponent(componentId, pluginId).then(() => { - toast({ - title: "Plugin deleted successfully", - description: - "Plugin has been deleted successfully. Please check the latest version of the component.", - duration: 3000, + fetchComponent.then((response) => { + setPlugins(response.installedPlugins || []); + setFilteredPlugins(response.installedPlugins || []); }); - refreshComponent(); - }); - } - }; + }; - const handleAddPlugin = () => { - const pluginData = { - name: newPlugin.name, - priority: newPlugin.priority, - version: newPlugin.version, - parameters: {}, + // Handle search input + const handleSearch = (e: React.ChangeEvent) => { + const query = e.target.value.trim().toLowerCase(); + setFilteredPlugins( + plugins.filter((plugin) => plugin.name.toLowerCase().includes(query)) + ); }; - API.addPluginToComponent(componentId, pluginData) - .then(() => { - toast({ - title: "Plugin added successfully", - description: "The new plugin has been added successfully.", - duration: 3000, - }); - refreshComponent(); - setIsDialogOpen(false); - }) - .catch((error) => { - toast({ - title: "Failed to add plugin", - description: "An error occurred while adding the plugin.", - variant: "destructive", - duration: 5000, - }); - }); - }; - return ( -
-
-
-
- {/* Search Input */} -
- - -
+ // Handle delete action + const handleDeletePlugin = (pluginId: string) => { + const versionList = component.versionList || []; + const latestVersion = versionList[versionList.length - 1]; + if (latestVersion === versionChange) { + API.deletePluginToComponent(componentId, pluginId).then(() => { + toast({ + title: "Plugin deleted successfully", + description: + "Plugin has been deleted successfully. Please check the latest version of the component.", + duration: 3000, + }); + refreshComponent(); + }); + } + }; + + const handleAddPlugin = () => { + const pluginData = { + name: newPlugin.name, + priority: newPlugin.priority, + version: newPlugin.version, + parameters: {}, + }; + API.addPluginToComponent(componentId, pluginData) + .then(() => { + toast({ + title: "Plugin added successfully", + description: "The new plugin has been added successfully.", + duration: 3000, + }); + refreshComponent(); + setIsDialogOpen(false); + }) + .catch((error) => { + toast({ + title: "Failed to add plugin", + description: `An error occurred while adding the plugin. ${error}`, + variant: "destructive", + duration: 5000, + }); + }); + }; - {/* Version Selector */} - {versionList.length > 0 && ( - - )} + return ( +
+
+
+
+ {/* Search Input */} +
+ + +
- {/* Add Plugin Button */} - - - - - - Add New Plugin - - Enter the details of the new plugin you want to add. - -
-
- - -
-
- - handleVersionChange(Number(value))} + > + + v{versionChange} + + + {versionList.map((version) => ( + + v{version} + + ))} + + )} - - -
-
- - - setNewPlugin({ - ...newPlugin, - priority: Number(e.target.value), - }) - } - /> -
-
-
- - - - -
-
-
-
- {/* Plugins Table */} -
- - - - Name - Version - Priority - Actions - - - - {filteredPlugins.length > 0 ? ( - filteredPlugins.map((plugin) => ( - - - {plugin.name} - - - {plugin.version} - - - {plugin.priority} - - - - - - )) - ) : ( - - - - )} - -
- No plugins found. -
-
+ {/* Add Plugin Button */} + + + + + + Add New Plugin + + Enter the details of the new plugin you want to add. + +
+
+ + +
+
+ + +
+
+ + + setNewPlugin({ + ...newPlugin, + priority: Number(e.target.value), + }) + } + /> +
+
+
+ + + + +
+
+
+
+ + {/* Plugins Table */} +
+ + + + Name + Version + Priority + Actions + + + + {filteredPlugins.length > 0 ? ( + filteredPlugins.map((plugin) => ( + + + {plugin.name} + + + {plugin.version} + + + {plugin.priority} + + + + + + )) + ) : ( + + + + )} + +
+ No plugins found. +
+
+
+
-
-
- ); + ); }