2025-08-19 13:31:09 +01:00
|
|
|
import React, { useMemo, useRef, useLayoutEffect, useState } from "react";
|
2025-08-22 14:40:27 +01:00
|
|
|
import { Box, Stack } from "@mantine/core";
|
2025-05-29 17:26:32 +01:00
|
|
|
import { useTranslation } from "react-i18next";
|
2025-08-22 14:40:27 +01:00
|
|
|
import { ToolRegistryEntry } from "../../data/toolsTaxonomy";
|
2025-08-19 13:31:09 +01:00
|
|
|
import "./toolPicker/ToolPicker.css";
|
2025-08-22 14:40:27 +01:00
|
|
|
import { useToolSections } from "../../hooks/useToolSections";
|
2025-08-19 13:31:09 +01:00
|
|
|
import NoToolsFound from "./shared/NoToolsFound";
|
2025-08-22 14:40:27 +01:00
|
|
|
import { renderToolButtons } from "./shared/renderToolButtons";
|
2025-05-21 21:47:44 +01:00
|
|
|
|
|
|
|
interface ToolPickerProps {
|
2025-07-16 17:53:50 +01:00
|
|
|
selectedToolKey: string | null;
|
2025-05-21 21:47:44 +01:00
|
|
|
onSelect: (id: string) => void;
|
2025-08-19 13:31:09 +01:00
|
|
|
filteredTools: [string, ToolRegistryEntry][];
|
|
|
|
isSearching?: boolean;
|
2025-05-21 21:47:44 +01:00
|
|
|
}
|
|
|
|
|
2025-08-19 13:31:09 +01:00
|
|
|
const ToolPicker = ({ selectedToolKey, onSelect, filteredTools, isSearching = false }: ToolPickerProps) => {
|
2025-05-29 17:26:32 +01:00
|
|
|
const { t } = useTranslation();
|
2025-08-19 13:31:09 +01:00
|
|
|
const [quickHeaderHeight, setQuickHeaderHeight] = useState(0);
|
|
|
|
const [allHeaderHeight, setAllHeaderHeight] = useState(0);
|
|
|
|
|
|
|
|
const scrollableRef = useRef<HTMLDivElement>(null);
|
|
|
|
const quickHeaderRef = useRef<HTMLDivElement>(null);
|
|
|
|
const allHeaderRef = useRef<HTMLDivElement>(null);
|
|
|
|
const quickAccessRef = useRef<HTMLDivElement>(null);
|
|
|
|
const allToolsRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
2025-08-25 15:46:44 +01:00
|
|
|
// Keep header heights in sync with any dynamic size changes
|
2025-08-19 13:31:09 +01:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
const update = () => {
|
|
|
|
if (quickHeaderRef.current) {
|
2025-08-25 15:46:44 +01:00
|
|
|
setQuickHeaderHeight(quickHeaderRef.current.offsetHeight || 0);
|
2025-08-19 13:31:09 +01:00
|
|
|
}
|
|
|
|
if (allHeaderRef.current) {
|
2025-08-25 15:46:44 +01:00
|
|
|
setAllHeaderHeight(allHeaderRef.current.offsetHeight || 0);
|
2025-08-19 13:31:09 +01:00
|
|
|
}
|
|
|
|
};
|
2025-08-25 15:46:44 +01:00
|
|
|
|
2025-08-19 13:31:09 +01:00
|
|
|
update();
|
2025-08-25 15:46:44 +01:00
|
|
|
|
|
|
|
// Update on window resize
|
2025-08-19 13:31:09 +01:00
|
|
|
window.addEventListener("resize", update);
|
2025-08-25 15:46:44 +01:00
|
|
|
|
|
|
|
// Update on element resize (e.g., font load, badge count change, zoom)
|
|
|
|
const observers: ResizeObserver[] = [];
|
|
|
|
if (typeof ResizeObserver !== "undefined") {
|
|
|
|
const observe = (el: HTMLDivElement | null, cb: () => void) => {
|
|
|
|
if (!el) return;
|
|
|
|
const ro = new ResizeObserver(() => cb());
|
|
|
|
ro.observe(el);
|
|
|
|
observers.push(ro);
|
|
|
|
};
|
|
|
|
observe(quickHeaderRef.current, update);
|
|
|
|
observe(allHeaderRef.current, update);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("resize", update);
|
|
|
|
observers.forEach(o => o.disconnect());
|
|
|
|
};
|
2025-08-19 13:31:09 +01:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const { sections: visibleSections } = useToolSections(filteredTools);
|
|
|
|
|
|
|
|
const quickSection = useMemo(
|
2025-08-22 13:53:06 +01:00
|
|
|
() => visibleSections.find(s => s.key === 'quick'),
|
2025-08-19 13:31:09 +01:00
|
|
|
[visibleSections]
|
|
|
|
);
|
|
|
|
const allSection = useMemo(
|
2025-08-22 13:53:06 +01:00
|
|
|
() => visibleSections.find(s => s.key === 'all'),
|
2025-08-19 13:31:09 +01:00
|
|
|
[visibleSections]
|
|
|
|
);
|
|
|
|
|
|
|
|
const scrollTo = (ref: React.RefObject<HTMLDivElement | null>) => {
|
|
|
|
const container = scrollableRef.current;
|
|
|
|
const target = ref.current;
|
|
|
|
if (container && target) {
|
|
|
|
const stackedOffset = ref === allToolsRef
|
|
|
|
? (quickHeaderHeight + allHeaderHeight)
|
|
|
|
: quickHeaderHeight;
|
|
|
|
const top = target.offsetTop - container.offsetTop - (stackedOffset || 0);
|
|
|
|
container.scrollTo({
|
|
|
|
top: Math.max(0, top),
|
|
|
|
behavior: "smooth"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build flat list by subcategory for search mode
|
|
|
|
const { searchGroups } = useToolSections(isSearching ? filteredTools : []);
|
2025-05-21 21:47:44 +01:00
|
|
|
|
|
|
|
return (
|
2025-08-19 13:31:09 +01:00
|
|
|
<Box
|
|
|
|
h="100vh"
|
|
|
|
style={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
background: "var(--bg-toolbar)"
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Box
|
|
|
|
ref={scrollableRef}
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
overflowY: "auto",
|
|
|
|
overflowX: "hidden",
|
|
|
|
minHeight: 0,
|
2025-08-25 12:53:33 +01:00
|
|
|
height: "100%",
|
|
|
|
marginTop: -2
|
2025-08-19 13:31:09 +01:00
|
|
|
}}
|
|
|
|
className="tool-picker-scrollable"
|
|
|
|
>
|
|
|
|
{isSearching ? (
|
|
|
|
<Stack p="sm" gap="xs">
|
|
|
|
{searchGroups.length === 0 ? (
|
|
|
|
<NoToolsFound />
|
|
|
|
) : (
|
2025-08-22 13:53:06 +01:00
|
|
|
searchGroups.map(group => renderToolButtons(t, group, selectedToolKey, onSelect))
|
2025-08-19 13:31:09 +01:00
|
|
|
)}
|
|
|
|
</Stack>
|
2025-05-21 21:47:44 +01:00
|
|
|
) : (
|
2025-08-19 13:31:09 +01:00
|
|
|
<>
|
|
|
|
{quickSection && (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
ref={quickHeaderRef}
|
|
|
|
style={{
|
|
|
|
position: "sticky",
|
|
|
|
top: 0,
|
|
|
|
zIndex: 2,
|
|
|
|
borderTop: `0.0625rem solid var(--tool-header-border)`,
|
|
|
|
borderBottom: `0.0625rem solid var(--tool-header-border)`,
|
|
|
|
padding: "0.5rem 1rem",
|
|
|
|
fontWeight: 700,
|
|
|
|
background: "var(--tool-header-bg)",
|
|
|
|
color: "var(--tool-header-text)",
|
|
|
|
cursor: "pointer",
|
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
2025-08-25 12:53:33 +01:00
|
|
|
justifyContent: "space-between",
|
2025-08-19 13:31:09 +01:00
|
|
|
}}
|
|
|
|
onClick={() => scrollTo(quickAccessRef)}
|
2025-05-21 21:47:44 +01:00
|
|
|
>
|
2025-08-19 13:31:09 +01:00
|
|
|
<span>{t("toolPicker.quickAccess", "QUICK ACCESS")}</span>
|
|
|
|
<span
|
|
|
|
style={{
|
|
|
|
background: "var(--tool-header-badge-bg)",
|
|
|
|
color: "var(--tool-header-badge-text)",
|
|
|
|
borderRadius: ".5rem",
|
|
|
|
padding: "0.125rem 0.5rem",
|
|
|
|
fontSize: ".75rem",
|
|
|
|
fontWeight: 700
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{quickSection?.subcategories.reduce((acc, sc) => acc + sc.tools.length, 0)}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Box ref={quickAccessRef} w="100%">
|
|
|
|
<Stack p="sm" gap="xs">
|
2025-08-22 13:53:06 +01:00
|
|
|
{quickSection?.subcategories.map(sc =>
|
|
|
|
renderToolButtons(t, sc, selectedToolKey, onSelect, false)
|
2025-08-19 13:31:09 +01:00
|
|
|
)}
|
|
|
|
</Stack>
|
|
|
|
</Box>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{allSection && (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
ref={allHeaderRef}
|
|
|
|
style={{
|
|
|
|
position: "sticky",
|
2025-08-25 15:46:44 +01:00
|
|
|
top: quickSection ? quickHeaderHeight -1 : 0,
|
2025-08-19 13:31:09 +01:00
|
|
|
zIndex: 2,
|
|
|
|
borderTop: `0.0625rem solid var(--tool-header-border)`,
|
|
|
|
borderBottom: `0.0625rem solid var(--tool-header-border)`,
|
|
|
|
padding: "0.5rem 1rem",
|
|
|
|
fontWeight: 700,
|
|
|
|
background: "var(--tool-header-bg)",
|
|
|
|
color: "var(--tool-header-text)",
|
|
|
|
cursor: "pointer",
|
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "space-between"
|
|
|
|
}}
|
|
|
|
onClick={() => scrollTo(allToolsRef)}
|
|
|
|
>
|
|
|
|
<span>{t("toolPicker.allTools", "ALL TOOLS")}</span>
|
|
|
|
<span
|
|
|
|
style={{
|
|
|
|
background: "var(--tool-header-badge-bg)",
|
|
|
|
color: "var(--tool-header-badge-text)",
|
|
|
|
borderRadius: ".5rem",
|
|
|
|
padding: "0.125rem 0.5rem",
|
|
|
|
fontSize: ".75rem",
|
|
|
|
fontWeight: 700
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{allSection?.subcategories.reduce((acc, sc) => acc + sc.tools.length, 0)}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Box ref={allToolsRef} w="100%">
|
|
|
|
<Stack p="sm" gap="xs">
|
2025-08-22 13:53:06 +01:00
|
|
|
{allSection?.subcategories.map(sc =>
|
|
|
|
renderToolButtons(t, sc, selectedToolKey, onSelect, true)
|
2025-08-19 13:31:09 +01:00
|
|
|
)}
|
|
|
|
</Stack>
|
|
|
|
</Box>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{!quickSection && !allSection && <NoToolsFound />}
|
|
|
|
|
|
|
|
{/* bottom spacer to allow scrolling past the last row */}
|
|
|
|
<div aria-hidden style={{ height: 200 }} />
|
|
|
|
</>
|
2025-05-21 21:47:44 +01:00
|
|
|
)}
|
2025-08-19 13:31:09 +01:00
|
|
|
</Box>
|
2025-05-21 21:47:44 +01:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ToolPicker;
|