|
| 1 | +import React from 'react'; |
| 2 | +import { observer } from 'mobx-react-lite'; |
| 3 | +import { useStore } from '@tdev-hooks/useStore'; |
| 4 | +import { MetaInit, ModelMeta } from '../model/ModelMeta'; |
| 5 | +import { useFirstMainDocument } from '@tdev-hooks/useFirstMainDocument'; |
| 6 | +import SlideButton from '@tdev-components/shared/SlideButton'; |
| 7 | +import Badge from '@tdev-components/shared/Badge'; |
| 8 | +import styles from './styles.module.scss'; |
| 9 | +import clsx from 'clsx'; |
| 10 | +import { mdiFlashTriangle } from '@mdi/js'; |
| 11 | +import Icon from '@mdi/react'; |
| 12 | +import PageReadChecker from '../model'; |
| 13 | + |
| 14 | +interface Props extends MetaInit { |
| 15 | + id: string; |
| 16 | + text?: (unlocked: boolean, doc: PageReadChecker) => string; |
| 17 | + disabledReason?: (doc: PageReadChecker) => string; |
| 18 | + hideTime?: boolean; |
| 19 | + hideWarning?: boolean; |
| 20 | + continueAfterUnlock?: boolean; |
| 21 | +} |
| 22 | + |
| 23 | +const defaultText = (unlocked: boolean, doc: PageReadChecker) => |
| 24 | + unlocked ? `Gelesen ${doc.fReadTime}` : doc.fReadTime; |
| 25 | +const defaultDisabledReason = (doc: PageReadChecker) => |
| 26 | + `Mindestens ${doc.meta.fMinReadTime} lesen, um zu entsperren`; |
| 27 | + |
| 28 | +const PageReadCheck = observer((props: Props) => { |
| 29 | + const { text = defaultText, disabledReason = defaultDisabledReason } = props; |
| 30 | + const [meta] = React.useState(new ModelMeta(props)); |
| 31 | + const ref = React.useRef<HTMLDivElement>(null); |
| 32 | + |
| 33 | + const viewStore = useStore('viewStore'); |
| 34 | + const doc = useFirstMainDocument(props.id, meta); |
| 35 | + const [animate, setAnimate] = React.useState(false); |
| 36 | + |
| 37 | + React.useEffect(() => { |
| 38 | + if (!viewStore.isPageVisible || !doc) { |
| 39 | + return; |
| 40 | + } |
| 41 | + if (doc.read && !props.continueAfterUnlock) { |
| 42 | + return; |
| 43 | + } |
| 44 | + const id = setInterval(() => { |
| 45 | + doc.incrementReadTime(1); |
| 46 | + }, 1000); |
| 47 | + return () => { |
| 48 | + clearInterval(id); |
| 49 | + }; |
| 50 | + }, [doc, doc?.read, viewStore.isPageVisible, props.continueAfterUnlock]); |
| 51 | + |
| 52 | + React.useEffect(() => { |
| 53 | + if (ref.current && doc?.scrollTo) { |
| 54 | + ref.current.scrollIntoView({ behavior: 'auto', block: 'center', inline: 'start' }); |
| 55 | + doc.setScrollTo(false); |
| 56 | + setAnimate(true); |
| 57 | + } |
| 58 | + }, [ref, doc?.scrollTo]); |
| 59 | + |
| 60 | + React.useEffect(() => { |
| 61 | + if (animate) { |
| 62 | + const timeout = setTimeout(() => { |
| 63 | + setAnimate(false); |
| 64 | + }, 2000); |
| 65 | + return () => { |
| 66 | + clearTimeout(timeout); |
| 67 | + }; |
| 68 | + } |
| 69 | + }, [animate]); |
| 70 | + |
| 71 | + if (!doc) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <div className={clsx(styles.pageReadCheck, animate && styles.animate)} ref={ref}> |
| 77 | + <SlideButton |
| 78 | + text={(unlocked) => text(unlocked, doc)} |
| 79 | + onUnlock={() => doc.setReadState(true)} |
| 80 | + onReset={() => doc.setReadState(false)} |
| 81 | + isUnlocked={doc.read} |
| 82 | + disabled={!doc.canUnlock} |
| 83 | + sliderWidth={320} |
| 84 | + disabledReason={disabledReason(doc)} |
| 85 | + /> |
| 86 | + <div className={clsx(styles.status)}> |
| 87 | + {!doc.canUnlock && ( |
| 88 | + <Badge title={disabledReason(doc)} className={clsx(styles.minReadTime)}> |
| 89 | + {doc.meta.fMinReadTime} |
| 90 | + </Badge> |
| 91 | + )} |
| 92 | + {doc.isDummy && !props.hideWarning && ( |
| 93 | + <Icon |
| 94 | + path={mdiFlashTriangle} |
| 95 | + size={0.7} |
| 96 | + color="var(--ifm-color-warning)" |
| 97 | + title="Wird nicht gespeichert." |
| 98 | + /> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + ); |
| 103 | +}); |
| 104 | + |
| 105 | +export default PageReadCheck; |
0 commit comments