|
| 1 | +import { createPortal } from "react-dom"; |
| 2 | +import { useEffect, useState } from "react"; |
| 3 | +import { UseFormWatch } from "react-hook-form"; |
| 4 | +import { FeedFormData } from "@/types/feed"; |
| 5 | + |
| 6 | +interface FlareSolverrIndicatorProps { |
| 7 | + watch: UseFormWatch<FeedFormData>; |
| 8 | + feedType?: "webScraping" | "api" | "email"; |
| 9 | +} |
| 10 | + |
| 11 | +export const FlareSolverrIndicator = ({ |
| 12 | + watch, |
| 13 | + feedType, |
| 14 | +}: FlareSolverrIndicatorProps) => { |
| 15 | + const isEnabled = watch("flaresolverr.enabled"); |
| 16 | + const serverUrl = watch("flaresolverr.serverUrl"); |
| 17 | + const isWebScraping = feedType === "webScraping"; |
| 18 | + const [connectionStatus, setConnectionStatus] = useState<"connected" | "error" | "checking">("checking"); |
| 19 | + |
| 20 | + // Verify FlareSolverr server is active |
| 21 | + useEffect(() => { |
| 22 | + if (!isEnabled || !serverUrl || !isWebScraping) { |
| 23 | + setConnectionStatus("checking"); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + setConnectionStatus("checking"); |
| 28 | + |
| 29 | + // Debounce the server check |
| 30 | + const timeoutId = setTimeout(async () => { |
| 31 | + try { |
| 32 | + // Check via our backend proxy to avoid CORS issues |
| 33 | + const response = await fetch("/api/flaresolverr/health", { |
| 34 | + method: "POST", |
| 35 | + headers: { "Content-Type": "application/json" }, |
| 36 | + body: JSON.stringify({ serverUrl }), |
| 37 | + signal: AbortSignal.timeout(5000), |
| 38 | + }); |
| 39 | + |
| 40 | + const data = await response.json(); |
| 41 | + setConnectionStatus(data.active === true ? "connected" : "error"); |
| 42 | + } catch (error) { |
| 43 | + setConnectionStatus("error"); |
| 44 | + } |
| 45 | + }, 500); // 500ms debounce |
| 46 | + |
| 47 | + return () => clearTimeout(timeoutId); |
| 48 | + }, [isEnabled, serverUrl, isWebScraping]); |
| 49 | + |
| 50 | + // Only show for web scraping feeds when FlareSolverr is enabled |
| 51 | + if (!isWebScraping || !isEnabled) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + const handleClick = () => { |
| 56 | + // Find the FlareSolverr section |
| 57 | + const flaresolverrSection = document.getElementById("flaresolverr-section"); |
| 58 | + |
| 59 | + if (flaresolverrSection) { |
| 60 | + // First, ensure the Additional Options accordion is open |
| 61 | + const accordionTrigger = document.querySelector( |
| 62 | + '[data-accordion-trigger="additional"]' |
| 63 | + ) as HTMLButtonElement; |
| 64 | + |
| 65 | + if (accordionTrigger) { |
| 66 | + const accordionContent = accordionTrigger.getAttribute("data-state"); |
| 67 | + |
| 68 | + // If accordion is closed, open it |
| 69 | + if (accordionContent === "closed") { |
| 70 | + accordionTrigger.click(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Wait a brief moment for accordion animation, then scroll |
| 75 | + setTimeout(() => { |
| 76 | + flaresolverrSection.scrollIntoView({ |
| 77 | + behavior: "smooth", |
| 78 | + block: "center", |
| 79 | + }); |
| 80 | + |
| 81 | + // Add a brief highlight effect |
| 82 | + flaresolverrSection.classList.add("ring-2", "ring-blue-500", "ring-offset-2", "rounded-lg"); |
| 83 | + setTimeout(() => { |
| 84 | + flaresolverrSection.classList.remove("ring-2", "ring-blue-500", "ring-offset-2", "rounded-lg"); |
| 85 | + }, 2000); |
| 86 | + }, 300); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + const badge = ( |
| 91 | + <button |
| 92 | + type="button" |
| 93 | + onClick={handleClick} |
| 94 | + className="fixed bottom-6 right-6 z-[9999] flex items-center gap-2 px-3 py-2 bg-slate-700 dark:bg-slate-800 text-white rounded-full shadow-lg hover:shadow-xl hover:bg-slate-600 dark:hover:bg-slate-700 transition-all hover:scale-105 cursor-pointer" |
| 95 | + title={`FlareSolverr: ${connectionStatus === "connected" ? "Connected" : connectionStatus === "error" ? "Connection Error" : "Checking..."}`} |
| 96 | + > |
| 97 | + <img |
| 98 | + src="/public/flaresolverr.svg" |
| 99 | + alt="FlareSolverr" |
| 100 | + className="h-5 w-5" |
| 101 | + /> |
| 102 | + <span className="text-sm font-medium">FlareSolverr</span> |
| 103 | + {/* Status indicator circle */} |
| 104 | + <div className="relative flex items-center justify-center"> |
| 105 | + <div |
| 106 | + className={`h-2 w-2 rounded-full ${ |
| 107 | + connectionStatus === "connected" |
| 108 | + ? "bg-green-500" |
| 109 | + : connectionStatus === "error" |
| 110 | + ? "bg-red-500" |
| 111 | + : "bg-yellow-500" |
| 112 | + }`} |
| 113 | + /> |
| 114 | + {connectionStatus === "connected" && ( |
| 115 | + <div className="absolute h-2 w-2 rounded-full bg-green-500 animate-ping opacity-75" /> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + </button> |
| 119 | + ); |
| 120 | + |
| 121 | + // Use portal to render at document body level to ensure fixed positioning works |
| 122 | + return typeof document !== "undefined" ? createPortal(badge, document.body) : null; |
| 123 | +}; |
0 commit comments