Files
parcer/src/context/browserActions.tsx

126 lines
4.1 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';
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' | '';
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;
customLimit: string;
captureStage: CaptureStage; // New captureStage property
setCaptureStage: (stage: CaptureStage) => void; // Setter for captureStage
2024-09-05 22:42:34 +05:30
startPaginationMode: () => 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;
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 }) => {
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');
2024-09-06 23:13:42 +05:30
const { socket } = useSocketStore();
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 });
};
2024-09-08 12:45:15 +05:30
const stopPaginationMode = () => setPaginationMode(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
2024-07-24 21:34:28 +05:30
const startGetText = () => setGetText(true);
const stopGetText = () => setGetText(false);
2024-07-24 19:30:42 +05:30
const startGetList = () => {
setGetList(true);
socket?.emit('setGetList', { getList: true });
2024-10-27 19:27:51 +05:30
setCaptureStage('initial');
}
2024-09-08 12:17:50 +05:30
const stopGetList = () => {
setGetList(false);
setPaginationType('');
setLimitType('');
setCustomLimit('');
2024-10-27 19:27:51 +05:30
setCaptureStage('complete');
2024-09-08 12:17:50 +05:30
};
2024-08-08 00:24:07 +05:30
2024-07-24 21:34:28 +05:30
const startGetScreenshot = () => setGetScreenshot(true);
const stopGetScreenshot = () => setGetScreenshot(false);
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,
customLimit,
captureStage,
setCaptureStage,
startGetText,
stopGetText,
startGetList,
stopGetList,
startGetScreenshot,
stopGetScreenshot,
startPaginationMode,
2024-09-08 12:17:50 +05:30
stopPaginationMode,
startLimitMode,
stopLimitMode,
updatePaginationType,
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;
};