Skip to content

Commit

Permalink
Fix empty strings overwriting during scrape (#3647)
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants authored Apr 7, 2023
1 parent 0cd0151 commit 9b8d124
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions ui/v2.5/src/components/Scenes/SceneMergeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ScrapedInputGroupRow,
ScrapedTextAreaRow,
ScrapeResult,
ZeroableScrapeResult,
} from "../Shared/ScrapeDialog";
import { clone, uniq } from "lodash-es";
import {
Expand Down Expand Up @@ -65,8 +66,9 @@ const SceneMergeDetails: React.FC<ISceneMergeDetailsProps> = ({
);

const [rating, setRating] = useState(
new ScrapeResult<number>(dest.rating100)
new ZeroableScrapeResult<number>(dest.rating100)
);
// zero values can be treated as missing for these fields
const [oCounter, setOCounter] = useState(
new ScrapeResult<number>(dest.o_counter)
);
Expand Down Expand Up @@ -118,7 +120,7 @@ const SceneMergeDetails: React.FC<ISceneMergeDetailsProps> = ({
const [stashIDs, setStashIDs] = useState(new ScrapeResult<GQL.StashId[]>([]));

const [organized, setOrganized] = useState(
new ScrapeResult<boolean>(dest.organized)
new ZeroableScrapeResult<boolean>(dest.organized)
);

const [image, setImage] = useState<ScrapeResult<string>>(
Expand Down
21 changes: 20 additions & 1 deletion ui/v2.5/src/components/Shared/ScrapeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class ScrapeResult<T> {
) {
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);
Expand Down Expand Up @@ -68,6 +70,23 @@ export class ScrapeResult<T> {
}
}

// for types where !!value is a valid value (boolean and number)
export class ZeroableScrapeResult<T> extends ScrapeResult<T> {
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<any>[]) {
return values.some((r) => r.scraped);
Expand Down

0 comments on commit 9b8d124

Please sign in to comment.