Skip to content

Commit

Permalink
Incomplete search pieces to help with generative ai search
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjarling committed Mar 1, 2024
1 parent 7d4007b commit 168e6d4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
. "$(dirname "$0")/_/husky.sh"

# npm run ts-lint-commit-hook
# npm run lint && npm run test:ci
# npm run lint && npm run test:ci
95 changes: 70 additions & 25 deletions components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,36 @@ import React, {
} from "react";

import { ALL_FACETS } from "@/lib/constants/facets-model";
import GenerativeAIDialog from "@/components/Shared/Dialog";
import { UserContext } from "@/context/user-context";
import useQueryParams from "@/hooks/useQueryParams";
import { useRouter } from "next/router";
import { useSearchState } from "@/context/search-context";

interface SearchProps {
isSearchActive: (value: boolean) => void;
}

const Search: React.FC<SearchProps> = ({ isSearchActive }) => {
const router = useRouter();
const { urlFacets } = useQueryParams();

const search = useRef<HTMLInputElement>(null);
const formRef = useRef<HTMLFormElement>(null);

const [searchValue, setSearchValue] = useState<string>("");
const [searchFocus, setSearchFocus] = useState<boolean>(false);

const [isLoaded, setIsLoaded] = useState<boolean>(false);
const { urlFacets } = useQueryParams();

const { searchState, searchDispatch } = useSearchState();

const { user } = React.useContext(UserContext);

const [modalGenerativeAI, setModalGenerativeUI] = useState({
isOpen: false,
title: "Generative AI Dialog",
});

const handleSubmit = (e: SyntheticEvent<HTMLFormElement>) => {
e.preventDefault();
Expand Down Expand Up @@ -70,6 +85,14 @@ const Search: React.FC<SearchProps> = ({ isSearchActive }) => {
});
};

const handleGenerativeAIChange = (e: ChangeEvent<HTMLInputElement>) => {
setModalGenerativeUI({ ...modalGenerativeAI, isOpen: e.target.checked });
searchDispatch({
isGenerativeAI: e.target.checked,
type: "updateGenerativeAI",
});
};

useEffect(() => {
setIsLoaded(true);
}, []);
Expand All @@ -87,30 +110,52 @@ const Search: React.FC<SearchProps> = ({ isSearchActive }) => {
}, [searchFocus, searchValue, isSearchActive]);

return (
<SearchStyled
ref={formRef}
onSubmit={handleSubmit}
data-testid="search-ui-component"
>
<Input
placeholder="Search by keyword or phrase, ex: Berkeley Music Festival"
onChange={handleSearchChange}
onFocus={handleSearchFocus}
onBlur={handleSearchFocus}
ref={search}
name="search"
role="search"
/>
{searchValue && (
<Clear onClick={clearSearchResults} type="reset">
<IconClear />
</Clear>
)}
<Button type="submit" data-testid="submit-button">
Search <IconArrowForward />
</Button>
{isLoaded && <IconSearch />}
</SearchStyled>
<>
<SearchStyled
ref={formRef}
onSubmit={handleSubmit}
data-testid="search-ui-component"
>
<Input
placeholder="Search by keyword or phrase, ex: Berkeley Music Festival"
onChange={handleSearchChange}
onFocus={handleSearchFocus}
onBlur={handleSearchFocus}
ref={search}
name="search"
role="search"
/>
{searchValue && (
<Clear onClick={clearSearchResults} type="reset">
<IconClear />
</Clear>
)}
<label htmlFor="isGenerativeAI">
<input
type="checkbox"
name="isGenerativeAI"
onChange={handleGenerativeAIChange}
/>
Use Generative AI [tooltip]
</label>
<Button type="submit" data-testid="submit-button">
Search <IconArrowForward />
</Button>
{isLoaded && <IconSearch />}
</SearchStyled>

<GenerativeAIDialog
isOpen={modalGenerativeAI.isOpen}
title={modalGenerativeAI.title}
handleCloseClick={() =>
setModalGenerativeUI({ ...modalGenerativeAI, isOpen: false })
}
>
<p>You must log in to proceed</p>
<Button>Login</Button>
<Button>Close</Button>
</GenerativeAIDialog>
</>
);
};

Expand Down
8 changes: 8 additions & 0 deletions context/search-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Action =
type: "updateAggregations";
aggregations: ApiResponseAggregation | undefined;
}
| { type: "updateGenerativeAI"; isGenerativeAI: boolean }
| { type: "updateSearch"; q: string }
| { type: "updateSearchFixed"; searchFixed: boolean };

Expand All @@ -19,6 +20,7 @@ type SearchProviderProps = {

const defaultState: SearchContextStore = {
aggregations: {},
isGenerativeAI: false,
searchFixed: false,
};

Expand All @@ -34,6 +36,12 @@ function searchReducer(state: State, action: Action) {
aggregations: action.aggregations,
};
}
case "updateGenerativeAI": {
return {
...state,
isGenerativeAI: action.isGenerativeAI,
};
}
case "updateSearch": {
return {
...state,
Expand Down
1 change: 1 addition & 0 deletions types/context/search-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApiResponseAggregation } from "@/types/api/response";

export interface SearchContextStore {
aggregations?: ApiResponseAggregation;
isGenerativeAI: boolean;
searchFixed: boolean;
}

Expand Down

0 comments on commit 168e6d4

Please sign in to comment.