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

random questions #296

Merged
merged 1 commit into from
Aug 6, 2023
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
60 changes: 60 additions & 0 deletions app/routes/random.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {useEffect} from 'react'
import type {LoaderFunction} from '@remix-run/cloudflare'
import {useOutletContext, useLoaderData} from '@remix-run/react'
import {reloadInBackgroundIfNeeded} from '~/server-utils/kv-cache'
import {loadAllQuestions, QuestionState, QuestionStatus} from '~/server-utils/stampy'
import {Header, Footer} from '~/components/layouts'
import {Question} from '~/routes/questions/$question'
import useQuestionStateInUrl from '~/hooks/useQuestionStateInUrl'

function randomItem(arr: any[]) {
return arr[Math.floor(Math.random() * arr.length)]
}

export const loader = async ({request}: Parameters<LoaderFunction>[0]) => {
try {
const {data} = await loadAllQuestions(request)
return {
...randomItem(data.filter((q) => q.status == QuestionStatus.LIVE_ON_SITE)),
questionState: QuestionState.OPEN,
}
} catch (e) {
console.error(e)
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the deal with the server side logging here, please? does it do anything different than letting the exception unhandled?

why not send the error to client a la https://github.com/StampyAI/stampy-ui/blob/master/app/routes/questions/%24question.tsx ?

}
}

export default function App() {
const minLogo = useOutletContext<boolean>()
const {initialQuestionsData} = useLoaderData<ReturnType<typeof loader>>()
const {data: initialQuestions = [], timestamp} = initialQuestionsData ?? {}

useEffect(() => {
if (timestamp) {
reloadInBackgroundIfNeeded('', timestamp)
}
}, [timestamp])

const {toggleQuestion, onLazyLoadQuestion, selectQuestion, glossary} = useQuestionStateInUrl(
minLogo,
initialQuestions
)

const question = useLoaderData<ReturnType<typeof loader>>()

return (
<>
<Header />
<main>
<Question
questionProps={question}
onLazyLoadQuestion={onLazyLoadQuestion}
onToggle={toggleQuestion}
Copy link
Collaborator

Choose a reason for hiding this comment

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

toggle does not work for me

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

doesn't work for me, either. Though it should be fine? It would be nice for this to also handle related questions and the search bar, but it didn't seem worth the bother...

Copy link
Collaborator

Choose a reason for hiding this comment

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

would be even better if there was no > chevron if the feature doesn't make sense .. which it probably doesn't 😅

a refresh button at the bottom (similar to load more questions) saying Reload for a another random question might also be nice some time in the future

selectQuestion={selectQuestion}
glossary={glossary}
/>
</main>

<Footer />
</>
)
}