import { Account, db } from '@/db' import { Card, Chip, List, ListItem, ListItemPrefix, ListItemSuffix } from '@material-tailwind/react' import generator, { Entity, MegalodonInterface } from 'megalodon' import { useRouter } from 'next/router' import { createContext, useContext, useEffect, useState } from 'react' import { useHotkeys } from 'react-hotkeys-hook' import { FaBell, FaBookmark, FaGlobe, FaHouse, FaList, FaStar, FaUsers, FaHashtag, FaUserPlus } from 'react-icons/fa6' import { useIntl } from 'react-intl' import Jump from '../Jump' import { useUnreads } from '@/provider/unreads' type Context = { reloadMenu: () => Promise } const TimelinesContext = createContext({ reloadMenu: async () => {} }) TimelinesContext.displayName = 'TimelinesContext' export const useTimelines = () => { return useContext(TimelinesContext) } type LayoutProps = { children: React.ReactNode } export type Timeline = { id: string title: string icon: JSX.Element path: string } export default function Layout({ children }: LayoutProps) { const router = useRouter() const { formatMessage } = useIntl() const { unreads, setUnreads } = useUnreads() const [account, setAccount] = useState(null) const [lists, setLists] = useState>([]) const [followedTags, setFollowedTags] = useState>([]) const [openJump, setOpenJump] = useState(false) const [client, setClient] = useState() const [followRequests, setFollowRequests] = useState>([]) useHotkeys('mod+k', () => setOpenJump(current => !current)) useEffect(() => { if (router.query.id) { const f = async () => { const acct = await db.accounts.get(parseInt(router.query.id as string)) if (!acct) return setAccount(acct) } f() } }, [router.query.id]) useEffect(() => { if (!account) return const c = generator(account.sns, account.url, account.access_token, 'Whalebird') setClient(c) const f = async () => { const res = await c.getLists() setLists(res.data) } f() const g = async () => { try { const res = await c.getFollowedTags() setFollowedTags(res.data) } catch (err) { setFollowedTags([]) console.error(err) } } g() const r = async () => { const res = await c.getFollowRequests() if (res.data.length > 0) { setUnreads(current => Object.assign({}, current, { [`${account.id?.toString()}_follow_requests`]: res.data.length }) ) console.log(unreads) setFollowRequests([ { id: 'follow_requests', title: formatMessage({ id: 'timeline.follow_requests' }), icon: , path: `/accounts/${router.query.id}/follow_requests` } ]) } else { setFollowRequests([]) } } r() }, [account]) const pages: Array = [ { id: 'home', title: formatMessage({ id: 'timeline.home' }), icon: , path: `/accounts/${router.query.id}/home` }, { id: 'notifications', title: formatMessage({ id: 'timeline.notifications' }), icon: , path: `/accounts/${router.query.id}/notifications` }, { id: 'local', title: formatMessage({ id: 'timeline.local' }), icon: , path: `/accounts/${router.query.id}/local` }, { id: 'public', title: formatMessage({ id: 'timeline.public' }), icon: , path: `/accounts/${router.query.id}/public` }, { id: 'favourites', title: formatMessage({ id: 'timeline.favourites' }), icon: , path: `/accounts/${router.query.id}/favourites` }, { id: 'bookmarks', title: formatMessage({ id: 'timeline.bookmarks' }), icon: , path: `/accounts/${router.query.id}/bookmarks` } ] const reloadMenu = async () => { const res = await client.getFollowedTags() setFollowedTags(res.data) } return (
setOpenJump(false)} timelines={pages} />

{account?.username}

@{account?.domain}

{pages.concat(followRequests).map(page => ( router.push(page.path)} className="sidebar-menu-item theme-text-primary overflow-hidden whitespace-nowrap" title={page.title} > {page.icon} {page.title} {(page.id === 'notifications' && unreads[account?.id?.toString()]) || (page.id === 'follow_requests' && unreads[`${account.id.toString()}_follow_requests`]) ? ( ) : null} ))} {lists.map(list => ( router.push({ pathname: `/accounts/${router.query.id}/list_${list.id}` })} className="sidebar-menu-item theme-text-primary overflow-hidden whitespace-nowrap" title={list.title} >
{list.title}
))} {followedTags.map(tag => ( router.push({ pathname: `/accounts/${router.query.id}/tag_${tag.name}` })} className="sidebar-menu-item theme-text-primary overflow-hidden whitespace-nowrap" title={tag.name} >
{tag.name}
))}
{children}
) }