refact(404): convert link correction function to functional component

This commit is contained in:
Khinshan Khan
2020-01-26 12:38:28 -05:00
committed by Jean-Philippe Sirois
parent 8ab5ec1737
commit b154d0c515
3 changed files with 30 additions and 13 deletions
+14 -7
View File
@@ -1,6 +1,6 @@
import { WindowLocation } from "@reach/router"
import { Link } from "gatsby"
import React, { Fragment } from "react"
import React, { FC, Fragment } from "react"
import { FileConnection } from "../../../generated/graphql"
@@ -55,12 +55,19 @@ function levenshteinDistance(term1: string, term2: string) {
return matrix[term2.length][term1.length]
}
export const getPossibleCorrections = (
basepath: string,
location: WindowLocation,
data: { allFile: FileConnection },
threshold: number
) => {
interface IPossibleCorrections {
basepath: string
location: WindowLocation
data: { allFile: FileConnection }
threshold?: number
}
export const PossibleCorrections: FC<IPossibleCorrections> = ({
basepath,
location,
data,
threshold = 8,
}) => {
// the data prop has the graphql result
// we're abstracting it to `linkArray` to just have array of edges matched
// must use `.node.absolutePath` on each edge to get each node's absolute link
+8 -2
View File
@@ -6,7 +6,7 @@ import { FileConnection } from "../../../generated/graphql"
import { ComponentQuery } from "../../../typings"
import { SEO } from "../../components/SEO"
import { getPossibleCorrections } from "../../components/404LinkCorrection"
import { PossibleCorrections } from "../../components/404LinkCorrection"
export default ({ data }: ComponentQuery<{ allFile: FileConnection }>) => (
<Fragment>
@@ -14,7 +14,13 @@ export default ({ data }: ComponentQuery<{ allFile: FileConnection }>) => (
<h1>RESOURCE NOT FOUND</h1>
<p>You just hit a route that doesn&#39;t exist... the sadness.</p>
<Location>
{({ location }) => getPossibleCorrections("archives/", location, data, 8)}
{({ location }) => (
<PossibleCorrections
basepath="archives/"
location={location}
data={data}
/>
)}
</Location>
</Fragment>
)
+8 -4
View File
@@ -4,7 +4,7 @@ import React, { Fragment } from "react"
import { FileConnection } from "../../../generated/graphql"
import { ComponentQuery } from "../../../typings"
import { getPossibleCorrections } from "../../components/404LinkCorrection"
import { PossibleCorrections } from "../../components/404LinkCorrection"
import { SEO } from "../../components/SEO"
export default ({ data }: ComponentQuery<{ allFile: FileConnection }>) => (
@@ -13,9 +13,13 @@ export default ({ data }: ComponentQuery<{ allFile: FileConnection }>) => (
<h1>RESOURCE NOT FOUND</h1>
<p>You just hit a route that doesn&#39;t exist... the sadness.</p>
<Location>
{({ location }) =>
getPossibleCorrections("resources/", location, data, 8)
}
{({ location }) => (
<PossibleCorrections
basepath="resources/"
location={location}
data={data}
/>
)}
</Location>
</Fragment>
)