diff --git a/.changeset/lucky-phones-listen.md b/.changeset/lucky-phones-listen.md new file mode 100644 index 000000000..d34cad76a --- /dev/null +++ b/.changeset/lucky-phones-listen.md @@ -0,0 +1,5 @@ +--- +'web': patch +--- + +fix: recommendation crashing website (unexpected behavior of useEffect) diff --git a/apps/web/src/modules/CourseSearch/components/RecommendationText/index.tsx b/apps/web/src/modules/CourseSearch/components/RecommendationText/index.tsx index e8b957f63..39b0a967e 100644 --- a/apps/web/src/modules/CourseSearch/components/RecommendationText/index.tsx +++ b/apps/web/src/modules/CourseSearch/components/RecommendationText/index.tsx @@ -1,16 +1,15 @@ -import { useEffect, useMemo, useState } from 'react' +import { useEffect, useMemo } from 'react' import { Link, Typography, styled, useTheme } from '@mui/material' import { observer } from 'mobx-react' import { Analytics } from '@web/common/context/Analytics/components/Analytics' import { useCourseGroup } from '@web/common/hooks/useCourseGroup' -import { useCourseSearchProvider } from '@web/modules/CourseSearch/context/CourseSearch/hooks/useCourseSearchProvider' import { useSearchCourseQueryParams } from '@web/modules/CourseSearch/hooks/useSearchCourseQueryParams' import { collectLogEvent } from '@web/services/logging' import { courseCartStore } from '@web/store' -import { SearchCourseQueryVariables, useRecommendCourseTextLazyQuery } from '@cgr/codegen' +import { useRecommendCourseTextQuery } from '@cgr/codegen' import { SelectedCourse } from './types' @@ -30,13 +29,17 @@ const RecommendationText: React.FC = (props: Recommenda const selectedCourses = props.selectedCourses const courseGroup = useCourseGroup() - const { courseSearchQuery } = useCourseSearchProvider() const { setFilter } = useSearchCourseQueryParams() - const [lastSearchQuery, setLastSearchQuery] = useState( - undefined - ) - const [fetchRecommendation, { data }] = useRecommendCourseTextLazyQuery() + const { data } = useRecommendCourseTextQuery({ + variables: { + req: { + variant, + semesterKey: courseGroup, + selectedCourses: selectedCourses, + }, + }, + }) const visibleRecommendation = useMemo(() => data?.recommend?.courses?.slice(0, 6) ?? [], [data]) const theme = useTheme() @@ -54,27 +57,6 @@ const RecommendationText: React.FC = (props: Recommenda }) }, [data, variant]) - useEffect(() => { - if (JSON.stringify(courseSearchQuery.variables) === JSON.stringify(lastSearchQuery)) return - fetchRecommendation({ - variables: { - req: { - variant, - semesterKey: courseGroup, - selectedCourses: selectedCourses, - }, - }, - }) - setLastSearchQuery(courseSearchQuery.variables) - }, [ - courseSearchQuery.variables, - selectedCourses, - courseGroup, - fetchRecommendation, - lastSearchQuery, - variant, - ]) - if (!visibleRecommendation || visibleRecommendation.length === 0) return null return (