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'
|
2024-01-29 16:10:44 +01:00
|
|
|
import { Context } from '@/provider/i18n'
|
2023-12-21 16:35:54 +01:00
|
|
|
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-29 16:10:44 +01:00
|
|
|
import { useUnreads } from '@/provider/unreads'
|
|
|
|
import { useAccounts } from '@/provider/accounts'
|
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>({})
|
2024-01-09 13:30:05 +01:00
|
|
|
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()
|
2023-12-01 15:06:16 +01:00
|
|
|
const { formatMessage } = useIntl()
|
2024-01-29 16:10:44 +01:00
|
|
|
const { unreads } = useUnreads()
|
|
|
|
const { addAccount, removeAccount, removeAll } = useAccounts()
|
2023-11-01 17:20:27 +01:00
|
|
|
|
2023-12-21 16:35:54 +01:00
|
|
|
for (let i = 1; i < 9; i++) {
|
2024-01-13 04:55:42 +01:00
|
|
|
useHotkeys(`mod+${i}`, () => {
|
2023-12-21 16:35:54 +01:00
|
|
|
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)
|
|
|
|
}
|
2023-12-01 15:06:16 +01:00
|
|
|
acct.forEach(async account => {
|
2024-01-29 16:10:44 +01:00
|
|
|
addAccount(account)
|
2023-12-01 15:06:16 +01:00
|
|
|
})
|
2023-11-01 17:20:27 +01:00
|
|
|
}
|
|
|
|
fn()
|
2023-12-01 15:06:16 +01:00
|
|
|
|
|
|
|
return () => {
|
2024-01-29 16:10:44 +01:00
|
|
|
removeAll()
|
2023-12-01 15:06:16 +01:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:10:44 +01:00
|
|
|
const deleteAccount = async (account: Account) => {
|
|
|
|
removeAccount(account)
|
|
|
|
await db.accounts.delete(account.id)
|
2023-11-02 16:50:17 +01:00
|
|
|
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)) {
|
2024-01-09 13:30:05 +01:00
|
|
|
return 'bg-blue-950 cursor-pointer text-center'
|
2023-11-02 16:54:44 +01:00
|
|
|
} else {
|
2024-01-09 13:30:05 +01:00
|
|
|
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)}>
|
2024-01-09 13:30:05 +01:00
|
|
|
<Popover>
|
|
|
|
<PopoverHandler>
|
|
|
|
<span id={`${account.id}`} />
|
|
|
|
</PopoverHandler>
|
|
|
|
<PopoverContent>
|
|
|
|
<List className="py-2 px-0">
|
2024-01-29 16:10:44 +01:00
|
|
|
<ListItem onClick={() => deleteAccount(account)} className="py-2 px-4 rounded-none">
|
2024-01-09 13:30:05 +01:00
|
|
|
<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>
|
|
|
|
))}
|
2024-01-09 13:30:05 +01:00
|
|
|
<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' })}
|
|
|
|
>
|
2024-01-09 13:30:05 +01:00
|
|
|
<FaPlus className="text-gray-400 text-xl" />
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
2023-12-04 17:08:12 +01:00
|
|
|
<NewAccount opened={openNewModal} close={closeNewModal} />
|
|
|
|
</div>
|
2024-01-09 13:30:05 +01:00
|
|
|
<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' })}
|
|
|
|
>
|
2024-01-09 13:30:05 +01:00
|
|
|
<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>
|
2024-01-09 13:30:05 +01:00
|
|
|
</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>
|
|
|
|
)
|
|
|
|
}
|