Skip to content

Commit 50b2b49

Browse files
authored
Remove lodash.get() calls (#259)
1 parent 1886dac commit 50b2b49

File tree

17 files changed

+57
-67
lines changed

17 files changed

+57
-67
lines changed

src/components/ScrobbleItem/ScrobbleItem.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Fragment } from 'react';
2-
import { get } from 'lodash-es';
32
import { Trans } from 'react-i18next';
43
import { LazyLoadImage } from 'react-lazy-load-image-component';
54

@@ -61,7 +60,7 @@ export default function ScrobbleItem({
6160
lazyScrollPosition,
6261
}: ScrobbleItemProps) {
6362
const { settings } = useSettings();
64-
const useOriginalTimestamp = noMenu ? get(settings, 'keepOriginalTimestamp') : false;
63+
const useOriginalTimestamp = noMenu ? settings?.keepOriginalTimestamp : false;
6564
const scrobbleAgain = useScrobbleAgain(scrobble, useOriginalTimestamp, analyticsEvent, cleanupPattern);
6665

6766
const showAlbumArt = !compact && !noCover;
@@ -190,7 +189,7 @@ export default function ScrobbleItem({
190189
{scrobble.status === 'error' && (
191190
<div className="error px-2">
192191
{scrobble.errorDescription && !scrobble.errorMessage ? <Trans i18nKey={scrobble.errorDescription} /> : null}
193-
{get(scrobble, 'errorMessage')}
192+
{scrobble.errorMessage}
194193
</div>
195194
)}
196195
</div>

src/domains/scrobbleAlbum/partials/AlbumResults.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { get } from 'lodash-es';
21
import ReactGA from 'react-ga-neo';
32
import { Trans } from 'react-i18next';
43
import { useNavigate } from 'react-router-dom';
@@ -26,9 +25,10 @@ export default function AlbumResults({
2625

2726
if (Array.isArray(albums)) {
2827
if (albums.length === 0) {
28+
const albumOrArtist = (query as any)?.name ?? query;
2929
return (
3030
<div className="col-12 text-center my-4">
31-
<Trans i18nKey="noAlbumsFound" values={{ albumOrArtist: get(query, 'name', query) }}>
31+
<Trans i18nKey="noAlbumsFound" values={{ albumOrArtist }}>
3232
No albums found for <em>your search query</em>
3333
</Trans>
3434
<br />

src/domains/scrobbleSong/SongForm.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Suspense, useContext, useEffect, useState } from 'react';
22
import addDays from 'date-fns/addDays';
33
import addSeconds from 'date-fns/addSeconds';
4-
import { get } from 'lodash-es';
54
import ReactGA from 'react-ga-neo';
65
import { Trans } from 'react-i18next';
76
import { lazyWithPreload } from 'react-lazy-with-preload';
@@ -285,7 +284,7 @@ export function SongForm() {
285284
return;
286285
}
287286
const info = await trackGetInfo({ artist, title });
288-
const suggestedAlbum = get(info, 'album.title');
287+
const suggestedAlbum = info?.album?.title;
289288

290289
if (suggestedAlbum === undefined) {
291290
setalbumAutoFillStatus(AutoFillStatus.Fail);

src/domains/scrobbleUser/ScrobbleUserResults.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useEffect, useState } from 'react';
22
import { keepPreviousData, useQuery } from '@tanstack/react-query';
3-
import { get } from 'lodash-es';
43
import ReactGA from 'react-ga-neo';
54
import { Trans } from 'react-i18next';
65
import { useSelector } from 'react-redux';
@@ -23,8 +22,11 @@ import { UserResultsHeading } from './partials/UserResultsHeading';
2322

2423
import { MAX_RECENT_USERS } from 'Constants';
2524

25+
import type { AxiosError } from 'axios';
2626
import type { RootState } from 'store';
2727

28+
type UserGetRecentTracksResult = Awaited<ReturnType<typeof userGetRecentTracks>>;
29+
2830
export function ScrobbleUserResults() {
2931
const { username: usernameFromParams } = useParams();
3032
const lowercaseUsername = usernameFromParams.toLowerCase();
@@ -34,7 +36,10 @@ export function ScrobbleUserResults() {
3436
const [recentUsers, setRecentUsers] = useLocalStorage('recentUsers', []);
3537
const navigate = useNavigate();
3638

37-
const { data, isLoading, isFetching, isError, error, isSuccess, isPlaceholderData } = useQuery({
39+
const { data, isLoading, isFetching, isError, error, isSuccess, isPlaceholderData } = useQuery<
40+
UserGetRecentTracksResult,
41+
AxiosError
42+
>({
3843
queryKey: ['profile', lowercaseUsername, 'scrobbles', currentPage],
3944
queryFn: () => userGetRecentTracks(lowercaseUsername, currentPage),
4045
staleTime: 1000 * 60 * 3, // minutes
@@ -84,7 +89,7 @@ export function ScrobbleUserResults() {
8489

8590
useEffect(() => {
8691
if (isError) {
87-
const errNumber = get(error, 'response.data.error');
92+
const errNumber = (error?.response?.data as any)?.error;
8893
// 6: User not found - 17: User has a private profile
8994
if (errNumber === 6 || errNumber === 17) {
9095
// ToDo: handle private profile case

src/store/reducers/scrobbleReducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { get, hasIn } from 'lodash-es';
1+
import { hasIn } from 'lodash-es';
22
import shortid from 'shortid';
33

44
import { scrobble } from 'utils/clients/api/methods/scrobble';
@@ -200,7 +200,7 @@ const scrobbleReducer = (state = initialState, action) => {
200200
}
201201
return overrideScrobbleProps(state, action.payload.config.headers.scrobbleUUID, {
202202
status: 'error',
203-
errorMessage: get(action.payload, 'data.message'),
203+
errorMessage: action.payload?.data?.message,
204204
errorDescription: 'errors.unexpectedResponse',
205205
});
206206
}

src/utils/axios.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import axios from 'axios';
2-
import { get } from 'lodash-es';
32
import qs from 'qs';
43
import ReactGA from 'react-ga-neo';
54

@@ -28,7 +27,7 @@ interface ExtendedAxiosRequestConfig extends InternalAxiosRequestConfig {
2827
* For invalid (ERROR) responses
2928
*/
3029
function axiosErrorHandler(payload, dispatch) {
31-
const errorNumber = payload ? get(payload, 'data.error', payload.status) : -1;
30+
const errorNumber = payload?.data?.error ?? payload?.status ?? -1;
3231
let newError: Partial<Alert> = {
3332
type: 'danger',
3433
persistent: false,
@@ -53,7 +52,7 @@ function axiosErrorHandler(payload, dispatch) {
5352
newError.persistent = true;
5453
showErrorNumber = true;
5554

56-
if (get(payload, 'config.params.method') !== 'user.getRecentTracks') {
55+
if (payload?.config?.params?.method !== 'user.getRecentTracks') {
5756
logOut(dispatch)(newError);
5857
}
5958
break;
@@ -82,7 +81,7 @@ function axiosErrorHandler(payload, dispatch) {
8281
default:
8382
newError.title = 'unexpectedError';
8483
// newError.message = 'unexpectedErrorMessage';
85-
newError.rawMessage = get(payload, 'data.message', null);
84+
newError.rawMessage = payload?.data?.message ?? null;
8685
showErrorNumber = true;
8786
}
8887

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { get } from 'lodash-es';
2-
31
import type { Settings } from 'utils/types/settings';
42

5-
export function settingsTransformer(settings = {}): Settings {
3+
export function settingsTransformer(settings: any = {}): Settings {
64
return {
7-
lang: get(settings, 'lang', 'auto'),
8-
use12Hours: !!get(settings, 'use12Hours', false),
9-
catchPaste: !!get(settings, 'catchPaste', true),
10-
showTrackNumbers: !!get(settings, 'showTrackNumbers', false),
11-
hasActiveSubscription: !!get(settings, 'activeSubscription', false),
12-
keepOriginalTimestamp: !!get(settings, 'keepOriginalTimestamp', true),
13-
patreonId: get(settings, 'patreonId', ''),
5+
lang: settings?.lang ?? 'auto',
6+
use12Hours: !!(settings?.use12Hours ?? false),
7+
catchPaste: !!(settings?.catchPaste ?? true),
8+
showTrackNumbers: !!(settings?.showTrackNumbers ?? false),
9+
hasActiveSubscription: !!(settings?.activeSubscription ?? false),
10+
keepOriginalTimestamp: !!(settings?.keepOriginalTimestamp ?? true),
11+
patreonId: settings?.patreonId ?? '',
1412
};
1513
}

src/utils/clients/discogs/transformers/topAlbumsResponse.transformer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { get } from 'lodash-es';
2-
31
import { sanitizeArtistName } from './common/sanitizeArtistName';
42

53
import type { Album } from 'utils/types/album';
64

75
export function topAlbumsTransformer(response: any): Album[] {
8-
return get(response, 'data.releases', [])
6+
return (response?.data?.releases ?? [])
97
.map((album) => ({
108
artist: sanitizeArtistName(album.artist),
119
discogsId: `${album.type === 'master' ? '' : 'release-'}${album.master_id || album.id}`,

src/utils/clients/discogs/transformers/tracksResponse.transformer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { get, hasIn } from 'lodash-es';
1+
import { hasIn } from 'lodash-es';
22
import shortid from 'shortid';
33

44
import { sanitizeArtistName } from './common/sanitizeArtistName';
@@ -29,8 +29,8 @@ export function tracksTransformer(
2929
return [];
3030
}
3131

32-
const albumArtist = get(options, 'artist', '');
33-
const album = get(options, 'album', '');
32+
const albumArtist = options?.artist ?? '';
33+
const album = options?.album ?? '';
3434

3535
return response?.data.tracklist
3636
.filter(({ type_: trackType }) => {
@@ -41,7 +41,7 @@ export function tracksTransformer(
4141
})
4242
.map((track) => {
4343
const transformedTrack = {
44-
artist: sanitizeArtistName(get(track, 'artists[0].name', options?.artist || '')),
44+
artist: sanitizeArtistName((track.artists?.[0]?.name ?? options?.artist) || ''),
4545
title: track.title,
4646
album,
4747
albumArtist,

src/utils/clients/lastfm/methods/trackGetInfo.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { get } from 'lodash-es';
2-
31
import { lastfmAPI } from '../apiClient';
42

53
export async function trackGetInfo(track: { mbid?: string; artist: string; title: string }) {
@@ -19,5 +17,5 @@ export async function trackGetInfo(track: { mbid?: string; artist: string; title
1917
},
2018
});
2119

22-
return get(response, 'data.track');
20+
return response?.data?.track;
2321
}

0 commit comments

Comments
 (0)