Skip to content

Commit

Permalink
Improve page editor utility method names
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller committed Jul 5, 2024
1 parent 7233420 commit 3e58f42
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/pageEditor/hooks/useCreateModFromModComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { useCreateModDefinitionMutation } from "@/data/service/api";
import { useDispatch, useSelector } from "react-redux";
import { actions as editorActions } from "@/pageEditor/store/editor/editorSlice";
import useUpsertModComponentFormState from "@/pageEditor/hooks/useUpsertModComponentFormState";
import { selectModMetadata } from "@/pageEditor/utils";
import { mapModDefinitionUpsertResponseToModMetadata } from "@/pageEditor/utils";
import { selectKeepLocalCopyOnCreateMod } from "@/pageEditor/store/editor/editorSelectors";
import { useRemoveModComponentFromStorage } from "@/pageEditor/hooks/useRemoveModComponentFromStorage";
import useBuildAndValidateMod from "@/pageEditor/hooks/useBuildAndValidateMod";
Expand Down Expand Up @@ -84,7 +84,7 @@ function useCreateModFromModComponent(
}).unwrap();

const newModComponent = produce(newModComponentFormState, (draft) => {
draft.modMetadata = selectModMetadata(
draft.modMetadata = mapModDefinitionUpsertResponseToModMetadata(
newModDefinition,
upsertResponse,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import {
} from "@/telemetry/traceHelpers";
import { DocumentRenderer } from "@/bricks/renderers/document";
import {
getBlockAnnotations,
filterBrickAnnotations,
getDocumentBuilderPipelinePaths,
getFoundationNodeAnnotations,
filterStarterBrickAnalysisAnnotations,
getVariableKeyForSubPipeline,
getPipelinePropNames,
} from "@/pageEditor/utils";
Expand Down Expand Up @@ -413,7 +413,7 @@ const usePipelineNodes = (): {
// eslint-disable-next-line security/detect-object-injection -- relying on nodeId being a UUID
const blockPath = maybePipelineMap?.[nodeId]?.path;
const blockAnnotations = blockPath
? getBlockAnnotations(blockPath, annotations)
? filterBrickAnnotations(blockPath, annotations)
: [];

contentProps = {
Expand Down Expand Up @@ -717,7 +717,7 @@ const usePipelineNodes = (): {
icon: starterBrickIcon,
runStatus: decideFoundationStatus({
hasTraces: modComponentHasTraces,
blockAnnotations: getFoundationNodeAnnotations(annotations),
blockAnnotations: filterStarterBrickAnalysisAnnotations(annotations),
}),
brickLabel: starterBrickLabel,
outputKey: "input" as OutputKey,
Expand Down
42 changes: 22 additions & 20 deletions src/pageEditor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ import AddDynamicTextSnippet from "@/bricks/effects/AddDynamicTextSnippet";
import { type PackageUpsertResponse } from "@/types/contract";
import { type UnsavedModDefinition } from "@/types/modDefinitionTypes";

export function mapModDefinitionUpsertResponseToModMetadata(
unsavedModDefinition: UnsavedModDefinition,
response: PackageUpsertResponse,
): ModComponentBase["_recipe"] {
return {
...unsavedModDefinition.metadata,
sharing: pick(response, ["public", "organizations"]),
...pick(response, ["updated_at"]),
};
}

export function getModComponentId(
modComponentOrFormState: ModComponentBase | ModComponentFormState,
): UUID {
Expand All @@ -65,7 +76,7 @@ export function getModId(
*
* Returns prop names in the order they should be displayed in the layout.
*
* @param brick the brick, or null if resolved block not available yet
* @param brick the brick, or null if resolved brick not available yet
* @param brickConfig the brick configuration
*
* @see PipelineToggleField
Expand Down Expand Up @@ -208,14 +219,16 @@ function getDocumentBuilderElementsPipelinePropNames(
return propNames;
}

export function getDocumentBuilderPipelinePaths(block: BrickConfig): string[] {
export function getDocumentBuilderPipelinePaths(
brickConfig: BrickConfig,
): string[] {
return getDocumentBuilderElementsPipelinePropNames(
"config.body",
(block.config.body ?? []) as DocumentBuilderElement[],
(brickConfig.config.body ?? []) as DocumentBuilderElement[],
);
}

export function getFoundationNodeAnnotations(
export function filterStarterBrickAnalysisAnnotations(
annotations: AnalysisAnnotation[],
): AnalysisAnnotation[] {
return annotations.filter(
Expand All @@ -224,20 +237,20 @@ export function getFoundationNodeAnnotations(
);
}

export function getBlockAnnotations(
blockPath: string,
export function filterBrickAnnotations(
brickPath: string,
annotations: AnalysisAnnotation[],
): AnalysisAnnotation[] {
const pathLength = blockPath.length;
const pathLength = brickPath.length;

const relatedAnnotations = annotations.filter((annotation) =>
annotation.position.path.startsWith(blockPath),
annotation.position.path.startsWith(brickPath),
);

return relatedAnnotations.filter((annotation) => {
const restPath = annotation.position.path.slice(pathLength);
// XXX: this may be not a reliable way to determine if the annotation
// is owned by the block or its sub pipeline.
// is owned by the brick or its sub pipeline.
// It assumes that it's only the pipeline field that can have a ".__value__" followed by "." in the path,
// and a pipeline field always has this pattern in its path.
return !restPath.includes(".__value__.");
Expand All @@ -252,14 +265,3 @@ export function selectPageEditorDimensions() {
window.innerWidth > window.innerHeight ? "landscape" : "portrait",
};
}

export function selectModMetadata(
unsavedModDefinition: UnsavedModDefinition,
response: PackageUpsertResponse,
): ModComponentBase["_recipe"] {
return {
...unsavedModDefinition.metadata,
sharing: pick(response, ["public", "organizations"]),
...pick(response, ["updated_at"]),
};
}

0 comments on commit 3e58f42

Please sign in to comment.