From 605c1d47d2af4cd0b1ccf4a3da658f3a89b15ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Tue, 10 Sep 2024 11:31:26 +0100 Subject: [PATCH] chore: Slightly better types --- .../SearchResultCard/DataDisplayMap.tsx | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/editor.planx.uk/src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/DataDisplayMap.tsx b/editor.planx.uk/src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/DataDisplayMap.tsx index 34a7395bcf..1d086bfb54 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/DataDisplayMap.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/Sidebar/Search/SearchResultCard/DataDisplayMap.tsx @@ -1,4 +1,8 @@ import { ComponentType, IndexedNode } from "@opensystemslab/planx-core/types"; +import { Calculate } from "@planx/components/Calculate/model"; +import { FileUploadAndLabel } from "@planx/components/FileUploadAndLabel/model"; +import { List } from "@planx/components/List/model"; +import { PublicProps } from "@planx/components/ui"; import { SearchResult } from "hooks/useSearch"; import { capitalize, get } from "lodash"; import { SLUGS } from "pages/FlowEditor/data/types"; @@ -34,7 +38,7 @@ const DISPLAY_DATA: Partial = { displayKey: "Option (data)", getTitle: ({ item }) => { const parentNode = useStore.getState().flow[item.parentId]; - return parentNode!.data.text!; + return parentNode.data.text; }, getHeadline: ({ item, key }) => get(item, key)?.toString(), }, @@ -44,37 +48,41 @@ const DISPLAY_DATA: Partial = { default: { displayKey: "File type (data)", getHeadline: ({ item, refIndex }) => - (item["data"]?.["fileTypes"] as [])[refIndex]["fn"], + (item["data"] as unknown as PublicProps)[ + "fileTypes" + ][refIndex]["fn"], }, }, // Calculate contains both input and output data values [ComponentType.Calculate]: { formula: { displayKey: "Formula", - getHeadline: ({ item }) => item.data!.formula as string, + getHeadline: ({ item }) => + (item.data as unknown as PublicProps).formula, }, "data.output": { displayKey: "Output (data)", - getHeadline: ({ item }) => item.data!.output as string, + getHeadline: ({ item }) => + (item.data as unknown as PublicProps).output, }, }, // List contains data variables nested within its schema [ComponentType.List]: { "data.schema.fields.data.fn": { - getHeadline: ({ item, refIndex }) => { - // TODO: Add type guards, remove "as" - return (item.data as any).schema.fields[refIndex].data.fn; - }, + getHeadline: ({ item, refIndex }) => + (item.data as unknown as PublicProps).schema.fields[refIndex].data + .fn, }, "data.schema.fields.data.options.data.val": { displayKey: "Option (data)", getHeadline: ({ item, refIndex }) => { // Fuse.js flattens deeply nested arrays when using refIndex - // TODO: Add type guards, remove "as" - const options = (item.data as any).schema.fields.flatMap( - (field: any) => field.data.options, - ); - return options[refIndex].data.val; + const options = ( + item.data as unknown as PublicProps + ).schema.fields + .filter((field) => field.type === "question") + .flatMap((field) => field.data.options); + return options[refIndex].data.val || ""; }, }, },