Skip to content

Turn tags into links on tools-and-libraries page #1841

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 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 57 additions & 13 deletions src/components/tools-and-libraries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@/icons"
import { Card, Tag } from "@/components"
import NextLink from "next/link"
import NextHead from "next/head"
import { useMounted } from "nextra/hooks"
import Markdown from "markdown-to-jsx"
import { evaluate } from "nextra/components"
Expand Down Expand Up @@ -66,16 +67,20 @@ export function CodePage({ allTags, data }: CodePageProps) {
const [search, setSearch] = useState("")
const [queryParamsTags, setTags] = useQueryParam("tags", TagParam)

const handleQuery = useCallback((e: MouseEvent<HTMLButtonElement>) => {
const tag = e.currentTarget.dataset.tag!
const handleQuery = useCallback(
(e: MouseEvent<HTMLAnchorElement | HTMLButtonElement>) => {
e.preventDefault()
const tag = e.currentTarget.dataset.tag!

setTags(prevTags => {
if (prevTags!.includes(tag)) {
return prevTags!.filter(t => t !== tag)
}
return [...prevTags!, tag]
})
}, [])
setTags(prevTags => {
if (prevTags!.includes(tag)) {
return prevTags!.filter(t => t !== tag)
}
return [...prevTags!, tag]
})
},
[setTags],
)

const mounted = useMounted()
const [isBackspacePressed, setIsBackspacePressed] = useState(false)
Expand Down Expand Up @@ -144,10 +149,37 @@ export function CodePage({ allTags, data }: CodePageProps) {
}
}, [mounted, data, queryParamsTags])

const selectedTagsAsString = useMemo(() => {
const tags = queryParamsTags
.slice()
.map(tag => allTagsMap.get(tag as string)?.name ?? tag)
.filter((tag): tag is string => typeof tag === "string")

if (tags.length === 0) {
return ""
}

if (tags.length === 1) {
return tags[0]
}

return `${tags.slice(0, -1).join(", ")} and ${tags.slice(-1)[0]}`
}, [queryParamsTags, allTagsMap])

const [sort, setSort] = useState("popularity")

return (
<>
<NextHead>
<title>
{selectedTagsAsString ? selectedTagsAsString + " | " : ""}Tools and
Libraries | GraphQL
</title>
<meta
name="description"
content={`A collection of tools and libraries for GraphQL${selectedTagsAsString ? ` related to ${selectedTagsAsString}` : ""}`}
/>
</NextHead>
<div className="container py-10 md:py-20">
<h1 className="text-4xl md:text-7xl font-extrabold">
Code Using GraphQL
Expand Down Expand Up @@ -180,7 +212,8 @@ export function CodePage({ allTags, data }: CodePageProps) {
!search || name.toLowerCase().includes(search.toLowerCase())
if (!isTagMatchSearch) return
return (
<button
<NextLink
href={`/community/tools-and-libraries/?tags=${tag}`}
key={tag}
data-tag={tag}
className={clsx(
Expand All @@ -193,7 +226,7 @@ export function CodePage({ allTags, data }: CodePageProps) {
title={`${mounted && (queryParamsTags as string[]).includes(tag) ? "Remove" : "Add"} tag "${name}"`}
>
{name} ({count})
</button>
</NextLink>
)
})}
</div>
Expand Down Expand Up @@ -287,7 +320,7 @@ export function CodePage({ allTags, data }: CodePageProps) {
key={tag}
// @ts-expect-error -- fixme
as={NextLink}
href={`/code?tags=${tag}`}
href={`/community/tools-and-libraries/?tags=${tag}`}
className="hover:!bg-primary transition-colors hover:text-white cursor-pointer"
>
{allTagsMap.get(tag)!.name}
Expand Down Expand Up @@ -351,6 +384,17 @@ const RemoteContent = memo(function RemoteContent({
compiledSource: string
}) {
const { default: MDXContent } = evaluate(compiledSource)
const components = getComponents({ isRawLayout: false })
const components = getComponents({
isRawLayout: false,
components: {
// Rendering README.md with headings messes up the headings hierarchy of the page
h1: props => <strong {...props} />,
h2: props => <strong {...props} />,
h3: props => <strong {...props} />,
h4: props => <strong {...props} />,
h5: props => <strong {...props} />,
h6: props => <strong {...props} />,
},
})
return <MDXContent components={components} />
})
Loading