|
1 | | -"use client"; |
2 | | -import { useSearchParams } from "next/navigation"; |
3 | | -import { HelpModal } from './modal-view'; |
4 | | -import "./modal.css"; |
| 1 | +"use client" |
5 | 2 |
|
6 | | -export interface ModalProps { |
7 | | - setModalOpen: (arg: boolean) => void; |
8 | | -} |
| 3 | +import { ReadonlyURLSearchParams, useRouter, useSearchParams } from "next/navigation"; |
| 4 | +import { useRef, useState } from "react"; |
| 5 | +import { HelpView, MobileHelpView } from "./views/help"; |
| 6 | +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; |
| 7 | +import { faClose } from "@fortawesome/free-solid-svg-icons"; |
| 8 | +import useKeyboard, { KeyPressAction } from "@hooks/useKeyboard"; |
| 9 | +import useMobileDetect from "@hooks/useMobileDetect"; |
| 10 | +import useQuery from "@hooks/useQuery"; |
| 11 | +import { useWindowSize } from "@uidotdev/usehooks"; |
| 12 | +import Link from "next/link"; |
| 13 | +import { CopyView } from "./views/copy"; |
| 14 | +import accounts from "@/config/accounts.json"; |
| 15 | +import "./modal.css"; |
9 | 16 |
|
10 | 17 | export default function Modal() { |
11 | 18 | const searchParams = useSearchParams(); |
12 | | - const modal = searchParams.get("modal"); |
| 19 | + const modalParam = searchParams.get("modal"); |
| 20 | + const audioRef = useRef<HTMLAudioElement | null>(null); |
| 21 | + const router = useRouter(); |
| 22 | + const size = useWindowSize(); |
| 23 | + const mobileDetect = useMobileDetect(); |
| 24 | + |
| 25 | + const [modal, setModal] = useState<string | null>(modalParam); |
| 26 | + |
| 27 | + const play = () => { |
| 28 | + if (audioRef.current) { |
| 29 | + audioRef.current.volume = 1; |
| 30 | + audioRef.current.play(); |
| 31 | + } else { |
| 32 | + // Throw error |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + function onEsc() { |
| 37 | + play(); |
| 38 | + |
| 39 | + router.push('/'); |
| 40 | + } |
| 41 | + |
| 42 | + function onPathChanged(p: string, s: ReadonlyURLSearchParams, m: string | null) { |
| 43 | + setModal(m); |
| 44 | + } |
| 45 | + |
| 46 | + useQuery({ onPathChanged: onPathChanged }); |
| 47 | + |
| 48 | + const actions = new Map<string, KeyPressAction>(); |
| 49 | + |
| 50 | + if (modal) { |
| 51 | + actions.set('escape', { repeat: false, onKeyPress: onEsc }); |
| 52 | + } |
| 53 | + |
| 54 | + useKeyboard({ actions: actions, enabledOnModal: true }); |
13 | 55 |
|
14 | 56 | let view; |
| 57 | + let title; |
15 | 58 |
|
16 | 59 | if (modal === 'help') { |
17 | | - view = <HelpModal />; |
18 | | - } |
19 | | - else if (modal === 'true') { |
20 | | - view = <HelpModal />; |
| 60 | + title = 'Help'; |
| 61 | + if (mobileDetect.isMobile()) { |
| 62 | + view = <MobileHelpView />; |
| 63 | + } else { |
| 64 | + view = <HelpView />; |
| 65 | + } |
| 66 | + } else if (modal === 'bnet') { |
| 67 | + title = 'Battle.Net'; |
| 68 | + view = <CopyView title="BattleTag:" value={accounts.battleNet.id} /> |
21 | 69 | } |
22 | 70 |
|
23 | 71 | return ( |
24 | 72 | <> |
25 | 73 | {modal && ( |
26 | | - view |
| 74 | + <div> |
| 75 | + {/* <div className="right-0 top-0 absolute w-[30px] m-5 aspect-square z-[200]"> |
| 76 | + <FontAwesomeIcon icon={faClose} className="w-full h-full" /> |
| 77 | + </div> */} |
| 78 | + <dialog className="absolute left-0 top-0 w-full h-full bg-black bg-opacity-75 z-[100] overflow-none backdrop-blur flex"> |
| 79 | + <audio ref={audioRef} src='/audio/nav.mp3' /> |
| 80 | + <div className="absolute left-0 items-end w-full h-[15%] grid grid-cols-1 content-end"> |
| 81 | + <p className="modal-title text-xl"> |
| 82 | + {title} |
| 83 | + </p> |
| 84 | + <hr className="w-full" /> |
| 85 | + </div> |
| 86 | + {view} |
| 87 | + <div className="absolute left-0 bottom-0 items-start w-full h-[15%] grid grid-cols-1 content-start"> |
| 88 | + <hr className="w-full" /> |
| 89 | + <span className="text-white mt-[1em] ml-[0.5em] text-center modal-action"> |
| 90 | + {/* need to set the href so that the user can close modal by clicking on the buttton */} |
| 91 | + <Link href={'/'}> |
| 92 | + {mobileDetect.isMobile() && ( |
| 93 | + <div className="text-xl items-center justify-items-center align-items-center inline-flex"> |
| 94 | + <FontAwesomeIcon icon={faClose} className="w-7 h-7 aspect-square inline-flex my-auto modal-action" /> |
| 95 | + <span className="mx-2 my-auto">close</span> |
| 96 | + </div> |
| 97 | + )} |
| 98 | + {mobileDetect.isDesktop() && ( |
| 99 | + <> |
| 100 | + <kbd className="px-2 py-1.5 text-xs text-gray-800 bg-gray-100 mx-[3px] border border-gray-200 rounded-lg dark:bg-gray-400/25 dark:text-white dark:border-gray-500/25">Esc</kbd> close |
| 101 | + </> |
| 102 | + )} |
| 103 | + |
| 104 | + </Link> |
| 105 | + </span> |
| 106 | + </div> |
| 107 | + </dialog> |
| 108 | + </div> |
27 | 109 | )} |
28 | 110 | </> |
29 | 111 | ); |
|
0 commit comments