Skip to content

Commit

Permalink
Merge pull request #347 from StampyAI/tag-links
Browse files Browse the repository at this point in the history
Change tags buttons to links
  • Loading branch information
mruwnik committed Nov 14, 2023
2 parents 60fe1a3 + f48ed0f commit 60c574b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 46 deletions.
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 () => {
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

0 comments on commit 60c574b

Please sign in to comment.