|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import {useState} from 'react'; |
| 4 | + |
| 5 | +const MAX_COMPONENTS_ON_PAGE = 100; |
| 6 | + |
| 7 | +export function GitHubDomainChecker() { |
| 8 | + const [domain, setDomain] = useState(''); |
| 9 | + const [isValidDomain, setIsValidDomain] = useState(false); |
| 10 | + |
| 11 | + const isGitHubCom = domain.toLowerCase().trim() === 'github.com' || domain.toLowerCase().trim() === 'https://github.com'; |
| 12 | + const hasInput = domain.trim().length > 0; |
| 13 | + |
| 14 | + // Validate domain format |
| 15 | + const validateDomain = (inputDomain: string) => { |
| 16 | + const trimmedDomain = inputDomain.trim(); |
| 17 | + if (!trimmedDomain) { |
| 18 | + setIsValidDomain(false); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // Check if it's github.com (valid) |
| 23 | + if (trimmedDomain.toLowerCase() === 'github.com' || trimmedDomain.toLowerCase() === 'https://github.com') { |
| 24 | + setIsValidDomain(true); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // For enterprise, check if it's a valid URL or domain format |
| 29 | + const urlPattern = /^(https?:\/\/)?([\w\-\.]+\.[\w]{2,})(\/.*)?$/; |
| 30 | + const isValidUrl = urlPattern.test(trimmedDomain); |
| 31 | + setIsValidDomain(isValidUrl); |
| 32 | + }; |
| 33 | + |
| 34 | + const handleDomainChange = (ev) => { |
| 35 | + const newDomain = ev.target.value; |
| 36 | + setDomain(newDomain); |
| 37 | + validateDomain(newDomain); |
| 38 | + }; |
| 39 | + |
| 40 | + const inputClassName = |
| 41 | + 'form-input w-full rounded-md border-[1.5px] focus:ring-2 focus:ring-accent-purple/20 border-gray-200'; |
| 42 | + |
| 43 | + // This is to avoid in case multiple instances of this component are used on the page |
| 44 | + const randomCounter = Math.round(Math.random() * MAX_COMPONENTS_ON_PAGE); |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="space-y-4 p-6 border border-gray-100 rounded"> |
| 48 | + <div className="flex w-full"> |
| 49 | + <div className="flex items-center min-w-[16ch] px-4"> |
| 50 | + <label htmlFor={`gh-domain-${randomCounter}`} className="text-nowrap"> |
| 51 | + GitHub Domain |
| 52 | + </label> |
| 53 | + </div> |
| 54 | + <input |
| 55 | + id={`gh-domain-${randomCounter}`} |
| 56 | + value={domain} |
| 57 | + placeholder="https://github.com or https://ghe.example.com" |
| 58 | + className={inputClassName} |
| 59 | + onChange={handleDomainChange} |
| 60 | + /> |
| 61 | + </div> |
| 62 | + |
| 63 | + {hasInput && ( |
| 64 | + <div className="mt-4 p-4 rounded-md border"> |
| 65 | + <div className="text-sm font-medium mb-2"> |
| 66 | + Recommended Installation: |
| 67 | + </div> |
| 68 | + {isValidDomain ? ( |
| 69 | + isGitHubCom ? ( |
| 70 | + <div className="text-green-700 bg-green-50 p-3 rounded-md"> |
| 71 | + <div className="mb-2"> |
| 72 | + <strong>GitHub</strong> - Use the standard GitHub integration for github.com |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + ) : ( |
| 76 | + <div className="text-blue-700 bg-blue-50 p-3 rounded-md"> |
| 77 | + <div className="mb-2"> |
| 78 | + <strong>GitHub Enterprise</strong> - Use GitHub Enterprise integration for your domain |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + ) |
| 82 | + ) : ( |
| 83 | + <div className="text-red-700 bg-red-50 p-3 rounded-md"> |
| 84 | + <strong>Invalid Domain</strong> - Please enter a valid GitHub domain or URL |
| 85 | + </div> |
| 86 | + )} |
| 87 | + </div> |
| 88 | + )} |
| 89 | + </div> |
| 90 | + ); |
| 91 | +} |
0 commit comments