-
Notifications
You must be signed in to change notification settings - Fork 9
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
random questions #296
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
} | ||
|
||
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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. toggle does not work for me There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be even better if there was no a refresh button at the bottom (similar to load more questions) saying |
||
selectQuestion={selectQuestion} | ||
glossary={glossary} | ||
/> | ||
</main> | ||
|
||
<Footer /> | ||
</> | ||
) | ||
} |
There was a problem hiding this comment.
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 ?