mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-25 10:02:27 -04:00
126 lines
3.4 KiB
TypeScript
126 lines
3.4 KiB
TypeScript
import {Row} from 'common/supabase/utils'
|
|
import {sortBy} from 'lodash'
|
|
import {useEffect, useState} from 'react'
|
|
import {useFirebaseUser} from 'web/hooks/use-firebase-user'
|
|
import {usePersistentInMemoryState} from 'web/hooks/use-persistent-in-memory-state'
|
|
import {api} from 'web/lib/api'
|
|
import {useLocale} from 'web/lib/locale'
|
|
import {
|
|
getAllQuestions,
|
|
getFreeResponseQuestions,
|
|
getFRQuestionsWithAnswerCount,
|
|
getUserAnswers,
|
|
getUserCompatibilityAnswers,
|
|
} from 'web/lib/supabase/questions'
|
|
|
|
export const useQuestions = () => {
|
|
const [questions, setQuestions] = useState<Row<'compatibility_prompts'>[]>([])
|
|
useEffect(() => {
|
|
getAllQuestions().then(setQuestions)
|
|
}, [])
|
|
return questions
|
|
}
|
|
|
|
export const useFreeResponseQuestions = () => {
|
|
const [questions, setQuestions] = useState<Row<'compatibility_prompts'>[]>([])
|
|
useEffect(() => {
|
|
getFreeResponseQuestions().then(setQuestions)
|
|
}, [])
|
|
return questions
|
|
}
|
|
|
|
export const useUserAnswers = (userId: string | undefined) => {
|
|
const [answers, setAnswers] = usePersistentInMemoryState<Row<'compatibility_answers_free'>[]>(
|
|
[],
|
|
`answers-${userId}`,
|
|
)
|
|
|
|
useEffect(() => {
|
|
if (userId) {
|
|
getUserAnswers(userId).then(setAnswers)
|
|
}
|
|
}, [userId])
|
|
|
|
async function refreshAnswers() {
|
|
if (!userId) return
|
|
getUserAnswers(userId).then(setAnswers)
|
|
}
|
|
|
|
return {refreshAnswers, answers}
|
|
}
|
|
|
|
export const useUserCompatibilityAnswers = (userId: string | undefined) => {
|
|
const [compatibilityAnswers, setCompatibilityAnswers] = usePersistentInMemoryState<
|
|
Row<'compatibility_answers'>[]
|
|
>([], `compatibility-answers-${userId}`)
|
|
|
|
useEffect(() => {
|
|
if (userId) {
|
|
getUserCompatibilityAnswers(userId).then((answers) => {
|
|
const sortedAnswers = sortBy(
|
|
answers,
|
|
(a) => -a.importance,
|
|
(a) => (a.explanation ? 0 : 1),
|
|
)
|
|
setCompatibilityAnswers(sortedAnswers)
|
|
})
|
|
}
|
|
}, [userId])
|
|
|
|
async function refreshCompatibilityAnswers() {
|
|
if (!userId) return
|
|
getUserCompatibilityAnswers(userId).then(setCompatibilityAnswers)
|
|
}
|
|
|
|
return {refreshCompatibilityAnswers, compatibilityAnswers}
|
|
}
|
|
|
|
export type QuestionWithCountType = Row<'compatibility_prompts'> & {
|
|
answer_count: number
|
|
score: number
|
|
community_importance_score?: number
|
|
}
|
|
|
|
export const useFRQuestionsWithAnswerCount = () => {
|
|
const [FRquestionsWithCount, setFRQuestionsWithCount] = usePersistentInMemoryState<any>(
|
|
[],
|
|
`fr-questions-with-count`,
|
|
)
|
|
|
|
useEffect(() => {
|
|
getFRQuestionsWithAnswerCount().then((questions) => {
|
|
setFRQuestionsWithCount(questions)
|
|
})
|
|
}, [])
|
|
|
|
return FRquestionsWithCount as QuestionWithCountType[]
|
|
}
|
|
|
|
export const useCompatibilityQuestionsWithAnswerCount = () => {
|
|
const {locale} = useLocale()
|
|
const firebaseUser = useFirebaseUser()
|
|
const [compatibilityQuestions, setCompatibilityQuestions] = usePersistentInMemoryState<
|
|
QuestionWithCountType[]
|
|
>([], `compatibility-questions-with-count`)
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
|
|
async function refreshCompatibilityQuestions() {
|
|
if (!firebaseUser) return
|
|
setIsLoading(true)
|
|
return api('get-compatibility-questions', {locale}).then((res) => {
|
|
setCompatibilityQuestions(res.questions)
|
|
setIsLoading(false)
|
|
})
|
|
}
|
|
|
|
useEffect(() => {
|
|
refreshCompatibilityQuestions()
|
|
}, [firebaseUser, locale])
|
|
|
|
return {
|
|
refreshCompatibilityQuestions,
|
|
compatibilityQuestions,
|
|
isLoading,
|
|
}
|
|
}
|