AI-Assisted Frontend Refactoring: Rebuilding and Internationalizing a Small Web Interface
This article presents a practical approach to improving a small frontend interface with AI assistance. It covers how to turn vague design requests into verifiable requirements, standardize the UI, make keyboard shortcuts compatible with Chinese input methods, add automatic initialization and preset configurations, introduce internationalization, and reduce regression risks through incremental implementation and review.
1. Background and Goals
Many small projects reach a similar stage: the core functionality works, but the interface is rough and interaction details remain incomplete. A web interface for a mobile automation tool is a typical example. It can perform its primary tasks, but its buttons use inconsistent styles, text is not always centered, devices must be initialized manually, common configurations must be entered by users, and only one language is supported.
This refactoring effort had three goals:
- Make the interface visually closer to mainstream conversational applications.
- Complete interaction patterns that users already expect.
- Preserve all existing functionality.
The third goal is easy to overlook, but it determines how every change should be reviewed. If a modification changes functional behavior, that change must be documented and explicitly confirmed.
2. Turning Vague Requests into Verifiable Requirements
“Make the interface look better” is difficult to verify, and AI coding tools are unlikely to produce consistent results from such an ambiguous request. Before writing code, convert the request into a checklist that can be reviewed item by item.
The requirements for this task were divided as follows:
- Visual layer: Use a mature component library and theme consistently. Add simple icons to buttons, and center button text both horizontally and vertically.
- Interaction layer: Initialize automatically when the page opens. Do not send a message when the user presses Enter alone. Define clear shortcuts for sending and inserting line breaks.
- Configuration layer: Provide three common preset configurations on the settings page. Place custom configuration at the end of the list.
- Internationalization layer: Extract all interface text from the code and support language switching.
- Constraints: Do not change the existing business logic, API calls, or data formats.
After this decomposition, each requirement can be checked by performing a simple action, such as opening the page or pressing a shortcut. This level of detail also helps AI tools implement the work incrementally and makes it easier to identify the failed requirement when something goes wrong.
3. UI Refactoring: Establishing Consistent Visual Rules
Mainstream conversational applications tend to follow a familiar structure: conversation navigation on the left or at the top, a message area in the center, and an input area at the bottom with a send button on the right. Reusing this structure reduces the user’s learning curve.
For visual details, three rules are especially useful:
- Use consistent components: Prefer buttons, input fields, and dialogs from the same component library. Avoid excessive hand-written styling that can lead to an inconsistent visual language.
- Use clear icon metaphors: A paper-plane icon can represent sending, a gear can represent settings, and a trash can can represent clearing. Keep short text labels next to icons to reduce ambiguity.
- Make alignment explicit: Center button text through layout properties rather than adjusting padding until it looks correct. Alignment problems usually require checking the relevant flex or grid settings.
A mature community theme can be adopted directly, but its color contrast should be reviewed first. In dark mode, secondary text often fails to meet contrast requirements and may need separate adjustment.
4. Keyboard Shortcuts and Input Method Compatibility
In the original implementation, pressing Enter sent the message immediately. This is inconvenient for Chinese users because Enter is also used to select candidates in an input method editor (IME).
The revised rules are:
- Enter: Insert a line break and preserve the text field’s default behavior.
- Cmd/Ctrl + Enter or Alt + Enter: Send the message.
- Shift + Enter: Insert a line break, with the same result as pressing Enter alone.
The composition state of the input method must be handled carefully. When text is still being composed, pressing Enter is intended to confirm a candidate and must not send the message.
function handleKeyDown(event: KeyboardEvent) {// During IME composition, Enter confirms the candidate. Ignore it here.if (event.isComposing) return;if (event.key === "Enter" && (event.metaKey || event.ctrlKey || event.altKey)) {event.preventDefault();sendMessage();}// Let the text field handle all other cases, including line breaks.}
Testing should cover the following scenarios:
- Pressing Enter to select a candidate while using a Chinese IME.
- Pressing Enter directly during English input.
- Pressing Shift + Enter.
- Checking whether the input field is cleared after sending with Cmd/Ctrl + Enter.
- Verifying that repeated triggers during sending do not create duplicate requests.
5. Automatic Initialization and Preset Configurations
5.1 Initialize Automatically When the Page Opens
Manual initialization adds an extra step and is easy to forget. The initialization logic can run when the page is mounted. Two points require special attention:
- If initialization fails, display a clear error message and provide a retry action. Do not fail silently.
- If initialization depends on an external device or service, check whether it is available before deciding whether to proceed.
5.2 Provide Three Common Presets
The settings page should offer three preset configurations. Selecting one automatically fills in the form while still allowing manual edits.
The configuration structure can remain simple:
type ServiceConfig = {id: string;label: string;baseUrl: string;model: string;};const presets: ServiceConfig[] = [{ id: "preset-a", label: "Preset A", baseUrl: "https://example.com/v1", model: "model-a" },{ id: "preset-b", label: "Preset B", baseUrl: "https://example.org/v1", model: "model-b" },{ id: "preset-c", label: "Local Deployment", baseUrl: "http://localhost:8000/v1", model: "local-model" },];
The URLs and model names above are placeholders and should be replaced with the user’s actual service information. Custom configuration should appear at the end of the list, matching the common progression from frequently used options to personalized settings.
API keys should not be included in preset data. Users should enter their own keys, which should be stored in local environment variables or protected storage rather than committed to the repository with the source code.
6. Internationalization
The core of internationalization is separating interface text from application code. A common implementation approach is:
- Create a unified language resource file for each supported language, using consistent keys.
- Reference keys in business code and use a translation function to obtain text for the current locale.
- Refresh the interface immediately after a language switch and persist the user’s choice locally.
- Delegate date, number, and time formatting to the system’s internationalization APIs instead of assembling these values manually.
A simplified example is shown below:
const messages = {"zh-CN": { send: "发送", newChat: "新建会话", settings: "设置" },"en-US": { send: "Send", newChat: "New chat", settings: "Settings" },};function translate(locale: keyof typeof messages, key: keyof typeof messages["zh-CN"]) {return messages[locale][key] ?? key;}
Three types of omissions are especially common during localization:
- Button
titleattributes orplaceholdertext are left untranslated. - Error messages remain hard-coded in the source.
- Sentence fragments are concatenated under the assumption that word order is the same in every language.
The third issue should be handled with complete sentence templates and parameters. Avoid assembling sentences by joining multiple independent word fragments.
7. Working Effectively with AI Coding Tools
AI tools handled much of the boilerplate code and repetitive editing in this task, but the results depended heavily on the collaboration process.
The following practices were effective:
- Provide context: Share the relevant files, existing component structure, and functional constraints with the tool. If only the desired outcome is described, the generated changes may diverge from existing behavior.
- Work in small increments: Complete one requirement at a time and verify it before moving to the next. Asking the tool to complete everything at once makes problems difficult to isolate.
- Review against the checklist: Validate each requirement individually, with particular attention to keyboard shortcuts, initialization, and configuration switching, since these areas are more likely to introduce hidden regressions.
- Keep version history: Commit each step separately so that the project can be quickly rolled back to the last working state if necessary.
8. Risks and Considerations
- Functional regressions: UI changes can unintentionally affect state management. Walk through the core workflows manually before and after the refactoring, and add automated tests for critical paths when appropriate.
- Dependency bloat: When introducing a component library or theme, review its bundle size and license. Include only the components that are actually used.
- Credential security: Display keys in masked form in the interface, and never print complete keys in logs.
- Review generated code: AI-generated code may refer to nonexistent APIs or outdated usage patterns. Before integrating it, verify every call against the documentation or through type checking.
9. Conclusion
The main value of a small frontend refactoring effort lies in turning experience problems into verifiable requirements. The approach described here can be summarized in three steps: convert requests into a checklist, implement and verify each item separately, and use version history to preserve a reliable rollback path.
Visual design, interaction, configuration, and internationalization belong to different layers. Handling them separately makes problems easier to identify. For developers using AI-assisted programming, clear constraints, controlled implementation steps, and consistent acceptance checks are essential for turning generated output into reliable code.
