Skip to content

Commit d3634d8

Browse files
committed
Move interestChoices to app context to fix db blast
1 parent cdbba24 commit d3634d8

8 files changed

Lines changed: 59 additions & 42 deletions

File tree

web/components/filters/filters.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {Col} from 'web/components/layout/col'
3737
import {Row} from 'web/components/layout/row'
3838
import {NewBadge} from 'web/components/new-badge'
3939
import {ResetFiltersButton} from 'web/components/searches/button'
40-
import {useAllChoices, useChoices} from 'web/hooks/use-choices'
40+
import {useChoicesContext} from 'web/hooks/use-choices'
4141
import {useMeasurementSystem} from 'web/hooks/use-measurement-system'
4242
import {useT} from 'web/lib/locale'
4343
import {DietType, RelationshipType, RomanticType} from 'web/lib/util/convert-types'
@@ -76,7 +76,7 @@ function SelectedFiltersSummary(props: {
7676
}) {
7777
const {locationFilterProps, raisedInLocationFilterProps, updateFilter, clearFilters} = props
7878
const t = useT()
79-
const choicesIdsToLabels = useAllChoices()
79+
const choicesIdsToLabels = useChoicesContext()
8080
const {measurementSystem} = useMeasurementSystem()
8181

8282
const filters = removeNullOrUndefinedProps({...props.filters, orderBy: undefined})
@@ -870,14 +870,8 @@ export function FiltersElement(props: {
870870
updateDisplayOptions,
871871
} = props
872872
const youSeekingRelationship = youProfile?.pref_relation_styles?.includes('relationship')
873-
const {choices: interestChoices} = useChoices('interests')
874-
const {choices: causeChoices} = useChoices('causes')
875-
const {choices: workChoices} = useChoices('work')
876-
const choices = {
877-
interests: interestChoices,
878-
causes: causeChoices,
879-
work: workChoices,
880-
}
873+
const _choices = useChoicesContext()
874+
const choices = {interests: _choices.interests, causes: _choices.causes, work: _choices.work}
881875
return (
882876
<Filters
883877
filters={filters}

web/components/filters/interest-filter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {FilterFields} from 'common/filters'
33
import {OptionTableKey} from 'common/profiles/constants'
44
import {invert} from 'lodash'
55
import {MultiCheckbox} from 'web/components/multi-checkbox'
6-
import {useChoices} from 'web/hooks/use-choices'
6+
import {useChoicesContext} from 'web/hooks/use-choices'
77
import {useLocale, useT} from 'web/lib/locale'
88
import stringOrStringArrayToText from 'web/lib/util/string-or-string-array-to-text'
99

@@ -15,7 +15,7 @@ export function InterestFilterText(props: {
1515
const {options, highlightedClass, label} = props
1616
const t = useT()
1717
const length = (options ?? []).length
18-
const {choices} = useChoices(label)
18+
const choices = useChoicesContext()?.[label]
1919

2020
if (!options || length < 1) {
2121
return (

web/components/filters/search.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {Input} from 'web/components/widgets/input'
1515
import {Select} from 'web/components/widgets/select'
1616
import {Tooltip} from 'web/components/widgets/tooltip'
1717
import {BookmarkedSearchesType} from 'web/hooks/use-bookmarked-searches'
18-
import {useChoices} from 'web/hooks/use-choices'
18+
import {useChoicesContext} from 'web/hooks/use-choices'
1919
import {useIsClearedFilters} from 'web/hooks/use-is-cleared-filters'
2020
import {useUser} from 'web/hooks/use-user'
2121
import {useT} from 'web/lib/locale'
@@ -179,7 +179,7 @@ export const Search = forwardRef<
179179
const [openStarBookmarks, setOpenStarBookmarks] = useState(false)
180180
const user = useUser()
181181
const isClearedFilters = useIsClearedFilters(filters)
182-
const {choices: interestChoices} = useChoices('interests')
182+
const choices = useChoicesContext()
183183

184184
useEffect(() => {
185185
if (isHolding) return
@@ -195,7 +195,7 @@ export const Search = forwardRef<
195195
setTimeout(() => {
196196
setPlaceholder('')
197197
setCharIndex(0)
198-
setTextToType(getRandomPair(Object.values(interestChoices))) // pick new pair
198+
setTextToType(getRandomPair(Object.values(choices?.['interests']))) // pick new pair
199199
setIsHolding(false)
200200
}, HOLD_TIME)
201201
return prev

web/components/profile-about.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {TbBulb, TbCheck, TbMoodSad, TbUsers} from 'react-icons/tb'
3333
import {Col} from 'web/components/layout/col'
3434
import {Row} from 'web/components/layout/row'
3535
import {UserHandles} from 'web/components/user/user-handles'
36-
import {useChoices} from 'web/hooks/use-choices'
36+
import {useChoicesContext} from 'web/hooks/use-choices'
3737
import {useLocale, useT} from 'web/lib/locale'
3838
import {getSeekingConnectionText} from 'web/lib/profile/seeking'
3939
import {convertRace} from 'web/lib/util/convert-types'
@@ -83,9 +83,7 @@ export default function ProfileAbout(props: {
8383
}) {
8484
const {profile, userActivity, isCurrentUser} = props
8585
const t = useT()
86-
const {choices: interestsById} = useChoices('interests')
87-
const {choices: causesById} = useChoices('causes')
88-
const {choices: workById} = useChoices('work')
86+
const choices = useChoicesContext()
8987
const {locale} = useLocale()
9088

9189
return (
@@ -98,7 +96,7 @@ export default function ProfileAbout(props: {
9896
icon={<FaBriefcase className="h-5 w-5" />}
9997
text={
10098
profile.work
101-
?.map((id) => workById[id])
99+
?.map((id) => choices?.['work']?.[id])
102100
.filter(Boolean)
103101
.sort((a, b) => a.localeCompare(b, locale)) as string[]
104102
}
@@ -124,7 +122,7 @@ export default function ProfileAbout(props: {
124122
icon={<FaStar className="h-5 w-5" />}
125123
text={
126124
profile.interests
127-
?.map((id) => interestsById[id])
125+
?.map((id) => choices?.['interests']?.[id])
128126
.filter(Boolean)
129127
.sort((a, b) => a.localeCompare(b, locale)) as string[]
130128
}
@@ -134,7 +132,7 @@ export default function ProfileAbout(props: {
134132
icon={<FaHandsHelping className="h-5 w-5" />}
135133
text={
136134
profile.causes
137-
?.map((id) => causesById[id])
135+
?.map((id) => choices?.['causes']?.[id])
138136
.filter(Boolean)
139137
.sort((a, b) => a.localeCompare(b, locale)) as string[]
140138
}

web/components/profile-grid.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {Content} from 'web/components/widgets/editor'
3131
import HideProfileButton from 'web/components/widgets/hide-profile-button'
3232
import {CompassLoadingIndicator} from 'web/components/widgets/loading-indicator'
3333
import {LoadMoreUntilNotVisible} from 'web/components/widgets/visibility-observer'
34-
import {useAllChoices} from 'web/hooks/use-choices'
34+
import {useChoicesContext} from 'web/hooks/use-choices'
3535
import {useUser} from 'web/hooks/use-user'
3636
import {useT} from 'web/lib/locale'
3737
import {getSeekingConnectionText} from 'web/lib/profile/seeking'
@@ -157,7 +157,7 @@ function ProfilePreview(props: {
157157
cardSize,
158158
} = displayOptions ?? {}
159159
const {user} = profile
160-
const choicesIdsToLabels = useAllChoices()
160+
const choicesIdsToLabels = useChoicesContext()
161161
const t = useT()
162162
// const currentUser = useUser()
163163

web/components/searches/button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {Modal, MODAL_CLASS, SCROLLABLE_MODAL_CLASS} from 'web/components/layout/
1313
import {Row} from 'web/components/layout/row'
1414
import {Avatar} from 'web/components/widgets/avatar'
1515
import {BookmarkedSearchesType} from 'web/hooks/use-bookmarked-searches'
16-
import {useAllChoices} from 'web/hooks/use-choices'
16+
import {useChoicesContext} from 'web/hooks/use-choices'
1717
import {useMeasurementSystem} from 'web/hooks/use-measurement-system'
1818
import {useUser} from 'web/hooks/use-user'
1919
import {api} from 'web/lib/api'
@@ -68,7 +68,7 @@ function ButtonModal(props: {
6868
}) {
6969
const {open, setOpen, bookmarkedSearches, refreshBookmarkedSearches} = props
7070
const t = useT()
71-
const choicesIdsToLabels = useAllChoices()
71+
const choicesIdsToLabels = useChoicesContext()
7272
const {measurementSystem} = useMeasurementSystem()
7373
return (
7474
<Modal
Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1+
import {debug} from 'common/logger'
12
import {OptionTableKey} from 'common/profiles/constants'
23
import {run} from 'common/supabase/utils'
3-
import {useEffect} from 'react'
4+
import {createContext, ReactNode, useContext, useEffect} from 'react'
45
import {usePersistentInMemoryState} from 'web/hooks/use-persistent-in-memory-state'
56
import {useLocale} from 'web/lib/locale'
67
import {db} from 'web/lib/supabase/db'
78

9+
const ChoicesContext = createContext<UseAllChoices | null>(null)
10+
11+
export const ChoicesProvider = ({children}: {children: ReactNode}) => {
12+
const choices = useAllChoices()
13+
return <ChoicesContext.Provider value={choices}>{children}</ChoicesContext.Provider>
14+
}
15+
16+
export const useChoicesContext = () => {
17+
const ctx = useContext(ChoicesContext)
18+
if (!ctx) throw new Error('useChoicesContext must be used within a ChoicesProvider')
19+
return ctx
20+
}
21+
822
export async function fetchChoices(label: OptionTableKey, locale: string) {
23+
debug('Fetching choices for', label)
924
const choicesById: Record<string, string> = {}
1025
const {data} = await run(
1126
db
@@ -32,32 +47,39 @@ export async function fetchChoices(label: OptionTableKey, locale: string) {
3247
return choicesById
3348
}
3449

35-
export const useChoices = (label: OptionTableKey) => {
50+
const useChoices = (label: OptionTableKey) => {
3651
const [choices, setChoices] = usePersistentInMemoryState<Record<string, string>>(
3752
{},
3853
`${label}-choices`,
3954
)
4055
const {locale} = useLocale()
4156

4257
const refreshChoices = async () => {
43-
try {
44-
const results = await fetchChoices(label, locale)
45-
setChoices(results)
46-
} catch (err) {
47-
console.error('Error fetching choices:', err)
48-
return {}
49-
}
58+
fetchChoices(label, locale)
59+
.then(setChoices)
60+
.catch((err) => {
61+
console.error('Error fetching choices:', err?.message ?? err)
62+
return {}
63+
})
5064
}
5165

5266
useEffect(() => {
53-
// debug('Fetching choices in use effect...')
5467
refreshChoices()
5568
}, [locale])
5669

5770
return {choices, refreshChoices}
5871
}
5972

60-
export const useAllChoices = () => {
73+
export type UseAllChoices = {
74+
interests: Record<string, string>
75+
causes: Record<string, string>
76+
work: Record<string, string>
77+
refreshInterests: () => void
78+
refreshCauses: () => void
79+
refreshWork: () => void
80+
}
81+
82+
const useAllChoices = () => {
6183
const {choices: interests, refreshChoices: refreshInterests} = useChoices('interests')
6284
const {choices: causes, refreshChoices: refreshCauses} = useChoices('causes')
6385
const {choices: work, refreshChoices: refreshWork} = useChoices('work')

web/pages/_app.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {useEffect, useState} from 'react'
2020
import {AuthProvider, AuthUser} from 'web/components/auth-context'
2121
import {ErrorBoundary} from 'web/components/error-boundary'
2222
import {LiveRegionProvider} from 'web/components/live-region'
23+
import {ChoicesProvider} from 'web/hooks/use-choices'
2324
import {useFontPreferenceManager} from 'web/hooks/use-font-preference'
2425
import {useHasLoaded} from 'web/hooks/use-has-loaded'
2526
import {HiddenProfilesProvider} from 'web/hooks/use-hidden-profiles'
@@ -196,11 +197,13 @@ function MyApp(props: AppProps<PageProps>) {
196197
<I18nContext.Provider value={{locale, setLocale}}>
197198
<ErrorBoundary>
198199
<AuthProvider serverUser={pageProps.auth}>
199-
<HiddenProfilesProvider>
200-
<WebPush />
201-
<AndroidPush />
202-
<Component {...pageProps} />
203-
</HiddenProfilesProvider>
200+
<ChoicesProvider>
201+
<HiddenProfilesProvider>
202+
<WebPush />
203+
<AndroidPush />
204+
<Component {...pageProps} />
205+
</HiddenProfilesProvider>
206+
</ChoicesProvider>
204207
</AuthProvider>
205208
</ErrorBoundary>
206209
</I18nContext.Provider>

0 commit comments

Comments
 (0)