|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 3 | +import { faSpinner, faCopy, faCheck } from '@fortawesome/free-solid-svg-icons'; |
| 4 | +import { SchedulingContactButton } from '@site/src/components/contactButton'; |
| 5 | + |
| 6 | +const SALT = 'adv-devops-sched-2026'; |
| 7 | +const EXPIRY_DAYS = 30; |
| 8 | +const SCHEDULE_URL = 'https://oncehub.com/adventures-in-devops'; |
| 9 | + |
| 10 | +// Simple sync hash: djb2 variant with salt mixing |
| 11 | +function hashCode(input: string): number { |
| 12 | + let hash = 5381; |
| 13 | + const salted = `${SALT}:${input}`; |
| 14 | + for (let i = 0; i < salted.length; i++) { |
| 15 | + hash = ((hash << 5) + hash + salted.charCodeAt(i)) | 0; |
| 16 | + } |
| 17 | + return Math.abs(hash); |
| 18 | +} |
| 19 | + |
| 20 | +function daysSinceEpoch(): number { |
| 21 | + return Math.floor(Date.now() / (1000 * 60 * 60 * 24)); |
| 22 | +} |
| 23 | + |
| 24 | +function generateId(): string { |
| 25 | + const days = daysSinceEpoch(); |
| 26 | + const timestamp = days.toString(36); |
| 27 | + const random = Math.random().toString(36).substring(2, 5); |
| 28 | + const checksum = hashCode(`${timestamp}-${random}`).toString(36).substring(0, 4); |
| 29 | + return `${timestamp}-${random}-${checksum}`; |
| 30 | +} |
| 31 | + |
| 32 | +function validateId(id: string): { valid: boolean; expired?: boolean } { |
| 33 | + const parts = id.split('-'); |
| 34 | + if (parts.length !== 3) { |
| 35 | + return { valid: false }; |
| 36 | + } |
| 37 | + |
| 38 | + const [timestamp, random, checksum] = parts; |
| 39 | + |
| 40 | + const expected = hashCode(`${timestamp}-${random}`).toString(36).substring(0, 4); |
| 41 | + if (checksum !== expected) { |
| 42 | + return { valid: false }; |
| 43 | + } |
| 44 | + |
| 45 | + const days = parseInt(timestamp, 36); |
| 46 | + const now = daysSinceEpoch(); |
| 47 | + if (isNaN(days) || days > now || now - days > EXPIRY_DAYS) { |
| 48 | + return { valid: false, expired: true }; |
| 49 | + } |
| 50 | + |
| 51 | + return { valid: true }; |
| 52 | +} |
| 53 | + |
| 54 | +export default function ScheduleRedirect() { |
| 55 | + const [mode, setMode] = useState<'loading' | 'generate' | 'invalid' | 'expired'>('loading'); |
| 56 | + const [generatedUrl, setGeneratedUrl] = useState(''); |
| 57 | + const [copied, setCopied] = useState(false); |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + const params = new URLSearchParams(window.location.search); |
| 61 | + |
| 62 | + if (params.has('generate')) { |
| 63 | + const id = generateId(); |
| 64 | + const base = `${window.location.origin}/schedule?id=${id}`; |
| 65 | + setGeneratedUrl(base); |
| 66 | + setMode('generate'); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const id = params.get('id'); |
| 71 | + if (id) { |
| 72 | + const result = validateId(id); |
| 73 | + if (result.valid) { |
| 74 | + window.location.replace(SCHEDULE_URL); |
| 75 | + return; |
| 76 | + } |
| 77 | + setMode(result.expired ? 'expired' : 'invalid'); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + setMode('invalid'); |
| 82 | + }, []); |
| 83 | + |
| 84 | + function copyUrl() { |
| 85 | + navigator.clipboard.writeText(generatedUrl); |
| 86 | + setCopied(true); |
| 87 | + setTimeout(() => setCopied(false), 2000); |
| 88 | + } |
| 89 | + |
| 90 | + if (mode === 'generate') { |
| 91 | + return ( |
| 92 | + <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '300px', gap: '20px' }}> |
| 93 | + <h2>Schedule Link Generated</h2> |
| 94 | + <p style={{ color: 'var(--ifm-color-secondary-darkest)', fontSize: '14px' }}> |
| 95 | + This link expires in {EXPIRY_DAYS} days. |
| 96 | + </p> |
| 97 | + <div style={{ |
| 98 | + display: 'flex', alignItems: 'center', gap: '10px', |
| 99 | + background: 'var(--ifm-background-surface-color)', padding: '12px 16px', |
| 100 | + borderRadius: '8px', border: '1px solid var(--ifm-color-emphasis-300)', |
| 101 | + maxWidth: '100%', overflow: 'hidden' |
| 102 | + }}> |
| 103 | + <code style={{ fontSize: '14px', wordBreak: 'break-all' }}>{generatedUrl}</code> |
| 104 | + <button |
| 105 | + onClick={copyUrl} |
| 106 | + type="button" |
| 107 | + style={{ |
| 108 | + background: 'none', border: 'none', cursor: 'pointer', |
| 109 | + color: 'var(--ifm-link-color)', fontSize: '18px', flexShrink: 0 |
| 110 | + }} |
| 111 | + title="Copy to clipboard" |
| 112 | + > |
| 113 | + <FontAwesomeIcon icon={copied ? faCheck : faCopy} /> |
| 114 | + </button> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + if (mode === 'invalid' || mode === 'expired') { |
| 121 | + const message = mode === 'expired' |
| 122 | + ? 'This scheduling link has expired.' |
| 123 | + : 'This scheduling link is not valid.'; |
| 124 | + |
| 125 | + return ( |
| 126 | + <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '300px', gap: '20px', textAlign: 'center' }}> |
| 127 | + <h2>{message}</h2> |
| 128 | + <p> |
| 129 | + If you'd like to schedule an appearance on the podcast, please reach out to us directly. |
| 130 | + </p> |
| 131 | + <SchedulingContactButton /> |
| 132 | + </div> |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + // Loading/redirecting state |
| 137 | + return ( |
| 138 | + <div style={{ display: 'flex', justifyContent: 'center', height: '100%', alignItems: 'center', minHeight: '300px' }}> |
| 139 | + <FontAwesomeIcon icon={faSpinner} size="5x" spin /> |
| 140 | + </div> |
| 141 | + ); |
| 142 | +} |
0 commit comments