Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 103 additions & 16 deletions src/components/PATModal.jsx
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down Expand Up @@ -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);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// 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 (
<>
<div
onClick={onClose}
onClick={handleClose}
style={{
position: "fixed",
inset: 0,
Expand Down Expand Up @@ -117,7 +163,7 @@ export default function PATModal({ open, onClose }) {

<button
type="button"
onClick={onClose}
onClick={handleClose}
aria-label="Close"
title="Close"
style={{
Expand Down Expand Up @@ -159,40 +205,71 @@ export default function PATModal({ open, onClose }) {
style={{
display: "flex",
gap: 8,
marginBottom: 16,
marginBottom: error ? 10 : 16,
}}
>
<input
type={show ? "text" : "password"}
value={draft}
onChange={(e) => 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,
}}
/>

<button
type="button"
onClick={() => setShow((s) => !s)}
disabled={validating}
style={{
padding: "0 14px",
borderRadius: 8,
border: "1px solid var(--border)",
background: "transparent",
color: "var(--text)",
cursor: "pointer",
cursor: validating ? "not-allowed" : "pointer",
}}
>
{show ? <FiEyeOff /> : <FiEye />}
</button>
</div>

{error && (
<div
role="alert"
style={{
display: "flex",
alignItems: "center",
gap: 8,
padding: "8px 12px",
marginBottom: 16,
background: "rgba(239, 68, 68, 0.08)",
border: "1px solid rgba(239, 68, 68, 0.25)",
borderRadius: 8,
color: "var(--red)",
fontSize: 13,
}}
>
<FiAlertTriangle size={15} style={{ flexShrink: 0 }} />
<span>{error}</span>
</div>
)}

<div
style={{
display: "flex",
Expand All @@ -203,22 +280,32 @@ export default function PATModal({ open, onClose }) {
<button
type="button"
onClick={handleSave}
disabled={!draft.trim()}
disabled={!draft.trim() || validating || isSaved}
style={{
padding: "10px 18px",
border: "none",
borderRadius: 8,
background: "var(--accent)",
color: "#111",
fontWeight: 600,
cursor: "pointer",
cursor: (!draft.trim() || validating || isSaved) ? "not-allowed" : "pointer",
opacity: (!draft.trim() || validating) ? 0.6 : 1,
display: "flex",
alignItems: "center",
gap: 8,
}}
>
<FiSave />
{isSaved ? "Saved" : "Save"}
{validating ? (
<>
<FiLoader className="spin" size={16} />
Verifying...
</>
) : (
<>
<FiSave size={16} />
{isSaved ? "Saved" : "Save"}
</>
)}
</button>

<button
Expand Down Expand Up @@ -330,7 +417,7 @@ export default function PATModal({ open, onClose }) {
>
<button
type="button"
onClick={onClose}
onClick={handleClose}
style={{
padding: "10px 20px",
borderRadius: 8,
Expand Down
Loading
Loading