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'
|
2024-03-09 07:00:27 +01:00
|
|
|
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'
|
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'
|
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
|
|
|
|
2024-03-09 07:00:27 +01:00
|
|
|
const [modalState, dispatch] = useReducer(modalReducer, initialModalState)
|
|
|
|
|
2023-11-03 12:37:40 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (router.query.id) {
|
2023-12-05 17:02:10 +01:00
|
|
|
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])
|
|
|
|
|
2023-12-05 16:53:47 +01:00
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:14:00 +01:00
|
|
|
if (!account || !client) return null
|
2024-01-14 15:30:21 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{(router.query.timeline as string) === 'notifications' ? (
|
2024-03-09 07:00:27 +01:00
|
|
|
<Notifications
|
|
|
|
account={account}
|
|
|
|
client={client}
|
|
|
|
openMedia={(media: Array<Entity.Attachment>, index: number) =>
|
|
|
|
dispatch({ target: 'media', value: true, object: media, index: index })
|
|
|
|
}
|
|
|
|
/>
|
2024-01-14 15:30:21 +01:00
|
|
|
) : (
|
2024-03-09 07:00:27 +01:00
|
|
|
<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 })
|
|
|
|
}
|
|
|
|
/>
|
2024-01-14 15:30:21 +01:00
|
|
|
)}
|
2024-03-09 07:00:27 +01:00
|
|
|
<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} />}
|
2024-01-14 15:30:21 +01:00
|
|
|
</>
|
|
|
|
)
|
2023-11-01 17:20:27 +01:00
|
|
|
}
|
2024-03-09 07:00: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
|
|
|
|
}
|
|
|
|
}
|