|
| 1 | +import { useRef } from "react"; |
| 2 | +import CONFIG from "../config"; |
| 3 | +import { currentRef, useTranslatedConfig } from "../util"; |
| 4 | +import { Modal, ModalHandle } from "./Modal"; |
| 5 | +import { Button } from "./Button"; |
| 6 | +import { TextBlock } from "./Blocks/Text"; |
| 7 | + |
| 8 | + |
| 9 | +export const InitialConsent: React.FC = () => { |
| 10 | + const modalRef = useRef<ModalHandle>(null); |
| 11 | + const userConsent = localStorage.getItem("userConsent") ?? ""; |
| 12 | + const { title, text, button } = CONFIG.initialConsent; |
| 13 | + |
| 14 | + // Since this doesn't store any critical information, a simple, insecure hash |
| 15 | + // should suffice. |
| 16 | + // Source: https://gist.github.com/jlevy/c246006675becc446360a798e2b2d781 |
| 17 | + const simpleHash = (str: string) => { |
| 18 | + let hash = 0; |
| 19 | + for (let i = 0; i < str.length; i++) { |
| 20 | + hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0; |
| 21 | + } |
| 22 | + return (hash >>> 0).toString(36); |
| 23 | + }; |
| 24 | + |
| 25 | + const hash = simpleHash(CONFIG.initialConsent.toString()); |
| 26 | + |
| 27 | + return userConsent !== hash ? ( |
| 28 | + <Modal |
| 29 | + open |
| 30 | + ref={modalRef} |
| 31 | + title={useTranslatedConfig(title)} |
| 32 | + closable={false} |
| 33 | + > |
| 34 | + <TextBlock content={useTranslatedConfig(text)} /> |
| 35 | + <Button autoFocus css={{ marginTop: 20 }} onClick={() => { |
| 36 | + localStorage.setItem("userConsent", hash); |
| 37 | + currentRef(modalRef).close?.(); |
| 38 | + }}>{useTranslatedConfig(button)}</Button> |
| 39 | + </Modal> |
| 40 | + ) : null; |
| 41 | +}; |
| 42 | + |
0 commit comments