import React, { createContext, useContext, useState, ReactNode } from 'react'; import { useSocketStore } from './socket'; export type PaginationType = 'scrollDown' | 'scrollUp' | 'clickNext' | 'clickLoadMore' | 'none' | ''; export type LimitType = '10' | '100' | 'custom' | ''; export type CaptureStage = 'initial' | 'pagination' | 'limit' | 'complete' | ''; interface ActionContextProps { getText: boolean; getList: boolean; getScreenshot: boolean; paginationMode: boolean; limitMode: boolean; paginationType: PaginationType; limitType: LimitType; customLimit: string; captureStage: CaptureStage; setCaptureStage: (stage: CaptureStage) => void; startPaginationMode: () => void; startGetText: () => void; stopGetText: () => void; startGetList: () => void; stopGetList: () => void; startGetScreenshot: () => void; stopGetScreenshot: () => void; stopPaginationMode: () => void; updatePaginationType: (type: PaginationType) => void; startLimitMode: () => void; stopLimitMode: () => void; updateLimitType: (type: LimitType) => void; updateCustomLimit: (limit: string) => void; } const ActionContext = createContext(undefined); export const ActionProvider = ({ children }: { children: ReactNode }) => { const [getText, setGetText] = useState(false); const [getList, setGetList] = useState(false); const [getScreenshot, setGetScreenshot] = useState(false); const [paginationMode, setPaginationMode] = useState(false); const [limitMode, setLimitMode] = useState(false); const [paginationType, setPaginationType] = useState(''); const [limitType, setLimitType] = useState(''); const [customLimit, setCustomLimit] = useState(''); const [captureStage, setCaptureStage] = useState('initial'); const { socket } = useSocketStore(); const updatePaginationType = (type: PaginationType) => setPaginationType(type); const updateLimitType = (type: LimitType) => setLimitType(type); const updateCustomLimit = (limit: string) => setCustomLimit(limit); const startPaginationMode = () => { setPaginationMode(true); setCaptureStage('pagination'); socket?.emit('setGetList', { getList: false }); }; const stopPaginationMode = () => setPaginationMode(false); const startLimitMode = () => { setLimitMode(true); setCaptureStage('limit'); }; const stopLimitMode = () => setLimitMode(false); const startGetText = () => setGetText(true); const stopGetText = () => setGetText(false); const startGetList = () => { setGetList(true); socket?.emit('setGetList', { getList: true }); setCaptureStage('initial'); } const stopGetList = () => { setGetList(false); setPaginationType(''); setLimitType(''); setCustomLimit(''); setCaptureStage('complete'); }; const startGetScreenshot = () => setGetScreenshot(true); const stopGetScreenshot = () => setGetScreenshot(false); return ( {children} ); }; export const useActionContext = () => { const context = useContext(ActionContext); if (context === undefined) { throw new Error('useActionContext must be used within an ActionProvider'); } return context; };