-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a really basic consent modal, that is only closeable by agreeing to the configured terms and conditions. Users are prompted when first visiting Tobira. The consent is saved in local storage, meaning it is done on a per-device basis rather than per-user. It gets away with using a simple hashing function since it does not contain any sensible information. Users are re-prompted once anything in the T&Cs changes.
- Loading branch information
Showing
3 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useRef } from "react"; | ||
import CONFIG from "../config"; | ||
import { currentRef, useTranslatedConfig } from "../util"; | ||
import { Modal, ModalHandle } from "./Modal"; | ||
import { Button } from "./Button"; | ||
import { TextBlock } from "./Blocks/Text"; | ||
|
||
|
||
export const InitialConsent: React.FC = () => { | ||
const modalRef = useRef<ModalHandle>(null); | ||
const userConsent = localStorage.getItem("userConsent") ?? ""; | ||
const { title, text, button } = CONFIG.initialConsent; | ||
|
||
// Since this doesn't store any critical information, a simple, insecure hash | ||
// should suffice. | ||
// Source: https://gist.github.com/jlevy/c246006675becc446360a798e2b2d781 | ||
const simpleHash = (str: string) => { | ||
let hash = 0; | ||
for (let i = 0; i < str.length; i++) { | ||
hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0; | ||
} | ||
return (hash >>> 0).toString(36); | ||
}; | ||
|
||
const hash = simpleHash(CONFIG.initialConsent.toString()); | ||
|
||
return userConsent !== hash ? ( | ||
<Modal | ||
open | ||
ref={modalRef} | ||
title={useTranslatedConfig(title)} | ||
closable={false} | ||
> | ||
<TextBlock content={useTranslatedConfig(text)} /> | ||
<Button autoFocus css={{ marginTop: 20 }} onClick={() => { | ||
localStorage.setItem("userConsent", hash); | ||
currentRef(modalRef).close?.(); | ||
}}>{useTranslatedConfig(button)}</Button> | ||
</Modal> | ||
) : null; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters