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

Improve Page Editor utils method names #8752

Merged
merged 2 commits into from
Jul 5, 2024
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
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,11 +34,11 @@ import {
} from "@/telemetry/traceHelpers";
import { DocumentRenderer } from "@/bricks/renderers/document";
import {
getBlockAnnotations,
getDocumentBuilderPipelinePaths,
getFoundationNodeAnnotations,
filterStarterBrickAnalysisAnnotations,
getVariableKeyForSubPipeline,
getPipelinePropNames,
filterAnnotationsByBrickPath,
} from "@/pageEditor/utils";
import { get, isEmpty } from "lodash";
import {
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)
? filterAnnotationsByBrickPath(annotations, blockPath)
: [];

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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: are we also referring to Packages as ModDefinitions? Maybe this could instead be mapPackageUpsertResponseToMod(Definition)Metadata

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we also referring to Packages as ModDefinitions

Packages is anything in the registry (could be a mod definition, brick definition, integration definition)

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 filterAnnotationsByBrickPath(
annotations: AnalysisAnnotation[],
brickPath: string,
): 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__.");
Comment on lines 252 to 256
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side note: I hate this 🤢

Expand All @@ -252,14 +265,3 @@ export function selectPageEditorDimensions() {
window.innerWidth > window.innerHeight ? "landscape" : "portrait",
};
}

export function selectModMetadata(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to collocate with other mod metadata-related methods

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