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

180 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-12-06 16:10:16 +01:00
import { CSSProperties, useContext, useEffect, useRef, useState } from 'react'
2023-12-04 17:08:12 +01:00
import { FaGear, FaPlus } 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 16:50:17 +01:00
import { Avatar, Dropdown } from 'flowbite-react'
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'
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)
2023-12-06 16:10:16 +01:00
const [style, setStyle] = useState<CSSProperties>({})
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>>([])
2023-11-01 17:20:27 +01:00
for (let i = 1; i < 9; i++) {
useHotkeys(`ctrl+${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()
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 dropdownTrigger = (accountId: number) => <span id={`${accountId}`} className="" />
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'
} else {
return 'cursor-pointer'
}
}
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)}>
<Avatar
alt={account.domain}
img={account.avatar}
rounded
key={account.id}
className="py-2"
onClick={() => openAccount(account.id)}
onContextMenu={() => openContextMenu(account.id)}
/>
<Dropdown label="" dismissOnClick={true} renderTrigger={() => dropdownTrigger(account.id)}>
<Dropdown.Item onClick={() => removeAccount(account.id)}>
<FormattedMessage id="accounts.remove" />
</Dropdown.Item>
</Dropdown>
</div>
))}
<button className="py-4 px-6 items-center" onClick={() => setOpenNewModal(true)}>
<FaPlus className="text-gray-400" />
</button>
<NewAccount opened={openNewModal} close={closeNewModal} />
</div>
<div className="settings text-gray-400 py-4 px-6 items-center">
2023-12-04 17:17:52 +01:00
<div className="relative cursor-pointer">
2023-12-04 17:08:12 +01:00
<Dropdown
label=""
dismissOnClick
renderTrigger={() => (
<span>
<FaGear />
</span>
)}
placement="right-start"
>
<Dropdown.Item onClick={() => setOpenSettings(true)}>
<FormattedMessage id="settings.title" />{' '}
</Dropdown.Item>
2023-11-02 16:50:17 +01:00
</Dropdown>
</div>
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} />
2023-11-01 17:20:27 +01:00
</main>
</div>
)
}