diff --git a/src/components/PATModal.jsx b/src/components/PATModal.jsx index 2168bc97..d1b483e2 100644 --- a/src/components/PATModal.jsx +++ b/src/components/PATModal.jsx @@ -1,18 +1,29 @@ -import React, { useState, useEffect } from "react"; -import { FiEye, FiEyeOff, FiSave, FiTrash2, FiX } from "react-icons/fi"; +import React, { useState, useEffect, useRef } from "react"; +import { FiEye, FiEyeOff, FiSave, FiTrash2, FiX, FiAlertTriangle, FiLoader } from "react-icons/fi"; import { useApp } from "../context/AppContext"; +import { validatePat } from "../services/github"; export default function PATModal({ open, onClose }) { const { pat, savePat } = useApp(); const [draft, setDraft] = useState(pat); const [show, setShow] = useState(false); - const isSaved = Boolean(pat && draft.trim() === pat) + const [validating, setValidating] = useState(false); + const [error, setError] = useState(""); + const isSaved = Boolean(pat && draft.trim() === pat); + const activeValidationId = useRef(0); useEffect(() => { if (open) { setDraft(pat); + setError(""); + setValidating(false); + } else { + activeValidationId.current += 1; } + return () => { + activeValidationId.current += 1; + }; }, [open, pat]); useEffect(() => { @@ -42,19 +53,54 @@ export default function PATModal({ open, onClose }) { if (!open) return null; - const handleSave = () => { - savePat(draft.trim()); + const handleClose = () => { + activeValidationId.current += 1; + setValidating(false); + onClose(); + }; + + // Validate token with GitHub API before persisting to storage + const handleSave = async () => { + const trimmed = draft.trim(); + if (!trimmed || validating) return; + + if (isSaved) return; + + activeValidationId.current += 1; + const currentValidationId = activeValidationId.current; + + setValidating(true); + setError(""); + + const res = await validatePat(trimmed); + + // Bail out if user deleted token, closed modal, or submitted again + if (currentValidationId !== activeValidationId.current) { + return; + } + + if (!res.valid) { + setError(res.error || "Invalid token"); + setValidating(false); + return; + } + + await savePat(trimmed); + setValidating(false); }; const handleDelete = () => { + activeValidationId.current += 1; + setValidating(false); savePat(""); setDraft(""); + setError(""); }; return ( <>
setDraft(e.target.value)} + onChange={(e) => { + setDraft(e.target.value); + if (error) setError(""); + }} + onKeyDown={(e) => { + if (e.key === "Enter" && !validating && draft.trim()) { + handleSave(); + } + }} + disabled={validating} placeholder="ghp_xxxxxxxxxxxxxxxxx" style={{ flex: 1, padding: "10px 12px", background: "var(--surface2)", color: "var(--text)", - border: "1px solid var(--border)", + border: error ? "1px solid var(--red)" : "1px solid var(--border)", borderRadius: 8, }} /> @@ -180,19 +235,41 @@ export default function PATModal({ open, onClose }) {
+ {error && ( +
+ + {error} +
+ )} +
- - {isSaved ? "Saved" : "Save"} + {validating ? ( + <> + + Verifying... + + ) : ( + <> + + {isSaved ? "Saved" : "Save"} + + )}