Skip to content

🎥 Make videos programmatically with React

new notification system

Jonny Burger7adb487c82f8
diff --git a/packages/studio/src/components/AssetSelectorItem.tsx b/packages/studio/src/components/AssetSelectorItem.tsx
index 899ea1149a..d7688151df 100644
--- a/packages/studio/src/components/AssetSelectorItem.tsx
+++ b/packages/studio/src/components/AssetSelectorItem.tsx
@@ -19,10 +19,7 @@ import {SidebarContext} from '../state/sidebar';
import type {RenderInlineAction} from './InlineAction';
import {InlineAction} from './InlineAction';
import {Row, Spacing} from './layout';
-import {
- notificationCenter,
- showNotification,
-} from './Notifications/NotificationCenter';
+import {showNotification} from './Notifications/NotificationCenter';
import {openInFileExplorer} from './RenderQueue/actions';
const ASSET_ITEM_HEIGHT = 32;
@@ -325,12 +322,7 @@ const AssetSelectorItem: React.FC<{
const content = `staticFile("${[parentFolder, item.name].join('/')}")`;
copyText(content)
.then(() => {
- notificationCenter.current?.addNotification({
- content: `Copied '${content}' to clipboard`,
- created: Date.now(),
- duration: 1000,
- id: String(Math.random()),
- });
+ showNotification(`Copied '${content}' to clipboard`, 1000);
})
.catch((err) => {
showNotification(`Could not copy: ${err.message}`, 2000);
diff --git a/packages/studio/src/components/CompositionSelectorItem.tsx b/packages/studio/src/components/CompositionSelectorItem.tsx
index 20ce47e296..66adafa1a4 100644
--- a/packages/studio/src/components/CompositionSelectorItem.tsx
+++ b/packages/studio/src/components/CompositionSelectorItem.tsx
@@ -14,7 +14,7 @@ import {ModalsContext} from '../state/modals';
import {ContextMenu} from './ContextMenu';
import {Row, Spacing} from './layout';
import type {ComboboxValue} from './NewComposition/ComboBox';
-import {notificationCenter} from './Notifications/NotificationCenter';
+import {showNotification} from './Notifications/NotificationCenter';
import {applyCodemod} from './RenderQueue/actions';
import {SidebarRenderButton} from './SidebarRenderButton';
@@ -175,20 +175,13 @@ export const CompositionSelectorItem: React.FC<{
throw new Error(res.reason);
}
- notificationCenter.current?.addNotification({
- content: `Deleted ${item.composition.id}`,
- created: Date.now(),
- duration: 1000,
- id: String(Math.random()),
- });
+ showNotification(`Deleted ${item.composition.id}`, 1000);
})
.catch((err) => {
- notificationCenter.current?.addNotification({
- content: `Could not delete composition: ${err.message}`,
- created: Date.now(),
- duration: 1000,
- id: String(Math.random()),
- });
+ showNotification(
+ `Could not delete composition: ${err.message}`,
+ 1000,
+ );
});
},
quickSwitcherLabel: null,
@@ -209,20 +202,13 @@ export const CompositionSelectorItem: React.FC<{
navigator.clipboard
.writeText(item.composition.id)
.catch((err) => {
- notificationCenter.current?.addNotification({
- content: `Could not copy to clipboard: ${err.message}`,
- created: Date.now(),
- duration: 1000,
- id: String(Math.random()),
- });
+ showNotification(
+ `Could not copy to clipboard: ${err.message}`,
+ 1000,
+ );
})
.then(() => {
- notificationCenter.current?.addNotification({
- content: 'Copied to clipboard',
- created: Date.now(),
- duration: 1000,
- id: String(Math.random()),
- });
+ showNotification('Copied to clipboard', 1000);
});
},
quickSwitcherLabel: null,
diff --git a/packages/studio/src/components/GlobalKeybindings.tsx b/packages/studio/src/components/GlobalKeybindings.tsx
index 690bd792fb..02a7ed4d86 100644
--- a/packages/studio/src/components/GlobalKeybindings.tsx
+++ b/packages/studio/src/components/GlobalKeybindings.tsx
@@ -3,7 +3,7 @@ import {useContext, useEffect} from 'react';
import {useKeybinding} from '../helpers/use-keybinding';
import {CheckerboardContext} from '../state/checkerboard';
import {ModalsContext} from '../state/modals';
-import {notificationCenter} from './Notifications/NotificationCenter';
+import {showNotification} from './Notifications/NotificationCenter';
export const GlobalKeybindings: React.FC = () => {
const keybindings = useKeybinding();
@@ -15,12 +15,10 @@ export const GlobalKeybindings: React.FC = () => {
event: 'keypress',
key: 'n',
callback: () => {
- notificationCenter.current?.addNotification({
- content: `To make a new composition, right-click an existing one and select "Duplicate"`,
- created: Date.now(),
- duration: 5000,
- id: String(Math.random()),
- });
+ showNotification(
+ `To make a new composition, right-click an existing one and select "Duplicate"`,
+ 5000,
+ );
},
commandCtrlKey: false,
preventDefault: true,
diff --git a/packages/studio/src/components/NewComposition/DuplicateComposition.tsx b/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
index 77f37d8ed2..4a30c63fd5 100644
--- a/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
+++ b/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
@@ -24,10 +24,7 @@ import {Button} from '../Button';
import {Flex, Row, Spacing} from '../layout';
import {ModalContainer} from '../ModalContainer';
import {NewCompHeader} from '../ModalHeader';
-import {
- notificationCenter,
- showNotification,
-} from '../Notifications/NotificationCenter';
+import {showNotification} from '../Notifications/NotificationCenter';
import {
ResolveCompositionBeforeModal,
ResolvedCompositionContext,
@@ -112,12 +109,10 @@ const DuplicateCompositionLoaded: React.FC<{
setProjectInfo(info.projectInfo);
})
.catch((err) => {
- notificationCenter.current?.addNotification({
- content: `Could not get project info: ${err.message}. Unable to duplicate composition`,
- created: Date.now(),
- duration: 3000,
- id: String(Math.random()),
- });
+ showNotification(
+ `Could not get project info: ${err.message}. Unable to duplicate composition`,
+ 3000,
+ );
});
return () => {
diff --git a/packages/studio/src/components/Notifications/NotificationCenter.tsx b/packages/studio/src/components/Notifications/NotificationCenter.tsx
index cc2606afbf..a37c9b2052 100644
--- a/packages/studio/src/components/Notifications/NotificationCenter.tsx
+++ b/packages/studio/src/components/Notifications/NotificationCenter.tsx
@@ -29,7 +29,7 @@ type TNotificationCenter = {
addNotification: (notification: TNotification) => void;
};
-export const notificationCenter = createRef<TNotificationCenter>();
+const notificationCenter = createRef<TNotificationCenter>();
export const showNotification = (
content: React.ReactNode,
diff --git a/packages/studio/src/components/RenderQueue/RenderQueueRemoveItem.tsx b/packages/studio/src/components/RenderQueue/RenderQueueRemoveItem.tsx
index 6645115038..12357f6b37 100644
--- a/packages/studio/src/components/RenderQueue/RenderQueueRemoveItem.tsx
+++ b/packages/studio/src/components/RenderQueue/RenderQueueRemoveItem.tsx
@@ -2,10 +2,7 @@ import type {RenderJob} from '@remotion/studio-shared';
import React, {useCallback, useMemo} from 'react';
import type {RenderInlineAction} from '../InlineAction';
import {InlineAction} from '../InlineAction';
-import {
- notificationCenter,
- showNotification,
-} from '../Notifications/NotificationCenter';
+import {showNotification} from '../Notifications/NotificationCenter';
import {removeRenderJob} from './actions';
export const RenderQueueRemoveItem: React.FC<{
@@ -16,12 +13,7 @@ export const RenderQueueRemoveItem: React.FC<{
e.stopPropagation();
removeRenderJob(job)
.then(() => {
- notificationCenter.current?.addNotification({
- content: 'Removed job',
- duration: 2000,
- created: Date.now(),
- id: String(Math.random()).replace('0.', ''),
- });
+ showNotification('Removed job', 2000);
})
.catch((err) => {
showNotification(`Could not remove item: ${err.message}`, 2000);
diff --git a/packages/studio/src/components/utils.ts b/packages/studio/src/components/utils.ts
index 8bd5412ea0..480c3770a7 100644
--- a/packages/studio/src/components/utils.ts
+++ b/packages/studio/src/components/utils.ts
@@ -1,13 +1,8 @@
-import {notificationCenter} from './Notifications/NotificationCenter';
+import {showNotification} from './Notifications/NotificationCenter';
export const handleUploadFile = async (file: File, assetPath: string) => {
if (!file) {
- notificationCenter.current?.addNotification({
- content: `Please select a file first!`,
- created: Date.now(),
- duration: 3000,
- id: String(Math.random()),
- });
+ showNotification('Please select a file first!', 3000);
return;
}
@@ -24,27 +19,12 @@ export const handleUploadFile = async (file: File, assetPath: string) => {
});
if (response.ok) {
- notificationCenter.current?.addNotification({
- content: `Added ${file.name} to ${assetPath}`,
- created: Date.now(),
- duration: 3000,
- id: String(Math.random()),
- });
+ showNotification(`Added ${file.name} to ${assetPath}`, 3000);
} else {
const jsonResponse = await response.json();
- notificationCenter.current?.addNotification({
- content: `Upload failed: ${jsonResponse.error}`,
- created: Date.now(),
- duration: 3000,
- id: String(Math.random()),
- });
+ showNotification(`Upload failed: ${jsonResponse.error}`, 3000);
}
} catch (error) {
- notificationCenter.current?.addNotification({
- content: `Error during upload: ${error}`,
- created: Date.now(),
- duration: 3000,
- id: String(Math.random()),
- });
+ showNotification(`Error during upload: ${error}`, 3000);
}
};
diff --git a/packages/studio/src/helpers/pick-color.tsx b/packages/studio/src/helpers/pick-color.tsx
index 1d32ed873d..223f760f2f 100644
--- a/packages/studio/src/helpers/pick-color.tsx
+++ b/packages/studio/src/helpers/pick-color.tsx
@@ -1,9 +1,6 @@
import {Spacing} from '../components/layout';
import {ColorDot} from '../components/Notifications/ColorDot';
-import {
- notificationCenter,
- showNotification,
-} from '../components/Notifications/NotificationCenter';
+import {showNotification} from '../components/Notifications/NotificationCenter';
import {copyText} from './copy-text';
export const pickColor = () => {
@@ -17,17 +14,13 @@ export const pickColor = () => {
.then((color) => {
copyText(color.sRGBHex)
.then(() => {
- notificationCenter.current?.addNotification({
- content: (
- <>
- <ColorDot color={color.sRGBHex} /> <Spacing x={1} /> Copied{' '}
- {color.sRGBHex}
- </>
- ),
- created: Date.now(),
- duration: 2000,
- id: String(Math.random()),
- });
+ showNotification(
+ <>
+ <ColorDot color={color.sRGBHex} /> <Spacing x={1} /> Copied{' '}
+ {color.sRGBHex}
+ </>,
+ 2000,
+ );
})
.catch((err) => {
showNotification(`Could not copy: ${err.message}`, 2000);
@@ -38,11 +31,6 @@ export const pickColor = () => {
return;
}
- notificationCenter.current?.addNotification({
- content: `Could not pick color.`,
- duration: 2000,
- created: Date.now(),
- id: String(Math.random()),
- });
+ showNotification(`Could not pick color.`, 2000);
});
};
diff --git a/packages/studio/src/hot-middleware-client/process-update.ts b/packages/studio/src/hot-middleware-client/process-update.ts
index b3644d8850..93353e4524 100644
--- a/packages/studio/src/hot-middleware-client/process-update.ts
+++ b/packages/studio/src/hot-middleware-client/process-update.ts
@@ -12,7 +12,7 @@
*/
import type {HotMiddlewareOptions, ModuleMap} from '@remotion/studio-shared';
-import {notificationCenter} from '../components/Notifications/NotificationCenter';
+import {showNotification} from '../components/Notifications/NotificationCenter';
import {reloadUrl} from '../helpers/url-state';
if (!__webpack_module__.hot) {
@@ -184,13 +184,10 @@ export const processUpdate = function (
if (options.warn) console.warn('[Fast refresh] Reloading page');
if (window.remotion_unsavedProps) {
- notificationCenter.current?.addNotification({
- id: 'random',
- content:
- 'Fast refresh needs to reload the page, but you have unsaved props. Save then reload the page to apply changes.',
- created: new Date().getMilliseconds(),
- duration: 1,
- });
+ showNotification(
+ 'Fast refresh needs to reload the page, but you have unsaved props. Save then reload the page to apply changes.',
+ 1000,
+ );
} else {
reloadUrl();
}