import { useRouter } from 'next/router' import Timeline from '@/components/timelines/Timeline' import { useEffect, useRef, useState } from 'react' import { Account, db } from '@/db' import generator, { Entity, MegalodonInterface, WebSocketInterface } from 'megalodon' import Notifications from '@/components/timelines/Notifications' import generateNotification from '@/utils/notification' import { useIntl } from 'react-intl' export default function Page() { const router = useRouter() const [account, setAccount] = useState(null) const [client, setClient] = useState(null) const streaming = useRef(null) const { formatMessage } = useIntl() useEffect(() => { if (router.query.id) { console.log(router) const f = async () => { const a = await db.accounts.get(parseInt(router.query.id as string)) if (a) { setAccount(a) const c = generator(a.sns, a.url, a.access_token, 'Whalebird') setClient(c) // Start user streaming for notification const instance = await c.getInstance() const ws = generator(a.sns, instance.data.urls.streaming_api, a.access_token, 'Whalebird') streaming.current = ws.userSocket() streaming.current.on('connect', () => { console.log('connect to user streaming') }) streaming.current.on('notification', (notification: Entity.Notification) => { const [title, body] = generateNotification(notification, formatMessage) if (title.length > 0) { new window.Notification(title, { body: body }) } }) } } f() } return () => { if (streaming.current) { streaming.current.removeAllListeners() streaming.current.stop() streaming.current = null console.log('close user streaming') } } }, [router.query.id]) if (!account || !client) return null switch (router.query.timeline as string) { case 'notifications': { return } default: { return } } }