Skip to content
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

Change tags buttons to links #347

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions app/root.css
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,10 @@ a[target='_blank']:not(.icon-link, .transparent-link):after {
font-size: 15px;
}

.all-tags-container .tag {
.tag {
all: unset;
}
.all-tags-container .tag {
.tag {
display: inline-block;
border-radius: 30px;
background: var(--colorTagBody);
Expand All @@ -875,6 +875,13 @@ a[target='_blank']:not(.icon-link, .transparent-link):after {
font-size: 18px;
padding-right: 10px;
}
.tags-container .tag {
padding: 2px 1px;
}
.tags-container .tag .tag-name {
font-size: inherit;
padding: 2px 10px;
}

.cache button {
margin: 0 5px;
Expand Down
65 changes: 34 additions & 31 deletions app/routes/tags/$tag.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useState, ReactNode} from 'react'
import {useState, useEffect, ReactNode} from 'react'
import {LoaderFunction} from '@remix-run/cloudflare'
import {reloadInBackgroundIfNeeded} from '~/server-utils/kv-cache'
import {loadTag, Tag as TagType} from '~/server-utils/stampy'
import {loadTag, Tag as TagType, QuestionState, RelatedQuestions} from '~/server-utils/stampy'
import Dialog from '~/components/dialog'

type Props = {
Expand Down Expand Up @@ -41,6 +41,36 @@ export async function fetchTag(tagName: string): Promise<TagType | never[]> {
})
}

export function Tag({
name,
questions: tqs,
showCount,
}: {
name: string
questions?: RelatedQuestions
showCount?: boolean
}) {
const [questions, setQuestions] = useState(tqs)
const pageIds = questions?.map((q) => q.pageid).join(QuestionState.COLLAPSED)

useEffect(() => {
const fetcher = async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the obvious downside of this is that there will be loads of extra queries flying around

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have them cached in CF, it should be fine 🤞

if (!questions) {
const tag = (await fetchTag(name)) as TagType
if (tag) setQuestions(tag.questions)
}
}
fetcher()
}, [questions, name])

return (
<a className="tag" href={`/?state=${pageIds}${QuestionState.COLLAPSED}`} key={name}>
<span className="tag-name">{name}</span>
{showCount && <span className="tag-stat">({questions?.length})</span>}
</a>
)
}

export function TagQuestions({
tag,
selectQuestion,
Expand Down Expand Up @@ -90,37 +120,10 @@ export function TagQuestions({
)
}

export function Tags({tags, selectQuestion}: Props) {
const [selectedTag, setSelectedTag] = useState<TagType | null>(null)

const setTag = async (tagName: string) => {
setSelectedTag({name: tagName} as TagType)

try {
const tag = (await fetchTag(tagName)) as TagType
setSelectedTag(tag)
} catch (error) {
console.error(error)
}
}

export function Tags({tags}: Props) {
return (
<div className="tags-container">
{selectedTag && (
<TagQuestions
tag={selectedTag}
selectQuestion={selectQuestion}
clearTag={() => setSelectedTag(null)}
/>
)}
<div>
{tags &&
tags.map((t) => (
<button className="question-tag" key={t} onClick={() => setTag(t)}>
{t}
</button>
))}
</div>
<div>{tags && tags.map((name) => <Tag key={name} name={name} />)}</div>
</div>
)
}
18 changes: 5 additions & 13 deletions app/routes/tags/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {useState} from 'react'
import type {LoaderFunction} from '@remix-run/cloudflare'
import {useLoaderData} from '@remix-run/react'
import {loadTags, Tag as TagType, QuestionState} from '~/server-utils/stampy'
import {loadTags, Tag as TagType} from '~/server-utils/stampy'
import {Header, Footer} from '~/components/layouts'
import {MagnifyingGlass} from '~/components/icons-generated'
import {TagQuestions} from './$tag'
import {TagQuestions, Tag} from './$tag'
import {Undo} from '../../components/icons-generated'

export const loader = async ({request}: Parameters<LoaderFunction>[0]) => {
Expand All @@ -16,16 +16,6 @@ export const loader = async ({request}: Parameters<LoaderFunction>[0]) => {
}
}

function Tag(tag: TagType) {
const pageIds = tag.questions.map((q) => q.pageid).join(QuestionState.COLLAPSED)
return (
<a className="tag" href={`/?state=${pageIds}${QuestionState.COLLAPSED}`} key={tag.tagId}>
<span className="tag-name">{tag.name}</span>
<span className="tag-stat">({tag.questions.length})</span>
</a>
)
}

interface SortFunc {
[key: string]: (a: TagType, b: TagType) => number
}
Expand Down Expand Up @@ -87,7 +77,9 @@ export default function App() {
.filter((tag) => tag.questions.length > 0)
.filter((tag) => tag.name.toLowerCase().includes(tagsFilter.toLowerCase()))
.sort(sortFuncs[sortBy])
.map(Tag)}
.map(({name, questions}) => (
<Tag key={name} name={name} questions={questions} showCount />
))}
</div>
</main>

Expand Down