-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe1f105
commit 342f660
Showing
36 changed files
with
311 additions
and
203 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
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
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
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 @@ | ||
export * from './DocumentItem'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-disable react/display-name */ | ||
import React from 'react'; | ||
import { Search } from './Search'; | ||
import { MockContext } from '../test/MockContext'; | ||
export default { | ||
title: 'Blocks/Search', | ||
component: Search, | ||
}; | ||
|
||
export const overview = () => ( | ||
<MockContext id="id-of-story"> | ||
<Search /> | ||
</MockContext> | ||
); |
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,90 @@ | ||
/** @jsx jsx */ | ||
import { jsx, Theme } from 'theme-ui'; | ||
import { useState, useContext, useRef, useEffect } from 'react'; | ||
import lunr, { Index } from 'lunr'; | ||
import normalizeUrl from 'normalize-url'; | ||
import { SearchInput } from '@component-controls/components'; | ||
import { | ||
PageType, | ||
defPageType, | ||
Pages, | ||
StoriesDoc, | ||
} from '@component-controls/core'; | ||
import { BlockContext } from '../context'; | ||
import { DocumentItem } from '../DocumentItem'; | ||
|
||
export interface SearchItem { | ||
id: string; | ||
title: string; | ||
type: PageType; | ||
component?: string; | ||
description?: string; | ||
body?: string; | ||
} | ||
export const Search = () => { | ||
const [items, setItems] = useState<Pages>([]); | ||
const [search, setSearch] = useState<string>(''); | ||
const { storeProvider } = useContext(BlockContext); | ||
const lunrRef = useRef<Index | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
if (!lunrRef.current) { | ||
lunrRef.current = lunr(function() { | ||
this.field('title'); | ||
this.field('description'); | ||
this.field('body'); | ||
this.field('author'); | ||
this.field('component'); | ||
storeProvider.pages.forEach(page => { | ||
this.add({ | ||
id: page.title, | ||
title: page.title.replace('/', ' '), | ||
type: page.type || defPageType, | ||
description: page.description, | ||
body: page.source, | ||
author: page.author, | ||
component: Object.keys(page.components).join(' '), | ||
}); | ||
}); | ||
}); | ||
} | ||
const searchResults = lunrRef.current.search(search); | ||
const newItems: Pages = searchResults | ||
.slice(0, 10) | ||
.map((item: { ref: string }) => { | ||
const page = storeProvider.getStoryDoc(item.ref) as StoriesDoc; | ||
return { ...page, id: page.title }; | ||
}); | ||
setItems(newItems); | ||
}, [search, storeProvider]); | ||
const onSearch = (search: string) => { | ||
setSearch(search); | ||
}; | ||
const onSelectItem = (item: StoriesDoc) => { | ||
const newurl = `${window.location.origin}${storeProvider.getPagePath( | ||
item.type, | ||
item.title, | ||
)}`; | ||
if (normalizeUrl(window.location.href) !== normalizeUrl(newurl)) { | ||
window.location.href = newurl; | ||
} | ||
}; | ||
return ( | ||
<SearchInput<StoriesDoc> | ||
aria-label="full text search" | ||
onSearch={onSearch} | ||
items={items} | ||
onSelect={onSelectItem} | ||
placeholder="search..." | ||
> | ||
{props => ( | ||
<DocumentItem | ||
sx={{ borderBottom: (t: Theme) => ` 1px solid ${t.colors?.shadow}` }} | ||
config={storeProvider.config} | ||
link={storeProvider.getPagePath(props.item.type, props.item.title)} | ||
page={props.item} | ||
/> | ||
)} | ||
</SearchInput> | ||
); | ||
}; |
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 @@ | ||
export * from './Search'; |
File renamed without changes.
Oops, something went wrong.