Skip to content

Commit

Permalink
Fix: Search results all show as files (#914)
Browse files Browse the repository at this point in the history
* fix search results folders

* fix lint

* Update packages/files-ui/src/Utils/Constants.ts

Co-authored-by: Ryan Noble <ryanjnoble@gmail.com>

* Apply suggestions from code review

Co-authored-by: Ryan Noble <ryanjnoble@gmail.com>

* Update packages/files-ui/src/Components/Modules/SearchModule.tsx

Co-authored-by: Tanmoy Basak Anjan <tanmoy3399@gmail.com>

* Update packages/files-ui/src/Components/Modules/SearchModule.tsx

Co-authored-by: Tanmoy Basak Anjan <tanmoy3399@gmail.com>

Co-authored-by: Ryan Noble <ryanjnoble@gmail.com>
Co-authored-by: Tanmoy Basak Anjan <tanmoy3399@gmail.com>
  • Loading branch information
3 people authored Apr 12, 2021
1 parent da1dc7e commit 78c7241
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const SearchFileBrowser: React.FC<IFilesBrowserModuleProps> = ({ controls = fals

const pathContents: FileSystemItem[] = searchResults.map((searchResult) => ({
...searchResult.content,
isFolder: searchResult.content.content_type === CONTENT_TYPES.Directory
isFolder: searchResult.content.content_type === CONTENT_TYPES.Directory || searchResult.content.content_type === CONTENT_TYPES.Directory2
}))

const itemOperations: IFilesTableBrowserProps["itemOperations"] = useMemo(() => ({
Expand Down
39 changes: 18 additions & 21 deletions packages/files-ui/src/Components/Modules/SearchModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,8 @@ const SearchModule: React.FC<ISearchModule> = ({
themeKey
})

const [searchString, setSearchString] = useState<string>("")
const [searchStringCallback, setSearchStringCallback] = useState<string>("")
const [searchResults, setSearchResults] = useState<SearchEntry[]>([])
const [searchQuery, setSearchQuery] = useState<string>("")
const [searchResults, setSearchResults] = useState<{results: SearchEntry[]; query: string} | undefined>(undefined)
const ref = useRef(null)
const { getSearchResults, currentSearchBucket } = useDrive()

Expand All @@ -159,8 +158,7 @@ const SearchModule: React.FC<ISearchModule> = ({
const onSearch = async (searchString: string) => {
try {
const results = await getSearchResults(searchString)
setSearchResults(results)
setSearchStringCallback(searchString)
setSearchResults({ results, query: searchString })
} catch (err) {
//
}
Expand All @@ -173,7 +171,7 @@ const SearchModule: React.FC<ISearchModule> = ({
])

const onSearchChange = (searchString: string) => {
setSearchString(searchString)
setSearchQuery(searchString)
debouncedSearch(searchString)
}

Expand All @@ -186,30 +184,30 @@ const SearchModule: React.FC<ISearchModule> = ({
const onSearchSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
setSearchActive(false)
redirect(ROUTE_LINKS.Search(searchString))
redirect(ROUTE_LINKS.Search(searchQuery))
}

const searchResultsFiles = searchResults.filter(
const searchResultsFiles = searchResults?.results.filter(
(searchResult) =>
searchResult.content.content_type !== CONTENT_TYPES.Directory
searchResult.content.content_type !== CONTENT_TYPES.Directory &&
searchResult.content.content_type !== CONTENT_TYPES.Directory2
)

const searchResultsFolders = searchResults.filter(
const searchResultsFolders = searchResults?.results.filter(
(searchResult) =>
searchResult.content.content_type === CONTENT_TYPES.Directory
searchResult.content.content_type === CONTENT_TYPES.Directory ||
searchResult.content.content_type === CONTENT_TYPES.Directory2
)

const onSearchEntryClickFolder = (searchEntry: SearchEntry) => {
redirect(ROUTE_LINKS.Home(searchEntry.path))
setSearchString("")
setSearchStringCallback("")
setSearchQuery("")
setSearchActive(false)
}

const onSearchEntryClickFile = (searchEntry: SearchEntry) => {
redirect(ROUTE_LINKS.Home(getParentPathFromFilePath(searchEntry.path)))
setSearchString("")
setSearchStringCallback("")
setSearchQuery("")
setSearchActive(false)
}

Expand Down Expand Up @@ -242,18 +240,17 @@ const SearchModule: React.FC<ISearchModule> = ({
placeholder={t`Search...`}
/>
</form>
{searchString && searchStringCallback ? (
{searchQuery && searchResults?.query ? (
<div
className={clsx(classes.resultsContainer, searchActive && "active")}
>
<div className={classes.resultsBox}>
{searchStringCallback && !searchResults.length ? (
{searchResults?.query && !searchResults.results.length ? (
<Typography className={classes.noResultsFound}>
<Trans>No search results for </Trans>
{searchStringCallback}
<Trans>No search results for </Trans>{` ${searchResults.query}`}
</Typography>
) : null}
{searchResultsFiles.length ? (
{searchResultsFiles && searchResultsFiles.length ? (
<div>
<div className={classes.resultHead}>
<Typography
Expand All @@ -277,7 +274,7 @@ const SearchModule: React.FC<ISearchModule> = ({
))}
</div>
) : null}
{searchResultsFolders.length ? (
{searchResultsFolders && searchResultsFolders.length ? (
<div>
<div className={classes.resultHeadFolder}>
<Typography
Expand Down
1 change: 1 addition & 0 deletions packages/files-ui/src/Utils/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum CONTENT_TYPES {
Directory = "application/chainsafe-files-directory",
Directory2 = "application/chainsafe-files-entry",
File = "*/*",
Image = "image/*",
Text = "text/*",
Expand Down
Loading

0 comments on commit 78c7241

Please sign in to comment.