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

Initial ModInstance type and adapter applied to re-/activation code (1/3) #9182

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export type ActivateModComponentParam = {

/**
* Transform a given ModComponentDefinition into an ActivatedModComponent.
*
* Assigns a fresh UUID to the mod component.
*
* Note: This function has no side effects, it's just a type-transformer. It does
* NOT save the activated mod component anywhere.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/activation/modOptionsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function autoCreateDatabaseOptionsArgsInPlace(
databaseFactory: (args: { name: string }) => Promise<UUID | undefined>,
): Promise<OptionsArgs> {
const optionsProperties = Object.entries(
modDefinition.options?.schema?.properties ?? {},
modDefinition.options?.schema.properties ?? {},
)
.filter(
([name, fieldSchema]) =>
Expand Down
1 change: 0 additions & 1 deletion src/activation/useActivateMod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ function setupInputs(): {
modDefinition: ModDefinition;
} {
const formValues: WizardValues = {
modComponents: { 0: true },
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped unused modComponents toggle field from the type

integrationDependencies: [],
optionsArgs: {},
};
Expand Down
27 changes: 11 additions & 16 deletions src/activation/useActivateMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import modComponentSlice from "@/store/modComponents/modComponentSlice";
import reportEvent from "@/telemetry/reportEvent";
import { getErrorMessage } from "@/errors/errorHelpers";
import { deactivateMod } from "@/store/deactivateUtils";
import { selectActivatedModComponents } from "@/store/modComponents/modComponentSelectors";
import { ensurePermissionsFromUserGesture } from "@/permissions/permissionsUtils";
import { checkModDefinitionPermissions } from "@/modDefinitions/modDefinitionPermissionsHelpers";
import {
Expand All @@ -37,6 +36,8 @@ import { type ReportEventData } from "@/telemetry/telemetryTypes";
import { type Deployment, type DeploymentPayload } from "@/types/contract";
import { PIXIEBRIX_INTEGRATION_ID } from "@/integrations/constants";
import notify from "@/utils/notify";
import { selectModInstanceMap } from "@/store/modComponents/modInstanceSelectors";
import { getIsPersonalDeployment } from "@/store/modComponents/modInstanceUtils";

export type ActivateResult = {
success: boolean;
Expand Down Expand Up @@ -81,17 +82,15 @@ function useActivateMod(
{ checkPermissions = true }: { checkPermissions?: boolean } = {},
): ActivateModFormCallback {
const dispatch = useDispatch();
const activatedModComponents = useSelector(selectActivatedModComponents);
const modInstanceMap = useSelector(selectModInstanceMap);

const [createDatabase] = useCreateDatabaseMutation();
const [createUserDeployment] = useCreateUserDeploymentMutation();

return useCallback(
async (formValues: WizardValues, modDefinition: ModDefinition) => {
const activeModComponent = activatedModComponents.find(
(x) => x._recipe?.id === modDefinition.metadata.id,
);
const isReactivate = Boolean(activeModComponent);
const modInstance = modInstanceMap.get(modDefinition.metadata.id);
const isReactivate = Boolean(modInstance);

if (source === "extensionConsole") {
// Note: The prefix "Marketplace" on the telemetry event name
Expand Down Expand Up @@ -150,24 +149,20 @@ function useActivateMod(
},
);

const existingModComponents = activatedModComponents.filter(
(x) => x._recipe?.id === modDefinition.metadata.id,
);

await deactivateMod(
modDefinition.metadata.id,
existingModComponents,
modInstance?.modComponentIds ?? [],
dispatch,
);

// TODO: handle updating a deployment from a previous version to the new version and
// TODO: handle updating a deployment from a previous version to the new version and
// handle deleting a deployment if the user turns off personal deployment
// https://github.com/pixiebrix/pixiebrix-extension/issues/9092
let createdUserDeployment: Deployment | undefined;
if (
formValues.personalDeployment &&
// Avoid creating a personal deployment if the mod already has one
!activeModComponent?._deployment?.isPersonalDeployment
// Avoid creating a personal deployment if the mod is already associated with one
!getIsPersonalDeployment(modInstance)
) {
const data: DeploymentPayload = {
name: `Personal deployment for ${modDefinition.metadata.name}, version ${modDefinition.metadata.version}`,
Expand Down Expand Up @@ -202,7 +197,7 @@ function useActivateMod(
configuredDependencies: integrationDependencies,
optionsArgs,
screen: source,
isReactivate: existingModComponents.length > 0,
isReactivate,
deployment: createdUserDeployment,
}),
);
Expand All @@ -226,7 +221,7 @@ function useActivateMod(
};
},
[
activatedModComponents,
modInstanceMap,
source,
checkPermissions,
dispatch,
Expand Down
2 changes: 1 addition & 1 deletion src/activation/useActivateModWizard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe("useActivateModWizard", () => {
],
defaultAuthOptions: {},
databaseOptions: [],
activatedModComponents: [],
modInstance: undefined,
optionsValidationSchema: Yup.object(),
initialModOptions: {},
});
Expand Down
Loading
Loading