mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-27 13:29:15 +07:00
* feat: Redesign doc * chore: uopdate site * chore: uopdate site * chore: uopdate site * chore: uopdate site * chore: uopdate site * feat: Uopdate content * chore: New doc * chore: Update content * chore: Update content * chore: add images * chore: add images * chore: add images * chore: add images * feat: Add more images * feat: Add more images * fix: Cannot reach end * chore: Update content * chore: Update content * chore: Update content * chore: Update content * chore: Update content * Revise README content and structure Updated README to reflect changes in project description and removed outdated notes. * Revise 'Getting Started' and TOC in README Updated the 'Getting Started' section and modified the table of contents. * chore: Update content * Revise README structure and content Updated the Getting Started section and removed the Table of Contents. Adjusted the Local Development instructions. * Remove custom themes section from README Removed section about custom themes from README. * Update README.md * Refine introduction and highlight cloud version Updated wording for clarity and added recommendation for cloud version. * chore: Update content * chore: Update content * chore: Update content * chore: Update content * chore: Update content * chore: Update content * chore: Update content * fix: add missing translation * 🔀 chore: Move README changes to feat/readme branch Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: add missing translation * chore: update cdn * docs: add migration guide from v1.x local database to v2.x and update help sections Signed-off-by: Innei <tukon479@gmail.com> * fix: add missing translation * fix: add missing images * fix: add missing changelogs * fix: add missing changelogs * fix: add missing changelogs * fix: add missing changelogs * fix: add missing changelogs * style: update cdn --------- Signed-off-by: Innei <tukon479@gmail.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: canisminor1990 <i@canisminor.cc> Co-authored-by: Innei <tukon479@gmail.com>
145 lines
4.9 KiB
Plaintext
145 lines
4.9 KiB
Plaintext
---
|
|
title: How to Develop a New Feature
|
|
description: >-
|
|
Learn how to implement the Chat Messages feature in LobeHub using Next.js and
|
|
TypeScript.
|
|
tags:
|
|
- LobeHub
|
|
- Next.js
|
|
- TypeScript
|
|
- Chat Feature
|
|
- Zustand
|
|
---
|
|
|
|
# How to Develop a New Feature
|
|
|
|
LobeHub is built on the Next.js framework and uses TypeScript as the primary development language. When developing a new feature, we need to follow a certain development process to ensure the quality and stability of the code. The general process can be divided into the following five steps:
|
|
|
|
1. Routing: Define routes (`src/app`).
|
|
2. Data Structure: Define data structures (`src/types`).
|
|
3. Business Logic Implementation: Zustand store (`src/store`).
|
|
4. Page Display: Write static components/pages. Create features in:
|
|
- `src/features/<feature-name>/` for **shared global features** (used across multiple pages)
|
|
- `src/app/<new-page>/features/<feature-name>/` for **page-specific features** (only used in this page)
|
|
5. Function Binding: Bind the store with page triggers (`const [state, function] = useNewStore(s => [s.state, s.function])`).
|
|
|
|
Taking the "Chat Messages" feature as an example, here are the brief steps to implement this feature:
|
|
|
|
## 1. Define Routes
|
|
|
|
In the `src/app` directory, we need to define a new route to host the "Chat Messages" page. Generally, we would create a new folder under `src/app`, for example, `chat`, and create a `page.tsx` file within this folder to export a React component as the main body of the page.
|
|
|
|
```tsx
|
|
// src/app/chat/page.tsx
|
|
import ChatPage from './features/chat';
|
|
|
|
export default ChatPage;
|
|
```
|
|
|
|
## 2. Define Data Structure
|
|
|
|
In the `src/types` directory, we need to define the data structure for "Chat Messages". For example, we create a `chat.ts` file and define the `ChatMessage` type within it:
|
|
|
|
```ts
|
|
// src/types/chat.ts
|
|
|
|
export type ChatMessage = {
|
|
id: string;
|
|
content: string;
|
|
timestamp: number;
|
|
sender: 'user' | 'bot';
|
|
};
|
|
```
|
|
|
|
## 3. Create Zustand Store
|
|
|
|
In the `src/store` directory, we need to create a new Zustand Store to manage the state of "Chat Messages". For example, we create a `chatStore.ts` file and define a Zustand Store within it:
|
|
|
|
```ts
|
|
// src/store/chatStore.ts
|
|
import create from 'zustand';
|
|
|
|
type ChatState = {
|
|
messages: ChatMessage[];
|
|
addMessage: (message: ChatMessage) => void;
|
|
};
|
|
|
|
export const useChatStore = create<ChatState>((set) => ({
|
|
messages: [],
|
|
addMessage: (message) => set((state) => ({ messages: [...state.messages, message] })),
|
|
}));
|
|
```
|
|
|
|
## 4. Create Page and Components
|
|
|
|
In `src/app/<new-page>/features/<new-feature>.tsx`, we need to create a new page or component to display "Chat Messages". In this file, we can use the Zustand Store created earlier and Ant Design components to build the UI:
|
|
|
|
```jsx
|
|
// src/app/chat/features/ChatPage/index.tsx
|
|
// Note: Use src/app/<page>/features/ for page-specific components
|
|
import { List, Typography } from 'antd';
|
|
import { useChatStore } from 'src/store/chatStore';
|
|
|
|
const ChatPage = () => {
|
|
const messages = useChatStore((state) => state.messages);
|
|
|
|
return (
|
|
<List
|
|
dataSource={messages}
|
|
renderItem={(message) => (
|
|
<List.Item>
|
|
<Typography.Text>{message.content}</Typography.Text>
|
|
|
|
</List.Item>
|
|
)}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default ChatPage;
|
|
```
|
|
|
|
> **Note on Feature Organization**: LobeHub uses two patterns for organizing features:
|
|
>
|
|
> - **Global features** (`src/features/`): Shared components like `ChatInput`, `Conversation` used across the app
|
|
> - **Page-specific features** (`src/app/<page>/features/`): Components used only within a specific page route
|
|
>
|
|
> Choose based on reusability. If unsure, start with page-specific and refactor to global if needed elsewhere.
|
|
|
|
## 5. Function Binding
|
|
|
|
In a page or component, we need to bind the Zustand Store's state and methods to the UI. In the example above, we have already bound the `messages` state to the `dataSource` property of the list. Now, we also need a method to add new messages. We can define this method in the Zustand Store and then use it in the page or component:
|
|
|
|
```jsx
|
|
import { Button } from 'antd';
|
|
|
|
const ChatPage = () => {
|
|
const messages = useChatStore((state) => state.messages);
|
|
const addMessage = useChatStore((state) => state.addMessage);
|
|
|
|
const handleSend = () => {
|
|
addMessage({ id: '1', content: 'Hello, world!', timestamp: Date.now(), sender: 'user' });
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<List
|
|
dataSource={messages}
|
|
renderItem={(message) => (
|
|
<List.Item>
|
|
<Typography.Text>{message.content}</Typography.Text>
|
|
|
|
</List.Item>
|
|
)}
|
|
/>
|
|
<Button onClick={handleSend}>Send</Button>
|
|
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default ChatPage;
|
|
```
|
|
|
|
The above is the step to implement the "chat message" feature in LobeHub. Of course, in the actual development of LobeHub, the business requirements and scenarios faced in real situations are far more complex than the above demo. Please develop according to the actual situation.
|