-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(404): add button for searches #869
base: main
Are you sure you want to change the base?
Changes from all commits
6f8c142
f105800
486b81a
0ddaab1
cdef7d4
fefb91f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default } from "./Infobar" | ||
export { default, compoundStyles } from "./Infobar" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use client" | ||
|
||
import { useLayoutEffect, useState } from "react" | ||
|
||
import { NotFoundPageSchemaType } from "~/engine" | ||
import { getWordsFromPermalink } from "~/utils" | ||
import { LinkButton } from "../../components/internal/LinkButton" | ||
|
||
type NotFoundSearchButtonProps = Pick<NotFoundPageSchemaType, "LinkComponent"> | ||
export const NotFoundSearchButton = ({ | ||
LinkComponent, | ||
}: NotFoundSearchButtonProps) => { | ||
const [permalink, setPermalink] = useState("") | ||
|
||
useLayoutEffect(() => { | ||
// The check for typeof window and navigator ensures this only runs in browser environments, not during server-side rendering | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will never be server-side rendered due to "use client" declarative at top There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not server-side rendered but nextjs will transpile then serve the js at runtime. this led to compile errors as |
||
if ( | ||
typeof window !== "undefined" && | ||
typeof window.location !== "undefined" | ||
) { | ||
setPermalink(window.location.pathname) | ||
} | ||
}, []) | ||
|
||
const missingPath = getWordsFromPermalink(permalink) | ||
|
||
return ( | ||
<LinkButton | ||
href={`/search?q=${missingPath}`} | ||
size="lg" | ||
LinkComponent={LinkComponent} | ||
isWithFocusVisibleHighlight | ||
> | ||
Search for this page | ||
</LinkButton> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { describe, expect, it } from "vitest" | ||
|
||
import { getWordsFromPermalink } from "~/utils" | ||
|
||
describe("getWordsFromPermalink", () => { | ||
it("should trim out all symbols and return the permalink as a space separated sentence for single level permalinks", () => { | ||
// Arrange | ||
const singleLevelPermalink = "/this-._single=level|" | ||
const expected = "this+single+level" | ||
|
||
// Act | ||
const actual = getWordsFromPermalink(singleLevelPermalink) | ||
|
||
// Assert | ||
expect(actual).toBe(expected) | ||
}) | ||
|
||
it("should trim out all symbols and return the last section as a space separated sentence for nested permalinks", () => { | ||
// Arrange | ||
const nestedPermalink = "/nested/deeply/this-._nest'fff=level|" | ||
const expected = "this+nest+fff+level" | ||
|
||
// Act | ||
const actual = getWordsFromPermalink(nestedPermalink) | ||
|
||
// Assert | ||
expect(actual).toBe(expected) | ||
}) | ||
|
||
it("should preserve `=` in the original permalink", () => { | ||
// Arrange | ||
const singleLevelPermalink = "/this-._single=level|" | ||
const expected = "this+single+level" | ||
|
||
// Act | ||
const actual = getWordsFromPermalink(singleLevelPermalink) | ||
|
||
// Assert | ||
expect(actual).toBe(expected) | ||
}) | ||
Comment on lines
+30
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsure if i understand the test description correctly but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is my bad, i was flipflopping on whether we should keep this due to query params but i decided teh |
||
|
||
it("should handle uri-encoded strings correctly", () => { | ||
// Arrange | ||
const singleLevelPermalink = "/this-._single=level|" | ||
const encodedPermalink = encodeURIComponent(singleLevelPermalink) | ||
const expected = "this+single+level" | ||
|
||
// Act | ||
const actual = getWordsFromPermalink(encodedPermalink) | ||
|
||
// Assert | ||
expect(actual).toBe(expected) | ||
}) | ||
|
||
it("should work with a trailing /", () => { | ||
// Arrange | ||
const singleLevelPermalink = "/this-._single=level|/" | ||
const expected = "this+single+level" | ||
|
||
// Act | ||
const actual = getWordsFromPermalink(singleLevelPermalink) | ||
|
||
// Assert | ||
expect(actual).toBe(expected) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const getWordsFromPermalink = (permalink: string): string => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checking understanding: so https://example.com/some-page_name+ will yield There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeap excatly |
||
const trimmedPermalink = permalink.endsWith("/") | ||
? permalink.slice(0, -1) | ||
: permalink | ||
const lastUrlSegment = trimmedPermalink.split("/").at(-1) ?? "" | ||
// NOTE: Replace all non-alphanumeric characters with spaces | ||
// then remove all spaces and join by `+`. | ||
// This is because we might have run-on spaces from sequences of symbols | ||
// like: `+=`, which would lead to 2 spaces | ||
return decodeURIComponent(lastUrlSegment) | ||
.replaceAll(/[\W_]/gi, " ") | ||
.split(" ") | ||
.filter((v) => !!v) | ||
.join("+") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need useLayoutEffect here? iirc, the use of this hook is discouraged in favour of useEffect