Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Add assets to quick switcher (#9686)

Jonny Burger06ca0b853256
diff --git a/packages/studio/src/components/AssetSelector.tsx b/packages/studio/src/components/AssetSelector.tsx
index c22e7d7b13..70f6e5e602 100644
--- a/packages/studio/src/components/AssetSelector.tsx
+++ b/packages/studio/src/components/AssetSelector.tsx
@@ -13,6 +13,7 @@ import {useZIndex} from '../state/z-index';
import {AssetFolderTree} from './AssetSelectorItem';
import {inlineCodeSnippet} from './Menu/styles';
import {showNotification} from './Notifications/NotificationCenter';
+import {ExplorerQuickSwitcherTrigger} from './QuickSwitcher/ExplorerQuickSwitcherTrigger';
import {useStaticFiles} from './use-static-files';
const container: React.CSSProperties = {
@@ -165,6 +166,11 @@ export const AssetSelector: React.FC<{
onDragOver={shouldAllowUpload ? onDragOver : undefined}
onDrop={shouldAllowUpload ? onDrop : undefined}
>
+ <ExplorerQuickSwitcherTrigger
+ mode="assets"
+ showShortcut
+ tabIndex={tabIndex}
+ />
{staticFiles.length === 0 ? (
publicFolderExists ? (
<div style={emptyState}>
diff --git a/packages/studio/src/components/CompositionSelector.tsx b/packages/studio/src/components/CompositionSelector.tsx
index 21a0023c5d..f5886373f2 100644
--- a/packages/studio/src/components/CompositionSelector.tsx
+++ b/packages/studio/src/components/CompositionSelector.tsx
@@ -9,25 +9,18 @@ import React, {
useState,
} from 'react';
import {Internals} from 'remotion';
-import {cmdOrCtrlCharacter} from '../error-overlay/remotion-overlay/ShortcutHint';
import {StudioServerConnectionCtx} from '../helpers/client-id';
-import {
- BACKGROUND,
- BLACK_HEX,
- LIGHT_TEXT,
- WHITE_ALPHA_12,
- WHITE_ALPHA_06,
-} from '../helpers/colors';
+import {BACKGROUND, WHITE_ALPHA_12} from '../helpers/colors';
import {createFolderTree} from '../helpers/create-folder-tree';
import {ExpandedFoldersContext} from '../helpers/persist-open-folders';
import {sortItemsByNonceHistory} from '../helpers/sort-by-nonce-history';
-import {areKeyboardShortcutsDisabled} from '../helpers/use-keybinding';
import {ModalsContext} from '../state/modals';
import {useZIndex} from '../state/z-index';
import {CompositionSelectorItem} from './CompositionSelectorItem';
import {ContextMenuForTarget} from './ContextMenu';
import {useSelectComposition} from './InitialCompositionLoader';
import {showNotification} from './Notifications/NotificationCenter';
+import {ExplorerQuickSwitcherTrigger} from './QuickSwitcher/ExplorerQuickSwitcherTrigger';
import {applyCodemod} from './RenderQueue/actions';
import {getRootCompositionMenuItems} from './root-composition-menu-items';
@@ -97,31 +90,6 @@ const container: React.CSSProperties = {
backgroundColor: BACKGROUND,
};
-const quickSwitcherArea: React.CSSProperties = {
- padding: '4px 12px',
- borderBottom: `1px solid ${BLACK_HEX}`,
-};
-
-const quickSwitcherTrigger: React.CSSProperties = {
- backgroundColor: WHITE_ALPHA_06,
- borderRadius: 5,
- padding: '4px 10px',
- color: LIGHT_TEXT,
- fontSize: 12,
- cursor: 'pointer',
- display: 'flex',
- alignItems: 'center',
- justifyContent: 'space-between',
- border: 'none',
- width: '100%',
- appearance: 'none',
-};
-
-const shortcutLabel: React.CSSProperties = {
- fontSize: 11,
- opacity: 0.6,
-};
-
const autoScrollThreshold = 70;
const maxAutoScrollSpeed = 18;
@@ -207,14 +175,6 @@ export const CompositionSelector: React.FC = () => {
[],
);
- const openQuickSwitcher = useCallback(() => {
- setSelectedModal({
- type: 'quick-switcher',
- mode: 'compositions',
- invocationTimestamp: Date.now(),
- });
- }, [setSelectedModal]);
-
const clearRootDragHover = useCallback(() => {
setRootDragHovered(false);
}, []);
@@ -392,19 +352,11 @@ export const CompositionSelector: React.FC = () => {
values={rootContextMenuItems}
onOpen={null}
/>
- <div style={quickSwitcherArea}>
- <button
- type="button"
- style={quickSwitcherTrigger}
- onClick={openQuickSwitcher}
- tabIndex={tabIndex}
- >
- Search...
- {areKeyboardShortcutsDisabled() ? null : (
- <span style={shortcutLabel}>{cmdOrCtrlCharacter}+K</span>
- )}
- </button>
- </div>
+ <ExplorerQuickSwitcherTrigger
+ mode="compositions"
+ showShortcut
+ tabIndex={tabIndex}
+ />
<div
ref={listRef}
className="__remotion-vertical-scrollbar"
diff --git a/packages/studio/src/components/ExplorerPanel.tsx b/packages/studio/src/components/ExplorerPanel.tsx
index 6c18e29f9a..c41dd2cbbb 100644
--- a/packages/studio/src/components/ExplorerPanel.tsx
+++ b/packages/studio/src/components/ExplorerPanel.tsx
@@ -3,7 +3,10 @@ import {BACKGROUND} from '../helpers/colors';
import {AssetSelector} from './AssetSelector';
import {CompositionSelector} from './CompositionSelector';
import {CompSelectorRef} from './CompSelectorRef';
-import {explorerSidebarTabs} from './ExplorerPanelRef';
+import {
+ explorerSidebarTabs,
+ type ExplorerSidebarPanel,
+} from './ExplorerPanelRef';
import {Tab, Tabs} from './Tabs';
const container: React.CSSProperties = {
@@ -15,11 +18,9 @@ const container: React.CSSProperties = {
flex: 1,
};
-type OptionsSidebarPanel = 'compositions' | 'assets';
-
const localStorageKey = 'remotion.sidebarPanel';
-const getSelectedPanel = (): OptionsSidebarPanel => {
+const getSelectedPanel = (): ExplorerSidebarPanel => {
const panel = localStorage.getItem(localStorageKey);
if (panel === 'assets') {
return 'assets';
@@ -32,14 +33,14 @@ const tabsContainer: React.CSSProperties = {
backgroundColor: BACKGROUND,
};
-const persistSelectedOptionsSidebarPanel = (panel: OptionsSidebarPanel) => {
+const persistSelectedOptionsSidebarPanel = (panel: ExplorerSidebarPanel) => {
localStorage.setItem(localStorageKey, panel);
};
export const ExplorerPanel: React.FC<{
readOnlyStudio: boolean;
}> = ({readOnlyStudio}) => {
- const [panel, setPanel] = useState<OptionsSidebarPanel>(() =>
+ const [panel, setPanel] = useState<ExplorerSidebarPanel>(() =>
getSelectedPanel(),
);
const onCompositionsSelected = useCallback(() => {
@@ -54,6 +55,7 @@ export const ExplorerPanel: React.FC<{
useImperativeHandle(explorerSidebarTabs, () => {
return {
+ getSelectedPanel: () => panel,
selectAssetsPanel: () => {
setPanel('assets');
persistSelectedOptionsSidebarPanel('assets');
@@ -63,7 +65,7 @@ export const ExplorerPanel: React.FC<{
persistSelectedOptionsSidebarPanel('compositions');
},
};
- }, []);
+ }, [panel]);
const onPointerDown = useCallback((e: React.PointerEvent<HTMLDivElement>) => {
// Prevent deselection of currently selected items
diff --git a/packages/studio/src/components/ExplorerPanelRef.ts b/packages/studio/src/components/ExplorerPanelRef.ts
index 8b35814a68..1fc98935d2 100644
--- a/packages/studio/src/components/ExplorerPanelRef.ts
+++ b/packages/studio/src/components/ExplorerPanelRef.ts
@@ -1,6 +1,9 @@
import {createRef} from 'react';
+export type ExplorerSidebarPanel = 'assets' | 'compositions';
+
export const explorerSidebarTabs = createRef<{
+ getSelectedPanel: () => ExplorerSidebarPanel;
selectAssetsPanel: () => void;
selectCompositionPanel: () => void;
}>();
diff --git a/packages/studio/src/components/GlobalKeybindings.tsx b/packages/studio/src/components/GlobalKeybindings.tsx
index 68cbdb813d..e8f24719d2 100644
--- a/packages/studio/src/components/GlobalKeybindings.tsx
+++ b/packages/studio/src/components/GlobalKeybindings.tsx
@@ -10,6 +10,7 @@ import {EditorSnappingContext} from '../state/editor-snapping';
import {ModalsContext} from '../state/modals';
import {askAiModalRef} from './AskAiModal';
import {useCompositionNavigation} from './CompositionSelector';
+import {explorerSidebarTabs} from './ExplorerPanelRef';
import {showNotification} from './Notifications/NotificationCenter';
import {
getTimelineSequenceSelectionKey,
@@ -146,7 +147,10 @@ export const GlobalKeybindings: React.FC = () => {
callback: () => {
setSelectedModal({
type: 'quick-switcher',
- mode: 'compositions',
+ mode:
+ explorerSidebarTabs.current?.getSelectedPanel() === 'assets'
+ ? 'assets'
+ : 'compositions',
invocationTimestamp: Date.now(),
});
},
diff --git a/packages/studio/src/components/MenuCompositionName.tsx b/packages/studio/src/components/MenuCompositionName.tsx
index 330c537497..e0009a164a 100644
--- a/packages/studio/src/components/MenuCompositionName.tsx
+++ b/packages/studio/src/components/MenuCompositionName.tsx
@@ -2,8 +2,10 @@ import React, {useCallback, useContext, useMemo, useState} from 'react';
import {Internals} from 'remotion';
import {StudioServerConnectionCtx} from '../helpers/client-id';
import {LIGHT_TEXT} from '../helpers/colors';
+import {getFileManagerName} from '../helpers/get-file-manager-name';
import {openOriginalPositionInEditor} from '../helpers/open-in-editor';
import {showNotification} from './Notifications/NotificationCenter';
+import {openInFileExplorer} from './RenderQueue/actions';
import {useResolvedStack} from './Timeline/use-resolved-stack';
const baseStyle: React.CSSProperties = {
@@ -54,33 +56,57 @@ export const MenuCompositionName: React.FC = () => {
compositions.find((c) => c.id === canvasContent.compositionId) ?? null
);
}, [canvasContent, compositions]);
+ const asset = canvasContent?.type === 'asset' ? canvasContent.asset : null;
const resolvedLocation = useResolvedStack(composition?.stack ?? null);
+ const fileManagerName = getFileManagerName(
+ window.remotion_fileSystemPlatform,
+ );
- const canOpen =
+ const canOpenComposition =
resolvedLocation &&
window.remotion_editorName &&
connectionStatus === 'connected';
+ const canOpenAsset =
+ asset !== null &&
+ window.remotion_publicFolderExists !== null &&
+ connectionStatus === 'connected';
+ const canOpen = canOpenComposition || canOpenAsset;
const handleClick = useCallback(async () => {
- if (!canOpen || !resolvedLocation) {
+ if (!canOpen || opening) {
return;
}
setOpening(true);
try {
- await openOriginalPositionInEditor(resolvedLocation);
+ if (asset !== null && window.remotion_publicFolderExists !== null) {
+ await openInFileExplorer({
+ directory: `${window.remotion_publicFolderExists}/${asset}`,
+ });
+ } else if (resolvedLocation) {
+ await openOriginalPositionInEditor(resolvedLocation);
+ }
} catch (err) {
showNotification((err as Error).message, 2000);
} finally {
setOpening(false);
}
- }, [canOpen, resolvedLocation]);
+ }, [asset, canOpen, opening, resolvedLocation]);
- if (!composition) {
+ if (!composition && asset === null) {
return null;
}
+ const name = composition?.id ?? asset;
+ const title = composition
+ ? canOpenComposition
+ ? `Open in ${window.remotion_editorName}`
+ : composition.id
+ : canOpenAsset
+ ? `Show in ${fileManagerName}`
+ : (asset ?? undefined);
+
return (
<>
<span style={slashStyle}>/</span>
@@ -92,14 +118,12 @@ export const MenuCompositionName: React.FC = () => {
: clickableStyle
: compositionNameStyle
}
- title={
- canOpen ? `Open in ${window.remotion_editorName}` : composition.id
- }
+ title={title}
onClick={handleClick}
onPointerEnter={() => setHovered(true)}
onPointerLeave={() => setHovered(false)}
>
- {composition.id}
+ {name}
</a>
</>
);
diff --git a/packages/studio/src/components/QuickSwitcher/ExplorerQuickSwitcherTrigger.tsx b/packages/studio/src/components/QuickSwitcher/ExplorerQuickSwitcherTrigger.tsx
new file mode 100644
index 0000000000..c04ba32304
--- /dev/null
+++ b/packages/studio/src/components/QuickSwitcher/ExplorerQuickSwitcherTrigger.tsx
@@ -0,0 +1,63 @@
+import React, {useCallback, useContext} from 'react';
+import {cmdOrCtrlCharacter} from '../../error-overlay/remotion-overlay/ShortcutHint';
+import {BLACK_HEX, LIGHT_TEXT, WHITE_ALPHA_06} from '../../helpers/colors';
+import {areKeyboardShortcutsDisabled} from '../../helpers/use-keybinding';
+import {ModalsContext} from '../../state/modals';
+import type {QuickSwitcherMode} from './NoResults';
+
+const quickSwitcherArea: React.CSSProperties = {
+ padding: '4px 12px',
+ borderBottom: `1px solid ${BLACK_HEX}`,
+};
+
+const quickSwitcherTrigger: React.CSSProperties = {
+ backgroundColor: WHITE_ALPHA_06,
+ borderRadius: 5,
+ padding: '4px 10px',
+ color: LIGHT_TEXT,
+ fontSize: 12,
+ cursor: 'pointer',
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'space-between',
+ border: 'none',
+ width: '100%',
+ appearance: 'none',
+};
+
+const shortcutLabel: React.CSSProperties = {
+ fontSize: 11,
+ opacity: 0.6,
+};
+
+export const ExplorerQuickSwitcherTrigger: React.FC<{
+ readonly mode: QuickSwitcherMode;
+ readonly showShortcut: boolean;
+ readonly tabIndex: number;
+}> = ({mode, showShortcut, tabIndex}) => {
+ const {setSelectedModal} = useContext(ModalsContext);
+
+ const openQuickSwitcher = useCallback(() => {
+ setSelectedModal({
+ type: 'quick-switcher',
+ mode,
+ invocationTimestamp: Date.now(),
+ });
+ }, [mode, setSelectedModal]);
+
+ return (
+ <div style={quickSwitcherArea}>
+ <button
+ type="button"
+ style={quickSwitcherTrigger}
+ onClick={openQuickSwitcher}
+ tabIndex={tabIndex}
+ >
+ Search...
+ {showShortcut && !areKeyboardShortcutsDisabled() ? (
+ <span style={shortcutLabel}>{cmdOrCtrlCharacter}+K</span>
+ ) : null}
+ </button>
+ </div>
+ );
+};
diff --git a/packages/studio/src/components/QuickSwitcher/NoResults.tsx b/packages/studio/src/components/QuickSwitcher/NoResults.tsx
index 1a06d4a8cb..b2cdcc41f0 100644
--- a/packages/studio/src/components/QuickSwitcher/NoResults.tsx
+++ b/packages/studio/src/components/QuickSwitcher/NoResults.tsx
@@ -8,9 +8,10 @@ const container: React.CSSProperties = {
fontSize: 14,
};
-export type QuickSwitcherMode = 'commands' | 'compositions' | 'docs';
+export type QuickSwitcherMode = 'assets' | 'commands' | 'compositions' | 'docs';
const MODE_TO_STRING: {[key in QuickSwitcherMode]: string} = {
+ assets: 'assets',
commands: 'commands',
compositions: 'compositions',
docs: 'documentation',
diff --git a/packages/studio/src/components/QuickSwitcher/QuickSwitcherContent.tsx b/packages/studio/src/components/QuickSwitcher/QuickSwitcherContent.tsx
index 37e80f053f..287a3f04b8 100644
--- a/packages/studio/src/components/QuickSwitcher/QuickSwitcherContent.tsx
+++ b/packages/studio/src/components/QuickSwitcher/QuickSwitcherContent.tsx
@@ -8,7 +8,9 @@ import React, {
} from 'react';
import {Internals} from 'remotion';
import {LIGHT_TEXT, WHITE} from '../../helpers/colors';
+import {getPreviewFileType} from '../../helpers/get-preview-file-type';
import {isCompositionStill} from '../../helpers/is-composition-still';
+import {pushUrl} from '../../helpers/url-state';
import {useKeybinding} from '../../helpers/use-keybinding';
import {
makeSearchResults,
@@ -20,8 +22,11 @@ import {KeyboardShortcutsExplainer} from '../KeyboardShortcutsExplainer';
import {Spacing} from '../layout';
import {VERTICAL_SCROLLBAR_CLASSNAME} from '../Menu/is-menu-item';
import {RemotionInput} from '../NewComposition/RemInput';
+import {useSelectAsset} from '../use-select-asset';
+import {useStaticFiles} from '../use-static-files';
import {algoliaSearch} from './algolia-search';
import {AlgoliaCredit} from './AlgoliaCredit';
+import {filterAssetsByType} from './asset-search';
import {fuzzySearch} from './fuzzy-search';
import type {QuickSwitcherMode} from './NoResults';
import {QuickSwitcherNoResults} from './NoResults';
@@ -74,7 +79,7 @@ const content: React.CSSProperties = {
};
const stripQuery = (query: string) => {
- if (query.startsWith('>') || query.startsWith('?')) {
+ if (query.startsWith('$') || query.startsWith('>') || query.startsWith('?')) {
return query.substring(1).trim();
}
@@ -86,10 +91,16 @@ const mapQueryToMode = (query: string): QuickSwitcherMode => {
? 'commands'
: query.startsWith('?')
? 'docs'
- : 'compositions';
+ : query.startsWith('$')
+ ? 'assets'
+ : 'compositions';
};
const mapModeToQuery = (mode: QuickSwitcherMode): string => {
+ if (mode === 'assets') {
+ return '$ ';
+ }
+
if (mode === 'commands') {
return '> ';
}
@@ -127,6 +138,7 @@ export const QuickSwitcherContent: React.FC<{
readonly readOnlyStudio: boolean;
}> = ({initialMode, invocationTimestamp, readOnlyStudio}) => {
const {compositions} = useContext(Internals.CompositionManager);
+ const staticFiles = useStaticFiles();
const [state, setState] = useState(() => {
return {
query: mapModeToQuery(initialMode),
@@ -143,6 +155,7 @@ export const QuickSwitcherContent: React.FC<{
const inputRef = useRef<HTMLInputElement>(null);
const selectComposition = useSelectComposition();
+ const selectAsset = useSelectAsset();
const closeMenu = useCallback(() => undefined, []);
const actions = useMenuStructure(closeMenu, readOnlyStudio);
@@ -166,6 +179,13 @@ export const QuickSwitcherContent: React.FC<{
return makeSearchResults(actions, setSelectedModal);
}, [actions, mode, setSelectedModal]);
+ const assetSearch = useMemo(() => {
+ return filterAssetsByType({
+ assets: staticFiles,
+ query: actualQuery,
+ });
+ }, [actualQuery, staticFiles]);
+
const resultsArray = useMemo((): TQuickSwitcherResult[] => {
if (mode === 'commands') {
return fuzzySearch(actualQuery, menuActions);
@@ -175,6 +195,25 @@ export const QuickSwitcherContent: React.FC<{
return docResults.results;
}
+ if (mode === 'assets') {
+ return fuzzySearch(
+ assetSearch.query,
+ assetSearch.assets.map((asset) => {
+ return {
+ id: 'asset-' + asset.name,
+ title: asset.name,
+ type: 'asset',
+ fileType: getPreviewFileType(asset.name),
+ onSelected: () => {
+ selectAsset(asset.name);
+ pushUrl(`/assets/${asset.name}`);
+ setSelectedModal(null);
+ },
+ };
+ }),
+ );
+ }
+
return fuzzySearch(
actualQuery,
compositions.map((c) => {
@@ -205,6 +244,8 @@ export const QuickSwitcherContent: React.FC<{
compositions,
menuActions,
docResults,
+ assetSearch,
+ selectAsset,
selectComposition,
setSelectedModal,
]);
@@ -324,6 +365,14 @@ export const QuickSwitcherContent: React.FC<{
inputRef.current?.focus();
}, []);
+ const onAssetsSelected = useCallback(() => {
+ setState((s) => ({
+ query: `$ ${stripQuery(s.query)}`,
+ selectedIndex: 0,
+ }));
+ inputRef.current?.focus();
+ }, []);
+
const onDocSearchSelected = useCallback(() => {
setState((s) => ({
query: `? ${stripQuery(s.query)}`,
@@ -336,6 +385,14 @@ export const QuickSwitcherContent: React.FC<{
const showKeyboardShortcuts = mode === 'docs' && actualQuery.trim() === '';
const showSearchLoadingState =
mode === 'docs' && docResults.type === 'loading';
+ const placeholder =
+ mode === 'assets'
+ ? 'Search assets...'
+ : mode === 'commands'
+ ? 'Search actions...'
+ : mode === 'docs'
+ ? 'Search documentation...'
+ : 'Search compositions...';
const container: React.CSSProperties = useMemo(() => {
return {
@@ -368,6 +425,14 @@ export const QuickSwitcherContent: React.FC<{
Compositions
</button>
<Spacing x={1} />
+ <button
+ onClick={onAssetsSelected}
+ style={mode === 'assets' ? modeActive : modeInactive}
+ type="button"
+ >
+ Assets
+ </button>
+ <Spacing x={1} />
<button
onClick={onActionsSelected}
style={mode === 'commands' ? modeActive : modeInactive}
@@ -393,7 +458,7 @@ export const QuickSwitcherContent: React.FC<{
status="ok"
value={state.query}
onChange={onTextChange}
- placeholder="Search compositions..."
+ placeholder={placeholder}
rightAlign={false}
/>
{showKeyboardShortcuts ? (
diff --git a/packages/studio/src/components/QuickSwitcher/QuickSwitcherResult.tsx b/packages/studio/src/components/QuickSwitcher/QuickSwitcherResult.tsx
index ab13a43193..66e6d715f5 100644
--- a/packages/studio/src/components/QuickSwitcher/QuickSwitcherResult.tsx
+++ b/packages/studio/src/components/QuickSwitcher/QuickSwitcherResult.tsx
@@ -5,9 +5,11 @@ import {
WHITE,
WHITE_ALPHA_06,
} from '../../helpers/colors';
+import type {AssetFileType} from '../../helpers/get-preview-file-type';
import {useKeybinding} from '../../helpers/use-keybinding';
import {StillIcon} from '../../icons/still';
import {FilmIcon} from '../../icons/video';
+import {AssetFileIcon} from '../AssetFileIcon';
import {Spacing} from '../layout';
import {
QUICK_SWITCHER_RESULT_LABEL_FONT_SIZE,
@@ -15,6 +17,10 @@ import {
} from './shared';
type QuickSwitcherResultDetail =
+ | {
+ type: 'asset';
+ fileType: AssetFileType;
+ }
| {
type: 'composition';
compositionType: 'composition' | 'still';
@@ -154,6 +160,12 @@ export const QuickSwitcherResult: React.FC<{
style={iconStyle}
/>
)
+ ) : result.type === 'asset' ? (
+ <AssetFileIcon
+ fileType={result.fileType}
+ color={selected || hovered ? WHITE : LIGHT_TEXT}
+ style={iconStyle}
+ />
) : null}
<Spacing x={1} />
<div style={labelContainer}>
diff --git a/packages/studio/src/components/QuickSwitcher/asset-search.ts b/packages/studio/src/components/QuickSwitcher/asset-search.ts
new file mode 100644
index 0000000000..e78f47e6f9
--- /dev/null
+++ b/packages/studio/src/components/QuickSwitcher/asset-search.ts
@@ -0,0 +1,38 @@
+import type {StaticFile} from '../../api/get-static-files';
+import {getPreviewFileType} from '../../helpers/get-preview-file-type';
+
+const typeFilterRegex = /(^|\s)type:([^\s]+)/gi;
+
+export const filterAssetsByType = ({
+ assets,
+ query,
+}: {
+ readonly assets: StaticFile[];
+ readonly query: string;
+}): {
+ readonly assets: StaticFile[];
+ readonly query: string;
+} => {
+ const typeFilters: string[] = [];
+ const queryWithoutTypeFilters = query
+ .replace(typeFilterRegex, (_match, whitespace: string, type: string) => {
+ typeFilters.push(type.toLowerCase());
+ return whitespace;
+ })
+ .replace(/\s+/g, ' ')
+ .trim();
+
+ if (typeFilters.length === 0) {
+ return {
+ assets,
+ query: queryWithoutTypeFilters,
+ };
+ }
+
+ return {
+ assets: assets.filter((asset) => {
+ return typeFilters.includes(getPreviewFileType(asset.name));
+ }),
+ query: queryWithoutTypeFilters,
+ };
+};
diff --git a/packages/studio/src/test/quick-switcher-asset-search.test.ts b/packages/studio/src/test/quick-switcher-asset-search.test.ts
new file mode 100644
index 0000000000..2c8682c25c
--- /dev/null
+++ b/packages/studio/src/test/quick-switcher-asset-search.test.ts
@@ -0,0 +1,59 @@
+import {expect, test} from 'bun:test';
+import type {StaticFile} from '../api/get-static-files';
+import {filterAssetsByType} from '../components/QuickSwitcher/asset-search';
+
+const assets: StaticFile[] = [
+ {
+ lastModified: 0,
+ name: 'audio/theme.mp3',
+ sizeInBytes: 1,
+ src: '/audio/theme.mp3',
+ },
+ {
+ lastModified: 0,
+ name: 'images/logo.png',
+ sizeInBytes: 1,
+ src: '/images/logo.png',
+ },
+ {
+ lastModified: 0,
+ name: 'videos/intro.mp4',
+ sizeInBytes: 1,
+ src: '/videos/intro.mp4',
+ },
+];
+
+test('filters quick switcher assets by type', () => {
+ const result = filterAssetsByType({
+ assets,
+ query: 'intro type:video clip',
+ });
+
+ expect(result.query).toBe('intro clip');
+ expect(result.assets.map((asset) => asset.name)).toEqual([
+ 'videos/intro.mp4',
+ ]);
+});
+
+test('supports multiple asset type filters', () => {
+ const result = filterAssetsByType({
+ assets,
+ query: 'type:audio type:IMAGE',
+ });
+
+ expect(result.query).toBe('');
+ expect(result.assets.map((asset) => asset.name)).toEqual([
+ 'audio/theme.mp3',
+ 'images/logo.png',
+ ]);
+});
+
+test('returns no assets for an unknown asset type', () => {
+ const result = filterAssetsByType({
+ assets,
+ query: 'type:spreadsheet',
+ });
+
+ expect(result.query).toBe('');
+ expect(result.assets).toEqual([]);
+});