input.tsx 4.8 KB

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