Whalebird-desktop-client-ma.../renderer/components/layouts/account.tsx

252 lines
8.6 KiB
TypeScript
Raw Normal View History

2023-12-06 16:10:16 +01:00
import { CSSProperties, useContext, useEffect, useRef, useState } from 'react'
2024-01-13 08:59:01 +01:00
import { FaGear, FaIdCard, FaPlus, FaTrash } from 'react-icons/fa6'
2023-11-01 17:20:27 +01:00
import { Account, db } from '@/db'
import NewAccount from '@/components/accounts/New'
2023-12-04 17:08:12 +01:00
import Settings from '@/components/Settings'
2023-11-02 15:02:57 +01:00
import { useRouter } from 'next/router'
2023-12-04 17:08:12 +01:00
import { FormattedMessage, useIntl } from 'react-intl'
import generateNotification from '@/utils/notification'
import generator, { Entity, WebSocketInterface } from 'megalodon'
2023-12-04 17:17:52 +01:00
import { Context } from '@/utils/i18n'
import { useHotkeys } from 'react-hotkeys-hook'
2024-01-28 14:17:47 +01:00
import {
Avatar,
Badge,
IconButton,
List,
ListItem,
ListItemPrefix,
Popover,
PopoverContent,
PopoverHandler
} from '@material-tailwind/react'
2024-01-13 08:59:01 +01:00
import Thirdparty from '../Thirdparty'
2024-01-28 14:17:47 +01:00
import { useUnreads } from '@/utils/unreads'
import { unreadCount } from '@/entities/marker'
2023-11-01 17:20:27 +01:00
type LayoutProps = {
children: React.ReactNode
}
export default function Layout({ children }: LayoutProps) {
const [accounts, setAccounts] = useState<Array<Account>>([])
const [openNewModal, setOpenNewModal] = useState(false)
2023-12-04 17:08:12 +01:00
const [openSettings, setOpenSettings] = useState(false)
2024-01-13 08:59:01 +01:00
const [openThirdparty, setOpenThirdparty] = useState(false)
2023-12-06 16:10:16 +01:00
const [style, setStyle] = useState<CSSProperties>({})
const [openPopover, setOpenPopover] = useState(false)
2023-12-04 17:17:52 +01:00
const { switchLang } = useContext(Context)
2023-11-02 15:02:57 +01:00
const router = useRouter()
const { formatMessage } = useIntl()
const streamings = useRef<Array<WebSocketInterface>>([])
2024-01-28 14:17:47 +01:00
const { unreads, setUnreads } = useUnreads()
2023-11-01 17:20:27 +01:00
for (let i = 1; i < 9; i++) {
2024-01-13 04:55:42 +01:00
useHotkeys(`mod+${i}`, () => {
const acct = accounts[i - 1]
if (acct && acct.id) {
router.push(`/accounts/${acct.id}`)
}
})
}
2023-11-01 17:20:27 +01:00
useEffect(() => {
2023-12-04 17:17:52 +01:00
loadSettings()
2023-11-01 17:20:27 +01:00
const fn = async () => {
const acct = await db.accounts.toArray()
setAccounts(acct)
if (acct.length === 0) {
setOpenNewModal(true)
}
acct.forEach(async account => {
// Start user streaming for notification
const client = generator(account.sns, account.url, account.access_token, 'Whalebird')
const instance = await client.getInstance()
2024-01-28 14:17:47 +01:00
const notifications = (await client.getNotifications()).data
const res = await client.getMarkers(['notifications'])
const marker = res.data as Entity.Marker
if (marker.notifications) {
const count = unreadCount(marker.notifications, notifications)
setUnreads(current =>
Object.assign({}, current, {
[account.id?.toString()]: count
})
)
}
const ws = generator(account.sns, instance.data.urls.streaming_api, account.access_token, 'Whalebird')
const socket = ws.userSocket()
streamings.current = [...streamings.current, socket]
socket.on('connect', () => {
console.log(`connect to user streaming for ${account.domain}`)
})
socket.on('notification', (notification: Entity.Notification) => {
const [title, body] = generateNotification(notification, formatMessage)
if (title.length > 0) {
new window.Notification(title, { body: body })
}
})
})
2023-11-01 17:20:27 +01:00
}
fn()
return () => {
streamings.current.forEach(streaming => {
streaming.removeAllListeners()
streaming.stop()
})
streamings.current = []
2023-12-02 03:50:42 +01:00
console.log('close user streamings')
}
2023-11-01 17:20:27 +01:00
}, [])
const closeNewModal = async () => {
const acct = await db.accounts.toArray()
setAccounts(acct)
setOpenNewModal(false)
2023-11-02 16:50:17 +01:00
if (acct.length === 0) {
setOpenNewModal(true)
} else if (!router.query.id) {
router.push(`/accounts/${acct[0].id}`)
}
2023-11-01 17:20:27 +01:00
}
2023-11-02 15:02:57 +01:00
const openAccount = (id: number) => {
router.push(`/accounts/${id}`)
}
2023-11-02 16:50:17 +01:00
const openContextMenu = (id: number) => {
document.getElementById(`${id}`).click()
}
const removeAccount = async (id: number) => {
await db.accounts.delete(id)
const acct = await db.accounts.toArray()
setAccounts(acct)
if (acct.length === 0) {
router.push('/')
setOpenNewModal(true)
}
}
2023-11-02 15:02:57 +01:00
2023-11-02 16:54:44 +01:00
const selectedClassName = (id: number) => {
if (id === parseInt(router.query.id as string)) {
return 'bg-blue-950 cursor-pointer text-center'
2023-11-02 16:54:44 +01:00
} else {
return 'cursor-pointer text-center'
2023-11-02 16:54:44 +01:00
}
}
2023-12-04 17:17:52 +01:00
const loadSettings = () => {
if (typeof localStorage !== 'undefined') {
const lang = localStorage.getItem('language')
switchLang(lang)
2023-12-06 16:10:16 +01:00
const fontSize = localStorage.getItem('fontSize')
if (parseInt(fontSize)) {
setStyle({
fontSize: `${fontSize}px`
})
}
2023-12-04 17:17:52 +01:00
}
}
2023-11-01 17:20:27 +01:00
return (
2023-12-06 16:10:16 +01:00
<div className="app flex flex-col min-h-screen" style={style}>
2023-11-01 17:20:27 +01:00
<main className="flex w-full box-border my-0 mx-auto min-h-screen">
2023-12-04 17:08:12 +01:00
<aside className="w-16 bg-gray-900 flex flex-col justify-between">
<div>
{accounts.map(account => (
<div key={account.id} className={selectedClassName(account.id)}>
<Popover>
<PopoverHandler>
<span id={`${account.id}`} />
</PopoverHandler>
<PopoverContent>
<List className="py-2 px-0">
<ListItem onClick={() => removeAccount(account.id)} className="py-2 px-4 rounded-none">
<ListItemPrefix>
<FaTrash />
</ListItemPrefix>
<FormattedMessage id="accounts.remove" />
</ListItem>
</List>
</PopoverContent>
</Popover>
2024-01-28 14:17:47 +01:00
<Badge className="mt-1" color="green" invisible={!(unreads[account.id.toString()] > 0)}>
<Avatar
alt={account.domain}
src={account.avatar}
title={`${account.username}@${account.domain}`}
aria-label={`${account.username}@${account.domain}`}
className="p-1"
onClick={() => openAccount(account.id)}
onContextMenu={() => openContextMenu(account.id)}
/>
</Badge>
2023-12-04 17:08:12 +01:00
</div>
))}
<div className="flex flex-col items-center">
2024-01-23 14:16:03 +01:00
<IconButton
variant="text"
size="lg"
onClick={() => setOpenNewModal(true)}
title={formatMessage({ id: 'accounts.new.title' })}
aria-label={formatMessage({ id: 'accounts.new.title' })}
>
<FaPlus className="text-gray-400 text-xl" />
</IconButton>
</div>
2023-12-04 17:08:12 +01:00
<NewAccount opened={openNewModal} close={closeNewModal} />
</div>
<div className="settings text-gray-400 flex flex-col items-center mb-2">
<Popover open={openPopover} handler={setOpenPopover}>
<PopoverHandler>
2024-01-23 14:16:03 +01:00
<IconButton
variant="text"
size="lg"
title={formatMessage({ id: 'settings.title' })}
aria-label={formatMessage({ id: 'settings.title' })}
>
<FaGear className="text-gray-400 text-xl" />
</IconButton>
</PopoverHandler>
<PopoverContent>
<List className="py-2 px-0">
<ListItem
onClick={() => {
setOpenSettings(true)
setOpenPopover(false)
}}
className="py-2 px-4 rounded-none"
>
<ListItemPrefix>
<FaGear />
</ListItemPrefix>
<FormattedMessage id="settings.title" />
</ListItem>
2024-01-13 08:59:01 +01:00
<ListItem
onClick={() => {
setOpenThirdparty(true)
setOpenPopover(false)
}}
className="py-2 px-4 rounded-none"
>
<ListItemPrefix>
<FaIdCard />
</ListItemPrefix>
<FormattedMessage id="thirdparty.title" />
</ListItem>
</List>
</PopoverContent>
</Popover>
2023-12-04 17:08:12 +01:00
</div>
2023-11-01 17:20:27 +01:00
</aside>
{children}
2023-12-04 17:17:52 +01:00
<Settings opened={openSettings} close={() => setOpenSettings(false)} reloadSettings={loadSettings} />
2024-01-13 08:59:01 +01:00
<Thirdparty opened={openThirdparty} close={() => setOpenThirdparty(false)} />
2023-11-01 17:20:27 +01:00
</main>
</div>
)
}