2025-08-08 01:05:28 +01:00
|
|
|
import React, { useState, useRef, useEffect, useMemo } from "react";
|
2025-08-14 16:14:57 +01:00
|
|
|
import { Stack, Button, Text } from "@mantine/core";
|
2025-08-08 01:05:28 +01:00
|
|
|
import { useTranslation } from "react-i18next";
|
2025-08-18 18:16:14 +01:00
|
|
|
import { ToolRegistryEntry } from "../../../data/toolsTaxonomy";
|
2025-08-14 16:14:57 +01:00
|
|
|
import { TextInput } from "../../shared/TextInput";
|
2025-08-14 15:40:02 +01:00
|
|
|
import './ToolPicker.css';
|
2025-08-08 01:05:28 +01:00
|
|
|
|
|
|
|
interface ToolSearchProps {
|
|
|
|
value: string;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
toolRegistry: Readonly<Record<string, ToolRegistryEntry>>;
|
|
|
|
onToolSelect?: (toolId: string) => void;
|
|
|
|
mode: 'filter' | 'dropdown';
|
|
|
|
selectedToolKey?: string | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ToolSearch = ({
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
toolRegistry,
|
|
|
|
onToolSelect,
|
|
|
|
mode = 'filter',
|
|
|
|
selectedToolKey
|
|
|
|
}: ToolSearchProps) => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const [dropdownOpen, setDropdownOpen] = useState(false);
|
|
|
|
const searchRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
const filteredTools = useMemo(() => {
|
|
|
|
if (!value.trim()) return [];
|
|
|
|
return Object.entries(toolRegistry)
|
|
|
|
.filter(([id, tool]) => {
|
|
|
|
if (mode === 'dropdown' && id === selectedToolKey) return false;
|
|
|
|
return tool.name.toLowerCase().includes(value.toLowerCase()) ||
|
|
|
|
tool.description.toLowerCase().includes(value.toLowerCase());
|
|
|
|
})
|
|
|
|
.slice(0, 6)
|
|
|
|
.map(([id, tool]) => ({ id, tool }));
|
|
|
|
}, [value, toolRegistry, mode, selectedToolKey]);
|
|
|
|
|
|
|
|
const handleSearchChange = (searchValue: string) => {
|
|
|
|
onChange(searchValue);
|
|
|
|
if (mode === 'dropdown') {
|
|
|
|
setDropdownOpen(searchValue.trim().length > 0 && filteredTools.length > 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleClickOutside = (event: MouseEvent) => {
|
|
|
|
if (searchRef.current && !searchRef.current.contains(event.target as Node)) {
|
|
|
|
setDropdownOpen(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
document.addEventListener('mousedown', handleClickOutside);
|
|
|
|
return () => document.removeEventListener('mousedown', handleClickOutside);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const searchInput = (
|
2025-08-14 15:40:02 +01:00
|
|
|
<div className="search-input-container">
|
2025-08-14 16:14:57 +01:00
|
|
|
<TextInput
|
2025-08-14 15:40:02 +01:00
|
|
|
ref={searchRef}
|
|
|
|
value={value}
|
2025-08-14 16:14:57 +01:00
|
|
|
onChange={handleSearchChange}
|
|
|
|
placeholder={t("toolPicker.searchPlaceholder", "Search tools...")}
|
|
|
|
icon={<span className="material-symbols-rounded">search</span>}
|
2025-08-14 15:40:02 +01:00
|
|
|
autoComplete="off"
|
|
|
|
/>
|
|
|
|
</div>
|
2025-08-08 01:05:28 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (mode === 'filter') {
|
|
|
|
return searchInput;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={searchRef} style={{ position: 'relative' }}>
|
|
|
|
{searchInput}
|
|
|
|
{dropdownOpen && filteredTools.length > 0 && (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
top: '100%',
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
zIndex: 1000,
|
|
|
|
backgroundColor: 'var(--bg-toolbar)',
|
|
|
|
border: '1px solid var(--border-default)',
|
|
|
|
borderRadius: '8px',
|
|
|
|
marginTop: '4px',
|
|
|
|
maxHeight: '300px',
|
|
|
|
overflowY: 'auto',
|
|
|
|
boxShadow: '0 4px 12px rgba(0, 0, 0, 0.15)'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Stack gap="xs" style={{ padding: '8px' }}>
|
|
|
|
{filteredTools.map(({ id, tool }) => (
|
|
|
|
<Button
|
|
|
|
key={id}
|
|
|
|
variant="subtle"
|
|
|
|
onClick={() => onToolSelect && onToolSelect(id)}
|
|
|
|
leftSection={
|
|
|
|
<div style={{ color: 'var(--tools-text-and-icon-color)' }}>
|
|
|
|
{tool.icon}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
fullWidth
|
|
|
|
justify="flex-start"
|
|
|
|
style={{
|
|
|
|
borderRadius: '6px',
|
|
|
|
color: 'var(--tools-text-and-icon-color)',
|
|
|
|
padding: '8px 12px'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div style={{ textAlign: 'left' }}>
|
|
|
|
<div style={{ fontWeight: 500 }}>{tool.name}</div>
|
|
|
|
<Text size="xs" c="dimmed" style={{ marginTop: '2px' }}>
|
|
|
|
{tool.description}
|
|
|
|
</Text>
|
|
|
|
</div>
|
|
|
|
</Button>
|
|
|
|
))}
|
|
|
|
</Stack>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ToolSearch;
|