input.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 '@/shared/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. departments?: string[]
  33. }
  34. group: {
  35. teamName?: string
  36. managedUsersEnabled?: boolean
  37. domainCaptureEnabled?: boolean
  38. ssoConfig?: {
  39. useUkamfSettings?: boolean
  40. enabled: boolean
  41. }
  42. }
  43. }
  44. let domainCache = new Map<string, DomainInfo>()
  45. export function clearDomainCache() {
  46. domainCache = new Map<string, DomainInfo>()
  47. }
  48. type InputProps = {
  49. onChange: (value: string, domain?: DomainInfo) => void
  50. handleAddNewEmail: () => void
  51. }
  52. function Input({ onChange, handleAddNewEmail }: InputProps) {
  53. const { signal } = useAbortController()
  54. const inputRef = useRef<HTMLInputElement>(null)
  55. const [suggestion, setSuggestion] = useState<string | null>(null)
  56. const [inputValue, setInputValue] = useState<string | null>(null)
  57. const [matchedDomain, setMatchedDomain] = useState<DomainInfo | null>(null)
  58. useEffect(() => {
  59. inputRef.current?.focus()
  60. }, [inputRef])
  61. useEffect(() => {
  62. if (inputValue == null) {
  63. return
  64. }
  65. if (matchedDomain && inputValue.endsWith(matchedDomain.hostname)) {
  66. onChange(inputValue, matchedDomain)
  67. } else {
  68. onChange(inputValue)
  69. }
  70. }, [onChange, inputValue, suggestion, matchedDomain])
  71. const handleEmailChange = useCallback(
  72. (event: ChangeEvent<HTMLInputElement>) => {
  73. const hint = event.target.value
  74. setInputValue(hint)
  75. const { local, domain } = matchLocalAndDomain(hint)
  76. if (domain && !matchedDomain?.hostname.startsWith(domain)) {
  77. setSuggestion(null)
  78. }
  79. if (!domain) {
  80. return
  81. }
  82. if (domainCache.has(domain)) {
  83. const cachedDomain = domainCache.get(domain) as DomainInfo
  84. setSuggestion(`${local}@${cachedDomain.hostname}`)
  85. setMatchedDomain(cachedDomain)
  86. return
  87. }
  88. if (domainBlocklist.some(d => domain.endsWith(d))) {
  89. return
  90. }
  91. const query = `?hostname=${domain}&limit=1`
  92. getJSON<Nullable<DomainInfo[]>>(`/institutions/domains${query}`, {
  93. signal,
  94. })
  95. .then(data => {
  96. if (!(data && data[0])) {
  97. return
  98. }
  99. if (domainBlocklist.some(d => data[0].hostname.endsWith(d))) {
  100. return
  101. }
  102. const hostname = data[0]?.hostname
  103. if (hostname) {
  104. domainCache.set(domain, data[0])
  105. setSuggestion(`${local}@${hostname}`)
  106. setMatchedDomain(data[0])
  107. } else {
  108. setSuggestion(null)
  109. setMatchedDomain(null)
  110. }
  111. })
  112. .catch(error => {
  113. debugConsole.error(error)
  114. setSuggestion(null)
  115. setMatchedDomain(null)
  116. })
  117. },
  118. [signal, matchedDomain]
  119. )
  120. const handleKeyDownEvent = useCallback(
  121. (event: KeyboardEvent) => {
  122. const setInputValueAndResetSuggestion = () => {
  123. setInputValue(suggestion)
  124. setSuggestion(null)
  125. }
  126. if (event.key === 'Enter') {
  127. event.preventDefault()
  128. if (suggestion) {
  129. setInputValueAndResetSuggestion()
  130. return
  131. }
  132. if (!inputValue) {
  133. return
  134. }
  135. const match = matchLocalAndDomain(inputValue)
  136. if (match.local && match.domain) {
  137. handleAddNewEmail()
  138. }
  139. }
  140. if (event.key === 'Tab' && suggestion) {
  141. event.preventDefault()
  142. setInputValueAndResetSuggestion()
  143. }
  144. },
  145. [inputValue, suggestion, handleAddNewEmail]
  146. )
  147. useEffect(() => {
  148. if (!inputValue) {
  149. setSuggestion(null)
  150. } else if (suggestion && !suggestion.startsWith(inputValue)) {
  151. setSuggestion(null)
  152. }
  153. }, [suggestion, inputValue])
  154. return (
  155. <div className="input-suggestions">
  156. <OLFormControl
  157. data-testid="affiliations-email-shadow"
  158. readOnly
  159. className="input-suggestions-shadow"
  160. value={suggestion || ''}
  161. />
  162. <OLFormControl
  163. id="affiliations-email"
  164. data-testid="affiliations-email"
  165. className="input-suggestions-main"
  166. type="email"
  167. onChange={handleEmailChange}
  168. onKeyDown={handleKeyDownEvent}
  169. value={inputValue || ''}
  170. ref={inputRef}
  171. />
  172. </div>
  173. )
  174. }
  175. export default Input