Skip to content

Commit

Permalink
Re-add method; drop unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller committed Sep 22, 2024
1 parent b3b6e1e commit 240e111
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 30 deletions.
29 changes: 0 additions & 29 deletions src/mods/hooks/useActivatedModComponents.ts

This file was deleted.

80 changes: 79 additions & 1 deletion src/store/modComponents/modInstanceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
ActivatedModComponent,
ModComponentBase,
} from "@/types/modComponentTypes";
import { omit, pick } from "lodash";
import { omit, pick, zip } from "lodash";
import { assertNotNullish } from "@/utils/nullishUtils";
import { uuidv4 } from "@/types/helpers";
import {
Expand All @@ -32,6 +32,18 @@ import { createPrivateSharing } from "@/utils/registryUtils";
import { emptyModOptionsDefinitionFactory } from "@/utils/modUtils";
import { assertModComponentNotHydrated } from "@/runtime/runtimeUtils";
import type { ModComponentDefinition } from "@/types/modDefinitionTypes";
import type { SetRequired } from "type-fest";
import { pickModDefinitionMetadata } from "@/modDefinitions/util/pickModDefinitionMetadata";
import getModDefinitionIntegrationIds from "@/integrations/util/getModDefinitionIntegrationIds";
import { emptyPermissionsFactory } from "@/permissions/permissionsUtils";

/**
* A version of ActivatedModComponent with stronger nullness guarantees to support type evolution in the future.
*/
type ModInstanceActivatedModComponent = SetRequired<
ActivatedModComponent,
"_recipe" | "definitions" | "integrationDependencies" | "permissions"
>;

/**
* Returns true if mod instance is defined and is associated with a personal deployment.
Expand All @@ -52,6 +64,72 @@ export function mapModComponentBaseToModComponentDefinition(
};
}

/**
* Returns the activated mod component for a given mod instance. Is side effect free -- only maps the shape, does
* not persist the mod components or modify the UI.
*
* @see mapModComponentDefinitionToActivatedModComponent
*/
export function mapModInstanceToActivatedModComponents(
modInstance: ModInstance,
): ModInstanceActivatedModComponent[] {
const { deploymentMetadata, integrationsArgs, updatedAt, definition } =
modInstance;

const modMetadata = pickModDefinitionMetadata(definition);

return zip(definition.extensionPoints, modInstance.modComponentIds).map(
([modComponentDefinition, modComponentId]) => {
assertNotNullish(
modComponentDefinition,
"extensionPoints mismatch with modComponentIds",
);
assertNotNullish(
modComponentId,
"extensionPoints mismatch with modComponentIds",
);

const componentIntegrationIds = getModDefinitionIntegrationIds({
extensionPoints: [modComponentDefinition],
});

const base = {
id: modComponentId,
active: true,
label: modComponentDefinition.label,
config: modComponentDefinition.config,
permissions:
modComponentDefinition.permissions ?? emptyPermissionsFactory(),
// Default to `v1` for backward compatability
apiVersion: definition.apiVersion ?? "v1",
_recipe: modMetadata,
// All definitions are pushed down into the mod components. That's OK because `resolveDefinitions` determines
// uniqueness based on the content of the definition. Therefore, bricks will be re-used as necessary
definitions: definition.definitions ?? {},
optionsArgs: modInstance.optionsArgs,
extensionPointId: modComponentDefinition.id,
createTimestamp: updatedAt,
updateTimestamp: updatedAt,
// XXX: do we have to filter to only the integrations referenced by this particular mod? Historically, was this
// only to simplify moving mod components in the Page Editor?
integrationDependencies: integrationsArgs.filter(({ integrationId }) =>
componentIntegrationIds.includes(integrationId),
),
} as ModInstanceActivatedModComponent;

if (modComponentDefinition.templateEngine) {
base.templateEngine = modComponentDefinition.templateEngine;
}

if (deploymentMetadata) {
base._deployment = deploymentMetadata;
}

return base;
},
);
}

/**
* Maps activated mod components to a mod instance.
* @param modComponents mod components from the mod
Expand Down

0 comments on commit 240e111

Please sign in to comment.