-
Notifications
You must be signed in to change notification settings - Fork 0
Search function #29
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
Merged
Merged
Search function #29
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ee62553
added search api, console logs the searches
levilevente 97a7fb8
lint fix
levilevente 66bcac4
basic search with results top5 displayed done
levilevente 73e7daa
lint fix
levilevente 61259a3
now if clicked on an item it will be console loged
levilevente 97ca921
page for the search result done
levilevente 1fbe0c5
Searched page implemented
levilevente 1bb146b
Added HTML sanitize for descriptions with HTML content in it
levilevente 11662ca
Added config to dompurify, fixed issues
levilevente File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
| import axios from 'axios'; | ||
|
|
||
| import type { | ||
| NasaImageAndVideoLibraryItemAssetType, | ||
| NasaImageAndVideoLibraryItemMetadataType, | ||
| NasaImageAndVideoLibraryType, | ||
| } from '../types/NasaImageAndVideoLibraryType.ts'; | ||
|
|
||
| export const nasaImageAndVideoLibraryApi = axios.create({ | ||
| baseURL: `https://images-api.nasa.gov`, | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
|
|
||
| export async function searchNasaLibrary(query: string): Promise<NasaImageAndVideoLibraryType> { | ||
| const res = await nasaImageAndVideoLibraryApi.get<NasaImageAndVideoLibraryType>('/search', { | ||
| params: { q: query }, | ||
| }); | ||
| return res.data; | ||
| } | ||
|
|
||
| export async function searchNasaLibraryAsset(nasaId: string): Promise<NasaImageAndVideoLibraryItemAssetType> { | ||
| const res = await nasaImageAndVideoLibraryApi.get<NasaImageAndVideoLibraryItemAssetType>( | ||
| `/asset/${encodeURIComponent(nasaId)}`, | ||
| ); | ||
| return res.data; | ||
| } | ||
|
|
||
| export async function searchNasaLibraryMetadata(nasaId: string): Promise<NasaImageAndVideoLibraryItemMetadataType> { | ||
| const res1 = await nasaImageAndVideoLibraryApi.get<{ location: string }>(`/metadata/${encodeURIComponent(nasaId)}`); | ||
| const location: string = res1.data.location; | ||
| const res2 = await axios.get<NasaImageAndVideoLibraryItemMetadataType>(location); | ||
| return res2.data; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,20 @@ | ||
| .formattedText { | ||
| /* 1. This preserves the \n characters as actual line breaks */ | ||
| white-space: pre-wrap; | ||
|
|
||
| /* 2. Standard formatting */ | ||
| font-family: inherit; | ||
| line-height: 1.6; | ||
| color: inherit; | ||
| } | ||
|
|
||
| /* 3. Make sure the links inside look nice and don't overflow */ | ||
| .formattedText a { | ||
| color: #0d6efd; /* Bootstrap Blue */ | ||
| text-decoration: none; | ||
| word-break: break-word; /* Prevents long URLs from breaking mobile layout */ | ||
| } | ||
|
|
||
| .formattedText a:hover { | ||
| text-decoration: underline; | ||
| } |
This file contains hidden or 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,32 @@ | ||
| import DOMPurify from 'dompurify'; | ||
|
|
||
| import style from './SafeHtml.module.css'; | ||
|
|
||
| interface SafeHtmlProps { | ||
| htmlContent?: string; | ||
| } | ||
|
|
||
| interface DomPurifyConfig { | ||
| ALLOWED_TAGS?: string[]; | ||
| ALLOWED_ATTR?: string[]; | ||
| [key: string]: unknown; | ||
| } | ||
|
|
||
| const SafeHtml = ({ htmlContent }: SafeHtmlProps) => { | ||
| const sanitizeFn = ( | ||
| DOMPurify as unknown as { | ||
| sanitize: (dirty: string, config?: DomPurifyConfig) => string; | ||
| } | ||
| ).sanitize; | ||
|
|
||
| const DOMPURIFY_CONFIG: DomPurifyConfig = { | ||
| ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a', 'ul', 'ol', 'li', 'p', 'br', 'span', 'code', 'pre'], | ||
| ALLOWED_ATTR: ['href', 'title', 'target', 'rel', 'class'], | ||
| }; | ||
|
|
||
| const cleanHtml: string = sanitizeFn(htmlContent ?? '', DOMPURIFY_CONFIG); | ||
|
|
||
| return <div className={style.formattedText} dangerouslySetInnerHTML={{ __html: cleanHtml }} />; | ||
| }; | ||
|
|
||
| export default SafeHtml; |
This file contains hidden or 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,25 @@ | ||
| .searchResultsContainer { | ||
| position: absolute; | ||
| top: 100%; | ||
| left: 0; | ||
| width: 250px; | ||
| max-height: 400px; | ||
| overflow-y: auto; | ||
| background-color: #fff; | ||
| border: 1px solid #ced4da; | ||
| border-radius: 0.375rem; | ||
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | ||
| z-index: 1000; | ||
| margin-top: 5px; | ||
| } | ||
|
|
||
| .searchResultsItem { | ||
| padding: 10px; | ||
| color: #212529; | ||
| border-bottom: 1px solid #e9ecef; | ||
| cursor: pointer; | ||
| font-size: 0.9rem; | ||
| transition: background-color 0.2s; | ||
| text-decoration: none; | ||
| display: block; | ||
| } |
This file contains hidden or 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,65 @@ | ||
| import { useNavigate } from 'react-router'; | ||
|
|
||
| import type { | ||
| NasaImageAndVideoLibraryItemType, | ||
| NasaImageAndVideoLibraryType, | ||
| } from '../../types/NasaImageAndVideoLibraryType.ts'; | ||
| import style from './SearchResults.module.css'; | ||
|
|
||
| interface SearchResultsProps { | ||
| results: NasaImageAndVideoLibraryType | null; | ||
| searchClosedOrSearched: () => void; | ||
| } | ||
|
|
||
| function SearchResults(props: SearchResultsProps) { | ||
| const { results, searchClosedOrSearched } = props; | ||
| const items: NasaImageAndVideoLibraryItemType[] = results ? getDistinctItemsByTitle(results.collection.items) : []; | ||
| const navigate = useNavigate(); | ||
|
|
||
| const clickedItem = async (item: NasaImageAndVideoLibraryItemType) => { | ||
| searchClosedOrSearched(); | ||
| await navigate(`/search/item/${item.data[0].nasa_id}`); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={style.searchResultsContainer}> | ||
| {items.length === 0 ? ( | ||
| <p>Error FIX IT</p> | ||
| ) : ( | ||
| /*Only the top 5 distinct based on title*/ | ||
| items.slice(0, 5).map((item, i) => ( | ||
| <div | ||
| className={style.searchResultsItem} | ||
| key={`${item.data[0].title}-${i}`} | ||
| onClick={() => void clickedItem(item)} | ||
| role="button" | ||
| tabIndex={0} | ||
| onKeyDown={(e) => { | ||
| if (e.key === 'Enter' || e.key === ' ') { | ||
levilevente marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (e.key === ' ') { | ||
| e.preventDefault(); | ||
| } | ||
| void clickedItem(item); | ||
| } | ||
| }} | ||
| > | ||
| <p>{item.data[0].title}</p> | ||
| </div> | ||
| )) | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function getDistinctItemsByTitle(items: NasaImageAndVideoLibraryItemType[]): NasaImageAndVideoLibraryItemType[] { | ||
| const titleMap = new Map<string, NasaImageAndVideoLibraryItemType>(); | ||
| for (const item of items) { | ||
| const title = item.data[0].title; | ||
| if (!titleMap.has(title)) { | ||
| titleMap.set(title, item); | ||
| } | ||
| } | ||
| return Array.from(titleMap.values()); | ||
| } | ||
|
|
||
| export default SearchResults; | ||
This file contains hidden or 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,60 @@ | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| import { searchNasaLibraryAsset, searchNasaLibraryMetadata } from '../api/nasaImageAndVideoLibrary.api.ts'; | ||
| import type { | ||
| NasaImageAndVideoLibraryItemAssetType, | ||
| NasaImageAndVideoLibraryItemMetadataType, | ||
| } from '../types/NasaImageAndVideoLibraryType.ts'; | ||
|
|
||
| function returnImagesOnly(assets: NasaImageAndVideoLibraryItemAssetType) { | ||
| return assets.collection.items.filter((item) => { | ||
| return item.href.endsWith('.jpg') || item.href.endsWith('.png') || item.href.endsWith('.jpeg'); | ||
| }); | ||
| } | ||
|
|
||
| export function useNasaItem(nasaId: string | undefined) { | ||
| const [metadata, setMetadata] = useState<NasaImageAndVideoLibraryItemMetadataType | null>(null); | ||
| const [assets, setAssets] = useState<NasaImageAndVideoLibraryItemAssetType | null>(null); | ||
| const [loading, setLoading] = useState<boolean>(false); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [image, setImage] = useState<string | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!nasaId) return; | ||
|
|
||
| let isMounted = true; | ||
|
|
||
| const fetchData = async () => { | ||
| setLoading(true); | ||
| setError(null); | ||
| try { | ||
| const [metadataResponse, assetsResponse] = await Promise.all([ | ||
| searchNasaLibraryMetadata(nasaId), | ||
| searchNasaLibraryAsset(nasaId), | ||
| ]); | ||
| if (isMounted) { | ||
| setMetadata(metadataResponse); | ||
| setAssets(assetsResponse); | ||
| const images = returnImagesOnly(assetsResponse); | ||
| setImage(images.length > 0 ? images[0].href : null); | ||
| } | ||
| } catch (err) { | ||
| if (isMounted) { | ||
| setError((err as Error).message); | ||
| } | ||
| } finally { | ||
| if (isMounted) { | ||
| setLoading(false); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| void fetchData(); | ||
|
|
||
| return () => { | ||
| isMounted = false; | ||
| }; | ||
| }, [nasaId]); | ||
|
|
||
| return { metadata, assets, image, loading, error }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.