From 5bf2373348f6b53136bb0fe435080946df168f3a Mon Sep 17 00:00:00 2001 From: Spencer Spenst Date: Wed, 4 Oct 2023 14:34:54 -0700 Subject: [PATCH] more accurate preventKeyDownEvent --- components/level/gameLayout.tsx | 24 +++++++++++++------- components/level/gameWrapper.tsx | 16 +++++++------ components/level/reviews/formattedReview.tsx | 11 +++++++-- components/level/reviews/reviewDropdown.tsx | 21 +++++++++++------ components/level/reviews/reviewForm.tsx | 1 + 5 files changed, 49 insertions(+), 24 deletions(-) diff --git a/components/level/gameLayout.tsx b/components/level/gameLayout.tsx index 51c9e711d..5af8d5d8a 100644 --- a/components/level/gameLayout.tsx +++ b/components/level/gameLayout.tsx @@ -45,10 +45,6 @@ export default function GameLayout({ controls, disableCheckpoints, gameState, hi setShowHeader(!fullScreen); }, [fullScreen, setShowHeader]); - useEffect(() => { - setPreventKeyDownEvent(isCheckpointOpen || isLevelInfoOpen); - }, [isCheckpointOpen, isLevelInfoOpen, setPreventKeyDownEvent]); - return (
setIsLevelInfoOpen(false)} + closeModal={() => { + setIsLevelInfoOpen(false); + setPreventKeyDownEvent(false); + }} isOpen={isLevelInfoOpen} level={level} /> @@ -104,7 +106,10 @@ export default function GameLayout({ controls, disableCheckpoints, gameState, hi data-tooltip-content='Checkpoints' data-tooltip-id='checkpoint-tooltip' id='checkpointBtn' - onClick={() => setIsCheckpointOpen(!isCheckpointOpen)} + onClick={() => { + setIsCheckpointOpen(!isCheckpointOpen); + setPreventKeyDownEvent(true); + }} > @@ -112,7 +117,10 @@ export default function GameLayout({ controls, disableCheckpoints, gameState, hi setIsCheckpointOpen(false)} + closeModal={() => { + setIsCheckpointOpen(false); + setPreventKeyDownEvent(true); + }} isOpen={isCheckpointOpen} /> diff --git a/components/level/gameWrapper.tsx b/components/level/gameWrapper.tsx index 191d0827b..849c0e350 100644 --- a/components/level/gameWrapper.tsx +++ b/components/level/gameWrapper.tsx @@ -1,5 +1,5 @@ import { PageContext } from '@root/contexts/pageContext'; -import React, { useContext, useEffect, useState } from 'react'; +import React, { useContext, useState } from 'react'; import Collection from '../../models/db/collection'; import { EnrichedLevel } from '../../models/db/level'; import User from '../../models/db/user'; @@ -19,10 +19,6 @@ export default function GameWrapper({ chapter, collection, level, onNext, onPrev const [postGameModalOpen, setShowPostGameModalOpen] = useState(false); const { setPreventKeyDownEvent } = useContext(PageContext); - useEffect(() => { - setPreventKeyDownEvent(postGameModalOpen); - }, [postGameModalOpen, setPreventKeyDownEvent]); - return ( <> setTimeout(() => setShowPostGameModalOpen(true), 200)} + onSolve={() => setTimeout(() => { + setShowPostGameModalOpen(true); + setPreventKeyDownEvent(true); + }, 200)} /> setShowPostGameModalOpen(false)} + closeModal={() => { + setShowPostGameModalOpen(false); + setPreventKeyDownEvent(false); + }} collection={collection} isOpen={postGameModalOpen} level={level} diff --git a/components/level/reviews/formattedReview.tsx b/components/level/reviews/formattedReview.tsx index b85205351..69a52d561 100644 --- a/components/level/reviews/formattedReview.tsx +++ b/components/level/reviews/formattedReview.tsx @@ -67,13 +67,14 @@ export function Stars({ stars }: StarsProps) { interface FormattedReviewProps { hideBorder?: boolean; + inModal?: boolean; level?: EnrichedLevel; onEditClick?: () => void; review: ReviewWithStats; user: User | undefined; } -export default function FormattedReview({ hideBorder, level, onEditClick, review, user }: FormattedReviewProps) { +export default function FormattedReview({ hideBorder, inModal, level, onEditClick, review, user }: FormattedReviewProps) { return (
- {onEditClick && user && } + {onEditClick && user && ( + + )}
{level && } diff --git a/components/level/reviews/reviewDropdown.tsx b/components/level/reviews/reviewDropdown.tsx index 60740e386..29a62abb2 100644 --- a/components/level/reviews/reviewDropdown.tsx +++ b/components/level/reviews/reviewDropdown.tsx @@ -4,15 +4,16 @@ import { LevelContext } from '@root/contexts/levelContext'; import { PageContext } from '@root/contexts/pageContext'; import isCurator from '@root/helpers/isCurator'; import classNames from 'classnames'; -import React, { Fragment, useContext, useEffect, useState } from 'react'; +import React, { Fragment, useContext, useState } from 'react'; import DeleteReviewModal from '../../modal/deleteReviewModal'; interface ReviewDropdownProps { + inModal?: boolean; onEditClick: () => void; userId: string; } -export default function ReviewDropdown({ onEditClick, userId }: ReviewDropdownProps) { +export default function ReviewDropdown({ inModal, onEditClick, userId }: ReviewDropdownProps) { const [isDeleteReviewOpen, setIsDeleteReviewOpen] = useState(false); const levelContext = useContext(LevelContext); const { setPreventKeyDownEvent } = useContext(PageContext); @@ -21,10 +22,6 @@ export default function ReviewDropdown({ onEditClick, userId }: ReviewDropdownPr const canEdit = userId === user?._id.toString() || isCurator(user); const isNotAuthor = user?._id.toString() !== userId; - useEffect(() => { - setPreventKeyDownEvent(isDeleteReviewOpen); - }, [isDeleteReviewOpen, setPreventKeyDownEvent]); - if (!canEdit) { return null; } @@ -71,7 +68,13 @@ export default function ReviewDropdown({ onEditClick, userId }: ReviewDropdownPr {({ active }) => (
setIsDeleteReviewOpen(true)} + onClick={() => { + setIsDeleteReviewOpen(true); + + if (!inModal) { + setPreventKeyDownEvent(true); + } + }} style={{ backgroundColor: active ? 'var(--bg-color-3)' : undefined, }} @@ -91,6 +94,10 @@ export default function ReviewDropdown({ onEditClick, userId }: ReviewDropdownPr closeModal={() => { setIsDeleteReviewOpen(false); levelContext?.getReviews(); + + if (!inModal) { + setPreventKeyDownEvent(false); + } }} isOpen={isDeleteReviewOpen} userId={userId} diff --git a/components/level/reviews/reviewForm.tsx b/components/level/reviews/reviewForm.tsx index a4736c5a1..fd1ead61e 100644 --- a/components/level/reviews/reviewForm.tsx +++ b/components/level/reviews/reviewForm.tsx @@ -74,6 +74,7 @@ export default function ReviewForm({ inModal, review }: ReviewFormProps) { <> setIsEditing(true)} review={review}