🎥 Make videos programmatically with React
`@remotion/studio`: Open read-only sources on GitHub
JonnyBurger05283182ad2f
diff --git a/packages/studio/src/components/InspectorOpenInEditor.tsx b/packages/studio/src/components/InspectorOpenInEditor.tsxindex d72f4a82d1..0af2b10c06 100644--- a/packages/studio/src/components/InspectorOpenInEditor.tsx+++ b/packages/studio/src/components/InspectorOpenInEditor.tsx@@ -5,6 +5,10 @@ import type {OriginalPosition} from '../error-overlay/react-overlay/utils/get-soimport {getBrowserStudioOperations} from '../helpers/browser-studio-operations';import {StudioServerConnectionCtx} from '../helpers/client-id';import {LIGHT_TEXT} from '../helpers/colors';+import {+ hasReadOnlyGitSource,+ openGitSource,+} from '../helpers/get-git-menu-item';import {openInCodingAgent,openInGitClient,@@ -13,6 +17,7 @@ import {} from '../helpers/open-in-editor';import {CaretDown} from '../icons/caret';import {EditorIcon} from '../icons/editor';+import {GitHubIcon} from '../icons/github';import {SetSelectedModalContext} from '../state/modals';import {getOpenInMenuItems} from './get-open-in-menu-items';import type {ComboboxValue} from './NewComposition/ComboBox';@@ -54,6 +59,8 @@ export const InspectorOpenInEditor: React.FC<{editorInfo,} = useEditorOpening(previewServerState.type === 'connected');const codingAgentInfo = useDefaultCodingAgentInfo(canConfigureApps);+ const canOpenInGitHub = hasReadOnlyGitSource();+ const defaultActionIsGitHub = !canOpenInEditor && canOpenInGitHub;const openWithEditor = useCallback(async (editorId: EditorPickerId) => {@@ -86,25 +93,37 @@ export const InspectorOpenInEditor: React.FC<{},[contextForAgents],);- const editorName = defaultEditorName ?? 'default editor';- const canOpenDefault = location !== null && canOpenInEditor;+ const defaultAppName = defaultActionIsGitHub+ ? 'GitHub'+ : (defaultEditorName ?? 'default editor');+ const canOpenDefault =+ location !== null && (canOpenInEditor || canOpenInGitHub);const onOpenDefault: React.MouseEventHandler<HTMLButtonElement> = useCallback((event) => {event.stopPropagation();- if (defaultEditorId) {+ if (defaultActionIsGitHub) {+ openGitSource({folder: locationType === 'folder', location});+ } else if (defaultEditorId) {openWithEditor(defaultEditorId).catch(() => undefined);}},- [defaultEditorId, openWithEditor],+ [+ defaultActionIsGitHub,+ defaultEditorId,+ location,+ locationType,+ openWithEditor,+ ],);const menuItems = useMemo((): ComboboxValue[] => {- return getOpenInMenuItems({+ const items = getOpenInMenuItems({codingAgentInfo,- editorDisabled: location === null,+ editorDisabled: location === null || !canOpenInEditor,editorInfo,excludeCodingAgentId: null,excludeEditorId: defaultEditorId,- fileManagerDisabled: !location?.source,+ fileManagerDisabled:+ !location?.source || previewServerState.type !== 'connected',folder: locationType === 'folder',location,onConfigureApps: canConfigureApps@@ -167,21 +186,28 @@ export const InspectorOpenInEditor: React.FC<{});},});++ return defaultActionIsGitHub+ ? items.filter((item) => item.id !== 'open-in-github')+ : items;}, [codingAgentInfo,canConfigureApps,+ canOpenInEditor,+ defaultActionIsGitHub,defaultEditorId,editorInfo,location,locationType,openWithCodingAgent,openWithEditor,+ previewServerState.type,setSelectedModal,]);const segments = useMemo((): SegmentedButtonSegment[] => {- return [+ const result: SegmentedButtonSegment[] = [{- ariaLabel: `Open in ${editorName}`,+ ariaLabel: `Open in ${defaultAppName}`,buttonId: null,disabled: !canOpenDefault,idleColor: LIGHT_TEXT,@@ -190,18 +216,25 @@ export const InspectorOpenInEditor: React.FC<{renderContent: () => (<>{label}- <EditorIcon- editorId={defaultEditorId}- size={editorButtonIconSize}- />+ {defaultActionIsGitHub ? (+ <GitHubIcon size={editorButtonIconSize} />+ ) : (+ <EditorIcon+ editorId={defaultEditorId}+ size={editorButtonIconSize}+ />+ )}</>),segmentId: 'default-editor',style: mainSegmentStyle,- title: `Open in ${editorName}`,+ title: `Open in ${defaultAppName}`,type: 'action',},- {+ ];++ if (menuItems.length > 0) {+ result.push({ariaLabel: 'Open in another app',buttonId: null,disabled: false,@@ -215,21 +248,25 @@ export const InspectorOpenInEditor: React.FC<{title: 'Open in another app',type: 'menu',values: menuItems,- },- ];+ });+ }++ return result;}, [canOpenDefault,+ defaultActionIsGitHub,+ defaultAppName,defaultEditorId,- editorName,label,menuItems,onOpenDefault,]);- if (- previewServerState.type !== 'connected' ||- getBrowserStudioOperations() !== null- ) {+ if (getBrowserStudioOperations() !== null) {+ return null;+ }++ if (previewServerState.type !== 'connected' && !canOpenInGitHub) {return null;}diff --git a/packages/studio/src/components/InspectorPanel/CompositionInspectorHeader.tsx b/packages/studio/src/components/InspectorPanel/CompositionInspectorHeader.tsxindex 98e2c15562..68544c02c9 100644--- a/packages/studio/src/components/InspectorPanel/CompositionInspectorHeader.tsx+++ b/packages/studio/src/components/InspectorPanel/CompositionInspectorHeader.tsx@@ -8,6 +8,10 @@ import {import {Internals} from 'remotion';import type {OriginalPosition} from '../../error-overlay/react-overlay/utils/get-source-map';import {StudioServerConnectionCtx} from '../../helpers/client-id';+import {+ hasReadOnlyGitSource,+ openGitSource,+} from '../../helpers/get-git-menu-item';import {isCompositionStill} from '../../helpers/is-composition-still';import {openOriginalPositionInEditor,@@ -50,6 +54,7 @@ export const CompositionInspectorHeader = () => {const {canOpenInEditor, defaultEditorId} = useEditorOpening(previewServerState.type === 'connected',);+ const canOpenInGitHub = hasReadOnlyGitSource();const currentComposition = useMemo(() => {if (!video) {@@ -97,28 +102,31 @@ export const CompositionInspectorHeader = () => {});}, [compositionFile, compositionId]);+ const openSourceLocation = useCallback(+ (location: OriginalPosition) => {+ if (canOpenInEditor && defaultEditorId) {+ openOriginalPositionInEditor(location, defaultEditorId).catch((err) => {+ showNotification((err as Error).message, 2000);+ });+ return;+ }++ if (canOpenInGitHub) {+ openGitSource({folder: false, location});+ }+ },+ [canOpenInEditor, canOpenInGitHub, defaultEditorId],+ );const openFileLocation = useCallback(() => {- if (!validatedLocation || !defaultEditorId || !canOpenInEditor) {- return;+ if (validatedLocation) {+ openSourceLocation(validatedLocation);}-- openOriginalPositionInEditor(validatedLocation, defaultEditorId).catch(- (err) => {- showNotification((err as Error).message, 2000);- },- );- }, [canOpenInEditor, defaultEditorId, validatedLocation]);+ }, [openSourceLocation, validatedLocation]);const openComponentLocation = useCallback(() => {- if (!componentLocation || !defaultEditorId || !canOpenInEditor) {- return;+ if (componentLocation) {+ openSourceLocation(componentLocation);}-- openOriginalPositionInEditor(componentLocation, defaultEditorId).catch(- (err) => {- showNotification((err as Error).message, 2000);- },- );- }, [canOpenInEditor, componentLocation, defaultEditorId]);+ }, [componentLocation, openSourceLocation]);const renderCompositionIcon = useCallback((color: string) => {if (!video) {@@ -153,7 +161,9 @@ export const CompositionInspectorHeader = () => {/><InspectorSourceLocationlocation={validatedLocation}- canOpen={validatedLocation !== null && canOpenInEditor}+ canOpen={+ validatedLocation !== null && (canOpenInEditor || canOpenInGitHub)+ }onOpen={openFileLocation}renderIcon={renderCompositionIcon}size="quick-action"@@ -165,7 +175,10 @@ export const CompositionInspectorHeader = () => {) : (<InspectorSourceLocationlocation={componentLocation}- canOpen={componentLocation !== null && canOpenInEditor}+ canOpen={+ componentLocation !== null &&+ (canOpenInEditor || canOpenInGitHub)+ }onOpen={openComponentLocation}renderIcon={renderReactIcon}size="quick-action"diff --git a/packages/studio/src/components/InspectorPanel/SequenceInspectorHeader.tsx b/packages/studio/src/components/InspectorPanel/SequenceInspectorHeader.tsxindex f76be454bb..614cbabc9a 100644--- a/packages/studio/src/components/InspectorPanel/SequenceInspectorHeader.tsx+++ b/packages/studio/src/components/InspectorPanel/SequenceInspectorHeader.tsx@@ -2,6 +2,10 @@ import React, {useCallback, useContext, useMemo} from 'react';import type {CodePosition} from '../../error-overlay/react-overlay/utils/get-source-map';import {StudioServerConnectionCtx} from '../../helpers/client-id';import {CURRENT_COLOR} from '../../helpers/colors';+import {+ hasReadOnlyGitSource,+ openGitSource,+} from '../../helpers/get-git-menu-item';import type {TimelineTrackData} from '../../helpers/get-timeline-sequence-sort-key';import {ReactIcon} from '../../icons/react';import {InlineEditableTitle} from '../InlineEditableTitle';@@ -54,7 +58,7 @@ const externalTabIndicatorStyle: React.CSSProperties = {};type SequenceInspectorSourceLocation = {- readonly canOpenInEditor: boolean;+ readonly canOpen: boolean;readonly openFileLocation: () => void;readonly validatedLocation: CodePosition | null;};@@ -64,6 +68,7 @@ export const useSequenceInspectorSourceLocation = (): SequenceInspectorSourceLocation => {const {canOpenInEditor, openInEditor, originalLocation} =useOpenSequenceInApps(sequence);+ const canOpenInGitHub = hasReadOnlyGitSource();const validatedLocation = useMemo(() => {if (@@ -82,15 +87,18 @@ export const useSequenceInspectorSourceLocation = (}, [originalLocation]);const openFileLocation = useCallback(() => {- if (!canOpenInEditor) {+ if (canOpenInEditor) {+ openInEditor(null);return;}- openInEditor(null);- }, [canOpenInEditor, openInEditor]);+ if (canOpenInGitHub && validatedLocation) {+ openGitSource({folder: false, location: validatedLocation});+ }+ }, [canOpenInEditor, canOpenInGitHub, openInEditor, validatedLocation]);return {- canOpenInEditor,+ canOpen: validatedLocation !== null && (canOpenInEditor || canOpenInGitHub),openFileLocation,validatedLocation,};@@ -178,7 +186,7 @@ export const SequenceInspectorHeader: React.FC<{)}<InspectorSourceLocationlocation={sourceLocation.validatedLocation}- canOpen={sourceLocation.canOpenInEditor}+ canOpen={sourceLocation.canOpen}onOpen={sourceLocation.openFileLocation}renderIcon={renderReactIcon}size="quick-action"diff --git a/packages/studio/src/components/get-open-in-menu-items.tsx b/packages/studio/src/components/get-open-in-menu-items.tsxindex 462cac0c43..377c768bb6 100644--- a/packages/studio/src/components/get-open-in-menu-items.tsx+++ b/packages/studio/src/components/get-open-in-menu-items.tsx@@ -9,10 +9,7 @@ import React from 'react';import {NoReactInternals} from 'remotion/no-react';import type {OriginalPosition} from '../error-overlay/react-overlay/utils/get-source-map';import {getFileManagerName} from '../helpers/get-file-manager-name';-import {- getGitRefUrl,- getGitSourceBranchUrl,-} from '../helpers/get-git-menu-item';+import {openGitSource} from '../helpers/get-git-menu-item';import {EditorIcon} from '../icons/editor';import {FinderIcon} from '../icons/finder';import {GitClientIcon} from '../icons/git-client';@@ -109,17 +106,7 @@ export const getOpenInMenuItems = ({label: <span style={menuLabel}>GitHub.com</span>,leftItem: <GitHubIcon size={18} />,onClick: () => {- const gitSource = window.remotion_gitSource;- if (!gitSource) {- return;- }-- window.open(- folder || !location- ? getGitSourceBranchUrl(gitSource)- : getGitRefUrl(gitSource, location, window.remotion_cwd),- '_blank',- );+ openGitSource({folder, location});},quickSwitcherLabel: null,subMenu: null,diff --git a/packages/studio/src/helpers/get-git-menu-item.ts b/packages/studio/src/helpers/get-git-menu-item.tsindex f8c93399ba..d13a6bdcf6 100644--- a/packages/studio/src/helpers/get-git-menu-item.ts+++ b/packages/studio/src/helpers/get-git-menu-item.ts@@ -72,6 +72,30 @@ export const getGitRefUrl = (throw new Error('Unknown git source type');};+export const hasReadOnlyGitSource = () => {+ return Boolean(window.remotion_isReadOnlyStudio && window.remotion_gitSource);+};++export const openGitSource = ({+ folder,+ location,+}: {+ folder: boolean;+ location: OriginalPosition | null;+}) => {+ const gitSource = window.remotion_gitSource;+ if (!gitSource) {+ return;+ }++ window.open(+ folder || !location+ ? getGitSourceBranchUrl(gitSource)+ : getGitRefUrl(gitSource, location, window.remotion_cwd),+ '_blank',+ );+};+export const getGitMenuItem = (): ComboboxValue | null => {if (!window.remotion_gitSource) {return null;diff --git a/packages/studio/src/test/git-source-open.test.ts b/packages/studio/src/test/git-source-open.test.tsnew file mode 100644index 0000000000..665f046332--- /dev/null+++ b/packages/studio/src/test/git-source-open.test.ts@@ -0,0 +1,89 @@+import {afterEach, expect, test} from 'bun:test';+import type {GitSource} from '@remotion/studio-shared';+import {+ hasReadOnlyGitSource,+ openGitSource,+} from '../helpers/get-git-menu-item';++const originalWindowDescriptor = Object.getOwnPropertyDescriptor(+ globalThis,+ 'window',+);++afterEach(() => {+ if (originalWindowDescriptor) {+ Object.defineProperty(globalThis, 'window', originalWindowDescriptor);+ } else {+ Reflect.deleteProperty(globalThis, 'window');+ }+});++const gitSource: GitSource = {+ name: 'remotion',+ org: 'remotion-dev',+ ref: 'feature/branch',+ relativeFromGitRoot: 'packages/docs',+ type: 'github',+};++const installTestWindow = ({+ git,+ readOnly,+}: {+ git: GitSource | null;+ readOnly: boolean;+}) => {+ const openedUrls: string[] = [];+ const testWindow: Pick<+ Window,+ 'open' | 'remotion_cwd' | 'remotion_gitSource' | 'remotion_isReadOnlyStudio'+ > = {+ open: (url) => {+ openedUrls.push(String(url));+ return null;+ },+ remotion_cwd: '/repo/packages/docs',+ remotion_gitSource: git,+ remotion_isReadOnlyStudio: readOnly,+ };++ Object.defineProperty(globalThis, 'window', {+ configurable: true,+ value: testWindow,+ });++ return openedUrls;+};++test('uses GitHub as an open target in a read-only Studio', () => {+ const openedUrls = installTestWindow({git: gitSource, readOnly: true});++ expect(hasReadOnlyGitSource()).toBe(true);+ openGitSource({+ folder: false,+ location: {+ column: 3,+ line: 12,+ source: '/repo/packages/docs/src/Video.tsx',+ },+ });++ expect(openedUrls).toEqual([+ 'https://github.com/remotion-dev/remotion/blob/feature/branch/packages/docs/src/Video.tsx#L12',+ ]);+});++test('opens the project folder on the configured GitHub ref', () => {+ const openedUrls = installTestWindow({git: gitSource, readOnly: true});++ openGitSource({folder: true, location: null});++ expect(openedUrls).toEqual([+ 'https://github.com/remotion-dev/remotion/tree/feature/branch/packages/docs',+ ]);+});++test('does not use GitHub as the fallback in an editable Studio', () => {+ installTestWindow({git: gitSource, readOnly: false});+ expect(hasReadOnlyGitSource()).toBe(false);+});