Skip to content

Commit

Permalink
refactor: use memo for title
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Apr 18, 2024
1 parent 8d69123 commit 03523ef
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/ui-react/src/pages/Search/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import React, { useEffect, useMemo } from 'react';
import { useParams } from 'react-router';
import { Helmet } from 'react-helmet';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -34,7 +34,19 @@ const Search = () => {
// User
const { user, subscription } = useAccountStore(({ user, subscription }) => ({ user, subscription }), shallow);

const getURL = (playlistItem: PlaylistItem) => mediaURL({ media: playlistItem, playlistId: features?.searchPlaylist });
const getURL = (playlistItem: PlaylistItem) =>
mediaURL({
media: playlistItem,
playlistId: features?.searchPlaylist,
});

const title = useMemo(() => {
if (isFetching) return t('heading');
if (!query) return t('start_typing');
if (!playlist?.playlist.length) return t('no_results_heading', { query });

return t('title', { count: playlist.playlist.length, query });
}, [isFetching, playlist?.playlist.length, query, t]);

// Update the search bar query to match the route param on mount
useEffect(() => {
Expand All @@ -56,14 +68,6 @@ const Search = () => {
};
}, []);

const title = isFetching
? t('heading')
: !query
? t('start_typing')
: playlist?.playlist.length
? t('title', { count: playlist.playlist.length, query })
: t('no_results_heading', { query });

if ((error || !playlist) && !isFetching) {
return (
<ErrorPage title={t('error_heading')}>
Expand Down

0 comments on commit 03523ef

Please sign in to comment.