| 1 | import { useId, useLayoutEffect, useRef, useState } from "react"; |
| 2 | import { createPortal } from "react-dom"; |
| 3 | import { ArrowUpRight, Layers, X } from "lucide-react"; |
| 4 | import { useT } from "../lib/i18n"; |
| 5 | import { openExternal } from "../lib/bridge"; |
| 6 | import { claimStudioIntroduction, STUDIO_DOWNLOAD_URL } from "../lib/studioDiscovery"; |
| 7 | import "./StudioIntroduction.css"; |
| 8 | |
| 9 | export function StudioIntroduction({ onClose }: { onClose: () => void }) { |
| 10 | const t = useT(); |
| 11 | const titleId = useId(); |
| 12 | const descriptionId = useId(); |
| 13 | const dialogRef = useRef<HTMLDivElement>(null); |
| 14 | const continueRef = useRef<HTMLButtonElement>(null); |
| 15 | const closeRef = useRef(onClose); |
| 16 | useLayoutEffect(() => { closeRef.current = onClose; }, [onClose]); |
| 17 | useLayoutEffect(() => { |
| 18 | const previous = document.activeElement; |
| 19 | continueRef.current?.focus(); |
| 20 | const onKeyDown = (event: KeyboardEvent) => { |
| 21 | if (event.key === "Escape") { |
| 22 | event.preventDefault(); |
| 23 | event.stopPropagation(); |
| 24 | closeRef.current(); |
| 25 | } |
| 26 | if (event.key !== "Tab") return; |
| 27 | const controls = Array.from(dialogRef.current?.querySelectorAll<HTMLElement>("button:not(:disabled), summary, a[href]") ?? []) |
| 28 | .filter((element) => element.getClientRects().length > 0); |
| 29 | const first = controls[0]; |
| 30 | const last = controls[controls.length - 1]; |
| 31 | if (!first || !last) return; |
| 32 | if (event.shiftKey && (document.activeElement === first || !dialogRef.current?.contains(document.activeElement))) { |
| 33 | event.preventDefault(); last.focus(); |
| 34 | } else if (!event.shiftKey && (document.activeElement === last || !dialogRef.current?.contains(document.activeElement))) { |
| 35 | event.preventDefault(); first.focus(); |
| 36 | } |
| 37 | }; |
| 38 | document.addEventListener("keydown", onKeyDown, true); |
| 39 | return () => { |
| 40 | document.removeEventListener("keydown", onKeyDown, true); |
| 41 | if (previous instanceof HTMLElement && previous.isConnected) previous.focus(); |
| 42 | }; |
| 43 | }, []); |
| 44 | |
| 45 | return createPortal( |
| 46 | <div className="modal-backdrop studio-intro-backdrop" data-app-overlay="" role="presentation" |
| 47 | onMouseDown={(event) => { if (event.target === event.currentTarget) onClose(); }}> |
| 48 | <div ref={dialogRef} className="modal studio-intro" role="dialog" aria-modal="true" aria-labelledby={titleId} aria-describedby={descriptionId}> |
| 49 | <div className="studio-intro__masthead"> |
| 50 | <span className="studio-intro__brand"><Layers size={18} aria-hidden="true" /> REASONIX STUDIO</span> |
| 51 | <button className="btn btn--small studio-intro__close" type="button" aria-label={t("studio.close")} onClick={onClose}><X size={18} aria-hidden="true" /></button> |
| 52 | </div> |
| 53 | <div className="studio-intro__body"> |
| 54 | <h2 id={titleId}>{t("studio.title")}</h2> |
| 55 | <p id={descriptionId} className="studio-intro__lead">{t("studio.description")}</p> |
| 56 | <div className="studio-intro__features"> |
| 57 | <div><span>01</span><h3>{t("studio.sessions")}</h3><p>{t("studio.sessionsBody")}</p></div> |
| 58 | <div><span>02</span><h3>{t("studio.control")}</h3><p>{t("studio.controlBody")}</p></div> |
| 59 | <div><span>03</span><h3>{t("studio.future")}</h3><p>{t("studio.futureBody")}</p></div> |
| 60 | </div> |
| 61 | <p className="studio-intro__reassurance">{t("studio.optional")}</p> |
| 62 | <details className="studio-intro__details"> |
| 63 | <summary>{t("studio.migrationTitle")}</summary> |
| 64 | <ul> |
| 65 | <li>{t("studio.migrationInstall")}</li> |
| 66 | <li>{t("studio.migrationData")}</li> |
| 67 | <li>{t("studio.migrationProjects")}</li> |
| 68 | </ul> |
| 69 | </details> |
| 70 | </div> |
| 71 | <div className="studio-intro__footer"> |
| 72 | <p>{t("studio.downloadHint")}</p> |
| 73 | <div className="studio-intro__actions"> |
| 74 | <button ref={continueRef} className="btn" type="button" onClick={onClose}>{t("studio.continue")}</button> |
| 75 | <button className="btn btn--primary" type="button" onClick={() => openExternal(STUDIO_DOWNLOAD_URL)}>{t("studio.download")}<ArrowUpRight size={16} aria-hidden="true" /></button> |
| 76 | </div> |
| 77 | </div> |
| 78 | </div> |
| 79 | </div>, document.body, |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | export function StudioDiscoveryCard() { |
| 84 | const t = useT(); |
| 85 | const [open, setOpen] = useState(false); |
| 86 | return <> |
| 87 | <div className="studio-discovery"> |
| 88 | <Layers size={24} aria-hidden="true" /> |
| 89 | <div><strong>{t("studio.explore")}</strong><p>{t("studio.cardBody")}</p></div> |
| 90 | <button className="btn btn--small" type="button" onClick={() => { claimStudioIntroduction(); setOpen(true); }}>{t("studio.learnMore")}<ArrowUpRight size={14} aria-hidden="true" /></button> |
| 91 | </div> |
| 92 | {open && <StudioIntroduction onClose={() => setOpen(false)} />} |
| 93 | </>; |
| 94 | } |
| 95 |