Files
parcer/src/context/browserActions.tsx

197 lines
6.7 KiB
TypeScript
Raw Normal View History

2024-07-24 21:34:28 +05:30
import React, { createContext, useContext, useState, ReactNode } from 'react';
import { useSocketStore } from './socket';
2025-01-03 18:46:24 +05:30
import { WorkflowFile } from 'maxun-core';
import { emptyWorkflow } from '../shared/constants';
2024-07-24 19:29:52 +05:30
2024-09-06 23:58:22 +05:30
export type PaginationType = 'scrollDown' | 'scrollUp' | 'clickNext' | 'clickLoadMore' | 'none' | '';
2024-09-08 12:17:50 +05:30
export type LimitType = '10' | '100' | 'custom' | '';
2024-10-27 19:27:51 +05:30
export type CaptureStage = 'initial' | 'pagination' | 'limit' | 'complete' | '';
2025-04-27 15:32:23 +05:30
export type ActionType = 'text' | 'list' | 'screenshot';
2024-09-06 23:08:01 +05:30
2024-07-24 21:34:28 +05:30
interface ActionContextProps {
2024-07-24 19:29:52 +05:30
getText: boolean;
2024-08-08 00:17:47 +05:30
getList: boolean;
2024-07-24 19:29:52 +05:30
getScreenshot: boolean;
2024-09-05 20:52:20 +05:30
paginationMode: boolean;
2024-09-08 12:17:50 +05:30
limitMode: boolean;
paginationType: PaginationType;
2024-09-08 12:17:50 +05:30
limitType: LimitType;
2025-01-03 18:46:24 +05:30
workflow: WorkflowFile;
2024-09-08 12:17:50 +05:30
customLimit: string;
2024-12-20 22:22:33 +05:30
captureStage: CaptureStage;
2025-01-03 00:17:55 +05:30
showPaginationOptions: boolean;
2025-04-27 15:32:23 +05:30
showLimitOptions: boolean;
activeAction: 'none' | 'text' | 'list' | 'screenshot';
setActiveAction: (action: 'none' | 'text' | 'list' | 'screenshot') => void;
2025-01-03 18:46:24 +05:30
setWorkflow: (workflow: WorkflowFile) => void;
2025-01-03 00:17:55 +05:30
setShowPaginationOptions: (show: boolean) => void;
setShowLimitOptions: (show: boolean) => void;
2024-12-20 22:22:33 +05:30
setCaptureStage: (stage: CaptureStage) => void;
2025-04-27 15:32:23 +05:30
startAction: (action: 'text' | 'list' | 'screenshot') => void;
finishAction: (action: 'text' | 'list' | 'screenshot') => void;
2024-07-24 21:34:28 +05:30
startGetText: () => void;
stopGetText: () => void;
2024-08-08 00:24:07 +05:30
startGetList: () => void;
stopGetList: () => void;
2024-07-24 21:34:28 +05:30
startGetScreenshot: () => void;
stopGetScreenshot: () => void;
2025-04-27 15:32:23 +05:30
startPaginationMode: () => void;
2024-09-05 22:42:57 +05:30
stopPaginationMode: () => void;
updatePaginationType: (type: PaginationType) => void;
2024-09-08 12:17:50 +05:30
startLimitMode: () => void;
stopLimitMode: () => void;
updateLimitType: (type: LimitType) => void;
updateCustomLimit: (limit: string) => void;
2024-07-24 19:29:52 +05:30
}
2024-07-24 21:34:28 +05:30
const ActionContext = createContext<ActionContextProps | undefined>(undefined);
2024-07-24 19:30:22 +05:30
2024-07-24 21:34:28 +05:30
export const ActionProvider = ({ children }: { children: ReactNode }) => {
2025-01-03 18:46:24 +05:30
const [workflow, setWorkflow] = useState<WorkflowFile>(emptyWorkflow);
2024-07-24 19:30:42 +05:30
const [getText, setGetText] = useState<boolean>(false);
2024-08-08 00:17:47 +05:30
const [getList, setGetList] = useState<boolean>(false);
2024-07-24 19:30:42 +05:30
const [getScreenshot, setGetScreenshot] = useState<boolean>(false);
2024-09-05 20:52:20 +05:30
const [paginationMode, setPaginationMode] = useState<boolean>(false);
2024-09-08 12:17:50 +05:30
const [limitMode, setLimitMode] = useState<boolean>(false);
2024-09-06 23:10:06 +05:30
const [paginationType, setPaginationType] = useState<PaginationType>('');
2024-09-08 12:17:50 +05:30
const [limitType, setLimitType] = useState<LimitType>('');
const [customLimit, setCustomLimit] = useState<string>('');
2024-10-27 19:27:51 +05:30
const [captureStage, setCaptureStage] = useState<CaptureStage>('initial');
2025-01-03 00:17:55 +05:30
const [showPaginationOptions, setShowPaginationOptions] = useState(false);
const [showLimitOptions, setShowLimitOptions] = useState(false);
2025-04-27 15:32:23 +05:30
const [activeAction, setActiveAction] = useState<'none' | 'text' | 'list' | 'screenshot'>('none');
2024-09-06 23:13:42 +05:30
const { socket } = useSocketStore();
2025-04-27 15:32:23 +05:30
const startAction = (action: 'text' | 'list' | 'screenshot') => {
if (activeAction !== 'none') return;
setActiveAction(action);
if (action === 'text') {
setGetText(true);
} else if (action === 'list') {
setGetList(true);
socket?.emit('setGetList', { getList: true });
setCaptureStage('initial');
} else if (action === 'screenshot') {
setGetScreenshot(true);
}
};
const finishAction = (action: 'text' | 'list' | 'screenshot') => {
if (activeAction !== action) return;
setActiveAction('none');
if (action === 'text') {
setGetText(false);
} else if (action === 'list') {
setGetList(false);
setPaginationType('');
setLimitType('');
setCustomLimit('');
setCaptureStage('complete');
2025-04-29 00:38:53 +05:30
socket?.emit('setGetList', { getList: false });
2025-04-27 15:32:23 +05:30
} else if (action === 'screenshot') {
setGetScreenshot(false);
}
};
2024-09-06 23:14:12 +05:30
const updatePaginationType = (type: PaginationType) => setPaginationType(type);
2024-09-08 12:17:50 +05:30
const updateLimitType = (type: LimitType) => setLimitType(type);
const updateCustomLimit = (limit: string) => setCustomLimit(limit);
2024-09-06 23:14:12 +05:30
const startPaginationMode = () => {
setPaginationMode(true);
setCaptureStage('pagination');
socket?.emit('setGetList', { getList: false });
socket?.emit('setPaginationMode', { pagination: true });
};
2025-04-29 00:38:53 +05:30
const stopPaginationMode = () => {
setPaginationMode(false),
socket?.emit('setPaginationMode', { pagination: false });
};
2024-09-08 12:17:50 +05:30
const startLimitMode = () => {
setLimitMode(true);
setCaptureStage('limit');
};
2024-09-08 12:17:50 +05:30
const stopLimitMode = () => setLimitMode(false);
2024-09-05 22:42:34 +05:30
2025-04-27 15:32:23 +05:30
const startGetText = () => startAction('text');
const stopGetText = () => {
setGetText(false);
setActiveAction('none');
};
const startGetList = () => startAction('list');
2024-09-08 12:17:50 +05:30
const stopGetList = () => {
setGetList(false);
2025-04-25 19:33:12 +05:30
socket?.emit('setGetList', { getList: false });
2024-09-08 12:17:50 +05:30
setPaginationType('');
setLimitType('');
setCustomLimit('');
2024-10-27 19:27:51 +05:30
setCaptureStage('complete');
2025-04-27 15:32:23 +05:30
setActiveAction('none');
};
const startGetScreenshot = () => startAction('screenshot');
const stopGetScreenshot = () => {
setGetScreenshot(false);
setActiveAction('none');
2024-09-08 12:17:50 +05:30
};
2024-07-24 19:30:42 +05:30
return (
<ActionContext.Provider value={{
getText,
getList,
getScreenshot,
paginationMode,
2024-09-08 12:17:50 +05:30
limitMode,
paginationType,
2024-09-08 12:17:50 +05:30
limitType,
2025-01-03 18:46:24 +05:30
workflow,
2024-09-08 12:17:50 +05:30
customLimit,
captureStage,
2025-01-03 00:17:55 +05:30
showPaginationOptions,
showLimitOptions,
2025-04-27 15:32:23 +05:30
activeAction,
setActiveAction,
2025-01-03 18:46:24 +05:30
setWorkflow,
2025-01-03 00:17:55 +05:30
setShowPaginationOptions,
setShowLimitOptions,
setCaptureStage,
2025-04-27 15:32:23 +05:30
startAction,
finishAction,
startGetText,
stopGetText,
startGetList,
stopGetList,
startGetScreenshot,
stopGetScreenshot,
startPaginationMode,
2024-09-08 12:17:50 +05:30
stopPaginationMode,
2025-04-27 15:32:23 +05:30
updatePaginationType,
2024-09-08 12:17:50 +05:30
startLimitMode,
stopLimitMode,
updateLimitType,
updateCustomLimit
}}>
2024-07-24 19:30:42 +05:30
{children}
</ActionContext.Provider>
);
2024-07-24 21:34:28 +05:30
};
export const useActionContext = () => {
const context = useContext(ActionContext);
if (context === undefined) {
throw new Error('useActionContext must be used within an ActionProvider');
}
return context;
2025-04-27 15:32:23 +05:30
};