diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 88ad625..8cfd52a 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -1,30 +1,17 @@ { "permissions": { "allow": [ - "Bash(bun add:*)", - "Bash(bun test:*)", - "Bash(bun run:*)", - "Bash(bun info:*)", - "Bash(npm view:*)", + "Bash(bun:*)", "Bash(find:*)", - "Bash(bun remove:*)", - "Bash(bunx tsc:*)", "Bash(rm:*)", "Bash(grep:*)", - "Bash(git add:*)", - "Bash(git commit:*)", - "Bash(bun tsc:*)", "Bash(ls:*)", - "Bash(bun x tsc:*)", - "Bash(bun:*)", "Bash(mkdir:*)", "Bash(cp:*)", "Bash(rg:*)", "Bash(mv:*)", - "Bash(gh workflow:*)", - "Bash(gh run list:*)", - "Bash(gh run view:*)", - "Bash(git push:*)" + "Bash(gh:*)", + "Bash(git:*)" ], "deny": [] } diff --git a/packages/examples/src/useeffect-interval.tsx b/packages/examples/src/useeffect-interval.tsx new file mode 100644 index 0000000..a572810 --- /dev/null +++ b/packages/examples/src/useeffect-interval.tsx @@ -0,0 +1,89 @@ +/// +import React, { useState, useEffect } from 'react'; +import { MtcuteAdapter } from '@react-telegram/mtcute-adapter'; + +// Timer component with useEffect and setInterval +const TimerApp = () => { + const [count, setCount] = useState(0); + const [isRunning, setIsRunning] = useState(true); + + useEffect(() => { + // Only set up interval if isRunning is true + if (!isRunning) return; + + const interval = setInterval(() => { + setCount((prevCount) => prevCount + 1); + }, 1000); + + // Cleanup function to clear interval + return () => { + clearInterval(interval); + }; + }, [isRunning]); // Re-run effect when isRunning changes + + return ( + <> + ⏱️ Timer with useEffect +
+
+ Seconds elapsed: {count} +
+
+ Status: {isRunning ? '🟢 Running' : '🔴 Paused'} +
+
+ + + + +
+
+ This example demonstrates useEffect with setInterval. + The timer automatically increments every second and properly + cleans up when paused or when the component unmounts. +
+ + ); +}; + +// Main bot setup +async function main() { + // You'll need to set these environment variables + const config = { + apiId: parseInt(process.env.API_ID || '0'), + apiHash: process.env.API_HASH || '', + botToken: process.env.BOT_TOKEN || '', + storage: process.env.STORAGE_PATH || '.mtcute' + }; + + if (!config.apiId || !config.apiHash || !config.botToken) { + console.error('Please set API_ID, API_HASH, and BOT_TOKEN environment variables'); + process.exit(1); + } + + const adapter = new MtcuteAdapter(config); + + // Set up command handler for timer + adapter.onCommand('timer', () => ); + adapter.onCommand('start', () => ( + <> + 🤖 Timer Bot +
+
+ Welcome! This bot demonstrates useEffect with setInterval. +
+
+ Use /timer to start the timer example. + + )); + + // Start the bot + await adapter.start(config.botToken); + + console.log('Timer bot is running! Send /start to begin.'); +} + +// Run the bot +main().catch(console.error); \ No newline at end of file