Skip to content

Commit

Permalink
Improved test app UX
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardo-forina committed Nov 27, 2024
1 parent 1e25ca0 commit 02d2b52
Show file tree
Hide file tree
Showing 6 changed files with 338,602 additions and 67 deletions.
15 changes: 10 additions & 5 deletions packages/test-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ self.MonacoEnvironment = {
loader.config({ monaco });

function App() {
const [spec, setSpec] = useState<string | null>(null);
const [captureChanges, setCaptureChanges] = useState(true);
const [output, setOutput] = useState("");
const { showDebugger, showXStateInspector } = useAppContext();
const { showDebugger, showXStateInspector, spec, setSpec } = useAppContext();
const { inspect, start, stop } = createBrowserInspector({
autoStart: false,
});
Expand Down Expand Up @@ -92,10 +91,16 @@ function App() {
// or, you could do
// editorRef.current.updateDocument(...newSpec...);
}
}, []);
}, [setSpec]);

if (spec === null) {
return <SpecUploader onSpec={setSpec} />;
if (spec === undefined) {
return (
<SpecUploader
onSpec={(spec) => {
setSpec(spec);
}}
/>
);
}
return (
<>
Expand Down
42 changes: 26 additions & 16 deletions packages/test-app/src/AppContext.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import {createContext, ReactNode, useContext, useState} from "react";
import { createContext, ReactNode, useContext, useState } from "react";

const AppContext = createContext<{
showDebugger: boolean,
showXStateInspector: boolean,
setDebugger: (value: boolean) => void,
setXStateInspector: (value: boolean) => void,
} | undefined>(undefined)
const AppContext = createContext<
| {
spec?: string;
showDebugger: boolean;
showXStateInspector: boolean;
setSpec: (value?: string) => void;
setDebugger: (value: boolean) => void;
setXStateInspector: (value: boolean) => void;
}
| undefined
>(undefined);

export function AppProvider({ children }: { children: ReactNode }) {
const [spec, setSpec] = useState<string | undefined>(undefined);
const [showDebugger, setDebugger] = useState(false);
const [showXStateInspector, setXStateInspector] = useState(false);
return (
<AppContext.Provider value={{
showDebugger,
showXStateInspector,
setDebugger,
setXStateInspector
}}>
<AppContext.Provider
value={{
spec,
showDebugger,
showXStateInspector,
setSpec,
setDebugger,
setXStateInspector,
}}
>
{children}
</AppContext.Provider>
)
);
}

export function useAppContext() {
const ctx = useContext(AppContext);
if (!ctx) throw new Error('useAppContext must be used within AppProvider');
if (!ctx) throw new Error("useAppContext must be used within AppProvider");
return ctx;
}
}
Loading

0 comments on commit 02d2b52

Please sign in to comment.