Skip to content

Commit

Permalink
feat(tags): redirect from index to default tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhender committed Feb 9, 2024
1 parent 09210d7 commit 84e7a74
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 95 deletions.
2 changes: 1 addition & 1 deletion app/components/ArticlesDropdown/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {Tag} from '~/server-utils/stampy'
import {TOCItem, Category, ADVANCED, INTRODUCTORY} from '~/routes/questions.toc'
import './dropdown.css'
import {sortFuncs} from '~/routes/tags._index'
import {sortFuncs} from '~/routes/tags.$tag'

const Link = ({to, text, className}: {to: string; text: string; className?: string}) => (
<div className={'articles-dropdown-entry ' + (className || '')}>
Expand Down
9 changes: 1 addition & 8 deletions app/routes/tags.$tag.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import {useState, useEffect, ReactNode} from 'react'
import {LoaderFunction} from '@remix-run/cloudflare'
import {reloadInBackgroundIfNeeded} from '~/server-utils/kv-cache'
import {
loadTag,
Tag as TagType,
QuestionState,
RelatedQuestions,
loadTags,
} from '~/server-utils/stampy'
import {Tag as TagType, QuestionState, RelatedQuestions, loadTags} from '~/server-utils/stampy'
import Dialog from '~/components/dialog'
import Footer from '~/components/Footer'
import Header from '~/components/Header'
Expand Down Expand Up @@ -149,7 +143,6 @@ export function Tags({tags}: Props) {

export default function App() {
const {tag, tags} = useLoaderData<ReturnType<typeof loader>>()
// const result = useLoaderData<ReturnType<typeof loader>>()
const {data = []} = tags ?? {}
const [selectedTag, setSelectedTag] = useState<TagType | null>(null)
const [tagsFilter, setTagsFilter] = useState<string>('')
Expand Down
91 changes: 5 additions & 86 deletions app/routes/tags._index.tsx
Original file line number Diff line number Diff line change
@@ -1,91 +1,10 @@
import {useEffect, useState} from 'react'
import type {LoaderFunction} from '@remix-run/cloudflare'
import {useLoaderData} from '@remix-run/react'
import {loadTags, Tag as TagType} from '~/server-utils/stampy'
import Header from '~/components/Header'
import Footer from '~/components/Footer'
import {CategoriesNav} from '../components/CategoriesNav/Menu'
import {ListTable} from '~/components/Table/ListTable'
import useToC from '~/hooks/useToC'
import {redirect} from '@remix-run/cloudflare'
import {loadTags} from '~/server-utils/stampy'

export const loader = async ({request}: Parameters<LoaderFunction>[0]) => {
try {
const tags = await loadTags(request)
return {tags}
} catch (e) {
console.error(e)
}
}

export const sortFuncs = {
alphabetically: (a: TagType, b: TagType) =>
a.name.toLowerCase().localeCompare(b.name.toLowerCase()),
'by number of questions': (a: TagType, b: TagType) => b.questions.length - a.questions.length,
}

export default function App() {
const {tags} = useLoaderData<ReturnType<typeof loader>>()
const tags = await loadTags(request)
const {data = []} = tags ?? {}
const [selectedTag, setSelectedTag] = useState<TagType | null>(null)
const [tagsFilter, setTagsFilter] = useState<string>('')
const {toc} = useToC()

const [sortBy, setSortBy] = useState<keyof typeof sortFuncs>('alphabetically')
const nextSortFunc = (): string => {
const sortFuncKeys = Object.keys(sortFuncs)
return sortFuncKeys[(sortFuncKeys.indexOf(sortBy) + 1) % sortFuncKeys.length]
}

const tag = data.filter((tag) => tag.tagId === selectedTag?.tagId)[0]
useEffect(() => {
if (selectedTag === null) {
setSelectedTag(data.filter((tag) => tag.questions.length > 0)[0])
}
}, [data, selectedTag])
if (selectedTag === null) {
return null
}
return (
<>
<Header toc={toc} categories={data} />
<div className={'top-margin-large'} />
<main>
<div className={'group-elements'}>
<CategoriesNav
categories={
data
.filter((tag) => tag.questions.length > 0)
.filter((tag) => tag.name.toLowerCase().includes(tagsFilter.toLowerCase()))
.sort(sortFuncs[sortBy])

// {title: "AI Safety", id: 1},
}
active={Number(selectedTag)}
onClick={setSelectedTag}
onChange={setTagsFilter}
/>

{selectedTag === null ? null : (
<div>
<h1 style={{marginTop: '0px'}}>{tag.name}</h1>
{tag.questions.length === 0 ? (
<div className={'no-questions'}>No questions found</div>
) : (
<p>
{tag.questions.length} pages tagged {`"${tag.name}"`}
</p>
)}
{selectedTag && <ListTable elements={tag.questions} />}
</div>
)}
</div>
</main>

<div className={'top-margin-large-with-border'} />

<div className={'top-margin-large'} />

<Footer />
</>
)
const defaultTag = data[0]
throw redirect(`${defaultTag.name}`)
}

0 comments on commit 84e7a74

Please sign in to comment.