Files
Stirling-PDF/frontend/src/components/tools/adjustContrast/AdjustContrastPreview.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

94 lines
3.3 KiB
TypeScript

import { useEffect, useRef, useState } from 'react';
import { AdjustContrastParameters } from '../../../hooks/tools/adjustContrast/useAdjustContrastParameters';
import { useThumbnailGeneration } from '../../../hooks/useThumbnailGeneration';
import ObscuredOverlay from '../../shared/ObscuredOverlay';
import { Text } from '@mantine/core';
import { useTranslation } from 'react-i18next';
import { applyAdjustmentsToCanvas } from './utils';
interface Props {
file: File | null;
parameters: AdjustContrastParameters;
}
export default function AdjustContrastPreview({ file, parameters }: Props) {
const { t } = useTranslation();
const containerRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const [thumb, setThumb] = useState<string | null>(null);
const { requestThumbnail } = useThumbnailGeneration();
useEffect(() => {
let active = true;
const load = async () => {
if (!file || file.type !== 'application/pdf') { setThumb(null); return; }
const id = `${file.name}:${file.size}:${file.lastModified}:page:1`;
const tUrl = await requestThumbnail(id, file, 1);
if (active) setThumb(tUrl || null);
};
load();
return () => { active = false; };
}, [file, requestThumbnail]);
useEffect(() => {
const revoked: string | null = null;
const render = async () => {
if (!thumb || !canvasRef.current) return;
const img = new Image();
img.crossOrigin = 'anonymous';
img.src = thumb;
await new Promise<void>((resolve, reject) => {
img.onload = () => resolve();
img.onerror = () => reject();
});
// Draw thumbnail to a source canvas
const src = document.createElement('canvas');
src.width = img.naturalWidth;
src.height = img.naturalHeight;
const sctx = src.getContext('2d');
if (!sctx) return;
sctx.drawImage(img, 0, 0);
// Apply accurate pixel adjustments
const adjusted = applyAdjustmentsToCanvas(src, parameters);
// Draw adjusted onto display canvas
const display = canvasRef.current;
display.width = adjusted.width;
display.height = adjusted.height;
const dctx = display.getContext('2d');
if (!dctx) return;
dctx.clearRect(0, 0, display.width, display.height);
dctx.drawImage(adjusted, 0, 0);
};
render();
return () => {
if (revoked) URL.revokeObjectURL(revoked);
};
}, [thumb, parameters]);
return (
<div>
<div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 6 }}>
<div style={{ flex: 1, height: 1, background: 'var(--border-color)' }} />
<div style={{ fontSize: 12, color: 'var(--text-color-muted)' }}>{t('common.preview', 'Preview')}</div>
<div style={{ flex: 1, height: 1, background: 'var(--border-color)' }} />
</div>
<ObscuredOverlay
obscured={!thumb}
overlayMessage={<Text size="sm" c="white" fw={600}>{t('adjustContrast.noPreview', 'Select a PDF to preview')}</Text>}
borderRadius={6}
>
<div ref={containerRef} style={{ aspectRatio: '8.5/11', width: '100%', border: '1px solid var(--border-color)', borderRadius: 8, overflow: 'hidden' }}>
{thumb && (
<canvas ref={canvasRef} style={{ width: '100%', height: '100%' }} />
)}
</div>
</ObscuredOverlay>
</div>
);
}