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

POC ToC #343

Merged
merged 3 commits into from
Oct 28, 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
4 changes: 4 additions & 0 deletions app/root.css
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,10 @@ a[target='_blank']:not(.icon-link, .transparent-link):after {
margin: 0 5px;
}

.toc-container {
display: flex;
}

/* Smaller screen stuff */

@media (max-width: 740px) {
Expand Down
3 changes: 3 additions & 0 deletions app/routes/questions/editors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export default function App() {
<li>
<a href="/random">Random question</a>
</li>
<li>
<a href="/toc">Table of Contents</a>
</li>
<li>
<button onClick={downloadTwine}>Download questions for Twine</button>
</li>
Expand Down
169 changes: 169 additions & 0 deletions app/routes/toc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import {useEffect, useState, MouseEvent} from 'react'
import {useOutletContext} from '@remix-run/react'
import {Header, Footer} from '~/components/layouts'
import useQuestionStateInUrl from '~/hooks/useQuestionStateInUrl'
import {LINK_WITHOUT_DETAILS_CLS, fetchQuestion, Question} from '~/routes/questions/$question'
import {QuestionState} from '~/server-utils/stampy'
import type {Question as QuestionType, PageId} from '~/server-utils/stampy'
import type {Context} from '~/root'

type Item = {
items?: Item[]
content?: string
pageId?: PageId
}

const normalizeName = (name: string | undefined) => name?.replace(/[^A-Za-z]/g, '').toLowerCase()

const MaybeURL = ({item, selectItem}: {item: Item; selectItem: (i: Item) => void}) => {
if (!item || !item.content) {
return null
} else if (!item.pageId) {
return <>{item.content}</>
}
return (
<a
href={item.pageId}
onClick={(e) => {
e.preventDefault()
selectItem(item)
}}
>
{item.content}
</a>
)
}

const ChildList = ({items, selectItem}: {items: Item[]; selectItem: (i: Item) => void}) => {
if (!items) return null

return (
<div style={{marginLeft: '15px'}}>
{items.map((child, i) => {
if (child?.items && child.items.length > 0) {
return (
<details key={i} id={normalizeName(child?.content)}>
<summary>
<MaybeURL item={child} selectItem={selectItem} />
</summary>
{child?.items && <ChildList items={child.items} selectItem={selectItem} />}
</details>
)
} else {
return (
<li key={i} style={{marginLeft: '15px'}}>
<MaybeURL item={child} selectItem={selectItem} />
</li>
)
}
})}
</div>
)
}

const ToC = ({toc, selectItem}: {toc: Item[]; selectItem: (i: Item) => void}) => {
return (
<nav className="toc" style={{flex: '1 1 500px', marginRight: '10px'}}>
{toc.map((item: Item, i: number) => (
<details key={i} open>
<summary>
<span id={normalizeName(item.content)} style={{padding: '5px'}}>
<MaybeURL item={item} selectItem={selectItem} />
</span>
</summary>
{item?.items && <ChildList items={item.items} selectItem={selectItem} />}
</details>
))}
</nav>
)
}

const showMore = (el: HTMLElement, toggle = false) => {
const button = el.closest('.answer')?.querySelector('.see-more')
if (toggle) {
button?.classList.toggle('visible')
} else {
button?.classList.add('visible')
}
// The AutoHeight component doesn't notice when a random <div> changes CSS class,
// so manually triggering toggle event (as if this was a <details> element).
document.dispatchEvent(new Event('toggle'))
}

export default function App() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
export default function App() {
export default function Toc() {

const [toc, setToc] = useState<Item[]>([])
const [loading, setLoading] = useState(false)
const [question, setQuestion] = useState<QuestionType>()
const {minLogo} = useOutletContext<Context>()
const {toggleQuestion, onLazyLoadQuestion, selectQuestion, glossary} = useQuestionStateInUrl(
minLogo,
[]
)

const selectItem = async (item: Item) => {
if (!item.pageId) return
setLoading(true)
try {
const question = await fetchQuestion(item.pageId)
setQuestion({...question, questionState: QuestionState.OPEN})
} catch (e) {
console.error(e)
}
setLoading(false)
}

useEffect(() => {
const fetcher = async () => {
selectItem({pageId: '9OGZ'})
const response = await fetch('/toc.json')
setToc(await response.json())
}
fetcher()
}, [])

const handleSpecialLinks = (e: MouseEvent) => {
const el = e.target as HTMLAnchorElement
if (
el.tagName !== 'A' ||
el.closest('.question-footer') ||
el.classList.contains('footnote-backref') ||
el.classList.contains(LINK_WITHOUT_DETAILS_CLS)
)
return

if (el.classList.contains('see-more')) {
showMore(el, true)
e.preventDefault()
return
}
if (el.parentElement?.classList.contains('footnote-ref')) {
showMore(el)
return
}
}

return (
<>
<style>{`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is fun :D

body { max-width: 1400px; }
`}</style>
<Header />
<main className="toc-container" onClick={handleSpecialLinks}>
<ToC toc={toc} selectItem={selectItem} />
<div style={{flex: '5 3 auto'}}>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would use either .toc-container {...} in global styles or use inline styles, but let's not combine both approaches in the same compoment, please

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

agreed, but can't be bothered to now :P

<div className={`search-loader ${loading ? 'loader' : ''}`}> </div>
{question && (
<Question
questionProps={question}
onLazyLoadQuestion={onLazyLoadQuestion}
onToggle={toggleQuestion}
selectQuestion={selectQuestion}
glossary={glossary}
/>
)}
</div>
</main>
<Footer />
</>
)
}
Loading