Skip to content

Commit

Permalink
fetch relative to baseurl and add back starting actions
Browse files Browse the repository at this point in the history
  • Loading branch information
timlrx committed Jul 22, 2023
1 parent f428075 commit 9f87280
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-maps-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pliny': patch
---

Fix kbar fetch to load relative to base url and add back starting actions
2 changes: 1 addition & 1 deletion packages/pliny/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const sampleConfig: PlinyConfig = {
search: {
provider: 'kbar', // kbar or algolia
kbarConfig: {
searchDocumentsPath: 'search.json', // path to load documents to search
searchDocumentsPath: 'search.json', // path to load documents to search relative to public folder
},
// algoliaConfig: {
// // The application ID provided by Algolia
Expand Down
42 changes: 18 additions & 24 deletions packages/pliny/src/search/KBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ export const KBarSearchProvider: FC<{
const [modalLoaded, setModalLoaded] = useState(false)
const [dataLoaded, setDataLoaded] = useState(false)

const startingActions = useMemo(() => {
return Array.isArray(defaultActions)
? defaultActions
: [
{
id: 'homepage',
name: 'Homepage',
keywords: '',
section: 'Home',
perform: () => router.push('/'),
},
]
}, [defaultActions, router])

const importDocSearchModalIfNeeded = useCallback(() => {
if (KBarModal) {
return Promise.resolve()
Expand All @@ -73,7 +59,18 @@ export const KBarSearchProvider: FC<{
}
}
const mapPosts = (posts: CoreContent<MDXDocument>[]) => {
const actions: Action[] = []
const startingActions = Array.isArray(defaultActions)
? defaultActions
: [
{
id: 'homepage',
name: 'Homepage',
keywords: '',
section: 'Home',
perform: () => router.push('/'),
},
]
const actions: Action[] = startingActions
for (const post of posts) {
actions.push({
id: post.path,
Expand All @@ -87,7 +84,11 @@ export const KBarSearchProvider: FC<{
return actions
}
async function fetchData() {
const res = await fetch(searchDocumentsPath)
const url =
searchDocumentsPath.indexOf('://') > 0 || searchDocumentsPath.indexOf('//') === 0
? searchDocumentsPath
: new URL(searchDocumentsPath, window.location.origin)
const res = await fetch(url)
const json = await res.json()
const actions = mapPosts(json)
setSearchActions(actions)
Expand All @@ -103,14 +104,7 @@ export const KBarSearchProvider: FC<{
/*removes event listener on cleanup*/
window.removeEventListener('keydown', handleKeyDown)
}
}, [
importDocSearchModalIfNeeded,
modalLoaded,
dataLoaded,
startingActions,
router,
searchDocumentsPath,
])
}, [importDocSearchModalIfNeeded, modalLoaded, dataLoaded, router, searchDocumentsPath])

return (
<>
Expand Down

0 comments on commit 9f87280

Please sign in to comment.