Skip to content

Commit

Permalink
fix(web): fix container logs (#6221)
Browse files Browse the repository at this point in the history
  • Loading branch information
scopsy authored Aug 1, 2024
1 parent ffbe038 commit f89547d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
40 changes: 37 additions & 3 deletions apps/web/src/pages/auth/onboarding/PlaygroundPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ export function PlaygroundPage() {
payload: {},
});
const [clickedStepId, setClickedStepId] = useState<string>('');
const { testUser, bridgeURL } = useStudioState();
const { testUser, bridgeURL, setBridgeURL } = useStudioState();

const { data: discover } = useDiscover();
const { data: discover, refetch } = useDiscover();

const workflow = discover?.workflows?.[0];
const steps = workflow?.steps;

const { initializeWebContainer, isBridgeAppLoading } = useContainer();
const { initializeWebContainer, isBridgeAppLoading, containerBridgeUrl } = useContainer();
const { toggleColorScheme, colorScheme } = useThemeChange();
const segment = useSegment();

Expand Down Expand Up @@ -77,9 +77,43 @@ export function PlaygroundPage() {
useEffectOnce(() => {
segment.track('Visit Playground page - [Playground]');

if (containerBridgeUrl) {
setBridgeURL(containerBridgeUrl);
setTimeout(() => {
refetch();
}, 500);
}

initializeWebContainer();
}, true);

useEffect(() => {
if (containerBridgeUrl) {
setBridgeURL(containerBridgeUrl);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
refetch();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [bridgeURL]);

useEffect(() => {
window.addEventListener('webcontainer:serverReady', () => {
if (bridgeURL !== containerBridgeUrl && containerBridgeUrl) {
setBridgeURL(containerBridgeUrl);
}

refetch();
});

return () => {
window.removeEventListener('webcontainer:serverReady', () => {});
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (colorScheme === 'light') {
toggleColorScheme();
Expand Down
17 changes: 13 additions & 4 deletions apps/web/src/pages/auth/onboarding/PlaygroundTourGuide.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Code } from '@mantine/core';
import { css } from '@novu/novui/css';
import { useState, useEffect } from 'react';
import Joyride, { CallBackProps, STATUS, Step, TooltipRenderProps } from 'react-joyride';
import Joyride, { ACTIONS, CallBackProps, LIFECYCLE, STATUS, Step, TooltipRenderProps } from 'react-joyride';
import { Button } from '@novu/novui';
import { Accordion } from '@mantine/core';

Expand Down Expand Up @@ -135,9 +135,18 @@ export function TourGuideComponent({
];

const handleJoyrideCallback = (data: CallBackProps) => {
const { status, lifecycle, action } = data;
const { status, lifecycle, action, index } = data;

if (action === 'next' && data.step.target === '.workflow-flow' && steps?.length && lifecycle === 'complete') {
if (action === ACTIONS.NEXT && lifecycle === LIFECYCLE.COMPLETE) {
setJoyStepIndex(index + 1);
}

if (
action === ACTIONS.NEXT &&
data.step.target === '.workflow-flow' &&
steps?.length &&
lifecycle === LIFECYCLE.COMPLETE
) {
setRunJoyride(false);
setJoyStepIndex(3);
setClickedStepId(steps[0].stepId);
Expand All @@ -153,7 +162,7 @@ export function TourGuideComponent({
};

useEffect(() => {
if (joyStepIndex !== undefined) {
if (joyStepIndex !== undefined && joyStepIndex !== null) {
setRunJoyride(true);
}
}, [joyStepIndex, setRunJoyride]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { WorkflowNodes } from '../../../studio/components/workflows/node-view/Wo
import { When } from '../../../components/utils/When';
import { HStack, VStack } from '@novu/novui/jsx';
import { StepNode } from '../../../studio/components/workflows/node-view/StepNode';
import { useBridgeAPI } from '../../../studio/hooks/useBridgeAPI';
import { useBridgeAPI, useDiscover } from '../../../studio/hooks/useBridgeAPI';
import { useControlsHandler } from '../../../hooks/workflow/useControlsHandler';
import { WorkflowsStepEditor } from '../../../components/workflow_v2/StepEditorComponent';
import { BackButton } from '../../../components/layout/components/LocalStudioHeader/BackButton';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ContainerState = {
setCode: (code: Record<string, string>) => void;
isBridgeAppLoading: boolean;
initializeWebContainer: () => Promise<void>;
containerBridgeUrl: string | null;
};

const ContainerContext = React.createContext<ContainerState | undefined>(undefined);
Expand All @@ -36,13 +37,14 @@ export const ContainerProvider: FCWithChildren = ({ children }) => {
'react-email.tsx': REACT_EMAIL_CODE,
});
const [isBridgeAppLoading, setIsBridgeAppLoading] = useState<boolean>(true);
const [containerBridgeUrl, setContainerBridgeUrl] = useState<string | null>(null);
const [webContainer, setWebContainer] = useState<typeof WebContainer | null>(null);
const [sandboxBridgeAddress, setSandboxBridgeAddress] = useState<string | null>(null);
const [initStarted, setInitStarted] = useState<boolean>(false);
const terminalRef = useRef<TerminalHandle>(null);
const studioState = useStudioState() || {};
const { setBridgeURL } = studioState;
const { refetch } = useDiscover();
const { setBridgeURL, bridgeURL } = studioState;

const segment = useSegment();

const writeOutput = (data: string) => {
Expand All @@ -57,11 +59,18 @@ export const ContainerProvider: FCWithChildren = ({ children }) => {
segment.track('Starting Playground - [Playground]');

setInitStarted(true);
setWebContainer(
await WebContainer.boot({
coep: 'credentialless',
})
);
const webContainerInstance = await WebContainer.boot({
coep: 'credentialless',
});

setWebContainer(webContainerInstance);

webContainerInstance.on('server-ready', (port, url) => {
segment.track('Sandbox bridge app is ready - [Playground]');
setSandboxBridgeAddress(url + ':' + port);

window.dispatchEvent(new CustomEvent('webcontainer:serverReady'));
});
}
} catch (error: any) {
segment.track('Error booting web container - [Playground]', {
Expand All @@ -85,8 +94,6 @@ export const ContainerProvider: FCWithChildren = ({ children }) => {
setSandboxBridgeAddress(url + ':' + port);

window.dispatchEvent(new CustomEvent('webcontainer:serverReady'));

refetch();
});

async function installDependencies() {
Expand Down Expand Up @@ -163,6 +170,7 @@ export const ContainerProvider: FCWithChildren = ({ children }) => {
write(data) {
if (data.includes('novu.sh')) {
setBridgeURL(data.trim());
setContainerBridgeUrl(data.trim());
setIsBridgeAppLoading(false);
}
writeOutput(data);
Expand All @@ -189,9 +197,9 @@ export const ContainerProvider: FCWithChildren = ({ children }) => {

return () => clearTimeout(debounceTimeout);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [code, refetch]);
}, [code]);

const value = { terminalRef, code, setCode, isBridgeAppLoading, initializeWebContainer };
const value = { terminalRef, code, setCode, isBridgeAppLoading, initializeWebContainer, containerBridgeUrl };

return <ContainerContext.Provider value={value}>{children}</ContainerContext.Provider>;
};
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/studio/hooks/useBridgeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const useDiscover = (options?: any) => {
const { refetch } = discoverQuery;

useEffect(() => {
refetch();
if (!bridgeURL) {
refetch();
}
}, [bridgeURL, setBridgeURL, refetch]);

return discoverQuery;
Expand Down

0 comments on commit f89547d

Please sign in to comment.