From 10a18f488b48ae55187d44e8f832c173f72bf433 Mon Sep 17 00:00:00 2001 From: Enrico Lamperti Date: Sun, 15 Sep 2024 17:41:43 -0300 Subject: [PATCH] Use context for cleanup pattern and misc changes --- public/locales/en.json | 2 +- public/locales/it.json | 1 - src/components/ScrobbleItem.css | 4 +- src/components/ScrobbleItem.tsx | 51 +++---- src/components/ScrobbleList.tsx | 8 +- .../scrobbleAlbum/CleanupContext.test.ts | 125 ++++++++++++++++++ src/domains/scrobbleAlbum/CleanupContext.ts | 123 +++++++++++++++++ .../scrobbleAlbum/ScrobbleAlbumTracklist.tsx | 14 +- .../scrobbleAlbum/partials/Tracklist.tsx | 26 +--- .../partials/TracklistCleanupForm.tsx | 50 ------- .../partials/TracklistFilter.scss | 5 + .../partials/TracklistFilter.tsx | 48 +++++++ src/hooks/usePatternSmart.ts | 37 ------ src/utils/common.test.ts | 86 +----------- src/utils/common.ts | 33 ----- 15 files changed, 345 insertions(+), 268 deletions(-) create mode 100644 src/domains/scrobbleAlbum/CleanupContext.test.ts create mode 100644 src/domains/scrobbleAlbum/CleanupContext.ts delete mode 100644 src/domains/scrobbleAlbum/partials/TracklistCleanupForm.tsx create mode 100644 src/domains/scrobbleAlbum/partials/TracklistFilter.scss create mode 100644 src/domains/scrobbleAlbum/partials/TracklistFilter.tsx delete mode 100644 src/hooks/usePatternSmart.ts diff --git a/public/locales/en.json b/public/locales/en.json index 26f7bd16..30e88e51 100644 --- a/public/locales/en.json +++ b/public/locales/en.json @@ -20,7 +20,7 @@ "customTimestamp": "Custom timestamp", "albumTimestampLogicDescription": "Scrobbling tracks \"now\" will scrobble the last track at the current time, backdating the previous ones accordingly (as if you had just finished listening). The \"custom timestamp\" will define the timestamp of the first track and add time to the following tracks from there (i.e. you specify the time you started listening to this album).", "albumArtist": "Album artist", - "albumCleanupPattern": "Removable text pattern from every track name", + "filter": "Filter", "albumCleanupPatternDescription": "Some albums tracks are displayed with some repeated text (i.e. Demo, Live, Remastered, ...). These are patterns that could be considered annoying to scrobble, for some users. Set the full pattern (including paranthesis, dashes, ...) that you would like to be removed, from the track names.", "history": "History", "yourProfile": "Your profile", diff --git a/public/locales/it.json b/public/locales/it.json index 98077ab8..33f62df2 100644 --- a/public/locales/it.json +++ b/public/locales/it.json @@ -20,7 +20,6 @@ "customTimestamp": "Timestamp personalizzato", "albumTimestampLogicDescription": "Scrobblando le canzoni \"adesso\" scrobblerà l'ultima canzone in questo momento, anticipando relativamente le precedenti (come se avessi appena finito di ascoltarle). Il \"timestamp personalizzato\" definirà il timestamp di ascolto della prima traccia ed agginguerà il relativo tempo alle canzoni successive (come se avessi iniziato ad ascoltare l'album nel timestamp selezionato)", "albumArtist": "Artista di un Album", - "albumCleanupPattern": "Pattern removibile da ogni nome di canzone", "albumCleanupPatternDescription": "Alcune canzoni di album sono visualizzate con un certo testo ripetuto (ad esempio Demo, Live, Remastered, ...). Questi sono pattern che potrebbero risultare fastidiosi da scrobblare, per alcuni utenti. Imposta il pattern completo (incluse parentesi, trattini, ...) che desideri venga rimosso dai nomi delle canzoni.", "history": "Storico", "yourProfile": "Tuo profilo", diff --git a/src/components/ScrobbleItem.css b/src/components/ScrobbleItem.css index 0dec2c1f..3ece4ee2 100644 --- a/src/components/ScrobbleItem.css +++ b/src/components/ScrobbleItem.css @@ -98,7 +98,7 @@ padding-right: 1.15rem; } -.scrobbled-item-cleanup { +.scrobbled-item del { color: var(--bs-secondary-color) !important; - text-decoration: line-through; + background-color: rgba(255, 32, 0, 0.2); } diff --git a/src/components/ScrobbleItem.tsx b/src/components/ScrobbleItem.tsx index a35aa6b3..06d52ac6 100644 --- a/src/components/ScrobbleItem.tsx +++ b/src/components/ScrobbleItem.tsx @@ -10,8 +10,7 @@ import isToday from 'date-fns/isToday'; import getYear from 'date-fns/getYear'; import { Button, Input, Dropdown, DropdownToggle, DropdownMenu, DropdownItem, FormGroup, Label } from 'reactstrap'; -import { useState } from 'react'; -import { renderToStaticMarkup } from 'react-dom/server'; +import { Fragment, useState } from 'react'; import { LazyLoadImage } from 'react-lazy-load-image-component'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { @@ -27,16 +26,16 @@ import { } from '@fortawesome/free-solid-svg-icons'; import { faClock, faCopy } from '@fortawesome/free-regular-svg-icons'; import { getAmznLink } from 'Constants'; +import { breakStringUsingPattern, cleanTitleWithPattern } from 'domains/scrobbleAlbum/CleanupContext'; import type { Scrobble } from 'utils/types/scrobble'; -import { replaceLastOccurrence } from 'utils/common'; import './ScrobbleItem.css'; import { useSettings } from 'hooks/useSettings'; interface ScrobbleItemProps { scrobble: Scrobble; - cleanupPattern?: string; + cleanupPattern?: RegExp; compact?: boolean; hideArtist?: boolean; muteArtist?: boolean; @@ -51,7 +50,7 @@ interface ScrobbleItemProps { export default function ScrobbleItem({ scrobble, - cleanupPattern = '', + cleanupPattern, compact = false, hideArtist = false, muteArtist = false, @@ -90,8 +89,9 @@ export default function ScrobbleItem({ enqueueScrobble(dispatch)([ { ...scrobble, - title: cleanupPattern ? scrobble.title.replaceAll(cleanupPattern, '').trim() : scrobble.title, + title: cleanTitleWithPattern(scrobble.title, cleanupPattern), timestamp: useOriginalTimestamp ? scrobble.timestamp : new Date(), + cleanupPattern: cleanupPattern || undefined, }, ]); setHasScrobbledAgain(true); @@ -109,20 +109,10 @@ export default function ScrobbleItem({ }); }; - const getCompleteItem = (item: string, highlight?: string) => { - const parsedItem = item; - - if (!highlight || !item.endsWith(highlight)) { - return parsedItem; - } - - const parsedComplete = highlight; - - const textComplete = renderToStaticMarkup({parsedComplete}); - - const itemComplete = replaceLastOccurrence(parsedItem, parsedComplete, textComplete); - - return itemComplete; + const strikethroughMatch = (text: string, pattern?: RegExp) => { + return breakStringUsingPattern(text, pattern).map(({ value, isMatch }, index) => ( + {!isMatch ? value : {value}} + )); }; let albumArt; @@ -220,18 +210,23 @@ export default function ScrobbleItem({ ); + const formattedTitle = strikethroughMatch(properCase(scrobble.title, true), cleanupPattern); if (!hideArtist) { if (muteArtist) { songFullTitle = ( <> - {properCase(scrobble.title, true)} {properCase(scrobble.artist)} + {formattedTitle} {properCase(scrobble.artist)} ); } else { - songFullTitle = `${properCase(scrobble.artist)} - ${properCase(scrobble.title, true)}`; + songFullTitle = ( + <> + {properCase(scrobble.artist)} - {formattedTitle} + + ); } } else { - songFullTitle = properCase(scrobble.title, true); + songFullTitle = formattedTitle; } const scrobbleItemInputId = `ScrobbleItem-checkbox-${scrobble.uuid}`; @@ -241,10 +236,7 @@ export default function ScrobbleItem({ songInfo = ( ); @@ -252,10 +244,7 @@ export default function ScrobbleItem({ // FULL view songInfo = ( <> - + {songFullTitle}