|
| 1 | +import { useFetcher, useNavigate } from "@remix-run/react"; |
| 2 | +import { AnimatePresence, motion } from "framer-motion"; |
| 3 | +import { useEffect, useRef, useState } from "react"; |
| 4 | +import { AISparkleIcon } from "~/assets/icons/AISparkleIcon"; |
| 5 | +import { Input } from "~/components/primitives/Input"; |
| 6 | +import { Popover, PopoverContent, PopoverTrigger } from "~/components/primitives/Popover"; |
| 7 | +import { ShortcutKey } from "~/components/primitives/ShortcutKey"; |
| 8 | +import { Spinner } from "~/components/primitives/Spinner"; |
| 9 | +import { useEnvironment } from "~/hooks/useEnvironment"; |
| 10 | +import { useOrganization } from "~/hooks/useOrganizations"; |
| 11 | +import { useProject } from "~/hooks/useProject"; |
| 12 | +import { cn } from "~/utils/cn"; |
| 13 | +import { objectToSearchParams } from "~/utils/searchParams"; |
| 14 | +import { type TaskRunListSearchFilters } from "./RunFilters"; |
| 15 | + |
| 16 | +type AIFilterResult = |
| 17 | + | { |
| 18 | + success: true; |
| 19 | + filters: TaskRunListSearchFilters; |
| 20 | + explanation?: string; |
| 21 | + } |
| 22 | + | { |
| 23 | + success: false; |
| 24 | + error: string; |
| 25 | + suggestions?: string[]; |
| 26 | + }; |
| 27 | + |
| 28 | +export function AIFilterInput() { |
| 29 | + const [text, setText] = useState(""); |
| 30 | + const [isFocused, setIsFocused] = useState(false); |
| 31 | + const navigate = useNavigate(); |
| 32 | + const organization = useOrganization(); |
| 33 | + const project = useProject(); |
| 34 | + const environment = useEnvironment(); |
| 35 | + const inputRef = useRef<HTMLInputElement>(null); |
| 36 | + const fetcher = useFetcher<AIFilterResult>(); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (fetcher.data?.success && fetcher.state === "loading") { |
| 40 | + setText(""); |
| 41 | + setIsFocused(false); |
| 42 | + |
| 43 | + const searchParams = objectToSearchParams(fetcher.data.filters); |
| 44 | + if (!searchParams) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + navigate(`${location.pathname}?${searchParams.toString()}`, { replace: true }); |
| 49 | + |
| 50 | + if (inputRef.current) { |
| 51 | + inputRef.current.focus(); |
| 52 | + } |
| 53 | + } |
| 54 | + }, [fetcher.data, navigate]); |
| 55 | + |
| 56 | + const isLoading = fetcher.state === "submitting"; |
| 57 | + |
| 58 | + return ( |
| 59 | + <fetcher.Form |
| 60 | + className="flex items-center gap-2" |
| 61 | + action={`/resources/orgs/${organization.slug}/projects/${project.slug}/env/${environment.slug}/runs/ai-filter`} |
| 62 | + method="post" |
| 63 | + > |
| 64 | + <ErrorPopover error={fetcher.data?.success === false ? fetcher.data.error : undefined}> |
| 65 | + <motion.div |
| 66 | + initial={{ width: "auto" }} |
| 67 | + animate={{ width: isFocused && text.length > 0 ? "24rem" : "auto" }} |
| 68 | + transition={{ |
| 69 | + type: "spring", |
| 70 | + stiffness: 300, |
| 71 | + damping: 30, |
| 72 | + }} |
| 73 | + className="relative h-6 min-w-44" |
| 74 | + > |
| 75 | + <AnimatePresence> |
| 76 | + {isFocused && ( |
| 77 | + <motion.div |
| 78 | + initial={{ opacity: 0 }} |
| 79 | + animate={{ opacity: 1 }} |
| 80 | + exit={{ opacity: 0 }} |
| 81 | + transition={{ duration: 0.2, ease: "linear" }} |
| 82 | + className="animated-gradient-glow-small pointer-events-none absolute inset-0 h-6" |
| 83 | + /> |
| 84 | + )} |
| 85 | + </AnimatePresence> |
| 86 | + <div className="absolute inset-0 left-0 top-0 h-6"> |
| 87 | + <Input |
| 88 | + type="text" |
| 89 | + name="text" |
| 90 | + variant="secondary-small" |
| 91 | + placeholder="Describe your filters…" |
| 92 | + value={text} |
| 93 | + onChange={(e) => setText(e.target.value)} |
| 94 | + disabled={isLoading} |
| 95 | + fullWidth |
| 96 | + ref={inputRef} |
| 97 | + className={cn( |
| 98 | + "disabled:text-text-dimmed/50", |
| 99 | + isFocused && "placeholder:text-text-dimmed/70" |
| 100 | + )} |
| 101 | + containerClassName="has-[:disabled]:opacity-100" |
| 102 | + onKeyDown={(e) => { |
| 103 | + if (e.key === "Enter" && text.trim() && !isLoading) { |
| 104 | + e.preventDefault(); |
| 105 | + const form = e.currentTarget.closest("form"); |
| 106 | + if (form) { |
| 107 | + form.requestSubmit(); |
| 108 | + } |
| 109 | + } |
| 110 | + }} |
| 111 | + onFocus={() => setIsFocused(true)} |
| 112 | + onBlur={() => { |
| 113 | + if (text.length === 0 || !isLoading) { |
| 114 | + setIsFocused(false); |
| 115 | + } |
| 116 | + }} |
| 117 | + icon={<AISparkleIcon className="size-4" />} |
| 118 | + accessory={ |
| 119 | + isLoading ? ( |
| 120 | + <Spinner |
| 121 | + color={{ |
| 122 | + background: "rgba(99, 102, 241, 1)", |
| 123 | + foreground: "rgba(217, 70, 239, 1)", |
| 124 | + }} |
| 125 | + className="size-4 opacity-80" |
| 126 | + /> |
| 127 | + ) : text.length > 0 ? ( |
| 128 | + <ShortcutKey |
| 129 | + shortcut={{ key: "enter" }} |
| 130 | + variant="small" |
| 131 | + className={cn("transition-opacity", text.length === 0 && "opacity-0")} |
| 132 | + /> |
| 133 | + ) : undefined |
| 134 | + } |
| 135 | + /> |
| 136 | + </div> |
| 137 | + </motion.div> |
| 138 | + </ErrorPopover> |
| 139 | + </fetcher.Form> |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +function ErrorPopover({ |
| 144 | + children, |
| 145 | + error, |
| 146 | + durationMs = 10_000, |
| 147 | +}: { |
| 148 | + children: React.ReactNode; |
| 149 | + error?: string; |
| 150 | + durationMs?: number; |
| 151 | +}) { |
| 152 | + const [isOpen, setIsOpen] = useState(false); |
| 153 | + const timeout = useRef<NodeJS.Timeout | undefined>(); |
| 154 | + |
| 155 | + useEffect(() => { |
| 156 | + if (error) { |
| 157 | + setIsOpen(true); |
| 158 | + } |
| 159 | + if (timeout.current) { |
| 160 | + clearTimeout(timeout.current); |
| 161 | + } |
| 162 | + timeout.current = setTimeout(() => { |
| 163 | + setIsOpen(false); |
| 164 | + }, durationMs); |
| 165 | + |
| 166 | + return () => { |
| 167 | + if (timeout.current) { |
| 168 | + clearTimeout(timeout.current); |
| 169 | + } |
| 170 | + }; |
| 171 | + }, [error, durationMs]); |
| 172 | + |
| 173 | + return ( |
| 174 | + <Popover open={isOpen}> |
| 175 | + <PopoverTrigger asChild>{children}</PopoverTrigger> |
| 176 | + <PopoverContent |
| 177 | + align="start" |
| 178 | + side="bottom" |
| 179 | + className="w-[var(--radix-popover-trigger-width)] min-w-[var(--radix-popover-trigger-width)] max-w-[var(--radix-popover-trigger-width)] border border-error/20 bg-[#2F1D24] px-3 py-2 text-xs text-text-dimmed" |
| 180 | + > |
| 181 | + {error} |
| 182 | + </PopoverContent> |
| 183 | + </Popover> |
| 184 | + ); |
| 185 | +} |
0 commit comments