|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import styles from './styles.module.css'; |
| 3 | + |
| 4 | +const CLASSIC_TOKEN_URL = |
| 5 | + 'https://github.com/settings/tokens/new?scopes=gist&description=The%20Python%20Ledger%20Sync'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Renders the "paste a GitHub PAT" flow: instructions, a token input, |
| 9 | + * and a status area. Doesn't know or care whether it's logging in fresh |
| 10 | + * or upgrading an existing local account — the caller supplies |
| 11 | + * `onSubmit(pat)`, which should verify the token and return the GitHub |
| 12 | + * user object (or throw). |
| 13 | + */ |
| 14 | +export default function GitHubPatForm({ onSubmit, onSuccess, submitLabel = 'Validate and Connect' }) { |
| 15 | + const [token, setToken] = useState(''); |
| 16 | + const [status, setStatus] = useState({ state: 'idle', message: '' }); |
| 17 | + const isMountedRef = useRef(true); |
| 18 | + |
| 19 | + useEffect( |
| 20 | + () => () => { |
| 21 | + isMountedRef.current = false; |
| 22 | + }, |
| 23 | + [] |
| 24 | + ); |
| 25 | + |
| 26 | + const handleSubmit = async () => { |
| 27 | + const trimmed = token.trim(); |
| 28 | + if (!trimmed) { |
| 29 | + setStatus({ state: 'error', message: 'Paste a token first.' }); |
| 30 | + return; |
| 31 | + } |
| 32 | + setStatus({ state: 'validating', message: 'Validating token with GitHub...' }); |
| 33 | + try { |
| 34 | + const user = await onSubmit(trimmed); |
| 35 | + // The caller (e.g. the profile page's upgrade section) may stop |
| 36 | + // rendering this form the instant the session flips to |
| 37 | + // 'github' — in that case there's nothing left to update. |
| 38 | + if (!isMountedRef.current) return; |
| 39 | + setStatus({ state: 'success', message: `Token verified. Connected as ${user.login}!` }); |
| 40 | + onSuccess?.(user); |
| 41 | + } catch (error) { |
| 42 | + if (!isMountedRef.current) return; |
| 43 | + setStatus({ state: 'error', message: error.message || 'Invalid token. Please check and try again.' }); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className={styles.wrapper}> |
| 49 | + <section className={styles.securityNotice}> |
| 50 | + <h3>🔐 Security & Privacy Notice</h3> |
| 51 | + <p> |
| 52 | + This site uses <strong>client-side tracking</strong>. Your Personal Access Token (PAT) is stored{' '} |
| 53 | + <em>only</em> in your browser's local storage and is sent directly to the GitHub API. It is{' '} |
| 54 | + <strong>never</strong> transmitted to our servers. |
| 55 | + </p> |
| 56 | + </section> |
| 57 | + |
| 58 | + <section className={styles.instructions}> |
| 59 | + <h3>How to Generate Your Token</h3> |
| 60 | + <ol> |
| 61 | + <li> |
| 62 | + Go to{' '} |
| 63 | + <a href={CLASSIC_TOKEN_URL} target="_blank" rel="noopener noreferrer"> |
| 64 | + GitHub's classic token page |
| 65 | + </a>{' '} |
| 66 | + (pre-filled with the <code>gist</code> scope). |
| 67 | + </li> |
| 68 | + <li> |
| 69 | + Click <strong>"Generate token"</strong>. |
| 70 | + </li> |
| 71 | + <li>Copy the generated token and paste it below.</li> |
| 72 | + </ol> |
| 73 | + <p className={styles.scopeNote}> |
| 74 | + Cloud sync backs your progress up to a private Gist, which needs a <strong>classic</strong> token |
| 75 | + with the <code>gist</code> scope — fine-grained tokens don't support Gists yet. Any token works |
| 76 | + for just verifying your identity, but only a classic <code>gist</code>-scoped token can back up |
| 77 | + your progress. |
| 78 | + </p> |
| 79 | + </section> |
| 80 | + |
| 81 | + <section className={styles.tokenInputArea}> |
| 82 | + <label htmlFor="patInput">Paste your PAT (ghp_... or github_pat_...) here:</label> |
| 83 | + <input |
| 84 | + type="password" |
| 85 | + id="patInput" |
| 86 | + value={token} |
| 87 | + onChange={(e) => setToken(e.target.value)} |
| 88 | + placeholder="ghp_aBC123..." |
| 89 | + /> |
| 90 | + </section> |
| 91 | + |
| 92 | + {status.message && ( |
| 93 | + <div className={`${styles.statusMessage} ${styles[status.state]}`}>{status.message}</div> |
| 94 | + )} |
| 95 | + |
| 96 | + <button |
| 97 | + type="button" |
| 98 | + className={styles.primaryButton} |
| 99 | + onClick={handleSubmit} |
| 100 | + disabled={status.state === 'validating'} |
| 101 | + > |
| 102 | + {status.state === 'validating' ? 'Verifying...' : submitLabel} |
| 103 | + </button> |
| 104 | + </div> |
| 105 | + ); |
| 106 | +} |
0 commit comments