feat: handle limit

This commit is contained in:
karishmas6
2024-09-08 13:52:01 +05:30
parent 53988dead7
commit 6a34f966ae

View File

@@ -6,7 +6,7 @@ import DocumentScannerIcon from '@mui/icons-material/DocumentScanner';
import { SimpleBox } from "../atoms/Box"; import { SimpleBox } from "../atoms/Box";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import { useGlobalInfoStore } from "../../context/globalInfo"; import { useGlobalInfoStore } from "../../context/globalInfo";
import { PaginationType, useActionContext } from '../../context/browserActions'; import { PaginationType, useActionContext, LimitType } from '../../context/browserActions';
import { useBrowserSteps } from '../../context/browserSteps'; import { useBrowserSteps } from '../../context/browserSteps';
import { useSocketStore } from '../../context/socket'; import { useSocketStore } from '../../context/socket';
import { ScreenshotSettings } from '../../shared/types'; import { ScreenshotSettings } from '../../shared/types';
@@ -35,9 +35,11 @@ export const RightSidePanel: React.FC<RightSidePanelProps> = ({ onFinishCapture
const [showPaginationOptions, setShowPaginationOptions] = useState(false); const [showPaginationOptions, setShowPaginationOptions] = useState(false);
const [showLimitOptions, setShowLimitOptions] = useState(false); const [showLimitOptions, setShowLimitOptions] = useState(false);
const [selectedLimit, setSelectedLimit] = useState<string>('10'); const [selectedLimit, setSelectedLimit] = useState<string>('10');
const [captureStage, setCaptureStage] = useState<'initial' | 'pagination' | 'limit' | 'complete'>('initial');
const { lastAction, notify } = useGlobalInfoStore(); const { lastAction, notify } = useGlobalInfoStore();
const { getText, startGetText, stopGetText, getScreenshot, startGetScreenshot, stopGetScreenshot, paginationMode, getList, startGetList, stopGetList, startPaginationMode, stopPaginationMode, paginationType, updatePaginationType, limitMode, limitType, customLimit, updateLimitType, updateCustomLimit, stopLimitMode } = useActionContext(); const { getText, startGetText, stopGetText, getScreenshot, startGetScreenshot, stopGetScreenshot, paginationMode, getList, startGetList, stopGetList, startPaginationMode, stopPaginationMode, paginationType, updatePaginationType, limitMode, limitType, customLimit, updateLimitType, updateCustomLimit, stopLimitMode, startLimitMode } = useActionContext();
const { browserSteps, updateBrowserTextStepLabel, deleteBrowserStep, addScreenshotStep } = useBrowserSteps(); const { browserSteps, updateBrowserTextStepLabel, deleteBrowserStep, addScreenshotStep } = useBrowserSteps();
const { socket } = useSocketStore(); const { socket } = useSocketStore();
@@ -54,10 +56,6 @@ export const RightSidePanel: React.FC<RightSidePanelProps> = ({ onFinishCapture
setSelectedLimit(event.target.value); setSelectedLimit(event.target.value);
}; };
const handleCustomLimitChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setCustomLimit(event.target.value);
};
const handleTextStepConfirm = (id: number) => { const handleTextStepConfirm = (id: number) => {
const label = textLabels[id]?.trim(); const label = textLabels[id]?.trim();
if (label) { if (label) {
@@ -143,8 +141,13 @@ export const RightSidePanel: React.FC<RightSidePanelProps> = ({ onFinishCapture
const resetListState = useCallback(() => { const resetListState = useCallback(() => {
setShowPaginationOptions(false); setShowPaginationOptions(false);
updatePaginationType(''); updatePaginationType(''); // Reset pagination type
}, []); setShowLimitOptions(false); // Reset limit options
setSelectedLimit('10'); // Reset limit value to default
updateLimitType(''); // Reset limit type
updateCustomLimit(''); // Reset custom limit value
}, [updatePaginationType, updateLimitType, updateCustomLimit]);
const handleStopGetList = useCallback(() => { const handleStopGetList = useCallback(() => {
stopGetList(); stopGetList();
@@ -152,7 +155,6 @@ export const RightSidePanel: React.FC<RightSidePanelProps> = ({ onFinishCapture
}, [stopGetList, resetListState]); }, [stopGetList, resetListState]);
const stopCaptureAndEmitGetListSettings = useCallback(() => { const stopCaptureAndEmitGetListSettings = useCallback(() => {
stopPaginationMode();
const settings = getListSettingsObject(); const settings = getListSettingsObject();
if (settings) { if (settings) {
socket?.emit('action', { action: 'scrapeList', settings }); socket?.emit('action', { action: 'scrapeList', settings });
@@ -164,31 +166,63 @@ export const RightSidePanel: React.FC<RightSidePanelProps> = ({ onFinishCapture
}, [stopGetList, getListSettingsObject, socket, notify, handleStopGetList]); }, [stopGetList, getListSettingsObject, socket, notify, handleStopGetList]);
const handleConfirmListCapture = useCallback(() => { const handleConfirmListCapture = useCallback(() => {
if (!paginationMode) { switch (captureStage) {
startPaginationMode(); case 'initial':
// Start pagination mode
startPaginationMode();
setShowPaginationOptions(true);
setCaptureStage('pagination');
break;
case 'pagination':
if (!paginationType) {
notify('error', 'Please select a pagination type.');
return;
}
const settings = getListSettingsObject();
const paginationSelector = settings.pagination?.selector;
if (['clickNext', 'clickLoadMore'].includes(paginationType) && !paginationSelector) {
notify('error', 'Please select the pagination element first.');
return;
}
// Close pagination mode and start limit mode
stopPaginationMode();
setShowPaginationOptions(false);
startLimitMode();
setShowLimitOptions(true);
setCaptureStage('limit');
break;
case 'limit':
if (!limitType || (limitType === 'custom' && !customLimit)) {
notify('error', 'Please select a limit or enter a custom limit.');
return;
}
const limit = limitType === 'custom' ? parseInt(customLimit) : parseInt(limitType);
if (isNaN(limit) || limit <= 0) {
notify('error', 'Please enter a valid limit.');
return;
}
// Close limit mode and emit settings
stopLimitMode();
setShowLimitOptions(false);
stopCaptureAndEmitGetListSettings();
setCaptureStage('complete');
break;
case 'complete':
// Reset the capture process
setCaptureStage('initial');
break;
} }
if (!paginationType) { }, [captureStage, paginationType, limitType, customLimit, startPaginationMode, stopPaginationMode, startLimitMode, stopLimitMode, notify, stopCaptureAndEmitGetListSettings, getListSettingsObject]);
setShowPaginationOptions(true);
return;
}
if (!selectedLimit || (selectedLimit === 'custom' && !customLimit)) {
setShowLimitOptions(true);
return;
}
const settings = getListSettingsObject();
const paginationSelector = settings.pagination?.selector;
if (['clickNext', 'clickLoadMore'].includes(paginationType)) {
if (paginationSelector === '') {
notify('error', 'Please select the pagination element first.');
return;
}
}
//updateListStepLimit(parseInt(selectedLimit === 'custom' ? customLimit : selectedLimit));
stopCaptureAndEmitGetListSettings();
setShowPaginationOptions(false);
setShowLimitOptions(false);
updatePaginationType('');
}, [paginationType, selectedLimit, customLimit, stopCaptureAndEmitGetListSettings, notify]);
const handlePaginationSettingSelect = (option: PaginationType) => { const handlePaginationSettingSelect = (option: PaginationType) => {
updatePaginationType(option); updatePaginationType(option);
@@ -220,20 +254,18 @@ export const RightSidePanel: React.FC<RightSidePanelProps> = ({ onFinishCapture
<Box display="flex" flexDirection="column" gap={2} style={{ margin: '15px' }}> <Box display="flex" flexDirection="column" gap={2} style={{ margin: '15px' }}>
{!getText && !getScreenshot && !getList && <Button variant="contained" onClick={startGetList}>Capture List</Button>} {!getText && !getScreenshot && !getList && <Button variant="contained" onClick={startGetList}>Capture List</Button>}
{getList && {getList && (
<> <>
<Box display="flex" justifyContent="space-between" gap={2} style={{ margin: '15px' }}> <Box display="flex" justifyContent="space-between" gap={2} style={{ margin: '15px' }}>
{ <Button variant="outlined" onClick={handleConfirmListCapture}>
paginationMode ? {captureStage === 'initial' ? 'Start Capture' :
paginationType !== "" ? captureStage === 'pagination' ? 'Confirm Pagination' :
<Button variant="outlined" onClick={handleConfirmListCapture}>Confirm</Button> captureStage === 'limit' ? 'Confirm Limit' : 'Finish Capture'}
: null </Button>
: <Button variant="outlined" onClick={handleConfirmListCapture}>Confirm</Button>
}
<Button variant="outlined" color="error" onClick={handleStopGetList}>Discard</Button> <Button variant="outlined" color="error" onClick={handleStopGetList}>Discard</Button>
</Box> </Box>
</> </>
} )}
{showPaginationOptions && ( {showPaginationOptions && (
<Box display="flex" flexDirection="column" gap={2} style={{ margin: '15px' }}> <Box display="flex" flexDirection="column" gap={2} style={{ margin: '15px' }}>
@@ -246,7 +278,7 @@ export const RightSidePanel: React.FC<RightSidePanelProps> = ({ onFinishCapture
</Box> </Box>
)} )}
{limitMode && ( {showLimitOptions && (
<FormControl> <FormControl>
<FormLabel> <FormLabel>
<h4>What is the maximum number of rows you want to extract?</h4> <h4>What is the maximum number of rows you want to extract?</h4>