|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import {useState} from 'react'; |
| 4 | + |
| 5 | +interface GitHubDomainCheckerProps { |
| 6 | + id?: string; |
| 7 | +} |
| 8 | + |
| 9 | +export function GitHubDomainChecker({id}: GitHubDomainCheckerProps = {}) { |
| 10 | + const [domain, setDomain] = useState(''); |
| 11 | + const [isValidDomain, setIsValidDomain] = useState(false); |
| 12 | + |
| 13 | + // Safe function to check if domain is github.com or its subdomain |
| 14 | + const isValidGitHubDomain = (input_domain: string): boolean => { |
| 15 | + try { |
| 16 | + const url = new URL( |
| 17 | + input_domain.startsWith('http') ? input_domain : `https://${input_domain}` |
| 18 | + ); |
| 19 | + const hostname = url.hostname.toLowerCase(); |
| 20 | + |
| 21 | + // Exact match for github.com or valid subdomain (like gist.github.com) |
| 22 | + return hostname === 'github.com' || hostname.endsWith('.github.com'); |
| 23 | + } catch { |
| 24 | + return false; |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + // Safe function to check if it's an enterprise GitHub domain |
| 29 | + const isValidEnterpriseDomain = (input_domain: string): boolean => { |
| 30 | + try { |
| 31 | + const url = new URL( |
| 32 | + input_domain.startsWith('http') ? input_domain : `https://${input_domain}` |
| 33 | + ); |
| 34 | + const hostname = url.hostname.toLowerCase(); |
| 35 | + |
| 36 | + // Must be a valid domain with TLD, but not github.com |
| 37 | + const domainPattern = /^[\w\-\.]+\.[\w]{2,}$/; |
| 38 | + return ( |
| 39 | + domainPattern.test(hostname) && |
| 40 | + !hostname.endsWith('.github.com') && |
| 41 | + hostname !== 'github.com' |
| 42 | + ); |
| 43 | + } catch { |
| 44 | + return false; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + const isGitHubCom = (() => { |
| 49 | + const trimmedDomain = domain.toLowerCase().trim(); |
| 50 | + if (!trimmedDomain) return false; |
| 51 | + |
| 52 | + return isValidGitHubDomain(trimmedDomain); |
| 53 | + })(); |
| 54 | + |
| 55 | + const hasInput = domain.trim().length > 0; |
| 56 | + |
| 57 | + // Validate domain format |
| 58 | + const validateDomain = (inputDomain: string) => { |
| 59 | + const trimmedDomain = inputDomain.trim(); |
| 60 | + if (!trimmedDomain) { |
| 61 | + setIsValidDomain(false); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Check if it's a valid GitHub.com domain or subdomain |
| 66 | + if (isValidGitHubDomain(trimmedDomain)) { |
| 67 | + setIsValidDomain(true); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // For enterprise, validate as proper domain |
| 72 | + if (isValidEnterpriseDomain(trimmedDomain)) { |
| 73 | + setIsValidDomain(true); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + setIsValidDomain(false); |
| 78 | + }; |
| 79 | + |
| 80 | + const handleDomainChange = ev => { |
| 81 | + const newDomain = ev.target.value; |
| 82 | + setDomain(newDomain); |
| 83 | + validateDomain(newDomain); |
| 84 | + }; |
| 85 | + |
| 86 | + // Improved input styling with dark mode support |
| 87 | + const inputClassName = |
| 88 | + 'form-input w-full rounded-md border-[1.5px] focus:ring-2 focus:ring-accent-purple/20 border-gray-200 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-100 dark:placeholder-gray-400'; |
| 89 | + |
| 90 | + // Use provided id or generate a fallback |
| 91 | + const inputId = id || `gh-domain-${Date.now()}`; |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="space-y-4 p-6 border border-gray-100 dark:border-gray-700 rounded"> |
| 95 | + <div className="flex w-full"> |
| 96 | + <div className="flex items-center min-w-[16ch] px-4"> |
| 97 | + <label htmlFor={inputId} className="text-nowrap"> |
| 98 | + GitHub Domain |
| 99 | + </label> |
| 100 | + </div> |
| 101 | + <input |
| 102 | + id={inputId} |
| 103 | + value={domain} |
| 104 | + placeholder="https://github.com or https://ghe.example.com" |
| 105 | + className={inputClassName} |
| 106 | + onChange={handleDomainChange} |
| 107 | + /> |
| 108 | + </div> |
| 109 | + |
| 110 | + {hasInput && ( |
| 111 | + <div className="mt-4 p-4 rounded-md border dark:border-gray-600"> |
| 112 | + {isValidDomain ? ( |
| 113 | + <div> |
| 114 | + <div className="text-sm font-medium mb-2">Recommended Installation:</div> |
| 115 | + {isGitHubCom ? ( |
| 116 | + <div className="text-green-700 bg-green-50 dark:text-green-300 dark:bg-green-900/30 p-3 rounded-md"> |
| 117 | + <div> |
| 118 | + <strong>GitHub</strong> - Use the standard GitHub integration for |
| 119 | + github.com |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + ) : ( |
| 123 | + <div className="text-blue-700 bg-blue-50 dark:text-blue-300 dark:bg-blue-900/30 p-3 rounded-md"> |
| 124 | + <div> |
| 125 | + <strong>GitHub Enterprise</strong> - Use GitHub Enterprise integration |
| 126 | + for your domain |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + ) : ( |
| 132 | + <div className="text-red-700 bg-red-50 dark:text-red-300 dark:bg-red-900/30 p-3 rounded-md"> |
| 133 | + <strong>Invalid Domain</strong> - Please enter a valid GitHub domain or URL |
| 134 | + </div> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + )} |
| 138 | + </div> |
| 139 | + ); |
| 140 | +} |
0 commit comments