Skip to content

Commit

Permalink
#146 integrate v2 search
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonatai committed Jan 25, 2024
1 parent 4d1b2e9 commit b6a328d
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 162 deletions.
18 changes: 6 additions & 12 deletions src/Hooks/useGenerateSearchEntries.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { useEffect, useState } from 'react';
import { ISpecification } from 'spock-react-types';
import {
IMinimizedSummaryEntry,
ISearchEntry,
} from 'spock-react/components/search-types';
import { IMinimizedSummaryEntry } from 'spock-react/components/search-types';
import { IGenerateSearchEntries } from 'spock-react/hooks-types';

import { cleanedSearchData } from '../components/Search/generateSearchEntries';

export const useGenerateSearchEntries = (
props: IGenerateSearchEntries
): ISearchEntry[] | null => {
): IMinimizedSummaryEntry[] | null => {
const { summary } = props;

const [searchEntries, setSearchEntries] = useState<ISearchEntry[] | null>(
null
);
const [searchEntries, setSearchEntries] = useState<
IMinimizedSummaryEntry[] | null
>(null);

useEffect(() => {
const minimizedSummary = summary?.specifications.map(
Expand All @@ -27,8 +22,7 @@ export const useGenerateSearchEntries = (
})
);

minimizedSummary !== undefined &&
setSearchEntries(cleanedSearchData(minimizedSummary));
minimizedSummary !== undefined && setSearchEntries(minimizedSummary);
}, [summary]);

return searchEntries;
Expand Down
4 changes: 2 additions & 2 deletions src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './styles.css';

import { useState } from 'react';
import { Dialog, DialogBackdrop, Separator, useDialogState } from 'reakit';
import { ISearch, ISearchHit } from 'spock-react/components/search-types';
import { IScore, ISearch } from 'spock-react/components/search-types';

import { SearchButton } from './SearchButton';
import { SearchFooter } from './SearchFooter';
Expand All @@ -14,7 +14,7 @@ export const Search = (props: ISearch): JSX.Element => {

const dialog = useDialogState();

const [searchHits, setSearchHits] = useState<ISearchHit[] | null>(null);
const [searchHits, setSearchHits] = useState<IScore[] | null>(null);
const [searchInput, setSearchInput] = useState('');

return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/Search/SearchHits/SearchCard/SearchCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const SearchCard = (props: ISearchCard): JSX.Element => {
return (
<Link
key={nanoid()}
to={hit.key}
to={hit.id}
onClick={onClick}
className="search__card"
>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Search/SearchHits/SearchCard/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
declare module 'spock-react/components/search-card-types' {
import { IExecutedFeatures, ISpecification } from 'spock-react-types';
import { ISearchHit } from 'spock-react/components/search-types';
import { IScore } from 'spock-react/components/search-types';

interface ISearchCard {
onClick: (e?: any) => void;
hit: ISearchHit;
hit: IScore;
spec?: ISpecification;
feature?: IExecutedFeatures;
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Search/SearchHits/SearchHits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export const SearchHits = (props: ISearchHits): JSX.Element => {
{searchHits !== null ? (
searchHits.map((hit) => {
const spec = summary.specifications.find(
(spec) => spec.className === hit.key
(spec) => spec.className === hit.id
);
const feature = summary.specifications.map((spec) =>
spec.executedFeatures.find(
(feature) => feature.id === hit.key
(feature) => feature.id === hit.id
)
)[0];

Expand Down
6 changes: 3 additions & 3 deletions src/components/Search/SearchHits/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
declare module 'spock-react/components/search-hits-types' {
import { DialogStateReturn } from 'reakit/ts';
import { ISummary } from 'spock-react-types';
import { ISearchHit } from 'spock-react/components/search-types';
import { IScore } from 'spock-react/components/search-types';

interface ISearchHits {
searchHits: ISearchHit[] | null;
setSearchHits: (searchHits: ISearchHit[] | null) => void;
searchHits: IScore[] | null;
setSearchHits: (searchHits: IScore[] | null) => void;
summary: ISummary;
setSearchInput: (input: string) => void;
dialog: DialogStateReturn;
Expand Down
22 changes: 3 additions & 19 deletions src/components/Search/SearchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import './styles.css';

import { Input } from 'reakit';
import { ISearchInput } from 'spock-react/components/search-input-types';
import { ISearchHit } from 'spock-react/components/search-types';

import { faMagnifyingGlass } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

import { useGenerateSearchEntries } from '../../../Hooks/useGenerateSearchEntries';
import { getSearchScore } from '../getSearchScore';
import { getSearchScoreV2TS } from '../getSearchScoreV2';

export const SearchInput = (props: ISearchInput): JSX.Element => {
const { summary, setSearchHits, setSearchInput, searchInput } = props;
Expand All @@ -21,23 +20,8 @@ export const SearchInput = (props: ISearchInput): JSX.Element => {
} else if (searchInput === '') {
setSearchHits(null);
} else {
const toLowerSearchInput = searchInput.trim().toLowerCase();

const scoreEntries: ISearchHit[] = searchEntries.map((entry) => {
const score = getSearchScore(
toLowerSearchInput,
entry.keywords
);
return {
key: entry.key,
score,
};
});

const hits = scoreEntries.filter((entry) => entry.score > 0);
hits.length > 0
? setSearchHits(hits.sort((a, b) => b.score - a.score))
: setSearchHits(null);
const hits = getSearchScoreV2TS(searchInput, searchEntries);
hits.length > 0 ? setSearchHits(hits) : setSearchHits(null);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/Search/SearchInput/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
declare module 'spock-react/components/search-input-types' {
import { ISummary } from 'spock-react-types';
import { ISearchHit } from 'spock-react/components/search-types';
import { IScore } from 'spock-react/components/search-types';

interface ISearchInput {
summary: ISummary;
setSearchHits: (searchHits: ISearchHit[] | null) => void;
setSearchHits: (searchHits: IScore[] | null) => void;
setSearchInput: (input: string) => void;
searchInput: string;
}
Expand Down
80 changes: 0 additions & 80 deletions src/components/Search/generateSearchEntries.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/components/Search/getSearchScore.ts

This file was deleted.

18 changes: 6 additions & 12 deletions src/components/Search/getSearchScoreV2.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { IMinimizedSummaryEntry } from 'spock-react/components/search-types';

interface IScore {
id: string;
score: number;
featureScores: IFeatureScore[];
}

interface IFeatureScore {
id: string;
score: number;
}
import {
IFeatureScore,
IMinimizedSummaryEntry,
IScore,
} from 'spock-react/components/search-types';

export const getSearchScoreV2TS = (
searchInput: string,
specifications: IMinimizedSummaryEntry[]
Expand Down
18 changes: 9 additions & 9 deletions src/components/Search/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
declare module 'spock-react/components/search-types' {
import { ISummary } from 'spock-react-types';

export interface ISearchHit {
score: number;
key: string;
}

interface ISearch {
summary: ISummary;
}
Expand All @@ -17,9 +12,14 @@ declare module 'spock-react/components/search-types' {
features: Array<{ id: string }>;
}

export interface ISearchEntry {
key: string;
href?: string;
keywords: string[];
interface IScore {
id: string;
score: number;
featureScores: IFeatureScore[];
}

interface IFeatureScore {
id: string;
score: number;
}
}

0 comments on commit b6a328d

Please sign in to comment.