import { CSSProperties, useContext, useEffect, useState } from 'react' import { FaGear, FaIdCard, FaPlus, FaTrash } from 'react-icons/fa6' import { Account, db } from '@/db' import NewAccount from '@/components/accounts/New' import Settings from '@/components/Settings' import { useRouter } from 'next/router' import { FormattedMessage, useIntl } from 'react-intl' import { Context } from '@/provider/i18n' import { useHotkeys } from 'react-hotkeys-hook' import { Avatar, Badge, IconButton, List, ListItem, ListItemPrefix, Popover, PopoverContent, PopoverHandler } from '@material-tailwind/react' import Thirdparty from '../Thirdparty' import { useUnreads } from '@/provider/unreads' import { useAccounts } from '@/provider/accounts' type LayoutProps = { children: React.ReactNode } export default function Layout({ children }: LayoutProps) { const [accounts, setAccounts] = useState>([]) const [openNewModal, setOpenNewModal] = useState(false) const [openSettings, setOpenSettings] = useState(false) const [openThirdparty, setOpenThirdparty] = useState(false) const [style, setStyle] = useState({}) const [openPopover, setOpenPopover] = useState(false) const [theme, setTheme] = useState('theme-blue') const { switchLang } = useContext(Context) const router = useRouter() const { formatMessage } = useIntl() const { unreads } = useUnreads() const { addAccount, removeAccount, removeAll } = useAccounts() for (let i = 1; i < 9; i++) { useHotkeys(`mod+${i}`, () => { const acct = accounts[i - 1] if (acct && acct.id) { router.push(`/accounts/${acct.id}`) } }) } useEffect(() => { loadSettings() const fn = async () => { const acct = await db.accounts.toArray() setAccounts(acct) if (acct.length === 0) { setOpenNewModal(true) } acct.forEach(async account => { addAccount(account) }) } fn() return () => { removeAll() } }, []) const closeNewModal = async () => { const acct = await db.accounts.toArray() setAccounts(acct) setOpenNewModal(false) if (acct.length === 0) { setOpenNewModal(true) } else if (!router.query.id) { router.push(`/accounts/${acct[0].id}`) } } const openAccount = (id: number) => { router.push(`/accounts/${id}`) } const openContextMenu = (id: number) => { document.getElementById(`${id}`).click() } const deleteAccount = async (account: Account) => { removeAccount(account) await db.accounts.delete(account.id) const acct = await db.accounts.toArray() setAccounts(acct) if (acct.length === 0) { router.push('/') setOpenNewModal(true) } } const selectedClassName = (id: number) => { if (id === parseInt(router.query.id as string)) { return 'theme-bg cursor-pointer text-center' } else { return 'cursor-pointer text-center' } } const loadSettings = () => { if (typeof localStorage !== 'undefined') { const lang = localStorage.getItem('language') switchLang(lang) const fontSize = localStorage.getItem('fontSize') if (parseInt(fontSize)) { setStyle({ fontSize: `${fontSize}px` }) } const t = localStorage.getItem('theme') if (t && t.length > 0) { setTheme(t) } } } return (
{children} setOpenSettings(false)} reloadSettings={loadSettings} /> setOpenThirdparty(false)} />
) }