1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2024-12-25 00:10:59 +01:00
Whalebird-desktop-client-ma.../renderer/components/layouts/account.tsx

243 lines
8.0 KiB
TypeScript
Raw Normal View History

2024-01-29 16:45:49 +01:00
import { CSSProperties, useContext, useEffect, 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 { Context } from '@/provider/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'
import { useUnreads } from '@/provider/unreads'
import { useAccounts } from '@/provider/accounts'
2024-09-09 18:43:49 +02:00
import { invoke } from '@/utils/invoke'
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)
2024-03-09 11:09:36 +01:00
const [theme, setTheme] = useState('theme-blue')
2024-03-12 15:02:18 +01:00
const [isDark, setIsDark] = 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 { unreads } = useUnreads()
const { addAccount, removeAccount, removeAll } = useAccounts()
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 => {
addAccount(account)
})
2023-11-01 17:20:27 +01:00
}
fn()
return () => {
removeAll()
}
2023-11-01 17:20:27 +01:00
}, [])
2024-03-12 15:02:18 +01:00
useEffect(() => {
console.log('isDark', isDark)
document.body.className = isDark ? 'dark' : 'light'
}, [isDark])
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 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-03-09 10:26:43 +01:00
return 'theme-bg 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`
})
}
2024-03-09 11:09:36 +01:00
const t = localStorage.getItem('theme')
if (t && t.length > 0) {
setTheme(t)
}
2024-03-12 15:02:18 +01:00
const dark = localStorage.getItem('color-mode')
if (dark && dark === 'dark') {
setIsDark(true)
} else {
setIsDark(false)
}
2024-09-09 18:43:49 +02:00
const proxyMode = localStorage.getItem('proxyMode')
const proxyProtocol = localStorage.getItem('proxyProtocol')
const proxyHost = localStorage.getItem('proxyHost')
const proxyPort = localStorage.getItem('proxyPort')
invoke('set-proxy', { mode: proxyMode, protocol: proxyProtocol, host: proxyHost, port: proxyPort })
2023-12-04 17:17:52 +01:00
}
}
2023-11-01 17:20:27 +01:00
return (
2024-03-09 11:09:36 +01:00
<div className={`app flex flex-col min-h-screen ${theme}`} style={style}>
2024-03-12 15:02:18 +01:00
<main className="flex w-full box-border my-0 mx-auto min-h-screen bg-white dark:bg-gray-900">
2024-03-09 10:26:43 +01:00
<aside className="w-16 theme-account-bg flex flex-col justify-between">
2023-12-04 17:08:12 +01:00
<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={() => deleteAccount(account)} 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>
)
}