import { useState } from "react"; import { SearchCode } from "lucide-react"; import { useTranslation } from "react-i18next"; import { registerWidget } from "./WidgetRegistry"; import type { SearchLinksConfig, SearchLinkShortcut, WidgetComponentProps, } from "@/types/homepage-types"; import { GRID_SIZE } from "@/types/homepage-types"; import { WidgetTitle } from "./WidgetTitle"; function SearchLinksWidget({ widget, config, isReadOnly, }: WidgetComponentProps) { const { t } = useTranslation(); const { shortcuts } = config; const [activeIdx, setActiveIdx] = useState(null); const [query, setQuery] = useState(""); const handleOpen = (shortcut: SearchLinkShortcut) => { if (!query.trim()) return; const url = shortcut.queryTemplate.replace( "{q}", encodeURIComponent(query.trim()), ); window.open(url, "_blank", "noopener,noreferrer"); setQuery(""); setActiveIdx(null); }; const handleButtonClick = (i: number) => { if (isReadOnly) return; if (activeIdx === i) { setActiveIdx(null); setQuery(""); } else { setActiveIdx(i); setQuery(""); } }; if (shortcuts.length === 0) { return (
{t("homepage.noSearchShortcuts")}
); } return (
} />
{shortcuts.map((s, i) => { const accent = s.accentColor || getComputedStyle(document.documentElement) .getPropertyValue("--accent-brand") .trim() || "#f59145"; const isActive = activeIdx === i; return ( ); })}
{activeIdx !== null && (
{ e.preventDefault(); handleOpen(shortcuts[activeIdx]); }} className="flex items-center gap-1.5 border border-border/60 px-2 py-1" onMouseDown={(e) => e.stopPropagation()} > setQuery(e.target.value)} placeholder={t("homepage.searchQueryPlaceholder")} className="flex-1 bg-transparent text-[10px] text-foreground placeholder:text-muted-foreground/60 outline-none border-none" />
)}
); } registerWidget({ id: "search_links", name: "Search Shortcuts", description: "Quick buttons to search different engines", category: "links", icon: , defaultConfig: { shortcuts: [] }, defaultSize: { w: GRID_SIZE * 10, h: GRID_SIZE * 7 }, minSize: { w: GRID_SIZE * 4, h: GRID_SIZE * 3 }, component: SearchLinksWidget, }); export { SearchLinksWidget };