|
| 1 | +/** |
| 2 | + * Modal shown before starting OAuth for providers whose subscription tokens |
| 3 | + * are restricted (or risky) when used outside the official client. |
| 4 | + */ |
| 5 | +import { useEffect, useId, useRef, useState } from "react"; |
| 6 | +import { useT } from "../i18n"; |
| 7 | +import { IconAlert } from "../icons"; |
| 8 | +import { |
| 9 | + oauthTosRisk, |
| 10 | + oauthTosRiskBodyKey, |
| 11 | + oauthTosRiskTitleKey, |
| 12 | +} from "../oauth-tos-risk"; |
| 13 | + |
| 14 | +export default function OAuthTosWarningModal({ |
| 15 | + providerId, |
| 16 | + providerLabel, |
| 17 | + onCancel, |
| 18 | + onContinue, |
| 19 | +}: { |
| 20 | + providerId: string; |
| 21 | + providerLabel: string; |
| 22 | + onCancel: () => void; |
| 23 | + onContinue: () => void; |
| 24 | +}) { |
| 25 | + const t = useT(); |
| 26 | + const titleId = useId(); |
| 27 | + const bodyId = useId(); |
| 28 | + const dialogRef = useRef<HTMLDivElement>(null); |
| 29 | + const previousFocusRef = useRef<HTMLElement | null>(null); |
| 30 | + const submittedRef = useRef(false); |
| 31 | + const [acknowledged, setAcknowledged] = useState(false); |
| 32 | + const [submitted, setSubmitted] = useState(false); |
| 33 | + const level = oauthTosRisk(providerId); |
| 34 | + |
| 35 | + // Capture-phase Escape so a parent modal (e.g. Add Provider) does not also close. |
| 36 | + useEffect(() => { |
| 37 | + const onKey = (e: KeyboardEvent) => { |
| 38 | + if (e.key !== "Escape") return; |
| 39 | + e.preventDefault(); |
| 40 | + e.stopImmediatePropagation(); |
| 41 | + onCancel(); |
| 42 | + }; |
| 43 | + window.addEventListener("keydown", onKey, true); |
| 44 | + return () => window.removeEventListener("keydown", onKey, true); |
| 45 | + }, [onCancel]); |
| 46 | + |
| 47 | + // Focus first control on open; restore on close. |
| 48 | + useEffect(() => { |
| 49 | + previousFocusRef.current = document.activeElement as HTMLElement | null; |
| 50 | + const dialog = dialogRef.current; |
| 51 | + if (dialog) { |
| 52 | + const focusable = dialog.querySelector<HTMLElement>( |
| 53 | + "input:not([disabled]), button:not([disabled]), [tabindex]:not([tabindex='-1'])", |
| 54 | + ); |
| 55 | + focusable?.focus(); |
| 56 | + } |
| 57 | + return () => { |
| 58 | + previousFocusRef.current?.focus(); |
| 59 | + }; |
| 60 | + }, []); |
| 61 | + |
| 62 | + // Unmarked provider: render nothing (callers must gate with oauthTosRisk). |
| 63 | + if (!level) return null; |
| 64 | + |
| 65 | + const handleContinue = () => { |
| 66 | + if (!acknowledged || submittedRef.current) return; |
| 67 | + submittedRef.current = true; |
| 68 | + setSubmitted(true); |
| 69 | + onContinue(); |
| 70 | + }; |
| 71 | + |
| 72 | + return ( |
| 73 | + <div |
| 74 | + role="dialog" |
| 75 | + aria-modal="true" |
| 76 | + aria-labelledby={titleId} |
| 77 | + aria-describedby={bodyId} |
| 78 | + className="modal-overlay" |
| 79 | + onClick={onCancel} |
| 80 | + style={{ zIndex: 60 }} |
| 81 | + > |
| 82 | + <div |
| 83 | + ref={dialogRef} |
| 84 | + className="modal-card" |
| 85 | + onClick={e => e.stopPropagation()} |
| 86 | + style={{ maxWidth: 460 }} |
| 87 | + > |
| 88 | + <h3 id={titleId}>{t(oauthTosRiskTitleKey(level), { provider: providerLabel })}</h3> |
| 89 | + <div |
| 90 | + id={bodyId} |
| 91 | + className="notice-warn" |
| 92 | + style={{ marginTop: 12, display: "flex", gap: 8, alignItems: "flex-start" }} |
| 93 | + > |
| 94 | + <IconAlert width={16} height={16} style={{ flexShrink: 0, marginTop: 2 }} aria-hidden="true" /> |
| 95 | + <p className="modal-desc" style={{ margin: 0 }}> |
| 96 | + {t(oauthTosRiskBodyKey(level), { provider: providerLabel })} |
| 97 | + </p> |
| 98 | + </div> |
| 99 | + <p className="muted text-label" style={{ marginTop: 12 }}> |
| 100 | + {t("oauthTos.saferPath")} |
| 101 | + </p> |
| 102 | + <label className="oauth-tos-ack" style={{ display: "flex", gap: 8, alignItems: "flex-start", marginTop: 14 }}> |
| 103 | + <input |
| 104 | + type="checkbox" |
| 105 | + checked={acknowledged} |
| 106 | + onChange={e => setAcknowledged(e.target.checked)} |
| 107 | + style={{ marginTop: 3 }} |
| 108 | + aria-required="true" |
| 109 | + /> |
| 110 | + <span className="text-label">{t("oauthTos.acknowledge")}</span> |
| 111 | + </label> |
| 112 | + <div className="modal-actions"> |
| 113 | + <button type="button" className="btn btn-ghost" onClick={onCancel}> |
| 114 | + {t("common.cancel")} |
| 115 | + </button> |
| 116 | + <button |
| 117 | + type="button" |
| 118 | + className="btn btn-primary" |
| 119 | + disabled={!acknowledged || submitted} |
| 120 | + onClick={handleContinue} |
| 121 | + > |
| 122 | + {t("oauthTos.continue")} |
| 123 | + </button> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +} |
0 commit comments