From c66cf83ef134a9818fc02e4416c685d1296e6d3f Mon Sep 17 00:00:00 2001 From: Piotr Janosz Date: Thu, 25 Jul 2019 17:32:56 +0200 Subject: [PATCH] Added types for hits in tools and autocomplete --- .../components/docs/search/autocomplete.tsx | 55 ++++++++++++++----- packages/website/ts/pages/docs/tools.tsx | 49 +++++++++++------ 2 files changed, 72 insertions(+), 32 deletions(-) diff --git a/packages/website/ts/components/docs/search/autocomplete.tsx b/packages/website/ts/components/docs/search/autocomplete.tsx index dd91250607..9d8233295f 100644 --- a/packages/website/ts/components/docs/search/autocomplete.tsx +++ b/packages/website/ts/components/docs/search/autocomplete.tsx @@ -1,7 +1,6 @@ -import React, { useState, useLayoutEffect } from 'react'; +import React, { useState } from 'react'; import Autosuggest from 'react-autosuggest'; import { connectAutoComplete, Highlight, Snippet } from 'react-instantsearch-dom'; -import styled from 'styled-components'; import { Link } from '@0x/react-shared'; @@ -10,6 +9,27 @@ import { AutocompleteWrapper } from 'ts/components/docs/search/autocomplete_wrap import { searchIndex } from 'ts/utils/algolia_search'; +interface IHit { + description: string; + difficulty: string; + id: number | string; + isCommunity?: boolean; + isFeatured?: boolean; + objectID: string; + tags?: string[]; + textContent: string; + title: string; + type?: string; + url: string; + _highlightResult: any; + _snippetResult: any; +} +interface ISnippetMatchLevels { + [index: string]: number; +} + +const SNIPPET_MATCH_LEVELS: ISnippetMatchLevels = { none: 0, partial: 1, full: 2 }; + interface IAutoCompleteProps { isHome?: boolean; hits?: object[]; @@ -17,16 +37,12 @@ interface IAutoCompleteProps { refine?: (value: string) => void; } -interface IHitProps { - [key: string]: any; -} - const CustomAutoComplete: React.FC = ({ isHome = false, hits = [], currentRefinement, refine }) => { const [value, setValue] = useState(''); - const onChange = (event: IHitProps, { newValue }: IHitProps): void => setValue(newValue); + const onChange = (event: React.KeyboardEvent, { newValue }: any): void => setValue(newValue); - const onSuggestionsFetchRequested = ({ value: newValue }: IHitProps): void => refine(newValue); + const onSuggestionsFetchRequested = ({ value: newValue }: any): void => refine(newValue); const onSuggestionsClearRequested = (): void => refine(''); @@ -34,24 +50,33 @@ const CustomAutoComplete: React.FC = ({ isHome = false, hits const onSuggestionHighlighted = (): void => {}; // tslint:disable-next-line: no-empty - const onSuggestionSelected = (event: IHitProps, { suggestion }: IHitProps): void => {}; + const onSuggestionSelected = (event: React.MouseEvent, { suggestion }: any): void => {}; - const getSuggestionValue = (hit: IHitProps): string => hit.textContent; + const getSuggestionValue = (hit: IHit): string => hit.textContent; + + const renderSuggestion = (hit: IHit): React.ReactNode => { + let attributeToSnippet = 'description'; + + const description: string = hit._snippetResult.description.matchLevel; + const textContent: string = hit._snippetResult.textContent.matchLevel; + + if (SNIPPET_MATCH_LEVELS[textContent] > SNIPPET_MATCH_LEVELS[description]) { + attributeToSnippet = 'textContent'; + } - const renderSuggestion = (hit: IHitProps): React.ReactNode => { return (
- + ); }; - const renderSectionTitle = (section: IHitProps): React.ReactNode => { + const renderSectionTitle = (section: any): React.ReactNode => { const { tools, guides } = searchIndex; - const titles: { [key: string]: any } = { + const titles: { [key: string]: string } = { [tools]: 'Tools', [guides]: 'Guides', }; @@ -62,7 +87,7 @@ const CustomAutoComplete: React.FC = ({ isHome = false, hits return null; }; - const getSectionSuggestions = (section: IHitProps): string => section.hits; + const getSectionSuggestions = (section: any): string => section.hits; const inputProps = { placeholder: 'Search docs…', diff --git a/packages/website/ts/pages/docs/tools.tsx b/packages/website/ts/pages/docs/tools.tsx index b0d888e4d0..49471bb392 100644 --- a/packages/website/ts/pages/docs/tools.tsx +++ b/packages/website/ts/pages/docs/tools.tsx @@ -15,6 +15,25 @@ import { Separator } from 'ts/components/docs/separator'; import { searchClient, searchIndex } from 'ts/utils/algolia_search'; +interface IHitsProps { + hits: IHit[]; +} +interface IHit { + description: string; + difficulty: string; + id: number | string; + isCommunity?: boolean; + isFeatured?: boolean; + objectID: string; + tags?: string[]; + textContent: string; + title: string; + type?: string; + url: string; + _highlightResult: any; + _snippetResult: any; +} + export const DocsTools: React.FC = () => { return ( @@ -31,40 +50,36 @@ export const DocsTools: React.FC = () => { ); }; -// @ts-ignore -const getUniqueContentTypes = hits => { +function getUniqueContentTypes(hits: IHit[]): string[] { + const contentTypes: string[] = []; // @ts-ignore - const types = []; - for (const hit of hits) { - // @ts-ignore - if (!types.includes(hit.type)) { - types.push(hit.type); + if (!contentTypes.includes(hit.type)) { + contentTypes.push(hit.type); } } - return types; -}; + return contentTypes; +} -// @ts-ignore -const Hits = ({ hits }) => { - const types = getUniqueContentTypes(hits); - const featuredLinks = hits.filter((hit: any) => hit.isFeatured); +const Hits: React.FC = ({ hits }) => { + const contentTypes = getUniqueContentTypes(hits); + const featuredTools = hits.filter((hit: IHit) => hit.isFeatured); return ( <> - {featuredLinks.length && ( + {featuredTools.length && ( Featured Tools - {featuredLinks.map((link: any, index: number) => ( - + {featuredTools.map((hit: IHit, index: number) => ( + ))} )} - {types.map(type => { + {contentTypes.map(type => { const filteredHits = hits.filter((hit: any) => hit.type === type); return (