Welcome to the final chapter of our series!
In the previous chapter, Local JSX Interface, we learned how to make React render inside a terminal window. We built a nice menu that asks the user, "What do you want to do?"
However, asking the question is the easy part. Doing the work is harder. If the user selects "Upgrade your plan," our current menu doesn't know how to process credit cards or change database records.
We need a way to hand this task off to a specialist. This pattern is called Workflow Delegation.
Imagine you are a General Contractor building a house. You know how to manage the project, but you don't pour the concrete or wire the electricity yourself. When it's time for wiring, you call an Electrician.
In our code, we don't want the RateLimitOptionsMenu to be 5,000 lines long containing every possible feature (Billing, Settings, Upgrades). Instead, we want it to be a clean "Router" that delegates work to other modules.
To make this work in React, we use three specific techniques.
upgradeCall)We import the "Start Button" of another module. Notice we rename it to be descriptive.
// We import the generic 'call' function from the upgrade file
// but we rename it to 'upgradeCall' so we know what it does.
import { call as upgradeCall } from '../upgrade/upgrade.js';
subCommandJSX)We need a spot in our memory to hold the "Electrician's" tools. If this spot is empty, we show the main menu. If it's full, we show the specialist's interface.
// Inside RateLimitOptionsMenu
// Initially null (empty)
const [subCommandJSX, setSubCommandJSX] = useState(null);
We check our placeholder before rendering anything else.
// If the specialist gave us a screen to show, show that!
if (subCommandJSX) {
return subCommandJSX;
}
// Otherwise, show the standard Rate Limit Menu...
return <Dialog ... />
Let's look at the exact moment the user clicks "Upgrade." We use the handleSelect function we hinted at in Chapter 3: Rate Limit Menu UI.
When the user selects 'upgrade', we call the specialist.
function handleSelect(value) {
if (value === 'upgrade') {
// 1. Log the analytics event
logEvent('rate_limit_menu_select_upgrade', {});
// 2. Call the specialist module
// We pass 'onDone' so they can close the app when finished
upgradeCall(onDone, context).then(handleNewScreen);
}
}
The upgradeCall returns a Promise. This means it says, "Give me a millisecond to load my graphics, and I will send you a UI component."
When that UI arrives, we save it into our State.
// This runs when upgradeCall returns
const handleNewScreen = (newJSX) => {
// If we got a valid screen back...
if (newJSX) {
// ...replace our current menu with this new one
setSubCommandJSX(newJSX);
}
};
Because we called setSubCommandJSX, React detects a change. It runs the component code again.
subCommandJSX null?"Let's visualize this hand-off. It is very similar to navigating pages on a website, but it happens inside a single command.
This Workflow Delegation pattern is powerful for three reasons:
call function.context and onDone tools to the sub-module.onDone().onDone came from the main app, it closes the entire program successfully.Congratulations! You have completed the Rate Limit Options tutorial.
Let's review what we built together:
You now understand the architecture of a complex, interactive CLI feature. You can use these patternsβContext, Command Definition, and Delegationβto build any feature, from simple prompts to complex billing systems, right inside the terminal.
Happy coding!
Generated by Code IQ