Whalebird-desktop-client-ma.../renderer/pages/accounts/[id]/[timeline].tsx

147 lines
4.4 KiB
TypeScript
Raw Normal View History

2023-11-01 17:20:27 +01:00
import { useRouter } from 'next/router'
2023-11-03 12:37:40 +01:00
import Timeline from '@/components/timelines/Timeline'
import { useEffect, useReducer, useState } from 'react'
2023-11-03 12:37:40 +01:00
import { Account, db } from '@/db'
2023-12-02 11:26:45 +01:00
import generator, { Entity, MegalodonInterface } from 'megalodon'
2023-11-04 10:14:00 +01:00
import Notifications from '@/components/timelines/Notifications'
import Search from '@/components/timelines/Search'
2023-12-02 11:26:45 +01:00
import Media from '@/components/Media'
2023-12-12 16:49:00 +01:00
import Report from '@/components/report/Report'
2024-10-12 07:46:04 +02:00
import FollowRequests from '@/components/timelines/FollowRequests'
2023-11-01 17:20:27 +01:00
2023-11-03 12:37:40 +01:00
export default function Page() {
2023-11-01 17:20:27 +01:00
const router = useRouter()
2023-11-03 12:37:40 +01:00
const [account, setAccount] = useState<Account | null>(null)
const [client, setClient] = useState<MegalodonInterface>(null)
2023-12-12 16:49:00 +01:00
const [report, setReport] = useState<Entity.Status | null>(null)
2023-11-03 12:37:40 +01:00
const [modalState, dispatch] = useReducer(modalReducer, initialModalState)
2023-11-03 12:37:40 +01:00
useEffect(() => {
if (router.query.id) {
if (router.query.id && typeof localStorage !== 'undefined') {
localStorage.setItem('lastAccount', router.query.id as string)
}
2023-11-03 12:37:40 +01:00
const f = async () => {
const a = await db.accounts.get(parseInt(router.query.id as string))
if (a) {
setAccount(a)
const c = generator(a.sns, a.url, a.access_token, 'Whalebird')
setClient(c)
}
}
f()
}
}, [router.query.id])
useEffect(() => {
if (router.query.timeline && router.query.id && typeof localStorage !== 'undefined') {
localStorage.setItem(`${router.query.id}_lastTimeline`, router.query.timeline as string)
}
}, [router.query.id, router.query.timeline])
2023-12-12 16:49:00 +01:00
useEffect(() => {
if (router.query.modal && router.query.report_target_id) {
const f = async () => {
const res = await client.getStatus(router.query.report_target_id as string)
setReport(res.data)
}
f()
}
}, [router.query.modal, router.query.report_target_id])
2024-01-14 16:39:42 +01:00
const closeReport = () => {
setReport(null)
router.push({
query: Object.assign({}, router.query, {
report_target_id: null
})
})
}
2024-10-12 07:46:04 +02:00
const timeline = (timeline: string) => {
switch (timeline) {
case 'notifications':
return (
<Notifications
account={account}
client={client}
openMedia={(media: Array<Entity.Attachment>, index: number) =>
dispatch({ target: 'media', value: true, object: media, index: index })
}
/>
)
case 'search':
return (
<Search
client={client}
account={account}
openMedia={(media: Array<Entity.Attachment>, index: number) =>
dispatch({ target: 'media', value: true, object: media, index: index })
}
/>
)
case 'follow_requests':
return (
<FollowRequests
client={client}
account={account}
openMedia={(media: Array<Entity.Attachment>, index: number) =>
dispatch({ target: 'media', value: true, object: media, index: index })
}
/>
)
default:
return (
<Timeline
timeline={router.query.timeline as string}
account={account}
client={client}
openMedia={(media: Array<Entity.Attachment>, index: number) =>
dispatch({ target: 'media', value: true, object: media, index: index })
}
/>
)
}
}
2023-11-04 10:14:00 +01:00
if (!account || !client) return null
return (
<>
2024-10-12 07:46:04 +02:00
{timeline(router.query.timeline as string)}
<Media
open={modalState.media.opened}
close={() => dispatch({ target: 'media', value: false, object: [], index: -1 })}
media={modalState.media.object}
index={modalState.media.index}
/>
2024-01-14 16:39:42 +01:00
{report && <Report open={report !== null} close={closeReport} status={report} client={client} />}
</>
)
2023-11-01 17:20:27 +01:00
}
type ModalState = {
media: {
opened: boolean
object: Array<Entity.Attachment>
index: number
}
}
const initialModalState: ModalState = {
media: {
opened: false,
object: [],
index: 0
}
}
const modalReducer = (current: ModalState, action: { target: string; value: boolean; object?: any; index?: number }): ModalState => {
switch (action.target) {
case 'media':
return { ...current, media: { opened: action.value, object: action.object, index: action.index } }
default:
return current
}
}