2024-11-29 14:05:10 +01:00
|
|
|
import { Trans } from "@lingui/react/macro"
|
2023-08-01 16:11:04 +02:00
|
|
|
import { Box, Dialog, Text } from "@mantine/core"
|
|
|
|
|
import { useAsync } from "react-async-hook"
|
2025-06-27 16:29:31 +02:00
|
|
|
import { useAppDispatch, useAppSelector } from "@/app/store"
|
|
|
|
|
import { setAnnouncementHash } from "@/app/user/slice"
|
|
|
|
|
import { Content } from "@/components/content/Content"
|
2023-08-01 16:11:04 +02:00
|
|
|
|
|
|
|
|
const sha256Hex = async (input: string | undefined) => {
|
|
|
|
|
const data = new TextEncoder().encode(input)
|
|
|
|
|
const buffer = await crypto.subtle.digest("SHA-256", data)
|
|
|
|
|
const array = Array.from(new Uint8Array(buffer))
|
|
|
|
|
return array.map(b => b.toString(16).padStart(2, "0")).join("")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function AnnouncementDialog() {
|
|
|
|
|
const announcement = useAppSelector(state => state.server.serverInfos?.announcement)
|
|
|
|
|
const announcementHash = useAsync(sha256Hex, [announcement]).result
|
2024-09-10 18:11:16 +02:00
|
|
|
const existingAnnouncementHash = useAppSelector(state => state.user.localSettings.announcementHash)
|
|
|
|
|
const dispatch = useAppDispatch()
|
2023-08-01 16:11:04 +02:00
|
|
|
|
2024-09-10 18:11:16 +02:00
|
|
|
const opened = !!announcementHash && announcementHash !== existingAnnouncementHash
|
|
|
|
|
const onClosed = () => announcementHash && dispatch(setAnnouncementHash(announcementHash))
|
2023-08-01 16:11:04 +02:00
|
|
|
|
|
|
|
|
if (!announcement) return null
|
|
|
|
|
return (
|
|
|
|
|
<Dialog opened={opened} withCloseButton onClose={onClosed} size="xl" radius="md">
|
|
|
|
|
<Box>
|
2023-12-29 23:09:30 +01:00
|
|
|
<Text fw="bold">
|
2023-08-01 16:11:04 +02:00
|
|
|
<Trans>Announcement</Trans>
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box>
|
|
|
|
|
<Content content={announcement} />
|
|
|
|
|
</Box>
|
|
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|