Skip to content

Commit

Permalink
refactor(project): combine and optimize Zustand selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed May 17, 2022
1 parent ef2db82 commit 8a4810e
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/components/Account/Account.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router-dom';
import shallow from 'zustand/shallow';

import type { FormSectionContentArgs, FormSectionProps } from '../Form/FormSection';
import Visibility from '../../icons/Visibility';
Expand Down Expand Up @@ -37,7 +38,14 @@ const Account = ({ panelClassName, panelHeaderClassName }: Props): JSX.Element =
const history = useHistory();
const [viewPassword, toggleViewPassword] = useToggle();

const { user: customer, customerConsents, publisherConsents } = useAccountStore();
const { customer, customerConsents, publisherConsents } = useAccountStore(
({ user, customerConsents, publisherConsents }) => ({
customer: user,
customerConsents,
publisherConsents,
}),
shallow,
);

const consentValues = useMemo(() => formatConsentValues(publisherConsents, customerConsents), [publisherConsents, customerConsents]);
const initialValues = useMemo(
Expand Down
3 changes: 2 additions & 1 deletion src/containers/AccountModal/AccountModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useState } from 'react';
import { useHistory } from 'react-router';
import shallow from 'zustand/shallow';

import Dialog from '../../components/Dialog/Dialog';
import useQueryParam from '../../hooks/useQueryParam';
Expand Down Expand Up @@ -28,7 +29,7 @@ const AccountModal = () => {
const viewParam = useQueryParam('u');
const [view, setView] = useState(viewParam);
const message = useQueryParam('message');
const { loading, auth } = useAccountStore();
const { loading, auth } = useAccountStore(({ loading, auth }) => ({ loading, auth }), shallow);
const config = useConfigStore((s) => s.config);
const {
assets: { banner },
Expand Down
3 changes: 2 additions & 1 deletion src/containers/AccountModal/forms/RenewSubscription.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router';
import shallow from 'zustand/shallow';

import { removeQueryParam } from '../../../utils/history';
import LoadingOverlay from '../../../components/LoadingOverlay/LoadingOverlay';
Expand All @@ -13,7 +14,7 @@ import { updateSubscription } from '#src/stores/AccountController';
const RenewSubscription = () => {
const { t } = useTranslation('account');
const history = useHistory();
const { subscription, user } = useAccountStore();
const { subscription, user } = useAccountStore(({ subscription, user }) => ({ subscription, user }), shallow);
const [renewed, setRenewed] = useState(false);
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import WindowScroller from 'react-virtualized/dist/commonjs/WindowScroller';
import List from 'react-virtualized/dist/commonjs/List';
import { useHistory } from 'react-router-dom';
import classNames from 'classnames';
import shallow from 'zustand/shallow';

import PlaylistContainer from '../../containers/Playlist/PlaylistContainer';
import { favoritesStore } from '../../stores/FavoritesStore';
Expand Down Expand Up @@ -53,8 +54,7 @@ const Home = (): JSX.Element => {
const updateBlurImage = useBlurImageUpdater(playlist);

// User
const user = useAccountStore((state) => state.user);
const subscription = !!useAccountStore((state) => state.subscription);
const { user, subscription } = useAccountStore(({ user, subscription }) => ({ user, subscription }), shallow);

const onCardClick = useCallback(
(playlistItem: PlaylistItem, playlistId?: string) => {
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Movie/Movie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { RouteComponentProps } from 'react-router-dom';
import { useHistory } from 'react-router';
import { Helmet } from 'react-helmet';
import { useTranslation } from 'react-i18next';
import shallow from 'zustand/shallow';

import { useFavorites } from '../../stores/FavoritesStore';
import useBlurImageUpdater from '../../hooks/useBlurImageUpdater';
Expand Down Expand Up @@ -69,8 +70,7 @@ const Movie = ({ match, location }: RouteComponentProps<MovieRouteParams>): JSX.
const [playTrailer, setPlayTrailer] = useState<boolean>(false);

// User
const user = useAccountStore((state) => state.user);
const subscription = useAccountStore((state) => state.subscription);
const { user, subscription } = useAccountStore(({ user, subscription }) => ({ user, subscription }), shallow);
const allowedToWatch = isAllowedToWatch(accessModel, !!user, itemRequiresSubscription, !!subscription);

// Handlers
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Playlist/Playlist.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useMemo, useState } from 'react';
import { RouteComponentProps, useHistory } from 'react-router-dom';
import { Helmet } from 'react-helmet';
import shallow from 'zustand/shallow';

import { cardUrl } from '../../utils/formatting';
import usePlaylist from '../../hooks/usePlaylist';
Expand Down Expand Up @@ -38,8 +39,7 @@ function Playlist({
const updateBlurImage = useBlurImageUpdater(filteredPlaylist);

// User
const user = useAccountStore((state) => state.user);
const subscription = !!useAccountStore((state) => state.subscription);
const { user, subscription } = useAccountStore(({ user, subscription }) => ({ user, subscription }), shallow);

useEffect(() => {
// reset filter when the playlist id changes
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { RouteComponentProps } from 'react-router-dom';
import { useHistory } from 'react-router';
import { Helmet } from 'react-helmet';
import { useTranslation } from 'react-i18next';
import shallow from 'zustand/shallow';

import useBlurImageUpdater from '../../hooks/useBlurImageUpdater';
import { UIStore } from '../../stores/UIStore';
Expand Down Expand Up @@ -41,8 +42,7 @@ const Search: React.FC<RouteComponentProps<SearchRouteParams>> = ({
const updateBlurImage = useBlurImageUpdater(playlist);

// User
const user = useAccountStore((state) => state.user);
const subscription = !!useAccountStore((state) => state.subscription);
const { user, subscription } = useAccountStore(({ user, subscription }) => ({ user, subscription }), shallow);

// Update the search bar query to match the route param on mount
useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Series/Series.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { RouteComponentProps } from 'react-router-dom';
import { useHistory } from 'react-router';
import { Helmet } from 'react-helmet';
import { useTranslation } from 'react-i18next';
import shallow from 'zustand/shallow';

import CardGrid from '../../components/CardGrid/CardGrid';
import { useFavorites } from '../../stores/FavoritesStore';
Expand Down Expand Up @@ -76,8 +77,7 @@ const Series = ({ match, location }: RouteComponentProps<SeriesRouteParams>): JS
const [playTrailer, setPlayTrailer] = useState<boolean>(false);

// User
const user = useAccountStore((state) => state.user);
const subscription = useAccountStore((state) => state.subscription);
const { user, subscription } = useAccountStore(({ user, subscription }) => ({ user, subscription }), shallow);
const allowedToWatch = isAllowedToWatch(accessModel, !!user, itemRequiresSubscription, !!subscription);

// Handlers
Expand Down

0 comments on commit 8a4810e

Please sign in to comment.