Skip to content

🎥 Make videos programmatically with React

URL state

Jonny Burger3b8c539cc50e
diff --git a/packages/studio/src/components/AssetSelectorItem.tsx b/packages/studio/src/components/AssetSelectorItem.tsx
index fa6fccdde2..60843d5a4f 100644
--- a/packages/studio/src/components/AssetSelectorItem.tsx
+++ b/packages/studio/src/components/AssetSelectorItem.tsx
@@ -9,6 +9,7 @@ import {
} from '../helpers/colors';
import {copyText} from '../helpers/copy-text';
import type {AssetFolder, AssetStructure} from '../helpers/create-folder-tree';
+import {pushUrl} from '../helpers/url-state';
import {ClipboardIcon} from '../icons/clipboard';
import {FileIcon} from '../icons/file';
import {CollapsedFolderIcon, ExpandedFolderIcon} from '../icons/folder';
@@ -208,7 +209,7 @@ const AssetSelectorItem: React.FC<{
? parentFolder + '/' + item.name
: item.name;
setCanvasContent({type: 'asset', asset: relativePath});
- window.history.pushState({}, 'Studio', `/assets/${relativePath}`);
+ pushUrl(`/assets/${relativePath}`);
}, [item.name, parentFolder, setCanvasContent]);
const style: React.CSSProperties = useMemo(() => {
diff --git a/packages/studio/src/components/InitialCompositionLoader.tsx b/packages/studio/src/components/InitialCompositionLoader.tsx
index 0096820f68..821d2e2b05 100644
--- a/packages/studio/src/components/InitialCompositionLoader.tsx
+++ b/packages/studio/src/components/InitialCompositionLoader.tsx
@@ -3,6 +3,7 @@ import {useContext, useEffect} from 'react';
import type {AnyComposition} from 'remotion';
import {getStaticFiles, Internals} from 'remotion';
import type {ExpandedFoldersState} from '../helpers/persist-open-folders';
+import {getPathname, pushUrl} from '../helpers/url-state';
import {FolderContext} from '../state/folders';
import {getKeysToExpand} from './CompositionSelector';
import {explorerSidebarTabs} from './ExplorerPanel';
@@ -39,7 +40,7 @@ export const useSelectComposition = () => {
return (c: AnyComposition, push: boolean) => {
if (push) {
- window.history.pushState({}, 'Studio', `/${c.id}`);
+ pushUrl(`/${c.id}`);
}
explorerSidebarTabs.current?.selectCompositionPanel();
@@ -113,7 +114,7 @@ export const InitialCompositionLoader: React.FC = () => {
const onchange = () => {
const newCanvas = deriveCanvasContentFromUrl();
if (newCanvas && newCanvas.type === 'composition') {
- const newComp = window.location.pathname.substring(1);
+ const newComp = getPathname().substring(1);
const exists = compositions.find((c) => c.id === newComp);
if (exists) {
selectComposition(exists, false);
diff --git a/packages/studio/src/components/RenderQueue/RenderQueueItem.tsx b/packages/studio/src/components/RenderQueue/RenderQueueItem.tsx
index 36b238947e..1a08e8d638 100644
--- a/packages/studio/src/components/RenderQueue/RenderQueueItem.tsx
+++ b/packages/studio/src/components/RenderQueue/RenderQueueItem.tsx
@@ -9,6 +9,7 @@ import React, {
import type {CanvasContent} from 'remotion';
import {Internals} from 'remotion';
import {getBackgroundFromHoverState} from '../../helpers/colors';
+import {pushUrl} from '../../helpers/url-state';
import {Row, Spacing} from '../layout';
import {
RenderQueueCopyToClipboard,
@@ -103,7 +104,7 @@ export const RenderQueueItem: React.FC<{
return {type: 'output', path: `/${job.outName}`};
});
- window.history.pushState({}, 'Studio', `/outputs/${job.outName}`);
+ pushUrl(`/outputs/${job.outName}`);
}, [
job.outName,
job.status,
diff --git a/packages/studio/src/components/Timeline/TimelineStack/index.tsx b/packages/studio/src/components/Timeline/TimelineStack/index.tsx
index 1d22e10bbe..9bbcbcbbce 100644
--- a/packages/studio/src/components/Timeline/TimelineStack/index.tsx
+++ b/packages/studio/src/components/Timeline/TimelineStack/index.tsx
@@ -9,6 +9,7 @@ import {
VERY_LIGHT_TEXT,
} from '../../../helpers/colors';
import {openInEditor} from '../../../helpers/open-in-editor';
+import {pushUrl} from '../../../helpers/url-state';
import {useSelectAsset} from '../../InitialCompositionLoader';
import {Spacing} from '../../layout';
import {sendErrorNotification} from '../../Notifications/NotificationCenter';
@@ -53,7 +54,7 @@ export const TimelineStack: React.FC<{
const navigateToAsset = useCallback(
(asset: string) => {
selectAsset(asset);
- window.history.pushState({}, 'Studio', `/assets/${asset}`);
+ pushUrl(`/assets/${asset}`);
},
[selectAsset],
);
diff --git a/packages/studio/src/components/ZoomPersistor.tsx b/packages/studio/src/components/ZoomPersistor.tsx
index a64e25f54c..bfeee58dd5 100644
--- a/packages/studio/src/components/ZoomPersistor.tsx
+++ b/packages/studio/src/components/ZoomPersistor.tsx
@@ -2,6 +2,7 @@ import type React from 'react';
import {useContext, useEffect} from 'react';
import type {CanvasContent} from 'remotion';
import {Internals} from 'remotion';
+import {getPathname} from '../helpers/url-state';
import {TimelineZoomCtx} from '../state/timeline-zoom';
const makeKey = () => {
@@ -18,25 +19,21 @@ export const getZoomFromLocalStorage = (): Record<string, number> => {
};
export const deriveCanvasContentFromUrl = (): CanvasContent | null => {
- const substrings = window.location.pathname.split('/').filter(Boolean);
+ const substrings = getPathname().split('/').filter(Boolean);
const lastPart = substrings[substrings.length - 1];
if (substrings[0] === 'assets') {
return {
type: 'asset',
- asset: decodeURIComponent(
- window.location.pathname.substring('/assets/'.length),
- ),
+ asset: decodeURIComponent(getPathname().substring('/assets/'.length)),
};
}
if (substrings[0] === 'outputs') {
return {
type: 'output',
- path: decodeURIComponent(
- window.location.pathname.substring('/outputs/'.length),
- ),
+ path: decodeURIComponent(getPathname().substring('/outputs/'.length)),
};
}
diff --git a/packages/studio/src/error-overlay/entry-basic.ts b/packages/studio/src/error-overlay/entry-basic.ts
index 02171c3338..c2d58fcfdb 100644
--- a/packages/studio/src/error-overlay/entry-basic.ts
+++ b/packages/studio/src/error-overlay/entry-basic.ts
@@ -1,3 +1,4 @@
+import {reloadUrl} from '../helpers/url-state';
import {didUnmountReactApp, startReportingRuntimeErrors} from './react-overlay';
import {mountRemotionOverlay} from './remotion-overlay';
import {setErrorsRef} from './remotion-overlay/Overlay';
@@ -115,7 +116,7 @@ export const startErrorOverlay = () => {
__webpack_module__.hot.addStatusHandler((status) => {
if (status === 'apply') {
if (didUnmountReactApp()) {
- return window.location.reload();
+ return reloadUrl();
}
setErrorsRef.current?.setErrors({
diff --git a/packages/studio/src/error-overlay/react-overlay/listen-to-runtime-errors.ts b/packages/studio/src/error-overlay/react-overlay/listen-to-runtime-errors.ts
index 621a9ca767..b6f1df2d8f 100644
--- a/packages/studio/src/error-overlay/react-overlay/listen-to-runtime-errors.ts
+++ b/packages/studio/src/error-overlay/react-overlay/listen-to-runtime-errors.ts
@@ -16,6 +16,7 @@ import {
* LICENSE file in the root directory of this source tree.
*/
import type {SymbolicatedStackFrame} from '@remotion/studio-shared';
+import {reloadUrl} from '../../helpers/url-state';
import {
register as registerError,
unregister as unregisterError,
@@ -74,7 +75,7 @@ const crashWithFrames = (crash: () => void) => (error: Error) => {
console.log('Hook order changed. Reloading app...');
window.remotion_unsavedProps = false;
- window.location.reload();
+ reloadUrl();
} else {
setErrorsRef.current?.addError(error);
diff --git a/packages/studio/src/error-overlay/remotion-overlay/DismissButton.tsx b/packages/studio/src/error-overlay/remotion-overlay/DismissButton.tsx
index 7d5f45a945..ad39919b61 100644
--- a/packages/studio/src/error-overlay/remotion-overlay/DismissButton.tsx
+++ b/packages/studio/src/error-overlay/remotion-overlay/DismissButton.tsx
@@ -1,4 +1,5 @@
import React, {useCallback} from 'react';
+import {clearUrl} from '../../helpers/url-state';
const size: React.CSSProperties = {
height: 20,
@@ -15,7 +16,7 @@ const style: React.CSSProperties = {
export const DismissButton: React.FC = () => {
const dismiss = useCallback(() => {
- window.location.href = '/';
+ clearUrl();
}, []);
return (
diff --git a/packages/studio/src/helpers/client-id.tsx b/packages/studio/src/helpers/client-id.tsx
index 15eb7ecbb6..d37c335581 100644
--- a/packages/studio/src/helpers/client-id.tsx
+++ b/packages/studio/src/helpers/client-id.tsx
@@ -5,6 +5,7 @@ import {Internals} from 'remotion';
import {sendErrorNotification} from '../components/Notifications/NotificationCenter';
import playBeepSound from '../components/PlayBeepSound';
import {renderJobsRef} from '../components/RenderQueue/context';
+import {reloadUrl} from './url-state';
type PreviewServerState =
| {
@@ -71,7 +72,7 @@ export const PreviewServerConnection: React.FC<{
newEvent.type === 'new-input-props' ||
newEvent.type === 'new-env-variables'
) {
- window.location.reload();
+ reloadUrl();
}
if (newEvent.type === 'init') {
diff --git a/packages/studio/src/helpers/url-state.ts b/packages/studio/src/helpers/url-state.ts
new file mode 100644
index 0000000000..754f2d91cf
--- /dev/null
+++ b/packages/studio/src/helpers/url-state.ts
@@ -0,0 +1,15 @@
+export const pushUrl = (url: string) => {
+ window.history.pushState({}, 'Studio', url);
+};
+
+export const clearUrl = () => {
+ window.location.href = '/';
+};
+
+export const reloadUrl = () => {
+ window.location.reload();
+};
+
+export const getPathname = () => {
+ return window.location.pathname;
+};
diff --git a/packages/studio/src/hot-middleware-client/process-update.ts b/packages/studio/src/hot-middleware-client/process-update.ts
index 223ae7cbae..b3644d8850 100644
--- a/packages/studio/src/hot-middleware-client/process-update.ts
+++ b/packages/studio/src/hot-middleware-client/process-update.ts
@@ -13,6 +13,7 @@
import type {HotMiddlewareOptions, ModuleMap} from '@remotion/studio-shared';
import {notificationCenter} from '../components/Notifications/NotificationCenter';
+import {reloadUrl} from '../helpers/url-state';
if (!__webpack_module__.hot) {
throw new Error('[Fast refresh] Hot Module Replacement is disabled.');
@@ -171,7 +172,7 @@ export const processUpdate = function (
'[Fast refresh] Update check failed: ' + (err.stack || err.message),
);
if (!window.remotion_unsavedProps) {
- window.location.reload();
+ reloadUrl();
}
}
}
@@ -191,7 +192,7 @@ export const processUpdate = function (
duration: 1,
});
} else {
- window.location.reload();
+ reloadUrl();
}
}
};