-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic search functionality
- Loading branch information
Showing
7 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Parlance.ClientApp/src/components/search/GlobalSearch.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
.scrim { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
|
||
background: rgba(0, 0, 0, 0.5); | ||
} | ||
|
||
.searchContainer { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
|
||
display: flex; | ||
flex-direction: column; | ||
align-items: stretch; | ||
} | ||
|
||
.searchBoxContainer { | ||
background: var(--background-color); | ||
display: flex; | ||
justify-content: center; | ||
} | ||
|
||
.searchBox.searchBox { | ||
max-width: var(--content-width); | ||
width: 100vw; | ||
|
||
border: none; | ||
outline: none; | ||
font-size: 20pt; | ||
} | ||
|
||
.searchResultsContainer { | ||
background: var(--background-color); | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
padding-bottom: 3px; | ||
padding-top: 20px; | ||
} | ||
|
||
.searchResult { | ||
max-width: var(--content-width); | ||
width: 100vw; | ||
|
||
padding: 3px; | ||
border-radius: var(--border-radius); | ||
cursor: default; | ||
} | ||
|
||
.searchResult:hover { | ||
background: var(--hover-color); | ||
} | ||
|
||
.searchResult:active { | ||
background: var(--active-color); | ||
} | ||
|
||
.selectedSearchResult { | ||
background: var(--hover-color); | ||
} | ||
|
||
.searchTitle { | ||
max-width: var(--content-width); | ||
width: 100vw; | ||
} |
123 changes: 123 additions & 0 deletions
123
Parlance.ClientApp/src/components/search/GlobalSearch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { useEffect, useRef, useState, KeyboardEvent } from "react"; | ||
import Styles from "./GlobalSearch.module.css"; | ||
import { useTranslation } from "react-i18next"; | ||
import Fetch from "@/helpers/Fetch"; | ||
import PageHeading from "@/components/PageHeading"; | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
interface SearchResult { | ||
name: string; | ||
href: string; | ||
type: "project" | "subproject"; | ||
} | ||
|
||
export function GlobalSearch({ | ||
open, | ||
onClose, | ||
}: { | ||
open: boolean; | ||
onClose: () => void; | ||
}) { | ||
const [searchQuery, setSearchQuery] = useState(""); | ||
const [searchResults, setSearchResults] = useState<SearchResult[]>([]); | ||
const [selected, setSelected] = useState<number>(); | ||
const { t } = useTranslation(); | ||
const navigate = useNavigate(); | ||
const searchBoxRef = useRef<HTMLInputElement>(null); | ||
|
||
const activateSearchResult = (result: SearchResult) => { | ||
navigate(result.href); | ||
onClose(); | ||
}; | ||
|
||
useEffect(() => { | ||
(async () => { | ||
const query = searchQuery; | ||
const response = await Fetch.post<SearchResult[]>("/api/search", { | ||
query: query, | ||
}); | ||
|
||
if (searchQuery == query) { | ||
setSelected(undefined); | ||
setSearchResults(response); | ||
} | ||
})(); | ||
}, [searchQuery]); | ||
|
||
useEffect(() => { | ||
if (open) { | ||
searchBoxRef.current?.focus(); | ||
searchBoxRef.current?.select(); | ||
} | ||
}, [open]); | ||
|
||
if (!open) return null; | ||
|
||
const keyPress = (event: KeyboardEvent<HTMLInputElement>) => { | ||
switch (event.key) { | ||
case "ArrowUp": | ||
setSelected(x => | ||
x == undefined | ||
? searchResults.length - 1 | ||
: (x - 1 + searchResults.length) % searchResults.length, | ||
); | ||
event.preventDefault(); | ||
break; | ||
case "ArrowDown": | ||
setSelected(x => | ||
x == undefined ? 0 : (x + 1) % searchResults.length, | ||
); | ||
event.preventDefault(); | ||
break; | ||
case "Enter": | ||
if (searchResults.length != 0) { | ||
activateSearchResult(searchResults[selected ?? 0]); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div className={Styles.scrim} onClick={onClose} /> | ||
<div className={Styles.searchContainer}> | ||
<div className={Styles.searchBoxContainer}> | ||
<input | ||
type={"text"} | ||
value={searchQuery} | ||
onChange={e => setSearchQuery(e.target.value)} | ||
className={Styles.searchBox} | ||
placeholder={t("Search for anything")} | ||
ref={searchBoxRef} | ||
onKeyDown={keyPress} | ||
/> | ||
</div> | ||
<div className={Styles.searchResultsContainer}> | ||
{searchResults.length > 0 && ( | ||
<> | ||
<PageHeading | ||
level={3} | ||
className={Styles.searchTitle} | ||
> | ||
{t("Search Results")} | ||
</PageHeading> | ||
{searchResults.map((result, i) => ( | ||
<div | ||
className={[ | ||
Styles.searchResult, | ||
...(i == selected | ||
? [Styles.selectedSearchResult] | ||
: []), | ||
].join(" ")} | ||
onClick={() => activateSearchResult(result)} | ||
onMouseEnter={() => setSelected(undefined)} | ||
> | ||
{result.name} | ||
</div> | ||
))} | ||
</> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.RateLimiting; | ||
using Parlance.Project; | ||
using Parlance.Services.Projects; | ||
|
||
namespace Parlance.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
[EnableRateLimiting("limiter")] | ||
public class SearchController(IProjectService projectService) : Controller | ||
{ | ||
[HttpPost] | ||
public async Task<IActionResult> Search([FromBody] SearchRequestData data) | ||
{ | ||
if (data.Query == string.Empty) | ||
{ | ||
return Json(Enumerable.Empty<object>()); | ||
} | ||
|
||
var projects = (await projectService.Projects()).ToList(); | ||
var subprojects = projects.SelectMany(project => project.GetParlanceProject().Subprojects); | ||
return Json(projects.Where(project => project.Name.IndexOf(data.Query, StringComparison.InvariantCultureIgnoreCase) > 0).Select(project => new | ||
{ | ||
Name = project.Name, | ||
Href = $"/projects/{project.SystemName}", | ||
Type = "project" | ||
}).Union(subprojects.Where(subproject => subproject.Name.IndexOf(data.Query, StringComparison.InvariantCultureIgnoreCase) > 0).Select(subproject => new | ||
{ | ||
Name = subproject.Name, | ||
Href = $"projects/{subproject.Project.SystemName}/{subproject.SystemName}", | ||
Type = "subproject" | ||
}))); | ||
} | ||
|
||
public class SearchRequestData | ||
{ | ||
public required string Query { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters