Skip to content

Commit 0dd4684

Browse files
committed
Update fuse to latest version
Closes #752 Also refactors search to use the current or default version of the CLI for results. This also refactors the get nav util to make more items private.
1 parent fd531e3 commit 0dd4684

File tree

11 files changed

+314
-262
lines changed

11 files changed

+314
-262
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
},
88
"private": true,
99
"scripts": {
10-
"//": "Fix for parcel segfaults",
1110
"//": "https://github.com/parcel-bundler/parcel/issues/7702",
1211
"gatsby": "PARCEL_WORKERS=0 gatsby",
1312
"develop": "npm run gatsby -- develop",
@@ -42,7 +41,7 @@
4241
"details-element-polyfill": "^2.4.0",
4342
"downshift": "^8.2.2",
4443
"framer-motion": "^10.16.4",
45-
"fuse.js": "^3.6.1",
44+
"fuse.js": "^6.6.2",
4645
"gatsby": "^5.12.7",
4746
"gatsby-plugin-manifest": "^5.12.1",
4847
"gatsby-plugin-mdx": "^5.12.1",

src/components/breadcrumbs.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ import * as getNav from '../util/get-nav'
55
import usePage from '../hooks/use-page'
66

77
const BreadcrumbItem = ({item, path}) => {
8-
const href = getNav.getLocation(item.url)
9-
const selected = getNav.isPathForItem(path, getNav.getItem(href))
8+
const selected = getNav.isPathForItem(path, getNav.getItem(item.url))
109

1110
return (
12-
<PrimerBreadcrumbs.Item as={GatsbyLink} to={href} {...(selected ? {selected} : {})}>
11+
<PrimerBreadcrumbs.Item as={GatsbyLink} to={item.url} {...(selected ? {selected} : {})}>
1312
{item.shortName || item.title}
1413
</PrimerBreadcrumbs.Item>
1514
)
1615
}
1716

1817
const Breadcrumbs = () => {
19-
const {location} = usePage()
20-
const path = getNav.getLocation(location.pathname)
21-
const items = getNav.getItemBreadcrumbs(location.pathname, {hideVariants: true})
18+
const {pathname} = usePage().location
19+
const items = getNav.getItemBreadcrumbs(pathname, {hideVariants: true})
2220

2321
if (items.length <= 1) {
2422
return null
@@ -27,7 +25,7 @@ const Breadcrumbs = () => {
2725
return (
2826
<PrimerBreadcrumbs sx={{mb: 4}}>
2927
{items.map(item => (
30-
<BreadcrumbItem key={item.url} item={item} path={path} />
28+
<BreadcrumbItem key={item.url} item={item} path={pathname} />
3129
))}
3230
</PrimerBreadcrumbs>
3331
)

src/components/nav-items.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ import usePage from '../hooks/use-page'
1010
import useSiteMetadata from '../hooks/use-site-metadata'
1111

1212
const NavItem = ({item, path, depth}) => {
13-
const href = getNav.getLocation(item.url)
14-
const isCurrent = getNav.isActiveUrl(path, href)
15-
const items = getNav.getHierarchy(item, {path: item.url, hideVariants: true})
13+
const isCurrent = getNav.isActiveUrl(path, item.url)
14+
const items = getNav.getHierarchy(item, item.url, {hideVariants: true})
1615

1716
return (
1817
<NavList.Item
1918
as={Link}
20-
to={href}
19+
to={item.url}
2120
defaultOpen={items && isCurrent}
2221
aria-current={isCurrent ? 'page' : null}
2322
sx={{fontSize: depth === 1 ? 2 : 1}}
@@ -54,18 +53,17 @@ const ExternalNavItem = ({title, ...props}) => (
5453
)
5554

5655
const Navigation = () => {
57-
const {location} = usePage()
5856
const {repositoryUrl} = useSiteMetadata()
59-
const path = getNav.getLocation(location.pathname)
60-
const items = getNav.getHierarchy(null, {path, hideVariants: true})
57+
const {pathname} = usePage().location
58+
const items = getNav.getHierarchy(null, pathname, {hideVariants: true})
6159

6260
return (
6361
<>
6462
<VisuallyHidden>
6563
<h3>Site navigation</h3>
6664
</VisuallyHidden>
6765
<NavList aria-label="Site">
68-
<NavItems items={items} path={path} />
66+
<NavItems items={items} path={pathname} />
6967
<NavList.Divider />
7068
{headerNavItems.map(item => (
7169
<Box key={item.title} sx={{display: ['flex', null, null, 'none']}}>

src/components/search-results.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import * as getNav from '../util/get-nav'
55

66
const Breadcrumbs = ({item}) => {
77
const siteMetadata = useSiteMetadata()
8-
const hierarchy = getNav.getItemBreadcrumbs(item.path).slice(0, -1)
8+
const variant = getNav.getVariant(getNav.getVariantRoot(item.path), item.path)
9+
const hierarchy = getNav.getItemBreadcrumbs(item.path)
10+
11+
// keep the variant in the breadcrumb if we have one and its not the
12+
// same as the last breadcrumb. this makes sure that variant index pages
13+
// don't all appear the same in the search results
14+
if (!variant || variant !== hierarchy[hierarchy.length - 1].shortName) {
15+
hierarchy.pop()
16+
}
917

1018
const text = hierarchy.length ? hierarchy.map(s => s.shortName || s.title).join(' / ') : siteMetadata.shortName
1119

src/components/variant-select.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ const VariantMenu = ({title, latest, current, prerelease, legacy, path}) => {
5959
{current && <VariantItem {...current} />}
6060
{prerelease && <VariantItem {...prerelease} />}
6161
</ActionList.Group>
62-
{legacy && (
63-
<ActionList.Group title="Legacy">
64-
{legacy.map(item => (
65-
<VariantItem key={item.title} {...item} />
66-
))}
67-
</ActionList.Group>
68-
)}
62+
<ActionList.Group title="Legacy">
63+
{legacy.map(item => (
64+
<VariantItem key={item.title} {...item} />
65+
))}
66+
</ActionList.Group>
6967
</ActionList>
7068
</ActionMenu.Overlay>
7169
</ActionMenu>
@@ -74,13 +72,10 @@ const VariantMenu = ({title, latest, current, prerelease, legacy, path}) => {
7472
}
7573

7674
const useVariants = () => {
77-
const {pathname} = usePage().location
75+
const {pathname: path} = usePage().location
7876

7977
return React.useMemo(() => {
80-
const root = getNav.getVariantRoot(pathname)
81-
const path = getNav.getPath(pathname)
82-
const vp = getNav.getVariantAndPage(root, path)
83-
const variantPages = vp ? getNav.getVariantsForPage(root, vp.page) : []
78+
const variantPages = getNav.getVariantsForPath(path)
8479

8580
if (!variantPages.length) {
8681
return null
@@ -106,7 +101,7 @@ const useVariants = () => {
106101
break
107102
default:
108103
result.legacy.push(item)
109-
typeDesc = ' Legacy'
104+
typeDesc = ' (Legacy)'
110105
}
111106
if (item.active) {
112107
result.title = `${item.title}${typeDesc}`
@@ -116,7 +111,7 @@ const useVariants = () => {
116111
result.legacy.sort((a, b) => parseInt(b.shortName.slice(1)) - parseInt(a.shortName.slice(1)))
117112

118113
return result
119-
}, [pathname])
114+
}, [path])
120115
}
121116

122117
const VariantSelect = () => {

src/hooks/use-search.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import React from 'react'
22
import {useCombobox} from 'downshift'
33
import {navigate, graphql, useStaticQuery} from 'gatsby'
44
import {useIsMobile} from './use-breakpoint'
5+
import usePage from './use-page'
6+
import * as getNav from '../util/get-nav'
7+
8+
// This worker can live for the entire duraction of the site
9+
const WORKER = new Worker(new URL('../util/search.worker.js', import.meta.url))
10+
const CLI_ROOT = '/cli'
511

612
const useSearchData = () => {
7-
// TODO: this static query runs for all pages changes in dev mode. When not
8-
// testing search explicitly it should be replaced with some static data.
9-
const rawData = useStaticQuery(graphql`
13+
const data = useStaticQuery(graphql`
1014
{
1115
allMdx {
1216
nodes {
@@ -27,15 +31,13 @@ const useSearchData = () => {
2731
`)
2832

2933
return React.useMemo(() => {
30-
const mdxNodes = rawData.allMdx.nodes.reduce((map, obj) => {
34+
const mdxNodes = data.allMdx.nodes.reduce((map, obj) => {
3135
map[obj.id] = obj
3236
return map
3337
}, {})
3438

35-
return rawData.allSitePage.nodes
36-
.filter(node => {
37-
return node.pageContext && node.pageContext.mdxId && mdxNodes[node.pageContext.mdxId] != null
38-
})
39+
return data.allSitePage.nodes
40+
.filter(node => mdxNodes[node.pageContext?.mdxId] != null)
3941
.map(node => {
4042
const mdxNode = mdxNodes[node.pageContext.mdxId]
4143
return {
@@ -44,53 +46,58 @@ const useSearchData = () => {
4446
body: mdxNode.body,
4547
}
4648
})
47-
}, [rawData])
49+
}, [data])
4850
}
4951

50-
function useSearch() {
51-
const isMobile = useIsMobile()
52-
53-
const queryRef = React.useRef()
54-
const workerRef = React.useRef()
52+
const useCliVersion = () => {
53+
return getNav.getCurrentOrDefaultVariant(
54+
getNav.getItem(getNav.getVariantRoot(`${CLI_ROOT}/`, {stripTrailing: false})),
55+
usePage().location.pathname,
56+
)
57+
}
5558

59+
function useSearch() {
5660
const [query, setQuery] = React.useState()
57-
const [items, setItems] = React.useState(null)
58-
59-
const searchData = useSearchData()
61+
const [results, setResults] = React.useState(null)
62+
const queryRef = React.useRef()
63+
const items = useSearchData()
64+
const isMobile = useIsMobile()
65+
const {url: cliUrl} = useCliVersion()
6066

6167
const handleSearchResults = React.useCallback(({data}) => {
68+
if (data.debug) {
69+
console.log(data.debug)
70+
}
6271
if (data.query && data.results && data.query === queryRef.current) {
63-
setItems(data.results)
72+
setResults(data.results)
6473
}
6574
}, [])
6675

6776
React.useEffect(() => {
68-
const worker = new Worker(new URL('../util/search.worker.js', import.meta.url))
69-
workerRef.current = worker
77+
WORKER.addEventListener('message', handleSearchResults)
78+
}, [handleSearchResults])
7079

71-
worker.addEventListener('message', handleSearchResults)
72-
worker.postMessage({data: searchData})
80+
React.useEffect(() => {
81+
WORKER.postMessage({items})
82+
}, [items])
7383

74-
return () => worker.terminate()
75-
}, [searchData, handleSearchResults])
84+
React.useEffect(() => {
85+
WORKER.postMessage({cli: {root: CLI_ROOT, current: cliUrl}})
86+
}, [cliUrl])
7687

7788
React.useEffect(() => {
7889
queryRef.current = query
7990

8091
if (query) {
81-
workerRef.current.postMessage({query})
92+
WORKER.postMessage({query})
8293
} else {
83-
setItems(null)
94+
setResults(null)
8495
}
8596
}, [query])
8697

8798
const combobox = useCombobox({
8899
id: 'search-box',
89-
// We don't need Downshift to keep track of a selected item because as
90-
// soon as an item is selected we navigate to a new page.
91-
// Let's avoid any unexpected states related to the selected item
92-
// by setting it to always be `null`.
93-
items: items || [],
100+
items: results || [],
94101
onInputValueChange: ({inputValue}) => setQuery(inputValue),
95102
onSelectedItemChange: ({selectedItem}) => {
96103
if (selectedItem) {
@@ -126,8 +133,8 @@ function useSearch() {
126133

127134
return {
128135
...combobox,
129-
results: items,
130-
isOpen: !!(combobox.isOpen && items),
136+
results,
137+
isOpen: !!(combobox.isOpen && results),
131138
}
132139
}
133140

src/mdx/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react'
22
import {Box, Heading, themeGet, Text, Octicon} from '@primer/react'
3-
import {withPrefix} from 'gatsby'
43
import styled from 'styled-components'
54
import {variant as styledVariant} from 'styled-system'
65
import {LinkIcon} from '@primer/octicons-react'
@@ -312,9 +311,7 @@ export const Image = styled(RequiredImage)`
312311
box-sizing: content-box;
313312
`
314313

315-
const ScreenshotImage = ({src, ...props}) => <RequiredImage src={withPrefix(src)} {...props} />
316-
317-
export const Screenshot = styled(ScreenshotImage)`
314+
export const Screenshot = styled(RequiredImage)`
318315
margin-top: 15px;
319316
max-width: min(100%, 525px);
320317
max-height: 300px;

src/mdx/nav-hierarchy.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,41 @@ import Link from '../components/link'
44
import * as getNav from '../util/get-nav'
55
import usePage from '../hooks/use-page'
66

7-
const HierarchyItem = ({item, depth, ...props}) => {
8-
const hierarchy = getNav.getHierarchy(item, props)
7+
const HierarchyItem = ({item, maxDepth, depth, hideVariants}) => {
8+
const hierarchy = getNav.getHierarchy(item, null, {hideVariants})
99

1010
return (
1111
<Box as="li" key={item.url}>
1212
<Link key={item.title} to={item.url}>
1313
{item.title}
1414
</Link>
1515
{item.description ? <Box sx={{fontSize: 1, mb: 1}}>{item.description}</Box> : null}
16-
{hierarchy ? <Hierarchy items={hierarchy} depth={depth + 1} {...props} /> : null}
16+
{hierarchy ? (
17+
<Hierarchy items={hierarchy} maxDepth={maxDepth} depth={depth + 1} hideVariants={hideVariants} />
18+
) : null}
1719
</Box>
1820
)
1921
}
2022

21-
const Hierarchy = ({items, ...props}) => {
22-
if (props.maxDepth && props.depth > props.maxDepth) {
23+
const Hierarchy = ({items, maxDepth, depth, hideVariants}) => {
24+
if (maxDepth && depth > maxDepth) {
2325
return null
2426
}
2527

2628
return (
2729
<Box as="ul">
2830
{items.map(item => (
29-
<HierarchyItem key={item.url} item={item} {...props} />
31+
<HierarchyItem key={item.url} item={item} maxDepth={maxDepth} depth={depth} hideVariants={hideVariants} />
3032
))}
3133
</Box>
3234
)
3335
}
3436

35-
function NavHierarchy({depth, ...props}) {
36-
const {location} = usePage()
37-
const path = getNav.getLocation(location.pathname)
38-
const navRoot = path.replace(/\/+$/g, '')
37+
function NavHierarchy({depth, hideVariants}) {
38+
const {pathname} = usePage().location
39+
const hierarchy = getNav.getHierarchy(getNav.getItem(pathname), null, {hideVariants})
3940

40-
const rootItem = getNav.getItem(navRoot)
41-
const hierarchy = getNav.getHierarchy(rootItem, props)
42-
43-
return <Hierarchy items={hierarchy} maxDepth={depth} depth={1} {...props} />
41+
return <Hierarchy items={hierarchy} maxDepth={depth} depth={1} hideVariants={hideVariants} />
4442
}
4543

4644
export default NavHierarchy

0 commit comments

Comments
 (0)