diff --git a/src/components/Cli/Cli.tsx b/src/components/Cli/Cli.tsx index 46a6728..d4b956a 100644 --- a/src/components/Cli/Cli.tsx +++ b/src/components/Cli/Cli.tsx @@ -5,11 +5,17 @@ import { } from '@/components/Cli/files/scripts'; import { ReactNode } from 'react'; -export const runPrompt = (args: string[]): ReactNode => { +export const runPrompt = ( + args: string[], +): { result: ReactNode; isStandalone: boolean } => { const cmd: string = args[0]; if (allScriptNames.includes(cmd)) { - return allScriptsByName[cmd].run(); + const script = allScriptsByName[cmd]; + return { + result: script.run(), + isStandalone: script.isStandalone, + }; } const flags: Record = {}; @@ -37,5 +43,9 @@ export const runPrompt = (args: string[]): ReactNode => { throw new Error(`Unknown command: ${cmd}`); } - return allCommandsByName[cmd].run({ flags, values }); + const command = allCommandsByName[cmd]; + return { + result: command.run({ flags, values }), + isStandalone: command.isStandalone, + }; }; diff --git a/src/components/Cli/files/CliFile.tsx b/src/components/Cli/files/CliFile.tsx index c218b62..45b7e1f 100644 --- a/src/components/Cli/files/CliFile.tsx +++ b/src/components/Cli/files/CliFile.tsx @@ -2,5 +2,6 @@ import { ReactNode } from 'react'; export abstract class CliFile { abstract get fileName(): string; + public isStandalone = false; public abstract run(props?: T): ReactNode; } diff --git a/src/components/Cli/files/scripts/projects/ProjectsScript.tsx b/src/components/Cli/files/scripts/projects/ProjectsScript.tsx index e31dbca..12681d5 100644 --- a/src/components/Cli/files/scripts/projects/ProjectsScript.tsx +++ b/src/components/Cli/files/scripts/projects/ProjectsScript.tsx @@ -6,7 +6,7 @@ import { ProjectCollectionName, } from '@/components/Cli/files/scripts/projects/types'; import { parseStrapiCollectionToCollectionByLocale } from '@/util/helper'; -import { StartStandaloneEvent, StopStandaloneEvent } from '@/util/types'; +import { StopStandaloneEvent } from '@/util/types'; import { graphql, useStaticQuery } from 'gatsby'; const ProjectsRun = () => { @@ -75,10 +75,9 @@ export class Projects extends CliFile { return `projects.sh`; } + public isStandalone: boolean = true; + public run() { - if (typeof window !== `undefined`) { - window.dispatchEvent(StartStandaloneEvent); - } return ; } } diff --git a/src/components/Shell/Shell.tsx b/src/components/Shell/Shell.tsx index 14922a0..1400f50 100644 --- a/src/components/Shell/Shell.tsx +++ b/src/components/Shell/Shell.tsx @@ -38,6 +38,7 @@ export const Shell = ({ username, domain }: Readonly) => { throw new Error(`No prompt provided in run event!`); } const cmdResTuple = processRunRequest(event.detail.prompt); + setIsStandaloneOpen(cmdResTuple.isStandalone); if (cmdResTuple.prompt !== new Clear().fileName) { setHistory((history) => [...history, cmdResTuple]); @@ -55,18 +56,15 @@ export const Shell = ({ username, domain }: Readonly) => { setHistory([]); }, [setHistory]); - const handleStartStandaloneEvent = useCallback(() => { - setIsStandaloneOpen(true); - }, [setIsStandaloneOpen]); - const handleStopStandaloneEvent = useCallback(() => { + if (!isStandaloneOpen) return; setIsStandaloneOpen(false); setHistory((history) => { const newHistory = [...history]; newHistory[newHistory.length - 1].response = null; return newHistory; }); - }, [setHistory, setIsStandaloneOpen]); + }, [isStandaloneOpen, setHistory]); useEffect(() => { textAreaRef.current?.scrollIntoView(); @@ -75,10 +73,6 @@ export const Shell = ({ username, domain }: Readonly) => { useEffect(() => { window.addEventListener(CustomEvents.clear, handleClearEvent); window.addEventListener(CustomEvents.run, handleRunEvent); - window.addEventListener( - CustomEvents.startStandalone, - handleStartStandaloneEvent, - ); window.addEventListener( CustomEvents.stopStandalone, handleStopStandaloneEvent, @@ -93,10 +87,6 @@ export const Shell = ({ username, domain }: Readonly) => { return () => { window.removeEventListener(CustomEvents.clear, handleClearEvent); window.removeEventListener(CustomEvents.run, handleRunEvent); - window.removeEventListener( - CustomEvents.startStandalone, - handleStartStandaloneEvent, - ); window.removeEventListener( CustomEvents.stopStandalone, handleStopStandaloneEvent, @@ -105,7 +95,6 @@ export const Shell = ({ username, domain }: Readonly) => { }, [ handleClearEvent, handleRunEvent, - handleStartStandaloneEvent, handleStopStandaloneEvent, location.search, ]); @@ -248,6 +237,7 @@ const processRunRequest = (userPrompt: string): PromptHistoryEntry => { const res: PromptHistoryEntry = { prompt: userPrompt, response: null, + isStandalone: false, }; const consecutivePrompts = userPrompt.split(`&&`).map((cmd) => cmd.trim()); // recursively process commands @@ -260,11 +250,14 @@ const processRunRequest = (userPrompt: string): PromptHistoryEntry => { {consecutiveCmdRes.response} ); + res.isStandalone = consecutiveCmdRes.isStandalone || res.isStandalone; } return res; } try { - res.response = runPrompt(userPrompt.split(` `)); + const { result, isStandalone } = runPrompt(userPrompt.split(` `)); + res.response = result; + res.isStandalone = isStandalone; } catch (e) { res.response = (e as Error).message; } diff --git a/src/context/PromptHistoryContext/types.ts b/src/context/PromptHistoryContext/types.ts index 52b871f..6e22572 100644 --- a/src/context/PromptHistoryContext/types.ts +++ b/src/context/PromptHistoryContext/types.ts @@ -3,4 +3,5 @@ import { ReactNode } from 'react'; export interface PromptHistoryEntry { prompt: string; response: ReactNode; + isStandalone: boolean; } diff --git a/src/util/types.ts b/src/util/types.ts index aee404f..4ed1a56 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -2,7 +2,6 @@ export enum CustomEvents { run = `run`, clear = `clear`, stopStandalone = `closeStandalone`, - startStandalone = `startStandalone`, } export const RunEvent = (prompt: string) => @@ -14,10 +13,6 @@ export const RunEvent = (prompt: string) => export const StopStandaloneEvent = new CustomEvent(CustomEvents.stopStandalone); -export const StartStandaloneEvent = new CustomEvent( - CustomEvents.startStandalone, -); - export const ClearEvent = new CustomEvent(CustomEvents.clear); export enum SearchParams {