input.tsx 4.0 KB

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