Skip to content

Commit

Permalink
fix: open preview url with params (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
cazala authored Sep 12, 2024
1 parent 5312ba3 commit f728ac7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
9 changes: 7 additions & 2 deletions packages/main/src/modules/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ export async function start(path: string) {
args: ['start', '--port', port.toString(), '--no-browser'],
cwd: path,
});
await previewServer.waitFor(/available/i);
return port;
const message = await previewServer.waitFor(/available/i);
const match = message.match(/http:\/\/(\d|\.)+:\d+\?(.*)\n/); // match url printed by success message
if (match) {
return match[0].slice(0, -1); // remove last char because it's a new line '\n'
} else {
return `http://localhost:${port}`; // if match fails fallback to localhost and port, it should never happen unless the message from the CLI is changed, and the regex is not updated
}
}

export let deployServer: Child | null = null;
Expand Down
11 changes: 3 additions & 8 deletions packages/renderer/src/hooks/useEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,13 @@ export const useEditor = () => {

const openPreview = useCallback(() => {
if (project) {
if (editor.previewPort > 0) {
dispatch(editorActions.openPreview(editor.previewPort));
if (editor.previewUrl) {
dispatch(editorActions.openPreview(editor.previewUrl));
} else {
dispatch(editorActions.runSceneAndOpenPreview(project));
}
}
}, [
editorActions.openPreview,
editorActions.runSceneAndOpenPreview,
project,
editor.previewPort,
]);
}, [editorActions.openPreview, editorActions.runSceneAndOpenPreview, project, editor.previewUrl]);

const openCode = useCallback(() => {
if (project) {
Expand Down
22 changes: 11 additions & 11 deletions packages/renderer/src/modules/store/editor/slice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { editor } from '#preload';
import { editor, misc } from '#preload';
import { createAsyncThunk, createSlice, type PayloadAction } from '@reduxjs/toolkit';

import { type ThunkAction } from '#store';
Expand All @@ -12,12 +12,12 @@ export const install = createAsyncThunk('editor/install', editor.install);
export const startInspector = createAsyncThunk('editor/startInspector', editor.startInspector);
export const runScene = createAsyncThunk('editor/runScene', editor.runScene);
export const publishScene = createAsyncThunk('editor/publishScene', editor.publishScene);
export const openPreview = createAsyncThunk('editor/openPreview', editor.openPreview);
export const openPreview = createAsyncThunk('editor/openPreview', misc.openExternal);
export const runSceneAndOpenPreview: (project: Project) => ThunkAction =
project => async dispatch => {
const action = dispatch(runScene(project.path));
const port = await action.unwrap();
await dispatch(openPreview(port));
const url = await action.unwrap();
await dispatch(openPreview(url));
};
export const openTutorial = createAsyncThunk('editor/openTutorial', editor.openTutorial);

Expand All @@ -27,7 +27,7 @@ export type EditorState = {
project?: Project;
inspectorPort: number;
publishPort: number;
previewPort: number;
previewUrl: string;
loadingInspector: boolean;
loadingPublish: boolean;
loadingPreview: boolean;
Expand All @@ -41,7 +41,7 @@ const initialState: EditorState = {
version: null,
inspectorPort: 0,
publishPort: 0,
previewPort: 0,
previewUrl: '',
loadingInspector: false,
loadingPublish: false,
loadingPreview: false,
Expand All @@ -60,7 +60,7 @@ export const slice = createSlice({
reducers: {
setProject: (state, { payload: project }: PayloadAction<Project>) => {
state.project = project;
state.previewPort = 0;
state.previewUrl = '';
},
},
extraReducers: builder => {
Expand Down Expand Up @@ -93,7 +93,7 @@ export const slice = createSlice({
});
builder.addCase(workspaceActions.createProject.fulfilled, (state, action) => {
state.project = action.payload;
state.previewPort = 0;
state.previewUrl = '';
});
builder.addCase(install.pending, state => {
state.isInstalling = true;
Expand Down Expand Up @@ -123,15 +123,15 @@ export const slice = createSlice({
}
});
builder.addCase(runScene.pending, state => {
state.previewPort = 0;
state.previewUrl = '';
state.loadingPreview = true;
});
builder.addCase(runScene.fulfilled, (state, { payload: port }) => {
state.previewPort = port;
state.previewUrl = port;
state.loadingPreview = false;
});
builder.addCase(runScene.rejected, state => {
state.previewPort = 0;
state.previewUrl = '';
state.loadingPreview = false;
});
},
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/types/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Ipc {
'bin.install': () => Promise<void>;
'bin.code': (path: string) => Promise<void>;
'cli.init': (path: string, repo?: string) => Promise<void>;
'cli.start': (path: string) => Promise<number>;
'cli.start': (path: string) => Promise<string>;
'cli.deploy': (opts: DeployOptions) => Promise<number>;
'analytics.track': (event: string, data?: Record<string, any>) => void;
'analytics.getUserId': () => Promise<string>;
Expand Down

0 comments on commit f728ac7

Please sign in to comment.