Skip to content

Commit

Permalink
Initial approach on posterhall performance optimisation (#1358)
Browse files Browse the repository at this point in the history
* Initial approach on posterhall performance optimisation

* Stylistic changes
  • Loading branch information
mike-lvov authored May 5, 2021
1 parent b9c5d59 commit 0210045
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/components/atoms/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ButtonProps } from "./Button.types";
import * as S from "./Button.styles";
import { Link } from "react-router-dom";

const AppButton: React.FC<ButtonProps> = ({
export const AppButton: React.FC<ButtonProps> = ({
customClass,
loading,
onClick,
Expand Down
4 changes: 4 additions & 0 deletions src/components/atoms/Button/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/**
* @deprecated Use named export instead of default export
*/
export { default } from "./Button";
export { AppButton as Button } from "./Button";
5 changes: 5 additions & 0 deletions src/components/templates/PosterHall/PosterHall.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
flex-wrap: wrap;
align-items: flex-start;
}

&__more-button {
display: flex;
justify-content: center;
}
}
20 changes: 18 additions & 2 deletions src/components/templates/PosterHall/PosterHall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { enterVenue } from "utils/url";

import { usePosters } from "hooks/posters";

import { Button } from "components/atoms/Button";

import { PosterPreview } from "./components/PosterPreview";
import { PosterHallSearch } from "./components/PosterHallSearch";

Expand All @@ -19,14 +21,18 @@ export interface PosterHallProps {
export const PosterHall: React.FC<PosterHallProps> = ({ venue }) => {
const {
posterVenues,
isPostersLoaded,

increaseDisplayedPosterCount,

searchInputValue,
setSearchInputValue,
liveFilter,
setLiveFilter,
} = usePosters(venue.id);

const renderedPosterPreviews = useMemo(() => {
return posterVenues?.map((posterVenue) => (
return posterVenues.map((posterVenue) => (
<PosterPreview
key={posterVenue.id}
posterVenue={posterVenue}
Expand All @@ -44,7 +50,17 @@ export const PosterHall: React.FC<PosterHallProps> = ({ venue }) => {
setLiveValue={setLiveFilter}
/>

<div className="PosterHall__posters">{renderedPosterPreviews}</div>
<div className="PosterHall__posters">
{isPostersLoaded ? renderedPosterPreviews : "Loading posters"}
</div>

<div className="PosterHall__more-button">
{isPostersLoaded && (
<Button onClick={increaseDisplayedPosterCount}>
Show more posters
</Button>
)}
</div>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ $active-poster-shadows: 0 10px 20px 0 rgba($primary, 0.56);
display: flex;
flex-direction: column;

content-visibility: auto;

height: $poster-preview-height;
width: $poster-preview-width;

Expand Down
46 changes: 31 additions & 15 deletions src/hooks/posters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useState, useMemo } from "react";
import { useState, useMemo, useCallback } from "react";
import { VenueTemplate } from "types/venues";
import Fuse from "fuse.js";

import { DEFAULT_DISPLAYED_POSTER_PREVIEW_COUNT } from "settings";

import { posterVenuesSelector } from "utils/selectors";

import { isLoaded, useFirestoreConnect } from "./useFirestoreConnect";
Expand Down Expand Up @@ -38,7 +40,7 @@ export const usePosterVenues = (posterHallId: string) => {
};

export const usePosters = (posterHallId: string) => {
const { posterVenues } = usePosterVenues(posterHallId);
const { posterVenues, isPostersLoaded } = usePosterVenues(posterHallId);

const {
searchInputValue,
Expand All @@ -47,37 +49,51 @@ export const usePosters = (posterHallId: string) => {
} = useDebounceSearch();

const [liveFilter, setLiveFilter] = useState<boolean>(false);
const [displayedPostersCount, setDisplayedPostersAmount] = useState(
DEFAULT_DISPLAYED_POSTER_PREVIEW_COUNT
);

const increaseDisplayedPosterCount = useCallback(() => {
setDisplayedPostersAmount(
(prevPostersNumber) =>
prevPostersNumber + DEFAULT_DISPLAYED_POSTER_PREVIEW_COUNT
);
}, []);

const filteredPosterVenues = useMemo(
() =>
liveFilter
? posterVenues.filter((posterVenue) => posterVenue.isLive)
: posterVenues,
[posterVenues, liveFilter]
);

const fuseVenues = useMemo(
() =>
new Fuse(posterVenues, {
new Fuse(filteredPosterVenues, {
keys: ["poster.title", "poster.authorName", "poster.categories"],
}),
[posterVenues]
[filteredPosterVenues]
);

const searchedPosterVenues = useMemo(() => {
if (!searchQuery) return posterVenues;
if (!searchQuery)
return filteredPosterVenues.slice(0, displayedPostersCount);

return fuseVenues
.search(searchQuery)
.slice(0, displayedPostersCount)
.map((fuseSearchItem) => fuseSearchItem.item);
}, [searchQuery, fuseVenues, posterVenues]);

const filteredPosterVenues = useMemo(
() =>
liveFilter
? searchedPosterVenues.filter((posterVenue) => posterVenue.isLive)
: searchedPosterVenues,
[searchedPosterVenues, liveFilter]
);
}, [searchQuery, fuseVenues, filteredPosterVenues, displayedPostersCount]);

return {
posterVenues: filteredPosterVenues,
posterVenues: searchedPosterVenues,
isPostersLoaded,

searchInputValue,
liveFilter,

increaseDisplayedPosterCount,
setSearchInputValue,
setLiveFilter,
};
Expand Down
10 changes: 7 additions & 3 deletions src/hooks/useDebounceSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ export const useDebounceSearch = () => {

const debouncedSearch = useMemo(
() =>
debounce((value: string) => {
setSearchQuery(value.toLowerCase());
}, SEARCH_DEBOUNCE_TIME),
debounce(
(value: string) => {
setSearchQuery(value.toLowerCase());
},
SEARCH_DEBOUNCE_TIME,
{ maxWait: SEARCH_DEBOUNCE_TIME * 3 }
),
[]
);

Expand Down
2 changes: 2 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,5 @@ export const ZENDESK_URL_PREFIXES = ["/admin"];
export const POSTERPAGE_MAX_VIDEO_PARTICIPANTS = 10;

export const SEARCH_DEBOUNCE_TIME = 200; // ms

export const DEFAULT_DISPLAYED_POSTER_PREVIEW_COUNT = 12;

0 comments on commit 0210045

Please sign in to comment.