Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Package][Genetics] Fix search for Genetics #356

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function GlobalSearchFreeListItem() {
const { inputValue } = useContext(SearchInputContext);

const { setOpen } = useContext(SearchContext);
const [openListItem] = useListOption();
const openListItem = useListOption();

const freeSearchTermObject = {
symbol: `Search for: ${inputValue}`,
Expand Down
41 changes: 21 additions & 20 deletions packages/ui/src/components/GlobalSearch/GlobalSearchList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function GlobalSearchList({ inputValue }) {
const [loading, setLoading] = useState(false);
const { searchQuery, setOpen, searchSuggestions } = useContext(SearchContext);
const [getSearchData] = useLazyQuery(searchQuery);
const [openListItem] = useListOption();
const openListItem = useListOption();
const [recentItems, setRecentItems] = useState(
JSON.parse(localStorage.getItem("search-history")) || []
);
Expand Down Expand Up @@ -128,25 +128,26 @@ function GlobalSearchList({ inputValue }) {
setRecentItems(JSON.parse(localStorage.getItem("search-history")) || []);
}

const SearchSuggestionEl = (
<Box
sx={{
pt: 1,
}}
>
<GlobalSearchListHeader listHeader="Search Suggestions" />
<List tabIndex={-1}>
{searchSuggestions.map(item => (
<GlobalSearchListItem
key={item.id || item.symbol}
item={item}
onItemClick={handleItemClick}
isTopHit={item.type === "topHit"}
/>
))}
</List>
</Box>
);
const SearchSuggestionEl =
searchSuggestions.length > 0 ? (
Copy link
Contributor

@chinmehta chinmehta May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is good to have a condition, although it is preferred that we modify and pass EXAMPLES from Genetics > HomePage.tsx so that we don't see empty search container.

<Box
sx={{
pt: 1,
}}
>
<GlobalSearchListHeader listHeader="Search Suggestions" />
<List tabIndex={-1}>
{searchSuggestions.map(item => (
<GlobalSearchListItem
key={item.id || item.symbol}
item={item}
onItemClick={handleItemClick}
isTopHit={item.type === "topHit"}
/>
))}
</List>
</Box>
) : null;

useEffect(() => {
focusOnItem();
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/GlobalSearch/SearchContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function SearchProvider({
children,
searchQuery,
searchPlaceholder = "Search...",
searchSuggestions,
searchSuggestions = [],
}: GlobalSearchProviderProps) {
const [open, setOpen] = useState(false);

Expand Down
7 changes: 4 additions & 3 deletions packages/ui/src/components/GlobalSearch/utils/searchUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ export const formatSearchData = unformattedData => {

Object.entries(unformattedData).forEach(([key, value]) => {
const typesArray = [];
// OpenTargets Genetics search format
if (isArray(value)) {
value.map(i =>
typesArray.push({
type: key === "topHit" ? "topHit" : key,
entity: key,
type: key === "topHit" ? "topHit" : i.__typename.toLowerCase(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apollo primarily provides __typename for its own functions like caching, we should try to use the response that is present in the schema.

entity: i.__typename.toLowerCase(),
...flattenObj(i),
})
);
// OpenTargets Platform search format
} else if (isArray(value.hits)) {
value.hits.map(i =>
typesArray.push({
Expand All @@ -72,7 +74,6 @@ export const formatSearchData = unformattedData => {
}
if (typesArray.length > 0) formattedData[key] = typesArray;
});

return formattedData;
};

Expand Down
12 changes: 8 additions & 4 deletions packages/ui/src/hooks/useListOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { addSearchToLocalStorage } from "../components/GlobalSearch/utils/search
function useListOption() {
const history = useHistory();

const openListItem = option => {
return option => {
if (!option) return;
const newOption = { ...option };
newOption.type = "recent";
Expand All @@ -13,15 +13,19 @@ function useListOption() {
if (newOption.entity === "search") {
history.push(`/search?q=${newOption.name}&page=1`);
} else if (newOption.entity === "study") {
history.push(`/${newOption.entity}/${newOption.studyId}`);
if (newOption.studyId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at this point we should use switch instead of if conditional statements

history.push(`/${newOption.entity}/${newOption.studyId}`);
} else {
history.push(`/${newOption.entity}/${newOption.id}`);
}
} else if (["gene", "variant"].includes(newOption.entity)) {
history.push(`/${newOption.entity}/${newOption.id}`);
} else {
history.push(
`/${newOption.entity}/${newOption.id}${newOption.entity !== "drug" ? "/associations" : ""}`
);
}
};

return [openListItem];
}

export default useListOption;
Loading