import React, { useState, useEffect, useMemo } from "react"; import Swal from "sweetalert2"; import { useUpload } from "../../context/UploadContext"; import ViewToggle from "../shared/ViewToggle"; import FilterBar from "../shared/FilterBar"; import SelectionToolbar from "../shared/SelectionToolbar"; import ImagePreviewModal from "../shared/ImagePreviewModal"; import { useGenerator } from "../../context/GeneratorContext"; import OpenAIGenerator from "../features/openai/OpenAIGenerator"; import GeminiGenerator from "../features/gemini/GeminiGenerator"; import PexelsGallery from "../features/pexels/PexelsGallery"; import PixabayGallery from "../features/pixabay/PixabayGallery"; import UnsplashGallery from "../features/unsplash/UnsplashGallery"; import OpenverseGallery from "../features/openverse/OpenverseGallery"; import WikimediaGallery from "../features/wikimedia/WikimediaGallery"; import GiphyGallery from "../features/giphy/GiphyGallery"; import FavoritesGallery from "../features/favorites/FavoritesGallery"; import GlobalGallery from "../features/global/GlobalGallery"; const PhotoIcon = ({ className = "w-5 h-5" }) => ( ); const ImageIcon = ({ className = "w-5 h-5" }) => ( ); const CameraIcon = ({ className = "w-5 h-5" }) => ( ); const GlobeIcon = ({ className = "w-5 h-5" }) => ( ); const GifIcon = ({ className = "w-5 h-5" }) => ( ); const SparklesIcon = ({ className = "w-5 h-5" }) => ( ); const StarsIcon = ({ className = "w-5 h-5" }) => ( ); const HeartIconSmall = ({ className = "w-5 h-5" }) => ( ); const SearchIconTab = ({ className = "w-5 h-5" }) => ( ); const PROVIDER_META = { global: { icon: SearchIconTab, color: "indigo", description: "Search across all enabled providers at once" }, favorites: { icon: HeartIconSmall, color: "pink", description: "Your saved favorites" }, pexels: { icon: PhotoIcon, color: "blue", description: "High-quality stock photos" }, pixabay: { icon: ImageIcon, color: "green", description: "Free images and videos" }, unsplash: { icon: CameraIcon, color: "purple", description: "Beautiful free images" }, openverse: { icon: GlobeIcon, color: "orange", description: "Open-licensed content" }, wikimedia: { icon: GlobeIcon, color: "cyan", description: "Wikimedia Commons images", badge: "NEW" }, giphy: { icon: GifIcon, color: "pink", description: "Animated GIFs", badge: "NEW" }, openai: { icon: SparklesIcon, color: "indigo", description: "AI-generated images" }, gemini: { icon: StarsIcon, color: "blue", description: "Google Gemini AI images", badge: "NEW" }, }; const GLOBAL_TAB = { id: "global", label: "Global Search", component: GlobalGallery }; const FAVORITES_TAB = { id: "favorites", label: "Favorites", component: FavoritesGallery }; const TABS_BY_ID = { pexels: { id: "pexels", label: "Pexels", component: PexelsGallery }, pixabay: { id: "pixabay", label: "Pixabay", component: PixabayGallery }, unsplash: { id: "unsplash", label: "Unsplash", component: UnsplashGallery }, openverse: { id: "openverse", label: "Openverse", component: OpenverseGallery }, wikimedia: { id: "wikimedia", label: "Wikimedia", component: WikimediaGallery, badge: "NEW" }, giphy: { id: "giphy", label: "Giphy", component: GiphyGallery, badge: "NEW" }, openai: { id: "openai", label: "OpenAI", component: OpenAIGenerator }, gemini: { id: "gemini", label: "Gemini", component: GeminiGenerator, badge: "NEW" }, }; const DEFAULT_ORDER = ["pexels", "pixabay", "openverse", "wikimedia", "unsplash", "giphy", "openai", "gemini"]; function getEnabledTabs() { const config = typeof AI_IMAGE_AdminConfig !== "undefined" ? AI_IMAGE_AdminConfig : {}; const enabled = config.provider_enabled || {}; const order = Array.isArray(config.provider_order) && config.provider_order.length > 0 ? config.provider_order : DEFAULT_ORDER; // Ensure all known provider IDs are in the order list const fullOrder = [...order]; DEFAULT_ORDER.forEach((id) => { if (!fullOrder.includes(id)) fullOrder.push(id); }); const providerTabs = fullOrder .filter((id) => TABS_BY_ID[id] && enabled[id] !== false) .map((id) => TABS_BY_ID[id]); // Global Search tab is conditionally shown based on setting (default: enabled) const globalEnabled = enabled.global !== false; const tabs = []; if (globalEnabled) tabs.push(GLOBAL_TAB); tabs.push(FAVORITES_TAB); tabs.push(...providerTabs); return tabs; } function getDefaultTabId() { const config = typeof AI_IMAGE_AdminConfig !== "undefined" ? AI_IMAGE_AdminConfig : {}; const TABS = getEnabledTabs(); const defaultId = config.general_settings?.default_provider || "global"; const exists = TABS.some((t) => t.id === defaultId); return exists ? defaultId : TABS[0]?.id || "global"; } export default function Dashboard() { // Force re-computation of tabs when settings are updated const [tabVersion, setTabVersion] = useState(0); const TABS = useMemo(() => getEnabledTabs(), [tabVersion]); // Listen for settings updates to re-compute tab order and visibility useEffect(() => { const handleSettingsUpdate = (event) => { const detail = event.detail; if (typeof AI_IMAGE_AdminConfig !== "undefined") { if (detail?.provider_order) { AI_IMAGE_AdminConfig.provider_order = detail.provider_order; } if (detail?.provider_enabled) { AI_IMAGE_AdminConfig.provider_enabled = { ...detail.provider_enabled }; } if (detail?.default_provider) { AI_IMAGE_AdminConfig.general_settings = AI_IMAGE_AdminConfig.general_settings || {}; AI_IMAGE_AdminConfig.general_settings.default_provider = detail.default_provider; } } setTabVersion((v) => v + 1); // Update active tab if default provider changed if (detail?.default_provider) { setActiveTab(detail.default_provider); } }; window.addEventListener("aiImageSettingsUpdated", handleSettingsUpdate); return () => window.removeEventListener("aiImageSettingsUpdated", handleSettingsUpdate); }, []); // Get initial tab from settings default provider const getInitialTab = () => { return getDefaultTabId(); }; const [activeTab, setActiveTab] = useState(getInitialTab); const { uploadImage } = useUpload(); const { selectionMode, setSelectionMode, clearSelection, isGenerating } = useGenerator(); const currentTabValid = TABS.some((t) => t.id === activeTab); const effectiveTab = currentTabValid ? activeTab : TABS[0]?.id; const effectiveTabs = TABS; // Clear selection when changing tabs useEffect(() => { clearSelection(); }, [effectiveTab, clearSelection]); useEffect(() => { const handleGlobalClick = (event) => { const btn = event.target.closest(".bdt-aimg-download-btn"); if (btn) { const imageUrl = btn.getAttribute("data-url"); const imageTitle = btn.getAttribute("data-title") || ""; const imageAuthor = btn.getAttribute("data-author") || ""; if (imageUrl) { uploadImage(imageUrl, imageTitle, imageAuthor); } } }; document.addEventListener("click", handleGlobalClick); if (!Swal.showToast) { Swal.showToast = Swal.mixin({ customClass: { container: 'ai-image-swal' }, toast: true, position: "top-end", showConfirmButton: false, timer: 3000, timerProgressBar: true, }); } return () => document.removeEventListener("click", handleGlobalClick); }, [uploadImage]); const ActiveComponent = effectiveTabs.find((t) => t.id === effectiveTab)?.component; if (effectiveTabs.length === 0) { return (

No image sources enabled. Enable at least one source in{" "} Dashboard → Image sources .

); } const activeMeta = PROVIDER_META[effectiveTab] || {}; return (
{/* Header card */}
{/* Branding row */}
Instant Image Generator v{typeof AI_IMAGE_AdminConfig !== "undefined" ? AI_IMAGE_AdminConfig.version : "1.5.11"}
{/* Tabs */}
{effectiveTabs.map((tab) => { const meta = PROVIDER_META[tab.id] || {}; const Icon = meta.icon; const isActive = effectiveTab === tab.id; return ( ); })}
{/* Controls */}
{effectiveTab !== "gemini" && effectiveTab !== "openai" && (
{selectionMode && ( )}
)}
{effectiveTab !== "gemini" && effectiveTab !== "openai" && ( )} {/* {activeMeta.description && (

{activeMeta.description}

)} */}
{ActiveComponent && }
); }