()
| 58 | } |
| 59 | |
| 60 | export default function Home() { |
| 61 | const [backendUrl, setBackendUrl] = useLocalStorageState( |
| 62 | 'tianji.backendUrl', |
| 63 | { |
| 64 | defaultValue: 'http://localhost:12345', |
| 65 | } |
| 66 | ); |
| 67 | const [websiteId, setWebsiteId] = useLocalStorageState('tianji.websiteId', { |
| 68 | defaultValue: '', |
| 69 | }); |
| 70 | const [injectedEl, setInjectedEl] = useState<HTMLScriptElement | null>(null); |
| 71 | const [isInjecting, setIsInjecting] = useState(false); |
| 72 | const [eventCounts, setEventCounts] = useState<Record<string, number>>({}); |
| 73 | const [isBatchSending, setIsBatchSending] = useState(false); |
| 74 | const [batchProgress, setBatchProgress] = useState(0); |
| 75 | |
| 76 | const injectTracker = async () => { |
| 77 | if (isInjecting) return; |
| 78 | |
| 79 | setIsInjecting(true); |
| 80 | |
| 81 | if (injectedEl) { |
| 82 | injectedEl.remove(); |
| 83 | setInjectedEl(null); |
| 84 | setEventCounts({}); |
| 85 | } |
| 86 | |
| 87 | if (!backendUrl || !websiteId) { |
| 88 | toast.error( |
| 89 | 'Missing required fields! Please fill in both Backend URL and Website ID.', |
| 90 | { |
| 91 | description: 'Both fields are required to initialize the tracker.', |
| 92 | } |
| 93 | ); |
| 94 | setIsInjecting(false); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | try { |
| 99 | const scriptEl = await initTianjiTracker({ |
| 100 | url: backendUrl, |
| 101 | websiteId, |
| 102 | }); |
| 103 | |
| 104 | console.log('scriptEl', scriptEl); |
| 105 | if (scriptEl) { |
| 106 | setInjectedEl(scriptEl); |
| 107 | } |
| 108 | toast.success('Tianji tracker injected successfully!', { |
| 109 | description: |
| 110 | 'You can now start sending events to your analytics dashboard.', |
| 111 | }); |
| 112 | } catch (error) { |
| 113 | toast.error( |
| 114 | 'Failed to inject tracker. Please check your configuration.', |
| 115 | { |
| 116 | description: 'Make sure your backend URL is correct and accessible.', |
| 117 | } |
nothing calls this directly
no test coverage detected