33import { TextField , InputAdornment , CircularProgress , Box } from "@mui/material"
44import { Search } from "@mui/icons-material"
55import { useSearch } from "@/providers/SearchProvider"
6+ import { useAuth } from "@/providers/AuthProvider"
67import React from "react"
78
8- export const SearchBar = ( ) => {
9+ type SearchBarProps = {
10+ showLoginRequiredMessage ?: boolean
11+ }
12+
13+ export const SearchBar = ( { showLoginRequiredMessage = false } : SearchBarProps ) => {
914 const {
1015 loading,
1116 setTerm,
1217 state : { term }
1318 } = useSearch ( )
19+ const { accessToken, hasHydrated } = useAuth ( )
1420 const [ localInput , setLocalInput ] = React . useState ( term )
21+ const [ showAuthError , setShowAuthError ] = React . useState ( false )
1522
1623 React . useEffect ( ( ) => {
1724 setLocalInput ( term )
1825 } , [ term ] )
1926
27+ React . useEffect ( ( ) => {
28+ if ( accessToken ) {
29+ setShowAuthError ( false )
30+ }
31+ } , [ accessToken ] )
32+
2033 return (
2134 < Box
2235 sx = { {
@@ -27,35 +40,55 @@ export const SearchBar = () => {
2740 width : "100%"
2841 } }
2942 >
30- < TextField
31- variant = "outlined"
32- value = { localInput }
33- onChange = { ( e ) => setLocalInput ( e . target . value ) }
34- onKeyDown = { ( e : React . KeyboardEvent < HTMLInputElement > ) => {
35- if ( e . key === "Enter" ) {
36- setTerm ( localInput )
37- }
38- } }
39- sx = { {
40- maxWidth : "546px" ,
41- width : "100%" ,
42- "& fieldset" : { borderRadius : "20px" } ,
43- "& .MuiInputBase-input" : {
44- overflow : "hidden" ,
45- textOverflow : "ellipsis"
46- }
47- } }
48- placeholder = "Search officer, unit, or agency"
49- slotProps = { {
50- input : {
51- startAdornment : (
52- < InputAdornment position = "start" >
53- < Search />
54- </ InputAdornment >
55- )
43+ < Box sx = { { width : "100%" , maxWidth : "546px" } } >
44+ < TextField
45+ variant = "outlined"
46+ value = { localInput }
47+ onChange = { ( e ) => {
48+ setLocalInput ( e . target . value )
49+ if ( showAuthError ) {
50+ setShowAuthError ( false )
51+ }
52+ } }
53+ onKeyDown = { ( e : React . KeyboardEvent < HTMLInputElement > ) => {
54+ if ( e . key === "Enter" ) {
55+ if ( showLoginRequiredMessage && hasHydrated && ! accessToken ) {
56+ setShowAuthError ( true )
57+ window . dispatchEvent ( new Event ( "npdc:login-required-search" ) )
58+ return
59+ }
60+
61+ setTerm ( localInput )
62+ }
63+ } }
64+ error = { showAuthError }
65+ helperText = {
66+ showAuthError ? "Please log in before searching." : " "
5667 }
57- } }
58- />
68+ sx = { {
69+ width : "100%" ,
70+ "& fieldset" : { borderRadius : "20px" } ,
71+ "& .MuiInputBase-input" : {
72+ overflow : "hidden" ,
73+ textOverflow : "ellipsis"
74+ } ,
75+ "& .MuiFormHelperText-root" : {
76+ marginLeft : 0 ,
77+ marginRight : 0
78+ }
79+ } }
80+ placeholder = "Search officer, unit, or agency"
81+ slotProps = { {
82+ input : {
83+ startAdornment : (
84+ < InputAdornment position = "start" >
85+ < Search />
86+ </ InputAdornment >
87+ )
88+ }
89+ } }
90+ />
91+ </ Box >
5992 { loading && (
6093 < CircularProgress
6194 size = { 24 }
0 commit comments