Skip to content

Commit

Permalink
Merge pull request #15 from nico-i/fix/standalone-bug
Browse files Browse the repository at this point in the history
fix: isStandalone removing help macro bug
  • Loading branch information
nico-i authored Dec 30, 2023
2 parents fc4e509 + afd6994 commit b9c8520
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
16 changes: 13 additions & 3 deletions src/components/Cli/Cli.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {};
Expand Down Expand Up @@ -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,
};
};
1 change: 1 addition & 0 deletions src/components/Cli/files/CliFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { ReactNode } from 'react';

export abstract class CliFile<T = {}> {
abstract get fileName(): string;
public isStandalone = false;
public abstract run(props?: T): ReactNode;
}
7 changes: 3 additions & 4 deletions src/components/Cli/files/scripts/projects/ProjectsScript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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 <ProjectsRun />;
}
}
23 changes: 8 additions & 15 deletions src/components/Shell/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
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]);
Expand All @@ -55,18 +56,15 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
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();
Expand All @@ -75,10 +73,6 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
useEffect(() => {
window.addEventListener(CustomEvents.clear, handleClearEvent);
window.addEventListener(CustomEvents.run, handleRunEvent);
window.addEventListener(
CustomEvents.startStandalone,
handleStartStandaloneEvent,
);
window.addEventListener(
CustomEvents.stopStandalone,
handleStopStandaloneEvent,
Expand All @@ -93,10 +87,6 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
return () => {
window.removeEventListener(CustomEvents.clear, handleClearEvent);
window.removeEventListener(CustomEvents.run, handleRunEvent);
window.removeEventListener(
CustomEvents.startStandalone,
handleStartStandaloneEvent,
);
window.removeEventListener(
CustomEvents.stopStandalone,
handleStopStandaloneEvent,
Expand All @@ -105,7 +95,6 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
}, [
handleClearEvent,
handleRunEvent,
handleStartStandaloneEvent,
handleStopStandaloneEvent,
location.search,
]);
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/context/PromptHistoryContext/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { ReactNode } from 'react';
export interface PromptHistoryEntry {
prompt: string;
response: ReactNode;
isStandalone: boolean;
}
5 changes: 0 additions & 5 deletions src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export enum CustomEvents {
run = `run`,
clear = `clear`,
stopStandalone = `closeStandalone`,
startStandalone = `startStandalone`,
}

export const RunEvent = (prompt: string) =>
Expand All @@ -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 {
Expand Down

0 comments on commit b9c8520

Please sign in to comment.