-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #696 from cozy/improvements
- Loading branch information
Showing
10 changed files
with
153 additions
and
75 deletions.
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
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
5 changes: 1 addition & 4 deletions
5
...ents/Sections/components/StoreAppItem.jsx → src/ducks/apps/components/StoreAppItem.jsx
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
File renamed without changes.
File renamed without changes.
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,85 @@ | ||
import React, { useCallback, useMemo } from 'react' | ||
import sortBy from 'lodash/sortBy' | ||
|
||
import Icon from 'cozy-ui/transpiled/react/Icon' | ||
import Label from 'cozy-ui/transpiled/react/Label' | ||
import { useI18n } from 'cozy-ui/transpiled/react/I18n' | ||
import useBreakpoints from 'cozy-ui/transpiled/react/hooks/useBreakpoints' | ||
import InputGroup from 'cozy-ui/transpiled/react/InputGroup' | ||
import Input from 'cozy-ui/transpiled/react/Input' | ||
|
||
import flag from 'cozy-flags' | ||
|
||
import StoreAppItem from 'ducks/apps/components/StoreAppItem' | ||
|
||
import { dumpMatches } from 'ducks/search/utils' | ||
|
||
export const SearchField = ({ onChange, value }) => { | ||
const { t } = useI18n() | ||
const { isMobile } = useBreakpoints() | ||
const handleChange = useCallback( | ||
ev => { | ||
onChange(ev.target.value) | ||
}, | ||
[onChange] | ||
) | ||
return ( | ||
<> | ||
{!isMobile ? ( | ||
<Label className="u-di u-mr-half" htmlFor="discover-search"> | ||
{t('discover-search-field.label')} | ||
</Label> | ||
) : null} | ||
<InputGroup | ||
className={isMobile ? '' : 'u-mb-1'} | ||
prepend={ | ||
isMobile ? ( | ||
<Icon icon="magnifier" className="u-pl-1 u-coolGrey" /> | ||
) : null | ||
} | ||
> | ||
<Input | ||
id="discover-search" | ||
placeholder={t('discover-search-field.placeholder')} | ||
onChange={handleChange} | ||
type="text" | ||
value={value} | ||
/> | ||
</InputGroup> | ||
</> | ||
) | ||
} | ||
|
||
export const SearchResults = ({ searchResults, onAppClick }) => { | ||
const sortedSortResults = useMemo(() => { | ||
return sortBy(searchResults, result => result.score) | ||
}, [searchResults]) | ||
return ( | ||
<div className="u-mv-1 u-flex u-flex-wrap"> | ||
{sortedSortResults.map(result => { | ||
const app = result.item | ||
return flag('store.show-search-score') ? ( | ||
<div> | ||
<StoreAppItem | ||
onClick={() => onAppClick(app.slug)} | ||
key={app.slug} | ||
app={app} | ||
/> | ||
<small | ||
onClick={() => dumpMatches(result)} | ||
title={JSON.stringify(result.matches, null, 2)} | ||
> | ||
{result.score.toFixed(2)} | ||
</small> | ||
</div> | ||
) : ( | ||
<StoreAppItem | ||
onClick={() => onAppClick(app.slug)} | ||
key={app.slug} | ||
app={app} | ||
/> | ||
) | ||
})} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Given a list of intervals, will return all the intervals | ||
* along with the implicit intervals that are between the original | ||
* intervals. If the interval was in the original input, it will | ||
* be returned with { mark: true}, else if it is an implied | ||
* interval, it will be returned with { mark: false }. | ||
*/ | ||
export const fillIntervals = (intervals, maxIndex) => { | ||
let cur = 0 | ||
let res = [] | ||
for (let idx of intervals) { | ||
if (idx[0] != cur) { | ||
res.push({ idx: [cur, idx[0]], mark: false }) | ||
} | ||
res.push({ idx, mark: true }) | ||
cur = idx[1] | ||
} | ||
if (cur != maxIndex) { | ||
res.push({ idx: [cur, maxIndex], mark: false }) | ||
} | ||
|
||
return res | ||
} | ||
|
||
/** Dumps a fuse.js in the console and will highlight the part that matched */ | ||
export const dumpMatches = result => { | ||
for (let m of result.matches) { | ||
const allindices = fillIntervals(m.indices, m.value.length) | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
allindices | ||
.map(o => { | ||
const [start, end] = o.idx | ||
const substr = m.value.substring(start, end) | ||
return o.mark ? `**${substr}**` : substr | ||
}) | ||
.join('') | ||
) | ||
} | ||
} |
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,13 @@ | ||
import { fillIntervals } from './utils' | ||
|
||
test('fillIntervals', () => { | ||
expect(fillIntervals([[1, 2], [4, 5], [9, 12]], 15)).toEqual([ | ||
{ idx: [0, 1], mark: false }, | ||
{ idx: [1, 2], mark: true }, | ||
{ idx: [2, 4], mark: false }, | ||
{ idx: [4, 5], mark: true }, | ||
{ idx: [5, 9], mark: false }, | ||
{ idx: [9, 12], mark: true }, | ||
{ idx: [12, 15], mark: false } | ||
]) | ||
}) |
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