-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat(app): improve frontend #418
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0784d89
feat(app): improve frontend
AdiPol1359 fb8591c
feat(app): change background color of active pagination item on hover
AdiPol1359 35e49f1
Merge branch 'develop' of https://github.com/typeofweb/devfaq into im…
AdiPol1359 af56bfe
feat(app): create LinkWithQuery component
AdiPol1359 ea9eca0
refactor(app): move props over component
AdiPol1359 e4b9912
refactor(app): move redirects into next config
AdiPol1359 03dc6b2
refactor(app): remove unused import
AdiPol1359 f8b7d36
Add tests for LinkWithQuery
typeofweb e116ccc
Add tests to pipeline
typeofweb 8a8d5f4
Fix types
typeofweb 6b3ed8f
Merge branch 'develop' of https://github.com/typeofweb/devfaq into im…
AdiPol1359 623d5eb
Merge branch 'develop' of https://github.com/typeofweb/devfaq into im…
AdiPol1359 02dc943
Merge branch 'develop' of https://github.com/typeofweb/devfaq into im…
AdiPol1359 5c4577c
refactor(app): add lists
xStrixU ef7e325
feat(app): improve LinkWithQuery
AdiPol1359 5decdc4
Merge branch 'improve-frontend' of https://github.com/typeofweb/devfa…
AdiPol1359 55957c3
Fix url
typeofweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
apps/app/src/app/(main-layout)/questions/[technology]/page.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,25 @@ | ||
"use client"; | ||
|
||
import Link from "next/link"; | ||
import { usePathname } from "next/navigation"; | ||
import { twMerge } from "tailwind-merge"; | ||
import type { ComponentProps } from "react"; | ||
import { LinkWithQuery } from "./LinkWithQuery/LinkWithQuery"; | ||
|
||
type ActiveLinkProps = Readonly<{ | ||
activeClassName: string; | ||
}> & | ||
ComponentProps<typeof Link>; | ||
ComponentProps<typeof LinkWithQuery>; | ||
|
||
export const ActiveLink = ({ | ||
href, | ||
className, | ||
activeClassName, | ||
children, | ||
...rest | ||
}: ActiveLinkProps) => { | ||
export const ActiveLink = ({ href, className, activeClassName, ...props }: ActiveLinkProps) => { | ||
const pathname = usePathname(); | ||
|
||
const isActive = pathname?.startsWith(href.toString()); | ||
|
||
return ( | ||
<Link href={href} className={twMerge(className, isActive && activeClassName)} {...rest}> | ||
{children} | ||
</Link> | ||
<LinkWithQuery | ||
href={href} | ||
className={twMerge(className, isActive && activeClassName)} | ||
{...props} | ||
/> | ||
); | ||
}; |
51 changes: 51 additions & 0 deletions
51
apps/app/src/components/LinkWithQuery/LinkWithQuery.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { createQueryHref } from "./LinkWithQuery"; | ||
|
||
describe("LinkWithQuery", () => { | ||
describe("createQueryHref", () => { | ||
it("should return given href and query", () => { | ||
expect(createQueryHref("test", { q1: "abc", q2: "123" })).toEqual("test?q1=abc&q2=123"); | ||
}); | ||
it("should work with empty href and query", () => { | ||
expect(createQueryHref("", {})).toEqual(""); | ||
}); | ||
it("should merge href and query when href is object", () => { | ||
expect(createQueryHref({ pathname: "test2" }, { q1: "abc", q2: "123" })).toEqual( | ||
"/test2?q1=abc&q2=123", | ||
); | ||
}); | ||
it("should merge href and query when href is object and has query inside", () => { | ||
expect( | ||
createQueryHref( | ||
{ pathname: "test3", query: { q1: "href1", q2: "href2", q3: "href3" }, hash: "fragment" }, | ||
{ q0: "query0", q1: "query1", q2: "query2" }, | ||
), | ||
).toEqual("/test3?q1=query1&q2=query2&q3=href3&q0=query0#fragment"); | ||
}); | ||
it("should preserve other fields in href", () => { | ||
expect(createQueryHref({ pathname: "test3", hash: "blablabla" }, {})).toEqual( | ||
"/test3#blablabla", | ||
); | ||
}); | ||
it("should merge queries when href is a string", () => { | ||
expect( | ||
createQueryHref("test4?q1=href1&q2=href2&q3=href3#fragment", { | ||
q0: "query0", | ||
q1: "query1", | ||
q2: "query2", | ||
q3: "href3", | ||
}), | ||
).toEqual("test4?q1=query1&q2=query2&q3=href3&q0=query0#fragment"); | ||
}); | ||
it("should merge queries when href is a an absolute URL", () => { | ||
expect( | ||
createQueryHref("https://google.com/test5?q1=href1&q2=href2&q3=href3#fragment", { | ||
q0: "query0", | ||
q1: "query1", | ||
q2: "query2", | ||
q3: "href3", | ||
}), | ||
).toEqual("https://google.com/test5?q1=query1&q2=query2&q3=href3&q0=query0#fragment"); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"use client"; | ||
import { UrlObject } from "url"; | ||
import Link, { LinkProps } from "next/link"; | ||
import { ComponentProps } from "react"; | ||
import { useDevFAQRouter } from "../../hooks/useDevFAQRouter"; | ||
import { escapeStringRegexp } from "../../lib/escapeStringRegex"; | ||
|
||
type Url = LinkProps["href"]; | ||
typeofweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const origin = process.env.NEXT_PUBLIC_APP_URL || "http://dummy.localhost:8080"; | ||
|
||
const urlObjectToUrl = (urlObject: UrlObject, origin: string): URL => { | ||
const url = new URL(origin); | ||
if (urlObject.protocol) url.protocol = urlObject.protocol; | ||
if (urlObject.auth) { | ||
const auth = urlObject.auth.split(":"); | ||
url.username = auth[0] || url.username; | ||
url.password = auth[1] || url.password; | ||
} | ||
if (urlObject.host) url.host = urlObject.host; | ||
if (urlObject.hostname) url.hostname = urlObject.hostname; | ||
if (urlObject.port) url.port = urlObject.port.toString(); | ||
if (urlObject.hash) url.hash = urlObject.hash; | ||
if (urlObject.search) url.search = urlObject.search; | ||
if (urlObject.query) | ||
url.search = new URLSearchParams(urlObject.query as Record<string, string> | string).toString(); | ||
if (urlObject.pathname) url.pathname = urlObject.pathname; | ||
|
||
return url; | ||
}; | ||
|
||
export const createQueryHref = (href: Url, query: Record<string, string>): string => { | ||
const url = typeof href === "string" ? new URL(href, origin) : urlObjectToUrl(href, origin); | ||
Object.entries(query).forEach(([key, value]) => url.searchParams.set(key, value)); | ||
|
||
const newHref = url.toString().replace(new RegExp("^" + escapeStringRegexp(origin)), ""); | ||
|
||
if (newHref.startsWith("/") && typeof href === "string" && !href.startsWith("/")) { | ||
// trim slash | ||
return newHref.slice(1); | ||
} | ||
return newHref; | ||
}; | ||
|
||
type LinkWithQueryProps = Readonly<{ | ||
mergeQuery?: boolean; | ||
}> & | ||
ComponentProps<typeof Link>; | ||
|
||
export const LinkWithQuery = ({ href, mergeQuery, ...props }: LinkWithQueryProps) => { | ||
const { queryParams } = useDevFAQRouter(); | ||
|
||
const linkHref = mergeQuery ? createQueryHref(href, queryParams) : createQueryHref(href, {}); | ||
|
||
return <Link href={linkHref} {...props} />; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js | ||
export function escapeStringRegexp(string: string): string { | ||
// Escape characters with special meaning either inside or outside character sets. | ||
// Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar. | ||
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d"); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.