Skip to content

Commit

Permalink
replace next/router with next/navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
timlrx committed Jul 16, 2023
1 parent 43c6929 commit 459c5d8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 146 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-keys-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pliny': patch
---

Update to use next 13 app dir navigation router
147 changes: 5 additions & 142 deletions packages/pliny/src/search/Algolia.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useState, useRef, useCallback } from 'react'
import { AnchorHTMLAttributes, DetailedHTMLProps } from 'react'
import { AnchorHTMLAttributes } from 'react'
import { createPortal } from 'react-dom'
import { useRouter } from 'next/router.js'
import { useRouter } from 'next/navigation.js'
import Link from 'next/link.js'
import Head from 'next/head.js'
import { useDocSearchKeyboardEvents } from '@docsearch/react'
import type { LinkProps } from 'next/link'
import type {
DocSearchModal as DocSearchModalType,
DocSearchProps,
Expand Down Expand Up @@ -32,19 +33,12 @@ export interface AlgoliaSearchContext {

let DocSearchModal: typeof DocSearchModalType | null = null

const CustomLink = ({
href,
...rest
}: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>) => {
const CustomLink = ({ href, ...rest }: LinkProps & AnchorHTMLAttributes<HTMLAnchorElement>) => {
const isInternalLink = href && href.startsWith('/')
const isAnchorLink = href && href.startsWith('#')

if (isInternalLink) {
return (
<Link href={href}>
<a {...rest} />
</Link>
)
return <Link href={href} {...rest} />
}

if (isAnchorLink) {
Expand All @@ -64,137 +58,6 @@ function Hit({
return <CustomLink href={hit.url}>{children}</CustomLink>
}

// export function AlgoliaSearch({
// ButtonChildren,
// algoliaConfig,
// className: buttonClassName,
// }: AlgoliaSearchProps) {
// const router = useRouter()
// const searchButtonRef = useRef<HTMLButtonElement>(null)
// const [isOpen, setIsOpen] = useState(false)
// const [initialQuery, setInitialQuery] = useState<string | undefined>(undefined)

// const importDocSearchModalIfNeeded = useCallback(() => {
// if (DocSearchModal) {
// return Promise.resolve()
// }

// return Promise.all([
// import('./AlgoliaModal'),
// // import('@docsearch/react/style'),
// ]).then(([{ default: Modal }]) => {
// DocSearchModal = Modal
// })
// }, [])

// const onOpen = useCallback(() => {
// importDocSearchModalIfNeeded().then(() => {
// setIsOpen(true)
// })
// }, [importDocSearchModalIfNeeded, setIsOpen])

// const onClose = useCallback(() => {
// setIsOpen(false)
// }, [setIsOpen])

// const onInput = useCallback(
// (event: KeyboardEvent) => {
// importDocSearchModalIfNeeded().then(() => {
// setIsOpen(true)
// setInitialQuery(event.key)
// })
// },
// [importDocSearchModalIfNeeded, setIsOpen, setInitialQuery]
// )

// const navigator = useRef({
// navigate({ itemUrl }: { itemUrl?: string }) {
// // Algolia results could contain URL's from other domains which cannot
// // be served through history and should navigate with window.location
// const isInternalLink = itemUrl.startsWith('/')
// const isAnchorLink = itemUrl.startsWith('#')
// if (!isInternalLink && !isAnchorLink) {
// window.location.href = itemUrl
// } else {
// router.push(itemUrl)
// }
// },
// }).current

// const transformItems = useRef<DocSearchModalProps['transformItems']>((items) =>
// items.map((item) => {
// // If Algolia contains a external domain, we should navigate without
// // relative URL
// const isInternalLink = item.url.startsWith('/')
// const isAnchorLink = item.url.startsWith('#')
// if (!isInternalLink && !isAnchorLink) {
// return item
// }

// // We transform the absolute URL into a relative URL.
// const url = new URL(item.url)
// return {
// ...item,
// // url: withBaseUrl(`${url.pathname}${url.hash}`),
// url: `${url.pathname}${url.hash}`,
// }
// })
// ).current

// const SearchButton = useCallback(() => {
// return (
// <button
// aria-label="Search"
// onTouchStart={importDocSearchModalIfNeeded}
// onFocus={importDocSearchModalIfNeeded}
// onMouseOver={importDocSearchModalIfNeeded}
// onClick={onOpen}
// className={buttonClassName}
// >
// <ButtonChildren />
// </button>
// )
// }, [ButtonChildren, importDocSearchModalIfNeeded, onOpen])

// useDocSearchKeyboardEvents({
// isOpen,
// onOpen,
// onClose,
// onInput,
// searchButtonRef,
// })

// return (
// <>
// <Head>
// {/* This hints the browser that the website will load data from Algolia,
// and allows it to preconnect to the DocSearch cluster. It makes the first
// query faster, especially on mobile. */}
// <link
// rel="preconnect"
// href={`https://${algoliaConfig.appId}-dsn.algolia.net`}
// crossOrigin="anonymous"
// />
// </Head>
// {ButtonChildren && <SearchButton />}
// {isOpen &&
// DocSearchModal &&
// createPortal(
// <DocSearchModal
// onClose={onClose}
// initialScrollY={window.scrollY}
// initialQuery={initialQuery}
// navigator={navigator}
// transformItems={transformItems}
// hitComponent={Hit}
// {...algoliaConfig}
// />,
// document.body
// )}
// </>
// )
// }

export const AlgoliaSearchContext = React.createContext<AlgoliaSearchContext>(
{} as AlgoliaSearchContext
)
Expand Down
5 changes: 3 additions & 2 deletions packages/pliny/src/search/KBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect, useCallback, FC, ReactNode } from 'react'
import type { Action } from 'kbar'
import Router from 'next/router.js'
import { useRouter } from 'next/navigation.js'
import { KBarModal as KBarModalType } from './KBarModal'

export interface KBarSearchProps {
Expand All @@ -19,6 +19,7 @@ export const KBarSearchProvider: FC<{
children: ReactNode
kbarConfig: KBarSearchProps
}> = ({ kbarConfig, children }) => {
const router = useRouter()
const { searchDocumentsPath, defaultActions } = kbarConfig
const [loaded, setLoaded] = useState(false)

Expand Down Expand Up @@ -56,7 +57,7 @@ export const KBarSearchProvider: FC<{
name: 'Homepage',
keywords: '',
section: 'Home',
perform: () => Router.push('/'),
perform: () => router.push('/'),
},
]

Expand Down
5 changes: 3 additions & 2 deletions packages/pliny/src/search/KBarPortal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react'
import Router from 'next/router.js'
import { useRouter } from 'next/navigation.js'
import { CoreContent, MDXDocument } from '../utils/contentlayer'
import { formatDate } from '../utils/formatDate'
import {
Expand All @@ -20,6 +20,7 @@ let init = false
export const Portal = ({ searchDocumentsPath }: { searchDocumentsPath: string }) => {
const [searchActions, setSearchActions] = useState([])
const { query } = useKBar()
const router = useRouter()

// Display on load as we already wait for crtl+k event to load it
useEffect(() => {
Expand All @@ -39,7 +40,7 @@ export const Portal = ({ searchDocumentsPath }: { searchDocumentsPath: string })
keywords: post?.summary || '',
section: 'Content',
subtitle: formatDate(post.date, 'en-US'),
perform: () => Router.push('/' + post.path),
perform: () => router.push('/' + post.path),
})
}
return actions
Expand Down

0 comments on commit 459c5d8

Please sign in to comment.