input.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {
  2. ChangeEvent,
  3. KeyboardEvent,
  4. useCallback,
  5. useEffect,
  6. useState,
  7. useRef,
  8. } from 'react'
  9. import { getJSON } from '../../../../../infrastructure/fetch-json'
  10. import useAbortController from '../../../../../shared/hooks/use-abort-controller'
  11. import domainBlocklist from '../../../domain-blocklist'
  12. const LOCAL_AND_DOMAIN_REGEX = /([^@]+)@(.+)/
  13. function matchLocalAndDomain(emailHint: string) {
  14. const match = emailHint.match(LOCAL_AND_DOMAIN_REGEX)
  15. if (match) {
  16. return { local: match[1], domain: match[2] }
  17. } else {
  18. return { local: null, domain: null }
  19. }
  20. }
  21. export type DomainInfo = {
  22. hostname: string
  23. confirmed?: boolean
  24. university: {
  25. id: number
  26. name: string
  27. ssoEnabled?: boolean
  28. ssoBeta?: boolean
  29. }
  30. }
  31. let domainCache = new Map<string, DomainInfo>()
  32. export function clearDomainCache() {
  33. domainCache = new Map<string, DomainInfo>()
  34. }
  35. type InputProps = {
  36. onChange: (value: string, domain?: DomainInfo) => void
  37. handleAddNewEmail: () => void
  38. }
  39. function Input({ onChange, handleAddNewEmail }: InputProps) {
  40. const { signal } = useAbortController()
  41. const inputRef = useRef<HTMLInputElement | null>(null)
  42. const [suggestion, setSuggestion] = useState<string | null>(null)
  43. const [inputValue, setInputValue] = useState<string | null>(null)
  44. const [matchedDomain, setMatchedDomain] = useState<DomainInfo | null>(null)
  45. useEffect(() => {
  46. inputRef.current?.focus()
  47. }, [inputRef])
  48. useEffect(() => {
  49. if (inputValue == null) {
  50. return
  51. }
  52. if (matchedDomain && inputValue.endsWith(matchedDomain.hostname)) {
  53. onChange(inputValue, matchedDomain)
  54. } else {
  55. onChange(inputValue)
  56. }
  57. }, [onChange, inputValue, suggestion, matchedDomain])
  58. const handleEmailChange = useCallback(
  59. (event: ChangeEvent<HTMLInputElement>) => {
  60. const hint = event.target.value
  61. setInputValue(hint)
  62. const { local, domain } = matchLocalAndDomain(hint)
  63. if (domain && !matchedDomain?.hostname.startsWith(domain)) {
  64. setSuggestion(null)
  65. }
  66. if (!domain) {
  67. return
  68. }
  69. if (domainCache.has(domain)) {
  70. const cachedDomain = domainCache.get(domain) as DomainInfo
  71. setSuggestion(`${local}@${cachedDomain.hostname}`)
  72. setMatchedDomain(cachedDomain)
  73. return
  74. }
  75. const query = `?hostname=${domain}&limit=1`
  76. getJSON<Nullable<DomainInfo[]>>(`/institutions/domains${query}`, {
  77. signal,
  78. })
  79. .then(data => {
  80. if (!(data && data[0])) {
  81. return
  82. }
  83. if (domainBlocklist.has(data[0].hostname)) {
  84. return
  85. }
  86. const hostname = data[0]?.hostname
  87. if (hostname) {
  88. domainCache.set(domain, data[0])
  89. setSuggestion(`${local}@${hostname}`)
  90. setMatchedDomain(data[0])
  91. } else {
  92. setSuggestion(null)
  93. setMatchedDomain(null)
  94. }
  95. })
  96. .catch(error => {
  97. setSuggestion(null)
  98. setMatchedDomain(null)
  99. console.error(error)
  100. })
  101. },
  102. [signal, matchedDomain]
  103. )
  104. const handleKeyDownEvent = useCallback(
  105. (event: KeyboardEvent) => {
  106. const setInputValueAndResetSuggestion = () => {
  107. setInputValue(suggestion)
  108. setSuggestion(null)
  109. }
  110. if (event.key === 'Enter') {
  111. event.preventDefault()
  112. if (suggestion) {
  113. setInputValueAndResetSuggestion()
  114. return
  115. }
  116. if (!inputValue) {
  117. return
  118. }
  119. const match = matchLocalAndDomain(inputValue)
  120. if (match.local && match.domain) {
  121. handleAddNewEmail()
  122. }
  123. }
  124. if (event.key === 'Tab' && suggestion) {
  125. event.preventDefault()
  126. setInputValueAndResetSuggestion()
  127. }
  128. },
  129. [inputValue, suggestion, handleAddNewEmail]
  130. )
  131. useEffect(() => {
  132. if (suggestion && inputValue && !suggestion.startsWith(inputValue)) {
  133. setSuggestion(null)
  134. }
  135. }, [suggestion, inputValue])
  136. return (
  137. <div className="input-suggestions">
  138. <div className="form-control input-suggestions-shadow">
  139. <div className="input-suggestions-shadow-suggested">
  140. {suggestion || ''}
  141. </div>
  142. </div>
  143. <input
  144. id="affiliations-email"
  145. className="form-control input-suggestions-main"
  146. type="email"
  147. onChange={handleEmailChange}
  148. onKeyDown={handleKeyDownEvent}
  149. value={inputValue || ''}
  150. placeholder="e.g. johndoe@mit.edu"
  151. ref={inputRef}
  152. />
  153. </div>
  154. )
  155. }
  156. export default Input