-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add tools table for creating and deleting tools #284
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useForm } from "react-hook-form"; | ||
|
||
import { CreateToolReference } from "~/lib/model/toolReferences"; | ||
import { ToolReferenceService } from "~/lib/service/api/toolreferenceService"; | ||
|
||
import { Button } from "~/components/ui/button"; | ||
import { Input } from "~/components/ui/input"; | ||
import { useAsync } from "~/hooks/useAsync"; | ||
|
||
interface CreateToolProps { | ||
onSuccess: () => void; | ||
} | ||
|
||
export function CreateTool({ onSuccess }: CreateToolProps) { | ||
const { register, handleSubmit, reset } = useForm<CreateToolReference>(); | ||
|
||
const { execute: onSubmit, isLoading } = useAsync( | ||
async (data: CreateToolReference) => { | ||
await ToolReferenceService.createToolReference({ | ||
toolReference: { ...data, toolType: "tool" }, | ||
}); | ||
reset(); | ||
onSuccess(); | ||
}, | ||
{ | ||
onError: (error) => | ||
console.error("Failed to create tool reference:", error), | ||
} | ||
); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4"> | ||
<div> | ||
<Input | ||
autoComplete="off" | ||
{...register("name", { required: "Name is required" })} | ||
placeholder="Tool Name" | ||
/> | ||
</div> | ||
<div> | ||
<Input | ||
autoComplete="off" | ||
{...register("reference", { | ||
required: "Reference is required", | ||
})} | ||
placeholder="Tool Reference" | ||
/> | ||
</div> | ||
<div> | ||
<Button type="submit" variant="secondary" disabled={isLoading}> | ||
{isLoading ? "Creating..." : "Register Tool"} | ||
</Button> | ||
</div> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { ColumnDef, createColumnHelper } from "@tanstack/react-table"; | ||
import { Trash } from "lucide-react"; | ||
|
||
import { ToolReference } from "~/lib/model/toolReferences"; | ||
import { timeSince } from "~/lib/utils"; | ||
|
||
import { TypographyP } from "~/components/Typography"; | ||
import { ConfirmationDialog } from "~/components/composed/ConfirmationDialog"; | ||
import { DataTable } from "~/components/composed/DataTable"; | ||
import { ToolIcon } from "~/components/tools/ToolIcon"; | ||
import { Button } from "~/components/ui/button"; | ||
|
||
interface ToolTableProps { | ||
tools: ToolReference[]; | ||
filter: string; | ||
onDelete: (id: string) => void; | ||
} | ||
|
||
export function ToolTable({ tools, filter, onDelete }: ToolTableProps) { | ||
const filteredTools = tools.filter( | ||
(tool) => | ||
tool.name?.toLowerCase().includes(filter.toLowerCase()) || | ||
tool.metadata?.category | ||
?.toLowerCase() | ||
.includes(filter.toLowerCase()) || | ||
tool.description?.toLowerCase().includes(filter.toLowerCase()) | ||
); | ||
|
||
return ( | ||
<DataTable | ||
columns={getColumns(onDelete)} | ||
data={filteredTools} | ||
sort={[{ id: "created", desc: true }]} | ||
/> | ||
); | ||
} | ||
|
||
function getColumns( | ||
onDelete: (id: string) => void | ||
): ColumnDef<ToolReference, string>[] { | ||
const columnHelper = createColumnHelper<ToolReference>(); | ||
|
||
return [ | ||
columnHelper.display({ | ||
id: "category", | ||
header: "Category", | ||
cell: ({ row }) => ( | ||
<TypographyP className="flex items-center gap-2"> | ||
<ToolIcon | ||
className="w-5 h-5" | ||
name={row.original.name} | ||
icon={row.original.metadata?.icon} | ||
/> | ||
{row.original.metadata?.category ?? "Uncategorized"} | ||
</TypographyP> | ||
), | ||
}), | ||
columnHelper.display({ | ||
id: "name", | ||
header: "Name", | ||
cell: ({ row }) => ( | ||
<TypographyP> | ||
{row.original.name} | ||
{row.original.metadata?.bundle ? " Bundle" : ""} | ||
</TypographyP> | ||
), | ||
}), | ||
columnHelper.accessor("reference", { | ||
header: "Reference", | ||
}), | ||
columnHelper.display({ | ||
id: "description", | ||
header: "Description", | ||
cell: ({ row }) => ( | ||
<TypographyP>{row.original.description}</TypographyP> | ||
), | ||
}), | ||
columnHelper.accessor("created", { | ||
header: "Created", | ||
cell: ({ getValue }) => ( | ||
<TypographyP>{timeSince(new Date(getValue()))} ago</TypographyP> | ||
), | ||
sortingFn: "datetime", | ||
}), | ||
columnHelper.display({ | ||
id: "actions", | ||
cell: ({ row }) => ( | ||
<ConfirmationDialog | ||
title="Delete Tool Reference" | ||
description="Are you sure you want to delete this tool reference? This action cannot be undone." | ||
onConfirm={() => onDelete(row.original.id)} | ||
confirmProps={{ | ||
variant: "destructive", | ||
children: "Delete", | ||
}} | ||
> | ||
<Button variant="ghost" size="sm"> | ||
<Trash className="h-4 w-4" /> | ||
</Button> | ||
</ConfirmationDialog> | ||
), | ||
}), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { PlusIcon, SearchIcon } from "lucide-react"; | ||
import { useState } from "react"; | ||
import useSWR, { preload } from "swr"; | ||
|
||
import { ToolReferenceService } from "~/lib/service/api/toolreferenceService"; | ||
|
||
import { TypographyH2 } from "~/components/Typography"; | ||
import { CreateTool } from "~/components/tools/CreateTool"; | ||
import { ToolTable } from "~/components/tools/ToolTable"; | ||
import { Button } from "~/components/ui/button"; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "~/components/ui/dialog"; | ||
import { Input } from "~/components/ui/input"; | ||
|
||
export async function clientLoader() { | ||
await Promise.all([ | ||
preload(ToolReferenceService.getToolReferences.key("tool"), () => | ||
ToolReferenceService.getToolReferences("tool") | ||
), | ||
]); | ||
return null; | ||
} | ||
|
||
export default function Tools() { | ||
const { data: tools, mutate } = useSWR( | ||
ToolReferenceService.getToolReferences.key("tool"), | ||
() => ToolReferenceService.getToolReferences("tool") | ||
); | ||
|
||
const [isDialogOpen, setIsDialogOpen] = useState(false); | ||
const [searchQuery, setSearchQuery] = useState(""); | ||
|
||
const handleCreateSuccess = () => { | ||
mutate(); | ||
setIsDialogOpen(false); | ||
}; | ||
|
||
const handleDelete = async (id: string) => { | ||
await ToolReferenceService.deleteToolReference(id); | ||
mutate(); | ||
}; | ||
|
||
return ( | ||
<div className="h-full p-8 flex flex-col gap-4"> | ||
<div className="flex justify-between items-center"> | ||
<TypographyH2>Tools</TypographyH2> | ||
<div className="flex items-center space-x-2"> | ||
<div className="relative"> | ||
<SearchIcon className="w-5 h-5 text-gray-400 absolute left-3 top-1/2 transform -translate-y-1/2" /> | ||
<Input | ||
type="text" | ||
placeholder="Search for tools..." | ||
value={searchQuery} | ||
onChange={(e) => setSearchQuery(e.target.value)} | ||
className="pl-10 w-64" | ||
/> | ||
</div> | ||
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}> | ||
<DialogTrigger asChild> | ||
<Button variant="outline"> | ||
<PlusIcon className="w-4 h-4 mr-2" /> | ||
Register New Tool | ||
</Button> | ||
</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle> | ||
Create New Tool Reference | ||
</DialogTitle> | ||
</DialogHeader> | ||
<CreateTool onSuccess={handleCreateSuccess} /> | ||
</DialogContent> | ||
</Dialog> | ||
</div> | ||
</div> | ||
|
||
{tools && ( | ||
<ToolTable | ||
tools={tools} | ||
filter={searchQuery} | ||
onDelete={handleDelete} | ||
/> | ||
)} | ||
</div> | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: preload in a
clientLoader
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!