From db052f6c80ae62c8269a9f6e621ff7b4d9ec868f Mon Sep 17 00:00:00 2001 From: DingDongSoLong4 <99329275+DingDongSoLong4@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:55:12 +0200 Subject: [PATCH] Accept incorrectly insensitivised frontpage config keys --- ui/v2.5/src/components/FrontPage/Control.tsx | 14 ++---- .../src/components/FrontPage/FrontPage.tsx | 5 ++- .../components/FrontPage/FrontPageConfig.tsx | 6 ++- ui/v2.5/src/core/config.ts | 45 ++++++++++++++++++- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/ui/v2.5/src/components/FrontPage/Control.tsx b/ui/v2.5/src/components/FrontPage/Control.tsx index 3cb2cf02111..32c4faee962 100644 --- a/ui/v2.5/src/components/FrontPage/Control.tsx +++ b/ui/v2.5/src/components/FrontPage/Control.tsx @@ -1,10 +1,6 @@ import React, { useContext, useMemo } from "react"; import { useIntl } from "react-intl"; -import { - FrontPageContent, - ICustomFilter, - ISavedFilterRow, -} from "src/core/config"; +import { FrontPageContent, ICustomFilter } from "src/core/config"; import * as GQL from "src/core/generated-graphql"; import { useFindSavedFilter } from "src/core/StashService"; import { ConfigurationContext } from "src/hooks/Config"; @@ -167,17 +163,15 @@ interface IProps { export const Control: React.FC = ({ content }) => { switch (content.__typename) { case "SavedFilter": - if (!(content as ISavedFilterRow).savedFilterId) { + if (!content.savedFilterId) { return
Error: missing savedFilterId
; } return ( - + ); case "CustomFilter": - return ; + return ; default: return <>; } diff --git a/ui/v2.5/src/components/FrontPage/FrontPage.tsx b/ui/v2.5/src/components/FrontPage/FrontPage.tsx index b7be66b8ac2..7a106f330b4 100644 --- a/ui/v2.5/src/components/FrontPage/FrontPage.tsx +++ b/ui/v2.5/src/components/FrontPage/FrontPage.tsx @@ -10,6 +10,7 @@ import { ConfigurationContext } from "src/hooks/Config"; import { FrontPageContent, generateDefaultFrontPageContent, + getFrontPageContent, IUIConfig, } from "src/core/config"; import { useScrollToTopOnMount } from "src/hooks/scrollToTop"; @@ -65,12 +66,12 @@ const FrontPage: React.FC = () => { onUpdateConfig(defaultContent); } - const { frontPageContent } = ui; + const frontPageContent = getFrontPageContent(ui); return (
- {frontPageContent?.map((content: FrontPageContent, i) => ( + {frontPageContent?.map((content, i) => ( ))}
diff --git a/ui/v2.5/src/components/FrontPage/FrontPageConfig.tsx b/ui/v2.5/src/components/FrontPage/FrontPageConfig.tsx index 04464b6558c..eee1342ffeb 100644 --- a/ui/v2.5/src/components/FrontPage/FrontPageConfig.tsx +++ b/ui/v2.5/src/components/FrontPage/FrontPageConfig.tsx @@ -15,6 +15,7 @@ import { ICustomFilter, FrontPageContent, generatePremadeFrontPageContent, + getFrontPageContent, } from "src/core/config"; interface IAddSavedFilterModalProps { @@ -299,8 +300,9 @@ export const FrontPageConfig: React.FC = ({ return; } - if (ui?.frontPageContent) { - setCurrentContent(ui.frontPageContent); + const frontPageContent = getFrontPageContent(ui); + if (frontPageContent) { + setCurrentContent(frontPageContent); } }, [allFilters, ui]); diff --git a/ui/v2.5/src/core/config.ts b/ui/v2.5/src/core/config.ts index 854ed78c54a..55edf2d0115 100644 --- a/ui/v2.5/src/core/config.ts +++ b/ui/v2.5/src/core/config.ts @@ -33,7 +33,8 @@ export type FrontPageContent = ISavedFilterRow | ICustomFilter; export const defaultMaxOptionsShown = 200; export interface IUIConfig { - frontPageContent?: FrontPageContent[]; + // unknown to prevent direct access - use getFrontPageContent + frontPageContent?: unknown; showChildTagContent?: boolean; showChildStudioContent?: boolean; @@ -81,6 +82,48 @@ export interface IUIConfig { pinnedFilters?: PinnedFilters; } +interface ISavedFilterRowBroken extends ISavedFilterRow { + savedfilterid?: number; +} + +interface ICustomFilterBroken extends ICustomFilter { + sortby?: string; +} + +type FrontPageContentBroken = ISavedFilterRowBroken | ICustomFilterBroken; + +// #4128: deal with incorrectly insensitivised keys (sortBy and savedFilterId) +export function getFrontPageContent( + ui: IUIConfig +): FrontPageContent[] | undefined { + return (ui.frontPageContent as FrontPageContentBroken[] | undefined)?.map( + (content) => { + switch (content.__typename) { + case "SavedFilter": + if (content.savedfilterid) { + return { + ...content, + savedFilterId: content.savedFilterId ?? content.savedfilterid, + savedfilterid: undefined, + }; + } + return content; + case "CustomFilter": + if (content.sortby) { + return { + ...content, + sortBy: content.sortBy ?? content.sortby, + sortby: undefined, + }; + } + return content; + default: + return content; + } + } + ); +} + function recentlyReleased( intl: IntlShape, mode: FilterMode,