Welcome to the final chapter of our tutorial series!
In the previous chapter, Lazy Module Loading, we learned how to efficiently fetch the heavy code for our "Remote Environment" feature from the disk.
Now, we have the code in our hands. But code is just text. How do we turn that code into a visual window that the user can actually click and type in?
This brings us to the UI Command Handler.
Imagine you have just downloaded a video game (Lazy Loading). To actually play it, you need a console or a PC to render the graphics on your TV.
The UI Command Handler is that rendering console.
We have fetched the remote-env.tsx file. Now we need to:
call FunctionTo make this work, the system and the feature need a standard agreement (an Interface).
The agreement is simple: "Every UI feature must export a function named call."
The system promises: "I will call this function when the user clicks." The feature promises: "I will return a React component (the UI) for you to display."
onDone: A "Callback". This is a button the system gives to the feature saying, "Press this when you are finished so I can close the window."ReactNode: The actual visual elements (buttons, text, forms) that React draws on the screen.
Let's write the code for remote-env.tsx. This is the file that was lazy-loaded in the previous chapter.
First, we import React and the actual design of our dialog box.
// remote-env.tsx
import * as React from 'react';
// The visual component (The "Look and Feel")
import { RemoteEnvironmentDialog } from '../../components/RemoteEnvironmentDialog.js';
// The type definition for the "Finished" signal
import type { LocalJSXCommandOnDone } from '../../types/command.js';
Explanation:
RemoteEnvironmentDialog: This is a standard React component (like a <div> or <button>) that contains our form, text inputs, and styling. We keep the styling separate from the command logic.
Now, we export the required call function. This is the entry point.
// ... inside remote-env.tsx
export async function call(
onDone: LocalJSXCommandOnDone
): Promise<React.ReactNode> {
// Return the visual component to the system
return <RemoteEnvironmentDialog onDone={onDone} />;
}
Explanation:
export async function call: This makes the function public so the System can find it.onDone: We receive this tool from the System. We pass it down to the Dialog so the Dialog can click it later.return <RemoteEnvironmentDialog ... />: We don't render this to the DOM ourselves. We just hand it back to the System. The System decides where to put it.Here is the complete, minimal file. It is surprisingly simple because its only job is to connect the System to the Visual Component.
// --- File: remote-env.tsx ---
import * as React from 'react';
import { RemoteEnvironmentDialog } from '../../components/RemoteEnvironmentDialog.js';
import type { LocalJSXCommandOnDone } from '../../types/command.js';
export async function call(onDone: LocalJSXCommandOnDone): Promise<React.ReactNode> {
// Pass the "onDone" switch to the dialog
return <RemoteEnvironmentDialog onDone={onDone} />;
}
How does a returned function result in pixels on the screen?
When you return the component, the System wraps it in a "Modal Layer" (a transparent overlay that sits on top of the rest of the app).
module.call().<RemoteEnvironmentDialog />.onDone to be called.
Here is a simplified look at the code inside the System (e.g., inside CommandRunner.tsx) that handles this process.
// System Code: CommandRunner.tsx
async function executeCommand(module: any) {
// 1. Create a way to close the window
const onDone = () => {
setModalVisible(false); // Hides the UI
console.log("Command finished");
};
// 2. Ask the module for the UI
const content = await module.call(onDone);
// 3. Put the UI on the screen
setModalContent(content);
setModalVisible(true);
}
Example Inputs & Outputs:
remote-env module we created.setModalVisible(false) runs, and the popup vanishes.
You might wonder: Why didn't we just write the React code directly inside call?
// โ Bad Practice: Mixing Logic and View
export async function call(onDone) {
return (
<div style={{ backgroundColor: 'red' }}>
<h1>My Dialog</h1>
<button onClick={onDone}>Close</button>
</div>
);
}
We avoid this because:
RemoteEnvironmentDialog might be used in other places (e.g., inside a Settings page), not just as a popup command.RemoteEnvironmentDialog) than a dynamic command handler.UI Command Handler is just a bridge. It shouldn't know about CSS or HTML.
Congratulations! You have completed the remote-env tutorial series.
You have built a sophisticated feature pipeline:
You now understand the core architecture of adding powerful, scalable features to the remote-env project. Happy coding!
Generated by Code IQ