UNPKG

debug-time-machine-react-client

Version:

๐Ÿ•ฐ๏ธ ์™„์ „ ์ž๋™ํ™”๋œ React ๋””๋ฒ„๊น… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ - ์„ค์น˜ํ•˜๊ณ  ๋ฐ”๋กœ ์‚ฌ์šฉํ•˜์„ธ์š”!

178 lines (134 loc) โ€ข 4.71 kB
# debug-time-machine-react-client React ์•ฑ์—์„œ Debug Time Machine์„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” React Hook ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ## ์„ค์น˜ ```bash npm install debug-time-machine-react-client # ๋˜๋Š” yarn add debug-time-machine-react-client # ๋˜๋Š” pnpm add debug-time-machine-react-client ``` ## ์‚ฌ์šฉ๋ฒ• ### ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• ```tsx import { useDebugTimeMachine } from 'debug-time-machine-react-client'; function App() { const { isConnected, clientId } = useDebugTimeMachine(); return ( <div> {isConnected ? ( <p>โœ… Time Machine connected! Client ID: {clientId}</p> ) : ( <p>๐Ÿ”„ Connecting to Time Machine...</p> )} <YourAppContent /> </div> ); } ``` ### ๊ณ ๊ธ‰ ์„ค์ • ```tsx import { useDebugTimeMachine } from 'debug-time-machine-react-client'; function App() { const timeMachine = useDebugTimeMachine({ websocketUrl: 'ws://localhost:3001/ws', debugMode: true, captureUserActions: true, captureErrors: true, captureStateChanges: true, maxReconnectAttempts: 5, reconnectInterval: 3000, developmentOnly: true, }); // ์ˆ˜๋™์œผ๋กœ ์ƒํƒœ ๋ณ€๊ฒฝ ์บก์ฒ˜ const handleStateChange = () => { timeMachine.captureStateChange( 'MyComponent', { count: 0 }, { count: 1 }, { userId: 123 } ); }; // ์ˆ˜๋™์œผ๋กœ ์—๋Ÿฌ ์บก์ฒ˜ const handleError = () => { try { throw new Error('Something went wrong!'); } catch (error) { timeMachine.captureError(error, { context: 'user action' }); } }; return ( <div> <h1>Debug Time Machine Demo</h1> <div> <p>์—ฐ๊ฒฐ ์ƒํƒœ: {timeMachine.isConnected ? 'โœ… ์—ฐ๊ฒฐ๋จ' : 'โŒ ์—ฐ๊ฒฐ ์•ˆ๋จ'}</p> <p>ํด๋ผ์ด์–ธํŠธ ID: {timeMachine.clientId || 'N/A'}</p> <p>์žฌ์—ฐ๊ฒฐ ์‹œ๋„: {timeMachine.connectionInfo.reconnectAttempts}</p> {timeMachine.connectionInfo.lastError && ( <p>๋งˆ์ง€๋ง‰ ์—๋Ÿฌ: {timeMachine.connectionInfo.lastError}</p> )} </div> <div> <button onClick={handleStateChange}>์ƒํƒœ ๋ณ€๊ฒฝ ์บก์ฒ˜</button> <button onClick={handleError}>์—๋Ÿฌ ์บก์ฒ˜</button> <button onClick={timeMachine.reconnect}>์ˆ˜๋™ ์žฌ์—ฐ๊ฒฐ</button> </div> </div> ); } ``` ## API ### `useDebugTimeMachine(config?)` Debug Time Machine ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐํ•˜๊ณ  ์ด๋ฒคํŠธ ์บก์ฒ˜๋ฅผ ์ž๋™์œผ๋กœ ์„ค์ •ํ•˜๋Š” React Hook์ž…๋‹ˆ๋‹ค. #### Parameters - `config?`: `DebugTimeMachineConfig` - ์˜ต์…˜ ์„ค์ • ๊ฐ์ฒด #### Returns `DebugTimeMachineHookReturn` ๊ฐ์ฒด: - `isConnected: boolean` - ์„œ๋ฒ„ ์—ฐ๊ฒฐ ์ƒํƒœ - `clientId: string | null` - ์„œ๋ฒ„์—์„œ ํ• ๋‹น๋ฐ›์€ ํด๋ผ์ด์–ธํŠธ ID - `reconnect: () => void` - ์ˆ˜๋™ ์žฌ์—ฐ๊ฒฐ ํ•จ์ˆ˜ - `sendMessage: (type: string, data: any) => boolean` - ์ปค์Šคํ…€ ๋ฉ”์‹œ์ง€ ์ „์†ก - `captureError: (error: Error, errorInfo?: any) => void` - ์ˆ˜๋™ ์—๋Ÿฌ ์บก์ฒ˜ - `captureStateChange: (componentName: string, prevState: any, newState: any, props?: any) => void` - ์ˆ˜๋™ ์ƒํƒœ ๋ณ€๊ฒฝ ์บก์ฒ˜ - `connectionInfo` - ์—ฐ๊ฒฐ ์ •๋ณด ๊ฐ์ฒด ### Configuration #### `DebugTimeMachineConfig` ```typescript interface DebugTimeMachineConfig { websocketUrl?: string; // ๊ธฐ๋ณธ๊ฐ’: 'ws://localhost:3001/ws' debugMode?: boolean; // ๊ธฐ๋ณธ๊ฐ’: development ํ™˜๊ฒฝ์—์„œ true captureUserActions?: boolean; // ๊ธฐ๋ณธ๊ฐ’: true captureErrors?: boolean; // ๊ธฐ๋ณธ๊ฐ’: true captureStateChanges?: boolean; // ๊ธฐ๋ณธ๊ฐ’: true maxReconnectAttempts?: number; // ๊ธฐ๋ณธ๊ฐ’: 10 reconnectInterval?: number; // ๊ธฐ๋ณธ๊ฐ’: 5000 (ms) developmentOnly?: boolean; // ๊ธฐ๋ณธ๊ฐ’: true } ``` ## ์ž๋™ ์บก์ฒ˜ ๊ธฐ๋Šฅ ### ์‚ฌ์šฉ์ž ์ƒํ˜ธ์ž‘์šฉ ์บก์ฒ˜ ๋‹ค์Œ ์ด๋ฒคํŠธ๋“ค์ด ์ž๋™์œผ๋กœ ์บก์ฒ˜๋ฉ๋‹ˆ๋‹ค: - ํด๋ฆญ (`click`) - ์ž…๋ ฅ (`input`, `change`) - ํผ ์ œ์ถœ (`submit`) - ํ‚ค๋ณด๋“œ ์ž…๋ ฅ (`keydown`) - ํฌ์ปค์Šค ๋ณ€๊ฒฝ (`focus`, `blur`) ### ์—๋Ÿฌ ์บก์ฒ˜ ๋‹ค์Œ ์—๋Ÿฌ๋“ค์ด ์ž๋™์œผ๋กœ ์บก์ฒ˜๋ฉ๋‹ˆ๋‹ค: - JavaScript ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ - Promise rejection - Console.error ํ˜ธ์ถœ ### ํ™˜๊ฒฝ๋ณ„ ๋™์ž‘ - **Development**: ๋ชจ๋“  ๊ธฐ๋Šฅ ํ™œ์„ฑํ™”, ์ฝ˜์†” ๋กœ๊ทธ ์ถœ๋ ฅ - **Production**: `developmentOnly: true`์ธ ๊ฒฝ์šฐ ์ž๋™ ๋น„ํ™œ์„ฑํ™” ## ์„œ๋ฒ„ ์„ค์ • Debug Time Machine ๋ฐฑ์—”๋“œ ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: ```bash cd apps/backend npm run dev ``` ์„œ๋ฒ„๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ `http://localhost:3001`์—์„œ ์‹คํ–‰๋˜๋ฉฐ, WebSocket์€ `/ws` ๊ฒฝ๋กœ์—์„œ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ## TypeScript ์ง€์› ์ด ํŒจํ‚ค์ง€๋Š” ์™„์ „ํ•œ TypeScript ์ง€์›์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํƒ€์ž…์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด IDE์—์„œ ์ž๋™์™„์„ฑ๊ณผ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ## ๋ผ์ด์„ ์Šค MIT License