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,53 @@ 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- )
56- }
57- } }
58- />
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 = { showAuthError ? "Please log in before searching." : " " }
66+ sx = { {
67+ width : "100%" ,
68+ "& fieldset" : { borderRadius : "20px" } ,
69+ "& .MuiInputBase-input" : {
70+ overflow : "hidden" ,
71+ textOverflow : "ellipsis"
72+ } ,
73+ "& .MuiFormHelperText-root" : {
74+ marginLeft : 0 ,
75+ marginRight : 0
76+ }
77+ } }
78+ placeholder = "Search officer, unit, or agency"
79+ slotProps = { {
80+ input : {
81+ startAdornment : (
82+ < InputAdornment position = "start" >
83+ < Search />
84+ </ InputAdornment >
85+ )
86+ }
87+ } }
88+ />
89+ </ Box >
5990 { loading && (
6091 < CircularProgress
6192 size = { 24 }
0 commit comments