-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to delete agents from agent edit page
Signed-off-by: Ryan Hopper-Lowe <[email protected]>
- Loading branch information
1 parent
a39f77d
commit 44289ca
Showing
4 changed files
with
167 additions
and
17 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { EllipsisVerticalIcon } from "lucide-react"; | ||
import { useNavigate } from "react-router"; | ||
import { $path } from "safe-routes"; | ||
import { toast } from "sonner"; | ||
import { mutate } from "swr"; | ||
|
||
import { Agent } from "~/lib/model/agents"; | ||
import { AgentService } from "~/lib/service/api/agentService"; | ||
|
||
import { ConfirmationDialog } from "~/components/composed/ConfirmationDialog"; | ||
import { Button } from "~/components/ui/button"; | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from "~/components/ui/dropdown-menu"; | ||
import { useConfirmationDialog } from "~/hooks/component-helpers/useConfirmationDropdown"; | ||
import { useAsync } from "~/hooks/useAsync"; | ||
|
||
export function AgentDropdownActions({ agent }: { agent: Agent }) { | ||
const navigate = useNavigate(); | ||
|
||
const deleteAgent = useAsync(AgentService.deleteAgent, { | ||
onSuccess: () => { | ||
mutate(AgentService.getAgents.key()); | ||
toast.success("Agent deleted"); | ||
navigate($path("/agents")); | ||
}, | ||
onError: (error) => { | ||
if (error instanceof Error) return toast.error(error.message); | ||
|
||
toast.error("Something went wrong"); | ||
}, | ||
}); | ||
|
||
const { dialogProps, interceptAsync } = useConfirmationDialog(); | ||
|
||
const handleDelete = () => | ||
interceptAsync(() => deleteAgent.executeAsync(agent.id)); | ||
|
||
return ( | ||
<> | ||
<DropdownMenu modal> | ||
<DropdownMenuTrigger> | ||
<Button size="icon" variant="ghost"> | ||
<EllipsisVerticalIcon className="w-4 h-4" /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
|
||
<DropdownMenuContent align="end"> | ||
<DropdownMenuItem | ||
variant="destructive" | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
handleDelete(); | ||
}} | ||
> | ||
Delete Agent | ||
</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
|
||
<ConfirmationDialog | ||
{...dialogProps} | ||
title="Are you sure you want to delete this agent?" | ||
description="This action cannot be undone." | ||
confirmProps={{ | ||
variant: "destructive", | ||
loading: deleteAgent.isLoading, | ||
disabled: deleteAgent.isLoading, | ||
}} | ||
/> | ||
</> | ||
); | ||
} |
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
54 changes: 54 additions & 0 deletions
54
ui/admin/app/hooks/component-helpers/useConfirmationDropdown.tsx
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,54 @@ | ||
import { ComponentProps, useCallback, useState } from "react"; | ||
|
||
import { noop } from "~/lib/utils"; | ||
|
||
import { ConfirmationDialog } from "~/components/composed/ConfirmationDialog"; | ||
|
||
type ConfirmationDialogProps = ComponentProps<typeof ConfirmationDialog>; | ||
type Handler = (e: React.MouseEvent<HTMLButtonElement>) => void; | ||
type AsyncHandler = ( | ||
e: React.MouseEvent<HTMLButtonElement> | ||
) => Promise<unknown>; | ||
|
||
export function useConfirmationDialog( | ||
baseProps?: Partial<ConfirmationDialogProps> | ||
) { | ||
const [props, setProps] = useState<ConfirmationDialogProps>({ | ||
title: "Are you sure?", | ||
onConfirm: noop, | ||
open: false, | ||
...baseProps, | ||
}); | ||
|
||
const updateProps = useCallback( | ||
(props: Partial<ConfirmationDialogProps>) => | ||
setProps((prev) => ({ ...prev, ...props })), | ||
[] | ||
); | ||
|
||
const intercept = useCallback( | ||
(handler: Handler, props?: Partial<ConfirmationDialogProps>) => | ||
updateProps({ | ||
onConfirm: handler, | ||
onCancel: () => updateProps({ open: false }), | ||
open: true, | ||
onOpenChange: (open) => updateProps({ open }), | ||
...props, | ||
}), | ||
[updateProps] | ||
); | ||
|
||
const interceptAsync = useCallback( | ||
(handler: AsyncHandler, props?: Partial<ConfirmationDialogProps>) => | ||
intercept( | ||
async (e) => { | ||
await handler(e); | ||
updateProps({ open: false }); | ||
}, | ||
{ closeOnConfirm: false, ...props } | ||
), | ||
[intercept, updateProps] | ||
); | ||
|
||
return { dialogProps: props, intercept, interceptAsync }; | ||
} |