|
| 1 | +import { QSP } from "@/config/qsp"; |
| 2 | +import { useAuth } from "@/entities/authentication/ui/useAuth"; |
| 3 | +import { useRunGeneratorMutation } from "@/entities/generators/domain/run-generator.mutation"; |
| 4 | +import { getNodeLabel } from "@/entities/nodes/object/utils/get-node-label"; |
| 5 | +import { RelationshipNode } from "@/entities/nodes/relationships/domain/types"; |
| 6 | +import { RelationshipComboboxList } from "@/entities/nodes/relationships/ui/relationship-combobox-list"; |
| 7 | +import { constructPath } from "@/shared/api/rest/fetch"; |
| 8 | +import { Menu, MenuItem } from "@/shared/components/aria/menu"; |
| 9 | +import { Button } from "@/shared/components/buttons/button-primitive"; |
| 10 | +import { ALERT_TYPES, Alert } from "@/shared/components/ui/alert"; |
| 11 | +import { Badge } from "@/shared/components/ui/badge"; |
| 12 | +import { Popover, PopoverContent, PopoverTrigger } from "@/shared/components/ui/popover"; |
| 13 | +import { focusVisibleStyle } from "@/shared/components/ui/style"; |
| 14 | +import { classNames } from "@/shared/utils/common"; |
| 15 | +import { PlayIcon } from "lucide-react"; |
| 16 | +import { useState } from "react"; |
| 17 | +import { Text } from "react-aria-components"; |
| 18 | +import { Link } from "react-router"; |
| 19 | +import { toast } from "react-toastify"; |
| 20 | + |
| 21 | +export interface RunGeneratorActionProps { |
| 22 | + generatorId: string; |
| 23 | + groupId: string; |
| 24 | +} |
| 25 | + |
| 26 | +export function GeneratorDefinitionRunButton({ generatorId, groupId }: RunGeneratorActionProps) { |
| 27 | + const [isPopoverOpen, setIsPopoverOpen] = useState(false); |
| 28 | + const [showTargetForm, setShowTargetForm] = useState(false); |
| 29 | + const { isPending, mutate } = useRunGeneratorMutation(); |
| 30 | + const { isAuthenticated } = useAuth(); |
| 31 | + |
| 32 | + const handlePopoverOpenChange = (open: boolean) => { |
| 33 | + setIsPopoverOpen(open); |
| 34 | + setShowTargetForm(false); |
| 35 | + }; |
| 36 | + |
| 37 | + const handleRunGenerator = (targetNodeIds?: string[]) => { |
| 38 | + mutate( |
| 39 | + { generatorId, targetNodeIds }, |
| 40 | + { |
| 41 | + onSuccess: ({ taskId }) => { |
| 42 | + const url = constructPath(window.location.pathname, [ |
| 43 | + { name: QSP.TAB, value: "tasks" }, |
| 44 | + { name: QSP.TASK_ID, value: taskId }, |
| 45 | + ]); |
| 46 | + |
| 47 | + toast( |
| 48 | + <Alert |
| 49 | + type={ALERT_TYPES.SUCCESS} |
| 50 | + message={ |
| 51 | + <> |
| 52 | + Generator started successfully. |
| 53 | + <br /> |
| 54 | + <Link to={url} className="underline flex items-center gap-1"> |
| 55 | + View task details |
| 56 | + </Link> |
| 57 | + </> |
| 58 | + } |
| 59 | + /> |
| 60 | + ); |
| 61 | + }, |
| 62 | + } |
| 63 | + ); |
| 64 | + setIsPopoverOpen(false); |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <Popover open={isPopoverOpen} onOpenChange={handlePopoverOpenChange}> |
| 69 | + <PopoverTrigger asChild> |
| 70 | + <Button variant="active" isLoading={isPending} disabled={isPending || !isAuthenticated}> |
| 71 | + {!isPending && <PlayIcon className="size-4 mr-2" />} |
| 72 | + Run |
| 73 | + </Button> |
| 74 | + </PopoverTrigger> |
| 75 | + |
| 76 | + <PopoverContent className="p-1 min-w-[200px] max-w-sm" align="end"> |
| 77 | + {showTargetForm ? ( |
| 78 | + <GeneratorTargetSelectionForm |
| 79 | + generatorId={generatorId} |
| 80 | + groupId={groupId} |
| 81 | + onSubmit={handleRunGenerator} |
| 82 | + onCancel={() => setShowTargetForm(false)} |
| 83 | + /> |
| 84 | + ) : ( |
| 85 | + <Menu aria-label="Run generator options"> |
| 86 | + <MenuItem onAction={() => handleRunGenerator()} className="flex-col gap-0 items-start"> |
| 87 | + <Text slot="label" className="font-semibold"> |
| 88 | + All targets |
| 89 | + </Text> |
| 90 | + <Text slot="description" className="text-gray-600 text-xs"> |
| 91 | + Generate for all members in the target group |
| 92 | + </Text> |
| 93 | + </MenuItem> |
| 94 | + <MenuItem |
| 95 | + onAction={() => setShowTargetForm(true)} |
| 96 | + className="flex-col gap-0 items-start" |
| 97 | + > |
| 98 | + <Text slot="label" className="font-semibold"> |
| 99 | + Selected targets |
| 100 | + </Text> |
| 101 | + <Text slot="description" className="text-gray-600 text-xs"> |
| 102 | + Choose specific members of target group |
| 103 | + </Text> |
| 104 | + </MenuItem> |
| 105 | + </Menu> |
| 106 | + )} |
| 107 | + </PopoverContent> |
| 108 | + </Popover> |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +interface GeneratorTargetSelectionFormProps extends RunGeneratorActionProps { |
| 113 | + onSubmit: (targetNodeIds: string[]) => void; |
| 114 | + onCancel?: () => void; |
| 115 | +} |
| 116 | + |
| 117 | +export function GeneratorTargetSelectionForm({ |
| 118 | + groupId, |
| 119 | + onSubmit, |
| 120 | + onCancel, |
| 121 | +}: GeneratorTargetSelectionFormProps) { |
| 122 | + const [selectedTargetNodes, setSelectedTargetNodes] = useState<RelationshipNode[]>([]); |
| 123 | + |
| 124 | + const handleRemoveTarget = (nodeId: string) => { |
| 125 | + setSelectedTargetNodes((prev) => prev.filter((node) => node.id !== nodeId)); |
| 126 | + }; |
| 127 | + |
| 128 | + const handleSelect = (selectedRelationship: RelationshipNode) => { |
| 129 | + setSelectedTargetNodes((prev) => [...prev, selectedRelationship]); |
| 130 | + }; |
| 131 | + |
| 132 | + const handleSubmit = () => { |
| 133 | + onSubmit(selectedTargetNodes.map((node) => node.id)); |
| 134 | + }; |
| 135 | + |
| 136 | + return ( |
| 137 | + <div className="flex flex-col gap-1"> |
| 138 | + <div className="flex items-center justify-between mb-1"> |
| 139 | + <h3 className="text-sm font-medium">Select target nodes</h3> |
| 140 | + <Button |
| 141 | + variant="ghost" |
| 142 | + size="xs" |
| 143 | + onClick={onCancel} |
| 144 | + className="text-gray-500 hover:text-gray-700 text-xs h-5 p-1" |
| 145 | + > |
| 146 | + Back |
| 147 | + </Button> |
| 148 | + </div> |
| 149 | + |
| 150 | + <div className="rounded border p-2"> |
| 151 | + {selectedTargetNodes.length > 0 ? ( |
| 152 | + <div className="flex flex-wrap gap-1"> |
| 153 | + {selectedTargetNodes.map((node) => { |
| 154 | + const label = getNodeLabel(node); |
| 155 | + |
| 156 | + return ( |
| 157 | + <Badge key={node.id} className="flex items-center gap-1"> |
| 158 | + {label} |
| 159 | + <button |
| 160 | + type="button" |
| 161 | + onClick={() => handleRemoveTarget(node.id)} |
| 162 | + className={classNames( |
| 163 | + focusVisibleStyle, |
| 164 | + "text-xs hover:text-gray-900 border border-transparent rounded-full size-3.5 flex items-center justify-center" |
| 165 | + )} |
| 166 | + aria-label={`Remove ${label}`} |
| 167 | + > |
| 168 | + × |
| 169 | + </button> |
| 170 | + </Badge> |
| 171 | + ); |
| 172 | + })} |
| 173 | + </div> |
| 174 | + ) : ( |
| 175 | + <span className="text-gray-400">No targets selected</span> |
| 176 | + )} |
| 177 | + </div> |
| 178 | + |
| 179 | + <RelationshipComboboxList |
| 180 | + autoFocus |
| 181 | + className="rounded border" |
| 182 | + peer="CoreNode" |
| 183 | + onSelect={handleSelect} |
| 184 | + filterQuery={{ |
| 185 | + member_of_groups__ids: groupId, |
| 186 | + }} |
| 187 | + filterItem={(node) => !selectedTargetNodes.some((v) => v.id === node.id)} |
| 188 | + /> |
| 189 | + |
| 190 | + <Button disabled={selectedTargetNodes.length === 0} variant="active" onClick={handleSubmit}> |
| 191 | + Run Generator |
| 192 | + </Button> |
| 193 | + </div> |
| 194 | + ); |
| 195 | +} |
0 commit comments