-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow users to force-refresh their custom tools (#1240)
* feat: allow users to force-refresh their custom tools Signed-off-by: Ryan Hopper-Lowe <ryan@acorn.io> * fix: implement polling while asynchronous tool refresh is unresolved - force refresh happens asynchronously from response. Must implement polling to ensure data displayed is up to date - removes the redundant `ToolReferenceService.getToolReferencesCategoryMap` method by extracting unique code and applying it as needed in components * chore: extract action code for easier updates --------- Signed-off-by: Ryan Hopper-Lowe <ryan@acorn.io>
- Loading branch information
1 parent
d7e5abb
commit d569be4
Showing
11 changed files
with
196 additions
and
74 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
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
50 changes: 50 additions & 0 deletions
50
ui/admin/app/components/tools/toolGrid/ToolCardActions.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,50 @@ | ||
import { EllipsisVerticalIcon } from "lucide-react"; | ||
import { toast } from "sonner"; | ||
|
||
import { ToolReference } from "~/lib/model/toolReferences"; | ||
import { ToolReferenceService } from "~/lib/service/api/toolreferenceService"; | ||
|
||
import { LoadingSpinner } from "~/components/ui/LoadingSpinner"; | ||
import { Button } from "~/components/ui/button"; | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from "~/components/ui/dropdown-menu"; | ||
import { useAsync } from "~/hooks/useAsync"; | ||
import { usePollSingleTool } from "~/hooks/usePollSingleTool"; | ||
|
||
export function ToolCardActions({ tool }: { tool: ToolReference }) { | ||
const { startPolling, isPolling } = usePollSingleTool(tool.id); | ||
|
||
const forceRefresh = useAsync( | ||
ToolReferenceService.forceRefreshToolReference, | ||
{ | ||
onSuccess: () => { | ||
toast.success("Tool reference force refreshed"); | ||
startPolling(); | ||
}, | ||
} | ||
); | ||
|
||
return ( | ||
<div className="flex items-center gap-2"> | ||
{(forceRefresh.isLoading || isPolling) && <LoadingSpinner />} | ||
|
||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="ghost" size="icon" className="m-0"> | ||
<EllipsisVerticalIcon /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
|
||
<DropdownMenuContent side="top" align="start"> | ||
<DropdownMenuItem onClick={() => forceRefresh.execute(tool.id)}> | ||
Refresh Tool | ||
</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</div> | ||
); | ||
} |
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,49 @@ | ||
import { useEffect, useState } from "react"; | ||
import useSWR from "swr"; | ||
|
||
import { ToolReferenceService } from "~/lib/service/api/toolreferenceService"; | ||
|
||
export function usePollSingleTool(toolId: string) { | ||
const [isPolling, setIsPolling] = useState(false); | ||
|
||
const { mutate: updateTools } = useSWR( | ||
isPolling ? ToolReferenceService.getToolReferences.key("tool") : null, | ||
({ type }) => ToolReferenceService.getToolReferences(type), | ||
{ fallbackData: [], revalidateIfStale: false } | ||
); | ||
|
||
const getTool = useSWR( | ||
isPolling ? ToolReferenceService.getToolReferenceById.key(toolId) : null, | ||
({ toolReferenceId }) => | ||
ToolReferenceService.getToolReferenceById(toolReferenceId), | ||
{ refreshInterval: 1000 } | ||
); | ||
|
||
useEffect(() => { | ||
if (!getTool.data) return; | ||
|
||
setIsPolling(!getTool.data.resolved); | ||
|
||
// resolved means async update is complete | ||
if (getTool.data.resolved) { | ||
updateTools( | ||
(tools) => { | ||
if (!getTool.data) return tools; | ||
if (!tools) return [getTool.data]; | ||
|
||
const index = tools.findIndex((tool) => tool.id === toolId); | ||
|
||
const copy = [...tools]; | ||
copy[index] = getTool.data; | ||
return copy; | ||
}, | ||
{ revalidate: false } | ||
); | ||
} | ||
}, [getTool.data, updateTools, toolId]); | ||
|
||
return { | ||
startPolling: () => setIsPolling(true), | ||
isPolling, | ||
}; | ||
} |
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
Oops, something went wrong.