import { useEffect, useState } from "react"; import { MdCheck } from "react-icons/md"; import { useScopedT } from "@/contexts/I18nContext"; import { Button } from "../ui/button"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "../ui/tabs"; import styles from "./SourceSelector.module.css"; interface DesktopSource { id: string; name: string; thumbnail: string | null; display_id: string; appIcon: string | null; } export function SourceSelector() { const t = useScopedT("launch"); const tc = useScopedT("common"); const [sources, setSources] = useState([]); const [selectedSource, setSelectedSource] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { async function fetchSources() { setLoading(true); try { const rawSources = await window.electronAPI.getSources({ types: ["screen", "window"], thumbnailSize: { width: 320, height: 180 }, fetchWindowIcons: true, }); setSources( rawSources.map((source) => ({ id: source.id, name: source.id.startsWith("window:") && source.name.includes(" — ") ? source.name.split(" — ")[1] || source.name : source.name, thumbnail: source.thumbnail, display_id: source.display_id, appIcon: source.appIcon, })), ); } catch (error) { console.error("Error loading sources:", error); } finally { setLoading(false); } } fetchSources(); }, []); const screenSources = sources.filter((s) => s.id.startsWith("screen:")); const windowSources = sources.filter((s) => s.id.startsWith("window:")); const handleSourceSelect = (source: DesktopSource) => setSelectedSource(source); const handleShare = async () => { if (selectedSource) await window.electronAPI.selectSource(selectedSource); }; if (loading) { return (

{t("sourceSelector.loading")}

); } const renderSourceCard = (source: DesktopSource) => { const isSelected = selectedSource?.id === source.id; return (
handleSourceSelect(source)} >
{source.name} {isSelected && (
)}
{source.appIcon && ( )}
{source.name}
); }; return (
{t("sourceSelector.screens", { count: String(screenSources.length) })} {t("sourceSelector.windows", { count: String(windowSources.length) })}
{screenSources.map(renderSourceCard)}
{windowSources.map(renderSourceCard)}
); }