63 lines
No EOL
1.7 KiB
TypeScript
63 lines
No EOL
1.7 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
import { usePathname } from 'next/navigation'
|
|
import { add, format } from 'date-fns'
|
|
import { getCookie } from 'cookies-next'
|
|
|
|
import { appState } from '~/utils/appState'
|
|
import UpdateToast from '~/components/toasts/UpdateToast'
|
|
|
|
export default function UpdateToastClient() {
|
|
const pathname = usePathname()
|
|
const [updateToastOpen, setUpdateToastOpen] = useState(false)
|
|
|
|
useEffect(() => {
|
|
if (appState.version) {
|
|
const cookie = getToastCookie()
|
|
const now = new Date()
|
|
const updatedAt = new Date(appState.version.updated_at)
|
|
const validUntil = add(updatedAt, { days: 7 })
|
|
|
|
if (now < validUntil && !cookie.seen) setUpdateToastOpen(true)
|
|
}
|
|
}, [])
|
|
|
|
function getToastCookie() {
|
|
if (appState.version && appState.version.updated_at !== '') {
|
|
const updatedAt = new Date(appState.version.updated_at)
|
|
const cookieValues = getCookie(
|
|
`update-${format(updatedAt, 'yyyy-MM-dd')}`
|
|
)
|
|
return cookieValues
|
|
? (JSON.parse(cookieValues as string) as { seen: true })
|
|
: { seen: false }
|
|
} else {
|
|
return { seen: false }
|
|
}
|
|
}
|
|
|
|
function handleToastActionClicked() {
|
|
setUpdateToastOpen(false)
|
|
}
|
|
|
|
function handleToastClosed() {
|
|
setUpdateToastOpen(false)
|
|
}
|
|
|
|
const path = pathname.replaceAll('/', '')
|
|
|
|
if (!['about', 'updates', 'roadmap'].includes(path) && appState.version) {
|
|
return (
|
|
<UpdateToast
|
|
open={updateToastOpen}
|
|
updateType={appState.version.update_type}
|
|
onActionClicked={handleToastActionClicked}
|
|
onCloseClicked={handleToastClosed}
|
|
lastUpdated={appState.version.updated_at}
|
|
/>
|
|
)
|
|
}
|
|
|
|
return null
|
|
} |