104 lines
2.7 KiB
TypeScript
104 lines
2.7 KiB
TypeScript
// ============================================================
|
|
// 第四阶段练习文件 — Zed 超能力
|
|
// ============================================================
|
|
//
|
|
// 练习 LSP 导航:
|
|
// - 光标放在 fetchUsers 上,按 g d 跳转到定义
|
|
// - 光标放在 User 上,按 g A 查看所有引用
|
|
// - 光标放在 processUsers 上,按 c d 重命名
|
|
//
|
|
// 练习 Tree-sitter 导航:
|
|
// - 用 ] m / [ m 在方法间跳转
|
|
// - 用 ] ] / [ [ 在类/接口间跳转
|
|
// - 用 g c 选择注释
|
|
//
|
|
// 练习多光标:
|
|
// - 光标放在 user 上,按 g l 选中所有 user
|
|
// - 然后修改它们看看多光标效果
|
|
//
|
|
// ============================================================
|
|
|
|
interface Address {
|
|
street: string;
|
|
city: string;
|
|
zipCode: string;
|
|
}
|
|
|
|
interface User {
|
|
id: number;
|
|
name: string;
|
|
email: string;
|
|
isActive: boolean;
|
|
address?: Address;
|
|
tags: string[];
|
|
}
|
|
|
|
class UserService {
|
|
private users: User[] = [];
|
|
|
|
async fetchUsers(): Promise<User[]> {
|
|
// TODO: Connect to API
|
|
const response = await fetch("/api/users");
|
|
const data = await response.json();
|
|
this.users = data;
|
|
return this.users;
|
|
}
|
|
|
|
findUserById(id: number): User | undefined {
|
|
return this.users.find((user) => user.id === id);
|
|
}
|
|
|
|
getActiveUsers(): User[] {
|
|
return this.users.filter((user) => user.isActive);
|
|
}
|
|
|
|
addTagToUser(userId: number, tag: string): void {
|
|
const user = this.findUserById(userId);
|
|
if (user) {
|
|
user.tags.push(tag);
|
|
}
|
|
}
|
|
|
|
removeTagFromUser(userId: number, tag: string): void {
|
|
const user = this.findUserById(userId);
|
|
if (user) {
|
|
user.tags = user.tags.filter((t) => t !== tag);
|
|
}
|
|
}
|
|
|
|
// 试试用 da f 删除整个方法,用 ci f 修改方法体
|
|
getUsersByCity(city: string): User[] {
|
|
return this.users.filter((user) => user.address?.city === city);
|
|
}
|
|
}
|
|
|
|
class NotificationService {
|
|
sendEmail(user: User, subject: string, body: string): void {
|
|
// Send email logic
|
|
console.log(`Sending email to ${user.email}: ${subject}`);
|
|
}
|
|
|
|
notifyActiveUsers(users: User[], message: string): void {
|
|
const activeUsers = users.filter((user) => user.isActive);
|
|
activeUsers.forEach((user) => {
|
|
this.sendEmail(user, "Notification", message);
|
|
});
|
|
}
|
|
}
|
|
|
|
// Main application
|
|
async function main(): Promise<void> {
|
|
const userService = new UserService();
|
|
const notificationService = new NotificationService();
|
|
|
|
const users = await userService.fetchUsers();
|
|
const activeUsers = userService.getActiveUsers();
|
|
|
|
notificationService.notifyActiveUsers(users, "Welcome back!");
|
|
|
|
console.log(`Total users: ${users.length}`);
|
|
console.log(`Active users: ${activeUsers.length}`);
|
|
}
|
|
|
|
main().catch(console.error);
|