Files
Stirling-PDF/frontend/src/components/tools/ToolPanel.tsx
T
81dec53488 style(frontend): remove redundant React imports in TypeScript components (#4738)
# Description of Changes

This pull request removes unnecessary imports of the default React
object from multiple frontend files. The changes help clean up the
codebase by only importing specific React hooks where needed, rather
than importing the entire React object.

**Code cleanup and import optimization:**

* Removed default `React` imports from component files, retaining only
the necessary React hooks (such as `useEffect`, `useState`, `useMemo`,
etc.) in files like `FileEditor.tsx`, `FullscreenToolList.tsx`,
`ToolPanel.tsx`, `PageNumberPreview.tsx`, `AdjustContrastPreview.tsx`,
`AutomationRun.tsx`, `LocalEmbedPDFWithAnnotations.tsx`,
`ToolRegistryProvider.tsx`, `useTranslatedToolRegistry.tsx`, and
`AdjustContrast.tsx`.
[[1]](diffhunk://#diff-481d0a2d8a1714d34d21181db63a020b08dfccfbfa80bf47ac9af382dff25310L1-R1)
[[2]](diffhunk://#diff-1d6e9507cb0744e03ec0e80c510874bfc5054986b0275ae3b8592eb67b5ec0f2L1-R1)
[[3]](diffhunk://#diff-8ee3da71652291722dc6130f44565c098fe0f9cdf5e8ec0ba3c631be8980b13eL1-R1)
[[4]](diffhunk://#diff-ecc12bf9b557e947ae2f1866d07446b19bad1fbdf143bf231dd3076b1e794826L1-R1)
[[5]](diffhunk://#diff-6ba4ca6f491368b62e160639e97207f5c1d35fee77f4eebd39133630e0ecb7a1L1-R1)
[[6]](diffhunk://#diff-ff7cba3dba3b1f4ec4c8758a9fbe539351f96225284d0c61cca2642ec7a8e486L1-R1)
[[7]](diffhunk://#diff-d99cf54aa50d266c08844fac31c79e73a7f1714adeedb186d1decab8b9fb7f78L1-R1)
[[8]](diffhunk://#diff-3467ae2b00d2ea95c360bc367adfbae124a4fb1d2627e889d12fb00e288bf508L1-R1)
[[9]](diffhunk://#diff-0a3e636736c137356dd9354ff3cacbd302ebda40147545e13c62d073525d1969L1-R1)
[[10]](diffhunk://#diff-2fed64bea41254c30dcc038f6b92943272bcaa771af200b8a3dc1a2cef6b5ca7L2-R2)
* Removed default `React` imports from presentational and settings
components that do not use JSX at the top level, such as
`Workbench.tsx`, `SliderWithInput.tsx`,
`AdjustContrastBasicSettings.tsx`, `AdjustContrastColorSettings.tsx`,
`AdjustContrastSingleStepSettings.tsx`, `FileSummaryHeader.tsx`,
`SignatureSection.tsx`, `SignatureStatusBadge.tsx`, and
`ThumbnailPreview.tsx`.
[[1]](diffhunk://#diff-6ffa9f7048b8e2a454ccf52b712179784cf32d42ecac9c85331c595a4cee39b4L1)
[[2]](diffhunk://#diff-a1159e58f6668bc6de9595b4014fd7b8e0a19f9efa75294ba80184cfe54b601fL1)
[[3]](diffhunk://#diff-ce5bbd748c15bc456e7f01180b7ff04c80c782e3d6662384f28e032af36ed3ccL1)
[[4]](diffhunk://#diff-494006ec5e237eb7b3a16b9bc144a6ed49ed38c547d95b68a89f69a5af6676ceL1)
[[5]](diffhunk://#diff-e61a3e2d98c9601eea868062258b925e6f6d672f49df14e3684b12f736622db4L1)
[[6]](diffhunk://#diff-97df8b451114e347bb3f581ff5c91057601fb821e224479e1106493ce9479dcdL1)
[[7]](diffhunk://#diff-cc070bfc4dc892a4e9a2be725c9f27ab66bdbc821a525fad10e14b27096d4e5aL1)
[[8]](diffhunk://#diff-c179df2634412e4938bcd686f86b3bdbd1a6039d8a8b62c44fd0c085cc58af74L1)
[[9]](diffhunk://#diff-64403230a8c8e90135bd8d7cd275c40d8e22bd3a22ed642dec5451018eec3c10L1)

These changes reduce unnecessary imports and make the codebase cleaner
and more consistent.

---

## Checklist

### General

- [ ] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [ ] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md)
(if applicable)
- [ ] I have performed a self-review of my own code
- [ ] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [ ] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing)
for more details.

Co-authored-by: ConnorYoh <[email protected]>
2025-10-27 16:40:43 +00:00

216 lines
7.5 KiB
TypeScript

import { useEffect, useMemo } from 'react';
import { useRainbowThemeContext } from '../shared/RainbowThemeProvider';
import { useToolWorkflow } from '../../contexts/ToolWorkflowContext';
import { usePreferences } from '../../contexts/PreferencesContext';
import ToolPicker from './ToolPicker';
import SearchResults from './SearchResults';
import ToolRenderer from './ToolRenderer';
import ToolSearch from './toolPicker/ToolSearch';
import { useSidebarContext } from "../../contexts/SidebarContext";
import rainbowStyles from '../../styles/rainbow.module.css';
import { ActionIcon, ScrollArea } from '@mantine/core';
import { ToolId } from '../../types/toolId';
import { useMediaQuery } from '@mantine/hooks';
import DoubleArrowIcon from '@mui/icons-material/DoubleArrow';
import { useTranslation } from 'react-i18next';
import FullscreenToolSurface from './FullscreenToolSurface';
import { useToolPanelGeometry } from '../../hooks/tools/useToolPanelGeometry';
import { useRightRail } from '../../contexts/RightRailContext';
import { Tooltip } from '../shared/Tooltip';
import './ToolPanel.css';
// No props needed - component uses context
export default function ToolPanel() {
const { t } = useTranslation();
const { isRainbowMode } = useRainbowThemeContext();
const { sidebarRefs } = useSidebarContext();
const { toolPanelRef, quickAccessRef, rightRailRef } = sidebarRefs;
const isMobile = useMediaQuery('(max-width: 1024px)');
const {
leftPanelView,
isPanelVisible,
searchQuery,
filteredTools,
toolRegistry,
setSearchQuery,
selectedToolKey,
handleToolSelect,
setPreviewFile,
toolPanelMode,
setToolPanelMode,
setLeftPanelView,
readerMode,
} = useToolWorkflow();
const { setAllRightRailButtonsDisabled } = useRightRail();
const { preferences, updatePreference } = usePreferences();
const isFullscreenMode = toolPanelMode === 'fullscreen';
const toolPickerVisible = !readerMode;
const fullscreenExpanded = isFullscreenMode && leftPanelView === 'toolPicker' && !isMobile && toolPickerVisible;
// Disable right rail buttons when fullscreen mode is active
useEffect(() => {
setAllRightRailButtonsDisabled(fullscreenExpanded);
}, [fullscreenExpanded, setAllRightRailButtonsDisabled]);
const fullscreenGeometry = useToolPanelGeometry({
enabled: fullscreenExpanded,
toolPanelRef,
quickAccessRef,
rightRailRef,
});
const toggleLabel = isFullscreenMode
? t('toolPanel.toggle.sidebar', 'Switch to sidebar mode')
: t('toolPanel.toggle.fullscreen', 'Switch to fullscreen mode');
const handleModeToggle = () => {
const nextMode = isFullscreenMode ? 'sidebar' : 'fullscreen';
setToolPanelMode(nextMode);
if (nextMode === 'fullscreen' && leftPanelView !== 'toolPicker') {
setLeftPanelView('toolPicker');
}
};
const computedWidth = () => {
if (isMobile) {
return '100%';
}
if (!isPanelVisible) {
return '0';
}
return '18.5rem';
};
const matchedTextMap = useMemo(() => {
const map = new Map<string, string>();
filteredTools.forEach(({ item: [id], matchedText }) => {
if (matchedText) {
map.set(id, matchedText);
}
});
return map;
}, [filteredTools]);
return (
<div
ref={toolPanelRef}
data-sidebar="tool-panel"
data-tour={fullscreenExpanded ? undefined : "tool-panel"}
className={`tool-panel flex flex-col ${fullscreenExpanded ? 'tool-panel--fullscreen-active' : 'overflow-hidden'} bg-[var(--bg-toolbar)] border-r border-[var(--border-subtle)] transition-all duration-300 ease-out ${
isRainbowMode ? rainbowStyles.rainbowPaper : ''
} ${isMobile ? 'h-full border-r-0' : 'h-screen'} ${fullscreenExpanded ? 'tool-panel--fullscreen' : ''}`}
style={{
width: computedWidth(),
padding: '0'
}}
>
{!fullscreenExpanded && (
<div
style={{
opacity: isMobile || isPanelVisible ? 1 : 0,
transition: 'opacity 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94)',
height: '100%',
display: 'flex',
flexDirection: 'column'
}}
>
<div
className="tool-panel__search-row"
style={{
backgroundColor: 'var(--tool-panel-search-bg)',
borderBottom: '1px solid var(--tool-panel-search-border-bottom)'
}}
>
<ToolSearch
value={searchQuery}
onChange={setSearchQuery}
toolRegistry={toolRegistry}
mode="filter"
/>
{!isMobile && leftPanelView === 'toolPicker' && (
<Tooltip
content={toggleLabel}
position="bottom"
arrow={true}
openOnFocus={false}
>
<ActionIcon
variant="subtle"
radius="xl"
style={{ color: 'var(--right-rail-icon)' }}
onClick={handleModeToggle}
aria-label={toggleLabel}
className="tool-panel__mode-toggle"
>
<DoubleArrowIcon fontSize="small" />
</ActionIcon>
</Tooltip>
)}
</div>
{searchQuery.trim().length > 0 ? (
<div className="flex-1 flex flex-col overflow-y-auto">
<SearchResults
filteredTools={filteredTools}
onSelect={(id) => handleToolSelect(id as ToolId)}
searchQuery={searchQuery}
/>
</div>
) : leftPanelView === 'toolPicker' ? (
<div className="flex-1 flex flex-col overflow-auto">
<ToolPicker
selectedToolKey={selectedToolKey}
onSelect={(id) => handleToolSelect(id as ToolId)}
filteredTools={filteredTools}
isSearching={Boolean(searchQuery && searchQuery.trim().length > 0)}
/>
</div>
) : (
<div className="flex-1 flex flex-col overflow-hidden">
<div className="flex-1 min-h-0 overflow-hidden">
<ScrollArea h="100%">
{selectedToolKey ? (
<ToolRenderer
selectedToolKey={selectedToolKey}
onPreviewFile={setPreviewFile}
/>
) : (
<div className="tool-panel__placeholder">
{t('toolPanel.placeholder', 'Choose a tool to get started')}
</div>
)}
</ScrollArea>
</div>
</div>
)}
</div>
)}
{fullscreenExpanded && (
<FullscreenToolSurface
searchQuery={searchQuery}
toolRegistry={toolRegistry}
filteredTools={filteredTools}
selectedToolKey={selectedToolKey}
showDescriptions={preferences.showLegacyToolDescriptions}
matchedTextMap={matchedTextMap}
onSearchChange={setSearchQuery}
onSelect={(id: ToolId) => handleToolSelect(id)}
onToggleDescriptions={() => updatePreference('showLegacyToolDescriptions', !preferences.showLegacyToolDescriptions)}
onExitFullscreenMode={() => setToolPanelMode('sidebar')}
toggleLabel={toggleLabel}
geometry={fullscreenGeometry}
/>
)}
</div>
);
}