From 9b8d124ac89d88f5bf4699dd6b71ee9e2b9c3dcb Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Sat, 8 Apr 2023 08:15:09 +1000 Subject: [PATCH] Fix empty strings overwriting during scrape (#3647) --- .../components/Scenes/SceneMergeDialog.tsx | 6 ++++-- .../src/components/Shared/ScrapeDialog.tsx | 21 ++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ui/v2.5/src/components/Scenes/SceneMergeDialog.tsx b/ui/v2.5/src/components/Scenes/SceneMergeDialog.tsx index 6625acc49..8f80ca1de 100644 --- a/ui/v2.5/src/components/Scenes/SceneMergeDialog.tsx +++ b/ui/v2.5/src/components/Scenes/SceneMergeDialog.tsx @@ -19,6 +19,7 @@ import { ScrapedInputGroupRow, ScrapedTextAreaRow, ScrapeResult, + ZeroableScrapeResult, } from "../Shared/ScrapeDialog"; import { clone, uniq } from "lodash-es"; import { @@ -65,8 +66,9 @@ const SceneMergeDetails: React.FC = ({ ); const [rating, setRating] = useState( - new ScrapeResult(dest.rating100) + new ZeroableScrapeResult(dest.rating100) ); + // zero values can be treated as missing for these fields const [oCounter, setOCounter] = useState( new ScrapeResult(dest.o_counter) ); @@ -118,7 +120,7 @@ const SceneMergeDetails: React.FC = ({ const [stashIDs, setStashIDs] = useState(new ScrapeResult([])); const [organized, setOrganized] = useState( - new ScrapeResult(dest.organized) + new ZeroableScrapeResult(dest.organized) ); const [image, setImage] = useState>( diff --git a/ui/v2.5/src/components/Shared/ScrapeDialog.tsx b/ui/v2.5/src/components/Shared/ScrapeDialog.tsx index 7b6d39492..0a5b1b9b2 100644 --- a/ui/v2.5/src/components/Shared/ScrapeDialog.tsx +++ b/ui/v2.5/src/components/Shared/ScrapeDialog.tsx @@ -36,7 +36,9 @@ export class ScrapeResult { ) { this.originalValue = originalValue ?? undefined; this.newValue = newValue ?? undefined; - const hasNewValue = this.newValue !== undefined; + // NOTE: this means that zero values are treated as null + // this is incorrect for numbers and booleans, but correct for strings + const hasNewValue = !!this.newValue; const valuesEqual = isEqual(originalValue, newValue); this.useNewValue = useNewValue ?? (hasNewValue && !valuesEqual); @@ -68,6 +70,23 @@ export class ScrapeResult { } } +// for types where !!value is a valid value (boolean and number) +export class ZeroableScrapeResult extends ScrapeResult { + public constructor( + originalValue?: T | null, + newValue?: T | null, + useNewValue?: boolean + ) { + super(originalValue, newValue, useNewValue); + + const hasNewValue = this.newValue !== undefined; + + const valuesEqual = isEqual(originalValue, newValue); + this.useNewValue = useNewValue ?? (hasNewValue && !valuesEqual); + this.scraped = hasNewValue && !valuesEqual; + } +} + // eslint-disable-next-line @typescript-eslint/no-explicit-any export function hasScrapedValues(values: ScrapeResult[]) { return values.some((r) => r.scraped);