From 5c2b237ed6fb06df97a331978a35067a29c4c51a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eline=20J=C3=A5sund?= Date: Fri, 6 Nov 2026 12:07:37 +0100 Subject: [PATCH 01/11] chore: add lazy loading to images --- Frontend/src/components/ActiveGameCard.tsx | 1 + Frontend/src/components/Footer.tsx | 8 +++++++- Frontend/src/components/Header.tsx | 7 ++++++- Frontend/src/components/PassiveGameCard.tsx | 1 + Frontend/src/components/WishlistGameCard.tsx | 1 + Frontend/src/pages/MainPage.tsx | 7 ++++++- 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Frontend/src/components/ActiveGameCard.tsx b/Frontend/src/components/ActiveGameCard.tsx index a07fc96..20d5347 100644 --- a/Frontend/src/components/ActiveGameCard.tsx +++ b/Frontend/src/components/ActiveGameCard.tsx @@ -140,6 +140,7 @@ export default function ActiveGameCard({ component="img" image={game.image} alt={`${game.name} game poster`} + loading="lazy" /> {isLoggedIn && ( + + + )} + + ) +} diff --git a/Frontend/src/components/SearchPageResults.tsx b/Frontend/src/components/SearchPageResults.tsx new file mode 100644 index 0000000..7f896f5 --- /dev/null +++ b/Frontend/src/components/SearchPageResults.tsx @@ -0,0 +1,87 @@ +import { memo } from 'react' +import { Pagination } from '@mui/material' +import type { Game } from '../../../Backend/Steam' +import PassiveGameCard from './PassiveGameCard' + +const SearchResultsGrid = memo(function SearchResultsGrid({ games }: { games: Game[] }) { + return ( +
+ {games.map((game) => { + const price = Number(game.price) + const salePercent = Number(game.discount) + + return ( + + ) + })} +
+ ) +}) + +interface SearchPageResultsProps { + games: Game[] + error: string | null + isLoading: boolean + totalCount: number + page: number + onPageChange: (page: number) => void +} + +const GAMES_PER_PAGE = 12 + +export default function SearchPageResults({ + games, + error, + isLoading, + totalCount, + page, + onPageChange +}: SearchPageResultsProps) { + const pageCount = Math.max(1, Math.ceil(totalCount / GAMES_PER_PAGE)) + + function HandleBottomPageChange(_: unknown, value: number) { + onPageChange(value) + window.scrollTo({ top: 0, behavior: 'smooth' }) + } + + return ( + <> +
+ {totalCount > 0 && ( +

+ {totalCount} {totalCount === 1 ? 'game' : 'games'} found +

+ )} + onPageChange(value)} + /> +
+ {error && ( +

+ Could not load games: {error} +

+ )} + {isLoading && games.length === 0 &&

Loading games...

} + {!isLoading && games.length === 0 &&

No games found.

} + {games.length > 0 && } + + + ) +} diff --git a/Frontend/src/hooks/useSearchPageResults.ts b/Frontend/src/hooks/useSearchPageResults.ts new file mode 100644 index 0000000..343ca14 --- /dev/null +++ b/Frontend/src/hooks/useSearchPageResults.ts @@ -0,0 +1,109 @@ +import { useEffect, useMemo, useState } from 'react' +import type { Game } from '../../../Backend/Steam' +import FetchGamesFromDB, { GetTotalGameCount } from '../FrontendLogic/GameLogicFrontend' +import { FetchGenres } from '../FrontendLogic/SearchPageLogicFrontend' + +const GAMES_PER_PAGE = 12 +const NO_GAMES: Game[] = [] + +interface SearchPageResultsOptions { + page: number + search: string + genres: string[] + sort: string +} + +interface SearchPageResult { + key: string + games: Game[] + error: string | null +} + +function AreGamesEqual(first: Game[], second: Game[]): boolean { + return ( + first.length === second.length && + first.every((game, index) => { + const other = second[index] + return ( + game.appid === other.appid && + game.name === other.name && + game.price === other.price && + game.initialprice === other.initialprice && + game.discount === other.discount && + game.img === other.img && + game.genre === other.genre && + game.description === other.description + ) + }) + ) +} + +export function useSearchPageResults({ page, search, genres, sort }: SearchPageResultsOptions) { + const [result, setResult] = useState(null) + const [totalCount, setTotalCount] = useState(0) + const [availableGenres, setAvailableGenres] = useState([]) + + useEffect(() => { + FetchGenres().then(setAvailableGenres).catch(console.error) + }, []) + + useEffect(() => { + let cancelled = false + GetTotalGameCount({ search, genres }) + .then((count) => { + if (!cancelled) setTotalCount(count) + }) + .catch(console.error) + return () => { + cancelled = true + } + }, [search, genres]) + + const requestKey = useMemo( + () => JSON.stringify([page, search, genres, sort]), + [page, search, genres, sort] + ) + const isCurrent = result !== null && result.key === requestKey + + useEffect(() => { + let cancelled = false + FetchGamesFromDB(GAMES_PER_PAGE, (page - 1) * GAMES_PER_PAGE, { + search, + genres, + sort + }) + .then((loadedGames) => { + if (!cancelled) { + setResult((previous) => ({ + key: requestKey, + games: + previous && AreGamesEqual(previous.games, loadedGames) + ? previous.games + : loadedGames, + error: null + })) + } + }) + .catch((err: unknown) => { + if (!cancelled) { + setResult({ + key: requestKey, + games: [], + error: err instanceof Error ? err.message : 'Could not load games' + }) + } + }) + + return () => { + cancelled = true + } + }, [requestKey, page, search, genres, sort]) + + return { + genres: availableGenres, + totalCount, + games: result?.games ?? NO_GAMES, + error: isCurrent ? result.error : null, + isLoading: !isCurrent + } +} diff --git a/Frontend/src/pages/SearchPage.tsx b/Frontend/src/pages/SearchPage.tsx index c813731..4b95070 100644 --- a/Frontend/src/pages/SearchPage.tsx +++ b/Frontend/src/pages/SearchPage.tsx @@ -1,25 +1,14 @@ import '../css/SearchPage.css' import Header from '../components/Header' import Footer from '../components/Footer' -import { - InputLabel, - FormControl, - Select, - MenuItem, - Paper, - InputBase, - IconButton, - Pagination, - Button -} from '@mui/material' -import { Funnel } from 'lucide-react' -import { memo, useState, useEffect, useMemo } from 'react' -import { useDebouncedValue } from '../hooks/useDebouncedValue' import PageHeading from '../components/PageHeading' -import PassiveGameCard from '../components/PassiveGameCard' -import FetchGamesFromDB, { GetTotalGameCount } from '../FrontendLogic/GameLogicFrontend' +import SearchPageControls, { type SortOption } from '../components/SearchPageControls' +import SearchPageResults from '../components/SearchPageResults' +import { useDebouncedValue } from '../hooks/useDebouncedValue' +import { useSearchPageResults } from '../hooks/useSearchPageResults' +import { useState, useEffect } from 'react' +import type { SelectChangeEvent } from '@mui/material' import { - FetchGenres, GetSavedFiltersSearchpage, SaveFiltersSearchpage, GetQuerySearchpage, @@ -31,11 +20,6 @@ import { SaveFilterMenuOpenSearchpage, GetFilterMenuOpenSearchpage } from '../FrontendLogic/SearchPageLogicFrontend' -import type { Game } from '../../../Backend/Steam' -import type { SelectChangeEvent } from '@mui/material' -import { X } from 'lucide-react' - -type SortOption = '' | 'discount-desc' | 'price-asc' | 'price-desc' | 'title-asc' | 'title-desc' const DEFAULT_SORT: SortOption = 'title-asc' const SORT_OPTIONS: SortOption[] = [ @@ -45,108 +29,47 @@ const SORT_OPTIONS: SortOption[] = [ 'price-asc', 'price-desc' ] -const GAMES_PER_PAGE = 12 - -const NO_GAMES: Game[] = [] const NO_FILTERS: string[] = [] -function AreGamesEqual(first: Game[], second: Game[]): boolean { - return ( - first.length === second.length && - first.every((game, index) => { - const other = second[index] - return ( - game.appid === other.appid && - game.name === other.name && - game.price === other.price && - game.initialprice === other.initialprice && - game.discount === other.discount && - game.img === other.img && - game.genre === other.genre && - game.description === other.description - ) - }) - ) -} - -const SearchResultsGrid = memo(function SearchResultsGrid({ games }: { games: Game[] }) { - return ( -
- {games.map((game) => { - const price = Number(game.price) - const salePercent = Number(game.discount) - - return ( - - ) - })} -
- ) -}) - function GetInitialSort(): SortOption { const saved = GetSortOptionSearchpage() return SORT_OPTIONS.includes(saved as SortOption) ? (saved as SortOption) : DEFAULT_SORT } export function SearchPage() { - const [result, setResult] = useState<{ - key: string - games: Game[] - error: string | null - } | null>(null) - const [totalCount, setTotalCount] = useState(0) const [page, setPage] = useState(GetPageSearchpage) - const [genres, setGenres] = useState([]) const [activeFilters, setActiveFilters] = useState(GetSavedFiltersSearchpage) const [activeSorting, setActiveSorting] = useState(GetInitialSort) - const [searchVal, setSearchVal] = useState(GetQuerySearchpage) - const debouncedSearchVal = useDebouncedValue(searchVal) - const searchQuery = debouncedSearchVal.trim() + const [searchValue, setSearchValue] = useState(GetQuerySearchpage) + const searchQuery = useDebouncedValue(searchValue).trim() const [isFilterActive, setIsFilterActive] = useState(GetFilterMenuOpenSearchpage) const appliedFilters = isFilterActive && activeFilters.length > 0 ? activeFilters : NO_FILTERS const appliedSorting = isFilterActive ? activeSorting : DEFAULT_SORT + const { genres, totalCount, games, error, isLoading } = useSearchPageResults({ + page, + search: searchQuery, + genres: appliedFilters, + sort: appliedSorting + }) - // fetches the genres from the DB - useEffect(() => { - FetchGenres().then(setGenres).catch(console.error) - }, []) - - // Opens the filter menu const ToggleActive = () => { const hasSelection = activeFilters.length > 0 || activeSorting !== DEFAULT_SORT - if (hasSelection) { - setPage(1) - } + if (hasSelection) setPage(1) setIsFilterActive(!isFilterActive) } - // saves the filter menu state (open: yes/no) - useEffect(() => { - SaveFilterMenuOpenSearchpage(isFilterActive) - }, [isFilterActive]) - - const HandleSearchSubmit = (e: React.FormEvent) => { - e.preventDefault() + const HandleSearchSubmit = (event: React.FormEvent) => { + event.preventDefault() } - const HandleFilterChange = (e: SelectChangeEvent) => { - const { value } = e.target + const HandleFilterChange = (event: SelectChangeEvent) => { + const { value } = event.target setActiveFilters(typeof value === 'string' ? value.split(',') : value) setPage(1) } - const HandleSortChange = (e: SelectChangeEvent) => { - setActiveSorting(e.target.value as SortOption) + const HandleSortChange = (event: SelectChangeEvent) => { + setActiveSorting(event.target.value as SortOption) setPage(1) } @@ -156,227 +79,67 @@ export function SearchPage() { } const HandleReset = () => { - setSearchVal('') + setSearchValue('') setActiveFilters(NO_FILTERS) setActiveSorting(DEFAULT_SORT) setPage(1) } const HandleRemoveSearch = () => { - setSearchVal('') + setSearchValue('') setPage(1) } - // These useEffects save the current filters, query and sorting option in session storage - // so that when the user goes back to the searchpage they are kept. + useEffect(() => { + SaveFilterMenuOpenSearchpage(isFilterActive) + }, [isFilterActive]) + useEffect(() => { SaveFiltersSearchpage(activeFilters) }, [activeFilters]) + useEffect(() => { SaveSortOptionSearchpage(activeSorting) }, [activeSorting]) + useEffect(() => { SaveQuerySearchpage(searchQuery) }, [searchQuery]) + useEffect(() => { SavePageSearchpage(page) }, [page]) - // Total count of games matching the current search and filters - useEffect(() => { - let cancelled = false - GetTotalGameCount({ search: searchQuery, genres: appliedFilters }) - .then((count) => { - if (!cancelled) setTotalCount(count) - }) - .catch(console.error) - return () => { - cancelled = true - } - }, [searchQuery, appliedFilters]) - - const requestKey = useMemo( - () => JSON.stringify([page, searchQuery, appliedFilters, appliedSorting]), - [page, searchQuery, appliedFilters, appliedSorting] - ) - const isCurrent = result !== null && result.key === requestKey - const games = result?.games ?? NO_GAMES - const error = isCurrent ? result.error : null - const isLoading = !isCurrent - - // One page of games, reloaded whenever page, search, filters or sorting change - useEffect(() => { - let cancelled = false - - FetchGamesFromDB(GAMES_PER_PAGE, (page - 1) * GAMES_PER_PAGE, { - search: searchQuery, - genres: appliedFilters, - sort: appliedSorting - }) - .then((loadedGames) => { - if (!cancelled) { - setResult((previous) => ({ - key: requestKey, - games: - previous && AreGamesEqual(previous.games, loadedGames) - ? previous.games - : loadedGames, - error: null - })) - } - }) - .catch((err: unknown) => { - if (!cancelled) { - setResult({ - key: requestKey, - games: [], - error: err instanceof Error ? err.message : 'Could not load games' - }) - } - }) - - return () => { - cancelled = true - } - }, [requestKey, page, searchQuery, appliedFilters, appliedSorting]) - - // Decides games per page and makes a constant for the pagination - const pageCount = Math.max(1, Math.ceil(totalCount / GAMES_PER_PAGE)) - return (
- - { - setSearchVal(e.target.value) - setPage(1) - }} - /> - - - - - - - {isFilterActive && ( -
- - Sort by - - - - - Filter by genre - - - - -
- )} -
- {totalCount > 0 && ( -

- {totalCount} {totalCount === 1 ? 'game' : 'games'} found -

- )} - - setPage(value)} - /> -
- {error && ( -

- Could not load games: {error} -

- )} - {isLoading && games.length === 0 &&

Loading games...

} - {!isLoading && games.length === 0 &&

No games found.

} - {games.length > 0 && } - { - setPage(value) - window.scrollTo({ top: 0, behavior: 'smooth' }) + { + setSearchValue(value) + setPage(1) }} + onSearchSubmit={HandleSearchSubmit} + onClearSearch={HandleRemoveSearch} + isFilterActive={isFilterActive} + onToggleFilters={ToggleActive} + genres={genres} + activeFilters={activeFilters} + onFilterChange={HandleFilterChange} + activeSorting={activeSorting} + onSortChange={HandleSortChange} + onRemoveFilters={HandleRemoveFilters} + onReset={HandleReset} + /> +
From dbd4d00a1f6305549e9a9af58015eb227b8e0561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eline=20J=C3=A5sund?= Date: Tue, 10 Nov 2026 13:27:27 +0100 Subject: [PATCH 10/11] style: remove redundant linespace --- Frontend/src/components/PassiveGameCard.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/Frontend/src/components/PassiveGameCard.tsx b/Frontend/src/components/PassiveGameCard.tsx index a54c369..45a7d63 100644 --- a/Frontend/src/components/PassiveGameCard.tsx +++ b/Frontend/src/components/PassiveGameCard.tsx @@ -38,7 +38,6 @@ export default function PassiveGameCard({ game, rank }: Props) { resizeObserver.disconnect() } }, [game.name]) - return (
Date: Tue, 10 Nov 2026 13:27:58 +0100 Subject: [PATCH 11/11] style: remove redundant linespace --- Frontend/src/components/PassiveGameCard.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/Frontend/src/components/PassiveGameCard.tsx b/Frontend/src/components/PassiveGameCard.tsx index 45a7d63..96291d9 100644 --- a/Frontend/src/components/PassiveGameCard.tsx +++ b/Frontend/src/components/PassiveGameCard.tsx @@ -43,7 +43,6 @@ export default function PassiveGameCard({ game, rank }: Props) { - {rank !== undefined && ( #{rank}