Skip to content

🎥 Make videos programmatically with React

Fix Element install dialog layout shifts

Igor Samokhovets9c73ee573c5e
diff --git a/packages/example/e2e/studio-protocol.test.mts b/packages/example/e2e/studio-protocol.test.mts
index 0a5d4370b6..1e97fea951 100644
--- a/packages/example/e2e/studio-protocol.test.mts
+++ b/packages/example/e2e/studio-protocol.test.mts
@@ -386,8 +386,35 @@ const CloseupPlaceholder = () => {
name: 'New composition',
});
await expect(currentDestination).toHaveAttribute('aria-pressed', 'true');
+ let releaseNewCompositionPreflight = () => {};
+ const newCompositionPreflightGate = new Promise<void>((resolve) => {
+ releaseNewCompositionPreflight = resolve;
+ });
+ let newCompositionPreflightIsPending = false;
+ await studioPage.route('**/api/prepare-element-install', async (route) => {
+ const body = route.request().postDataJSON();
+ if (
+ !newCompositionPreflightIsPending &&
+ body.installationName === 'protocol-element' &&
+ body.destination.type === 'new-composition'
+ ) {
+ newCompositionPreflightIsPending = true;
+ await newCompositionPreflightGate;
+ }
+
+ await route.continue();
+ });
await currentDestination.press('ArrowRight');
await expect(newDestination).toHaveAttribute('aria-pressed', 'true');
+ await expect.poll(() => newCompositionPreflightIsPending).toBe(true);
+ await expect(dialog.getByText('Destination', {exact: true})).toBeVisible();
+ const addToControl = dialog.getByText('Add to', {exact: true});
+ const addToPositionWhilePending = (await addToControl.boundingBox())?.y;
+ expect(addToPositionWhilePending).toBeDefined();
+ releaseNewCompositionPreflight();
+ await expect(dialog.getByText('Destination', {exact: true})).toBeVisible();
+ const addToPositionWhenReady = (await addToControl.boundingBox())?.y;
+ expect(addToPositionWhenReady).toBe(addToPositionWhilePending);
await newDestination.press('ArrowLeft');
await expect(currentDestination).toHaveAttribute('aria-pressed', 'true');
await expect(dialog.getByText(senderUrl, {exact: true})).toBeVisible();
@@ -534,15 +561,38 @@ const CloseupPlaceholder = () => {
await newCompositionDialog
.getByRole('button', {name: 'New composition', exact: true})
.click();
+ await expect(
+ newCompositionDialog.getByRole('radio', {name: 'Create a copy'}),
+ ).toBeChecked();
+ let releaseCurrentCompositionPreflight = () => {};
+ const currentCompositionPreflightGate = new Promise<void>((resolve) => {
+ releaseCurrentCompositionPreflight = resolve;
+ });
+ let currentCompositionPreflightIsPending = false;
+ await studioPage.route('**/api/prepare-element-install', async (route) => {
+ const body = route.request().postDataJSON();
+ if (
+ !currentCompositionPreflightIsPending &&
+ body.installationName === 'protocol-element' &&
+ body.destination.type === 'current-composition'
+ ) {
+ currentCompositionPreflightIsPending = true;
+ await currentCompositionPreflightGate;
+ }
+
+ await route.continue();
+ });
await newCompositionDialog
.getByRole('button', {name: 'Current composition', exact: true})
.click();
+ await expect.poll(() => currentCompositionPreflightIsPending).toBe(true);
await expect(
newCompositionDialog.getByRole('radio', {name: 'Create a copy'}),
).toBeChecked();
await expect(
newCompositionDialog.getByLabel('Installation name'),
).toHaveValue('protocol-element-copy-3');
+ releaseCurrentCompositionPreflight();
const independentFile = path.join(
temporaryProject,
'src',
@@ -559,9 +609,16 @@ const CloseupPlaceholder = () => {
await waitForFile(independentFile);
await route.fulfill({response});
});
+ const destinationRow = newCompositionDialog
+ .getByText('Destination', {exact: true})
+ .locator('..');
+ await expect(destinationRow).toContainText('.element.tsx');
+ const destinationBeforeTyping = await destinationRow.textContent();
+ expect(destinationBeforeTyping).not.toBeNull();
await newCompositionDialog
.getByLabel('Installation name')
.fill('speaker-name');
+ await expect(destinationRow).toHaveText(destinationBeforeTyping!);
await expect(
newCompositionDialog.getByRole('button', {name: /^Install/}),
).toBeEnabled();
diff --git a/packages/studio/src/components/ElementInstallConfirmation.tsx b/packages/studio/src/components/ElementInstallConfirmation.tsx
index ba5f80fc95..9b3c9f79aa 100644
--- a/packages/studio/src/components/ElementInstallConfirmation.tsx
+++ b/packages/studio/src/components/ElementInstallConfirmation.tsx
@@ -3,6 +3,7 @@ import React, {
useCallback,
useContext,
useEffect,
+ useLayoutEffect,
useMemo,
useRef,
useState,
@@ -336,7 +337,7 @@ type PreparedElementInstallation = {
basePlan: ElementInstallPlan;
name: string;
refresh: number;
- plan: ElementInstallPlan & {filePath: string};
+ plan: ElementInstallPlan;
};
type NewCompositionPlanState =
@@ -576,36 +577,80 @@ export const ElementInstallConfirmation: React.FC<{
}
const requestedName = input.name;
- const [preparation, setPreparation] = useState<{
- basePlan: ElementInstallPlan;
- refresh: number;
- requestedName: string | null;
- installation: PreparedElementInstallation | null;
- error: string | null;
- } | null>(null);
+ const [preparations, setPreparations] = useState<
+ Array<{
+ basePlan: ElementInstallPlan;
+ refresh: number;
+ requestedName: string | null;
+ installation: PreparedElementInstallation | null;
+ error: string | null;
+ }>
+ >([]);
const currentPreparation =
- preparation?.basePlan === selectedPlan &&
- preparation?.refresh === refreshPlan &&
- preparation?.requestedName === requestedName
- ? preparation
- : null;
+ preparations.find(
+ (preparation) =>
+ preparation.basePlan === selectedPlan &&
+ preparation.refresh === refreshPlan &&
+ preparation.requestedName === requestedName,
+ ) ?? null;
const preparedInstallation = currentPreparation?.installation ?? null;
const planError = currentPreparation?.error ?? null;
const installationName =
requestedName ?? preparedInstallation?.name ?? elementBaseName;
- const [existingInstallation, setExistingInstallation] =
- useState<PreparedElementInstallation | null>(null);
+ const [existingInstallations, setExistingInstallations] = useState<
+ PreparedElementInstallation[]
+ >([]);
const existingDestination =
- existingInstallation?.basePlan === selectedPlan &&
- existingInstallation?.refresh === refreshPlan
- ? existingInstallation
- : null;
+ existingInstallations.find(
+ (installation) =>
+ installation.basePlan === selectedPlan &&
+ installation.refresh === refreshPlan,
+ ) ?? null;
const overwriteExisting =
existingDestination !== null &&
input.overwritePlan === existingDestination.plan;
const activePlan = overwriteExisting
? existingDestination.plan
: (preparedInstallation?.plan ?? null);
+ const [displayedPlans, setDisplayedPlans] = useState<
+ Array<{
+ basePlan: ElementInstallPlan;
+ refresh: number;
+ plan: ElementInstallPlan;
+ }>
+ >([]);
+ const rememberedDisplayedPlan =
+ displayedPlans.find(
+ (displayed) =>
+ displayed.basePlan === selectedPlan &&
+ displayed.refresh === refreshPlan,
+ )?.plan ?? null;
+ const displayedPlan = activePlan ?? rememberedDisplayedPlan ?? selectedPlan;
+ useLayoutEffect(() => {
+ if (activePlan === null || selectedPlan === null) {
+ return;
+ }
+
+ setDisplayedPlans((previous) => {
+ const existing = previous.find(
+ (displayed) =>
+ displayed.basePlan === selectedPlan &&
+ displayed.refresh === refreshPlan,
+ );
+ if (existing?.plan === activePlan) {
+ return previous;
+ }
+
+ return [
+ ...previous.filter(
+ (displayed) =>
+ displayed.basePlan !== selectedPlan ||
+ displayed.refresh !== refreshPlan,
+ ),
+ {basePlan: selectedPlan, refresh: refreshPlan, plan: activePlan},
+ ];
+ });
+ }, [activePlan, refreshPlan, selectedPlan]);
useEffect(() => {
if (selectedPlan === null) {
return;
@@ -641,11 +686,14 @@ export const ElementInstallConfirmation: React.FC<{
plan: result.plan,
};
if (result.plan.expectedFileState.exists) {
- setExistingInstallation((previous) =>
- previous?.basePlan === selectedPlan &&
- previous.refresh === refreshPlan
+ setExistingInstallations((previous) =>
+ previous.some(
+ (installation) =>
+ installation.basePlan === selectedPlan &&
+ installation.refresh === refreshPlan,
+ )
? previous
- : prepared,
+ : [...previous, prepared],
);
if (requestedName === null) {
candidate = `${elementBaseName}-copy${copyNumber === 1 ? '' : `-${copyNumber}`}`;
@@ -654,24 +702,40 @@ export const ElementInstallConfirmation: React.FC<{
}
}
- setPreparation({
- basePlan: selectedPlan,
- refresh: refreshPlan,
- requestedName,
- installation: prepared,
- error: null,
- });
+ setPreparations((previous) => [
+ ...previous.filter(
+ (preparation) =>
+ preparation.basePlan !== selectedPlan ||
+ preparation.refresh !== refreshPlan ||
+ preparation.requestedName !== requestedName,
+ ),
+ {
+ basePlan: selectedPlan,
+ refresh: refreshPlan,
+ requestedName,
+ installation: prepared,
+ error: null,
+ },
+ ]);
return;
}
})().catch((error) => {
if (!canceled) {
- setPreparation({
- basePlan: selectedPlan,
- refresh: refreshPlan,
- requestedName,
- installation: null,
- error: error instanceof Error ? error.message : String(error),
- });
+ setPreparations((previous) => [
+ ...previous.filter(
+ (preparation) =>
+ preparation.basePlan !== selectedPlan ||
+ preparation.refresh !== refreshPlan ||
+ preparation.requestedName !== requestedName,
+ ),
+ {
+ basePlan: selectedPlan,
+ refresh: refreshPlan,
+ requestedName,
+ installation: null,
+ error: error instanceof Error ? error.message : String(error),
+ },
+ ]);
}
});
return () => {
@@ -840,14 +904,18 @@ export const ElementInstallConfirmation: React.FC<{
</dd>
</dl>
- {activePlan ? (
- <dl style={requestSourceStyle}>
- <dt style={sectionTitleStyle}>Destination</dt>
- <dd style={requestSourceDescriptionStyle}>
- {activePlan.filePath}
- </dd>
- </dl>
- ) : null}
+ <dl
+ style={{
+ ...requestSourceStyle,
+ visibility: displayedPlan === null ? 'hidden' : 'visible',
+ }}
+ aria-hidden={displayedPlan === null}
+ >
+ <dt style={sectionTitleStyle}>Destination</dt>
+ <dd style={requestSourceDescriptionStyle}>
+ {displayedPlan?.filePath ?? '\u00A0'}
+ </dd>
+ </dl>
<div style={destinationControlStyle}>
<div style={sectionTitleStyle}>Add to</div>
diff --git a/packages/studio/src/state/modals.ts b/packages/studio/src/state/modals.ts
index c62049c885..f8bbf3fd6c 100644
--- a/packages/studio/src/state/modals.ts
+++ b/packages/studio/src/state/modals.ts
@@ -178,6 +178,7 @@ export type CanvasCaptureImport = {
export type ElementInstallPlan = {
readonly compositionFile: string;
readonly expectedFileState: ElementInstallExpectedFileState;
+ readonly filePath: string;
};
export type ElementInstallModalState = {