Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle equality when scraping scene performers #4094

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
ScrapedTextAreaRow,
} from "src/components/Shared/ScrapeDialog/ScrapeDialog";
import clone from "lodash-es/clone";
import { ScrapeResult } from "src/components/Shared/ScrapeDialog/scrapeResult";
import {
ObjectListScrapeResult,
ScrapeResult,
} from "src/components/Shared/ScrapeDialog/scrapeResult";
import {
ScrapedPerformersRow,
ScrapedStudioRow,
Expand Down Expand Up @@ -97,9 +100,9 @@ export const GalleryScrapeDialog: React.FC<IGalleryScrapeDialogProps> = (
}

const [performers, setPerformers] = useState<
ScrapeResult<GQL.ScrapedPerformer[]>
ObjectListScrapeResult<GQL.ScrapedPerformer>
>(
new ScrapeResult<GQL.ScrapedPerformer[]>(
new ObjectListScrapeResult<GQL.ScrapedPerformer>(
sortStoredIdObjects(
props.galleryPerformers.map((p) => ({
stored_id: p.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { useIntl } from "react-intl";
import { uniq } from "lodash-es";
import { Performer } from "src/components/Performers/PerformerSelect";
import { IHasStoredID, sortStoredIdObjects } from "src/utils/data";
import { ScrapeResult } from "src/components/Shared/ScrapeDialog/scrapeResult";
import {
ObjectListScrapeResult,
ScrapeResult,
} from "src/components/Shared/ScrapeDialog/scrapeResult";
import {
ScrapedMoviesRow,
ScrapedPerformersRow,
Expand Down Expand Up @@ -117,9 +120,9 @@ export const SceneScrapeDialog: React.FC<ISceneScrapeDialogProps> = ({
}

const [performers, setPerformers] = useState<
ScrapeResult<GQL.ScrapedPerformer[]>
ObjectListScrapeResult<GQL.ScrapedPerformer>
>(
new ScrapeResult<GQL.ScrapedPerformer[]>(
new ObjectListScrapeResult<GQL.ScrapedPerformer>(
sortStoredIdObjects(
scenePerformers.map((p) => ({
stored_id: p.id,
Expand Down
9 changes: 5 additions & 4 deletions ui/v2.5/src/components/Scenes/SceneMergeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { RatingSystem } from "src/components/Shared/Rating/RatingSystem";
import { ModalComponent } from "../Shared/Modal";
import { IHasStoredID, sortStoredIdObjects } from "src/utils/data";
import {
ObjectListScrapeResult,
ScrapeResult,
ZeroableScrapeResult,
hasScrapedValues,
Expand Down Expand Up @@ -118,9 +119,9 @@ const SceneMergeDetails: React.FC<ISceneMergeDetailsProps> = ({
}

const [performers, setPerformers] = useState<
ScrapeResult<GQL.ScrapedPerformer[]>
ObjectListScrapeResult<GQL.ScrapedPerformer>
>(
new ScrapeResult<GQL.ScrapedPerformer[]>(
new ObjectListScrapeResult<GQL.ScrapedPerformer>(
sortStoredIdObjects(dest.performers.map(idToStoredID))
)
);
Expand Down Expand Up @@ -203,8 +204,8 @@ const SceneMergeDetails: React.FC<ISceneMergeDetailsProps> = ({
);

setPerformers(
new ScrapeResult(
dest.performers.map(idToStoredID),
new ObjectListScrapeResult<GQL.ScrapedPerformer>(
sortStoredIdObjects(dest.performers.map(idToStoredID)),
uniqIDStoredIDs(all.map((s) => s.performers.map(idToStoredID)).flat())
)
);
Expand Down
51 changes: 46 additions & 5 deletions ui/v2.5/src/components/Shared/ScrapeDialog/scrapeResult.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import isEqual from "lodash-es/isEqual";
import lodashIsEqual from "lodash-es/isEqual";
import clone from "lodash-es/clone";
import { IHasStoredID } from "src/utils/data";

export class ScrapeResult<T> {
public newValue?: T;
public originalValue?: T;
public scraped: boolean = false;
public useNewValue: boolean = false;
private isEqual: (
v1: T | undefined | null,
v2: T | undefined | null
) => boolean;

public constructor(
originalValue?: T | null,
newValue?: T | null,
useNewValue?: boolean
useNewValue?: boolean,
isEqual: (
v1: T | undefined | null,
v2: T | undefined | null
) => boolean = lodashIsEqual
) {
this.originalValue = originalValue ?? undefined;
this.newValue = newValue ?? undefined;
this.isEqual = isEqual;

// 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;
Expand All @@ -32,7 +43,7 @@ export class ScrapeResult<T> {
const ret = clone(this);

ret.newValue = value;
ret.useNewValue = !isEqual(ret.newValue, ret.originalValue);
ret.useNewValue = !this.isEqual(ret.newValue, ret.originalValue);

// #2691 - if we're setting the value, assume it should be treated as
// scraped
Expand All @@ -53,9 +64,13 @@ export class ZeroableScrapeResult<T> extends ScrapeResult<T> {
public constructor(
originalValue?: T | null,
newValue?: T | null,
useNewValue?: boolean
useNewValue?: boolean,
isEqual: (
v1: T | undefined | null,
v2: T | undefined | null
) => boolean = lodashIsEqual
) {
super(originalValue, newValue, useNewValue);
super(originalValue, newValue, useNewValue, isEqual);

const hasNewValue = this.newValue !== undefined;

Expand All @@ -65,6 +80,32 @@ export class ZeroableScrapeResult<T> extends ScrapeResult<T> {
}
}

function storedIDsEqual<T extends IHasStoredID>(
o1: T[] | undefined | null,
o2: T[] | undefined | null
) {
return (
!!o1 &&
!!o2 &&
o1.length === o2.length &&
o1.every((o) => {
return o2.find((oo) => o.stored_id === oo.stored_id);
})
);
}

export class ObjectListScrapeResult<
T extends IHasStoredID
> extends ScrapeResult<T[]> {
public constructor(
originalValue?: T[] | null,
newValue?: T[] | null,
useNewValue?: boolean
) {
super(originalValue, newValue, useNewValue, storedIDsEqual);
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function hasScrapedValues(values: ScrapeResult<any>[]) {
return values.some((r) => r.scraped);
Expand Down
Loading