input.tsx 4.6 KB

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