-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add agent and workflow dropdowns to workflow steps (#645)
Signed-off-by: Ryan Hopper-Lowe <[email protected]>
- Loading branch information
1 parent
8af3b96
commit 794e6e2
Showing
5 changed files
with
314 additions
and
20 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,58 @@ | ||
import useSWR from "swr"; | ||
|
||
import { Agent } from "~/lib/model/agents"; | ||
import { AgentService } from "~/lib/service/api/agentService"; | ||
|
||
import { SelectModule } from "~/components/composed/SelectModule"; | ||
import { | ||
Tooltip, | ||
TooltipContent, | ||
TooltipTrigger, | ||
} from "~/components/ui/tooltip"; | ||
|
||
type AgentSelectModuleProps = { | ||
onChange: (agents: string[]) => void; | ||
selection: string[]; | ||
}; | ||
|
||
export function AgentSelectModule(props: AgentSelectModuleProps) { | ||
const { data: agents } = useSWR( | ||
AgentService.getAgents.key(), | ||
AgentService.getAgents | ||
); | ||
|
||
return ( | ||
<SelectModule | ||
selection={props.selection} | ||
onChange={props.onChange} | ||
renderDropdownItem={(agent) => <AgentText agent={agent} />} | ||
renderListItem={(agent) => <AgentText agent={agent} />} | ||
getItemKey={(agent) => agent.id} | ||
buttonText="Add Agent" | ||
items={agents} | ||
/> | ||
); | ||
} | ||
|
||
function AgentText({ agent }: { agent: Agent }) { | ||
const content = ( | ||
<div className="flex items-center gap-2 overflow-hidden"> | ||
<span className="min-w-fit">{agent.name}</span> | ||
{agent.description && ( | ||
<> | ||
<span>-</span> | ||
<span className="text-muted-foreground truncate"> | ||
{agent.description} | ||
</span> | ||
</> | ||
)} | ||
</div> | ||
); | ||
|
||
return ( | ||
<Tooltip> | ||
<TooltipTrigger asChild>{content}</TooltipTrigger> | ||
<TooltipContent>{content}</TooltipContent> | ||
</Tooltip> | ||
); | ||
} |
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,185 @@ | ||
import { PlusIcon, TrashIcon } from "lucide-react"; | ||
import { useMemo } from "react"; | ||
|
||
import { Button } from "~/components/ui/button"; | ||
import { | ||
Command, | ||
CommandEmpty, | ||
CommandInput, | ||
CommandItem, | ||
CommandList, | ||
} from "~/components/ui/command"; | ||
import { | ||
Popover, | ||
PopoverContent, | ||
PopoverTrigger, | ||
} from "~/components/ui/popover"; | ||
|
||
interface SelectModuleProps<T> { | ||
items?: T[]; | ||
selection: string[]; | ||
onChange: (selection: string[]) => void; | ||
renderDropdownItem: (item: T) => React.ReactNode; | ||
renderListItem: (item: T) => React.ReactNode; | ||
buttonText?: string; | ||
searchPlaceholder?: string; | ||
emptyMessage?: string; | ||
getItemKey: (item: T) => string; | ||
} | ||
|
||
export function SelectModule<T>({ | ||
items = [], | ||
selection, | ||
onChange, | ||
renderDropdownItem, | ||
renderListItem, | ||
buttonText, | ||
searchPlaceholder, | ||
emptyMessage, | ||
getItemKey, | ||
}: SelectModuleProps<T>) { | ||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<SelectList | ||
selected={selection} | ||
items={items} | ||
onRemove={(id) => onChange(selection.filter((s) => s !== id))} | ||
renderItem={renderListItem} | ||
getItemKey={getItemKey} | ||
/> | ||
|
||
<SelectPopover | ||
className="self-end" | ||
items={items} | ||
onSelect={(item) => onChange([...selection, getItemKey(item)])} | ||
filter={(item) => !selection.includes(getItemKey(item))} | ||
renderItem={renderDropdownItem} | ||
buttonText={buttonText} | ||
searchPlaceholder={searchPlaceholder} | ||
emptyMessage={emptyMessage} | ||
getItemKey={getItemKey} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
interface SelectProps<T> { | ||
items?: T[]; | ||
onSelect: (item: T) => void; | ||
filter?: (item: T, index: number, array: T[]) => boolean; | ||
renderItem: (item: T) => React.ReactNode; | ||
searchPlaceholder?: string; | ||
emptyMessage?: string; | ||
getItemKey: (item: T) => string; | ||
} | ||
|
||
export function Select<T>({ | ||
items = [], | ||
onSelect, | ||
filter, | ||
renderItem, | ||
searchPlaceholder = "Search...", | ||
emptyMessage = "No items to select", | ||
getItemKey, | ||
}: SelectProps<T>) { | ||
const filteredItems = filter ? items.filter(filter) : items; | ||
|
||
return ( | ||
<Command> | ||
<CommandInput placeholder={searchPlaceholder} /> | ||
<CommandList> | ||
{filteredItems?.length ? ( | ||
filteredItems?.map((item) => ( | ||
<CommandItem | ||
key={getItemKey(item)} | ||
value={getItemKey(item)} | ||
onSelect={() => onSelect(item)} | ||
> | ||
{renderItem(item)} | ||
</CommandItem> | ||
)) | ||
) : ( | ||
<CommandEmpty>{emptyMessage}</CommandEmpty> | ||
)} | ||
</CommandList> | ||
</Command> | ||
); | ||
} | ||
|
||
interface SelectPopoverProps<T> extends SelectProps<T> { | ||
className?: string; | ||
buttonText?: string; | ||
} | ||
|
||
export function SelectPopover<T>({ | ||
className, | ||
buttonText = "Select Item", | ||
...props | ||
}: SelectPopoverProps<T>) { | ||
return ( | ||
<Popover> | ||
<PopoverTrigger asChild> | ||
<Button | ||
variant="secondary" | ||
startContent={<PlusIcon />} | ||
className={className} | ||
> | ||
{buttonText} | ||
</Button> | ||
</PopoverTrigger> | ||
|
||
<PopoverContent className="p-0" align="end"> | ||
<Select {...props} /> | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
} | ||
|
||
interface SelectListProps<T> { | ||
selected: string[]; | ||
items?: T[]; | ||
onRemove: (id: string) => void; | ||
renderItem: (item: T) => React.ReactNode; | ||
fallbackRender?: (id: string) => React.ReactNode; | ||
getItemKey: (item: T) => string; | ||
} | ||
|
||
export function SelectList<T>({ | ||
selected, | ||
items = [], | ||
onRemove, | ||
renderItem, | ||
fallbackRender = (id) => id, | ||
getItemKey, | ||
}: SelectListProps<T>) { | ||
const itemMap = useMemo(() => { | ||
return items.reduce( | ||
(acc, item) => { | ||
acc[getItemKey(item)] = item; | ||
return acc; | ||
}, | ||
{} as Record<string, T> | ||
); | ||
}, [items, getItemKey]); | ||
|
||
return ( | ||
<div className="flex flex-col gap-2 divide-y"> | ||
{selected.map((id) => ( | ||
<div | ||
key={id} | ||
className="flex items-center justify-between gap-2 pt-2" | ||
> | ||
{itemMap[id] ? renderItem(itemMap[id]) : fallbackRender(id)} | ||
|
||
<Button | ||
variant="ghost" | ||
size="icon" | ||
onClick={() => onRemove(id)} | ||
> | ||
<TrashIcon /> | ||
</Button> | ||
</div> | ||
))} | ||
</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,60 @@ | ||
import useSWR from "swr"; | ||
|
||
import { Workflow } from "~/lib/model/workflows"; | ||
import { WorkflowService } from "~/lib/service/api/workflowService"; | ||
|
||
import { SelectModule } from "~/components/composed/SelectModule"; | ||
import { | ||
Tooltip, | ||
TooltipContent, | ||
TooltipTrigger, | ||
} from "~/components/ui/tooltip"; | ||
|
||
type WorkflowSelectModuleProps = { | ||
onChange: (workflows: string[]) => void; | ||
selection: string[]; | ||
}; | ||
|
||
export function WorkflowSelectModule(props: WorkflowSelectModuleProps) { | ||
const { data: workflows } = useSWR( | ||
WorkflowService.getWorkflows.key(), | ||
WorkflowService.getWorkflows | ||
); | ||
|
||
return ( | ||
<SelectModule | ||
selection={props.selection} | ||
onChange={props.onChange} | ||
getItemKey={(workflow) => workflow.id} | ||
renderDropdownItem={(workflow) => ( | ||
<WorkflowText workflow={workflow} /> | ||
)} | ||
renderListItem={(workflow) => <WorkflowText workflow={workflow} />} | ||
buttonText="Add Workflow" | ||
items={workflows} | ||
/> | ||
); | ||
} | ||
|
||
function WorkflowText({ workflow }: { workflow: Workflow }) { | ||
const content = ( | ||
<div className="flex items-center gap-2 overflow-hidden"> | ||
<span className="min-w-fit">{workflow.name}</span> | ||
{workflow.description && ( | ||
<> | ||
<span>-</span> | ||
<span className="text-muted-foreground truncate"> | ||
{workflow.description} | ||
</span> | ||
</> | ||
)} | ||
</div> | ||
); | ||
|
||
return ( | ||
<Tooltip> | ||
<TooltipTrigger asChild>{content}</TooltipTrigger> | ||
<TooltipContent>{content}</TooltipContent> | ||
</Tooltip> | ||
); | ||
} |
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