@guilhermednobre/agent-kit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +84 -0
- package/bin/cli.js +195 -0
- package/package.json +35 -0
- package/src/detect.js +94 -0
- package/src/install.js +67 -0
- package/src/render.js +27 -0
- package/templates/AGENTS.md +159 -0
- package/templates/CLAUDE.md +330 -0
- package/templates/claude/commands/criar-prd.md +93 -0
- package/templates/claude/commands/criar-tasks.md +98 -0
- package/templates/claude/commands/criar-techspec.md +118 -0
- package/templates/claude/commands/executar-bugfix.md +173 -0
- package/templates/claude/commands/executar-qa.md +179 -0
- package/templates/claude/commands/executar-review.md +241 -0
- package/templates/claude/commands/executar-task.md +121 -0
- package/templates/claude/rules/README.md +64 -0
- package/templates/claude/rules/code-standards.md +307 -0
- package/templates/claude/rules/http.md +375 -0
- package/templates/claude/rules/logging.md +196 -0
- package/templates/claude/rules/node.md +339 -0
- package/templates/claude/rules/react.md +350 -0
- package/templates/claude/rules/tests.md +225 -0
- package/templates/claude/templates/bugs-template.md +80 -0
- package/templates/claude/templates/prd-template.md +61 -0
- package/templates/claude/templates/task-template.md +54 -0
- package/templates/claude/templates/tasks-template.md +27 -0
- package/templates/claude/templates/techspec-template.md +129 -0
- package/templates/claude/templates/worker-brief-template.md +66 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
# Node.js/JavaScript/TypeScript
|
|
2
|
+
|
|
3
|
+
> **Aplicabilidade:** condicional — vale somente se o projeto usar Node.js / TypeScript, decisão que
|
|
4
|
+
> ainda não foi tomada. Até lá, use este arquivo apenas como referência, nunca como
|
|
5
|
+
> justificativa para escolher a stack. Ver `.claude/rules/README.md`.
|
|
6
|
+
|
|
7
|
+
## TypeScript
|
|
8
|
+
|
|
9
|
+
Todo o código-fonte deve ser escrito em TypeScript.
|
|
10
|
+
|
|
11
|
+
**Exemplo:**
|
|
12
|
+
```typescript
|
|
13
|
+
// ✅ Prefira arquivos .ts
|
|
14
|
+
// user.service.ts
|
|
15
|
+
interface User {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
email: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class UserService {
|
|
22
|
+
getUser(id: string): User {
|
|
23
|
+
// implementação
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Gerenciador de Pacotes
|
|
29
|
+
|
|
30
|
+
O gerenciador de pacotes é uma decisão do projeto e ainda não foi tomada. Depois de escolhido, use
|
|
31
|
+
sempre o mesmo em todo o repositório e nunca misture gerenciadores.
|
|
32
|
+
|
|
33
|
+
Os exemplos abaixo usam npm apenas para ilustrar; substitua pelos comandos equivalentes do
|
|
34
|
+
gerenciador que for adotado.
|
|
35
|
+
|
|
36
|
+
**Exemplo:**
|
|
37
|
+
```bash
|
|
38
|
+
# Instalar dependências
|
|
39
|
+
npm install
|
|
40
|
+
|
|
41
|
+
# Adicionar nova dependência
|
|
42
|
+
npm install axios
|
|
43
|
+
|
|
44
|
+
# Adicionar dependência de desenvolvimento
|
|
45
|
+
npm install --save-dev @types/jest
|
|
46
|
+
|
|
47
|
+
# Executar scripts
|
|
48
|
+
npm run dev
|
|
49
|
+
npm test
|
|
50
|
+
npm run build
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Types de Bibliotecas
|
|
54
|
+
|
|
55
|
+
Se necessário, faça a instalação dos types das bibliotecas, por exemplo: `jest` e `@types/jest`.
|
|
56
|
+
|
|
57
|
+
**Exemplo:**
|
|
58
|
+
```bash
|
|
59
|
+
npm install jest --save-dev
|
|
60
|
+
npm install @types/jest --save-dev
|
|
61
|
+
|
|
62
|
+
npm install express
|
|
63
|
+
npm install @types/express --save-dev
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Validação de Tipagem
|
|
67
|
+
|
|
68
|
+
Antes de terminar uma tarefa, sempre valide se a tipagem está correta.
|
|
69
|
+
|
|
70
|
+
**Exemplo:**
|
|
71
|
+
```bash
|
|
72
|
+
# Executar verificação de tipos
|
|
73
|
+
npx tsc --noEmit
|
|
74
|
+
|
|
75
|
+
# Configure no package.json
|
|
76
|
+
{
|
|
77
|
+
"scripts": {
|
|
78
|
+
"type-check": "tsc --noEmit",
|
|
79
|
+
"build": "npm run type-check && tsc"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Declaração de Variáveis
|
|
85
|
+
|
|
86
|
+
Utilize `const` ao invés de `let` onde for possível. Nunca utilize `var` para declarar uma variável.
|
|
87
|
+
|
|
88
|
+
**Exemplo:**
|
|
89
|
+
```typescript
|
|
90
|
+
// ❌ Evite
|
|
91
|
+
var userName = 'John';
|
|
92
|
+
let userAge = 30; // se não vai mudar
|
|
93
|
+
|
|
94
|
+
// ✅ Prefira
|
|
95
|
+
const userName = 'John';
|
|
96
|
+
const userAge = 30;
|
|
97
|
+
let counter = 0; // apenas quando vai mudar
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Propriedades de Classe
|
|
101
|
+
|
|
102
|
+
Sempre declare as propriedades da classe como `private` ou `readonly`, evitando o uso de `public`.
|
|
103
|
+
|
|
104
|
+
**Exemplo:**
|
|
105
|
+
```typescript
|
|
106
|
+
// ❌ Evite
|
|
107
|
+
class UserService {
|
|
108
|
+
public database: Database;
|
|
109
|
+
public config: Config;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ✅ Prefira
|
|
113
|
+
class UserService {
|
|
114
|
+
private readonly database: Database;
|
|
115
|
+
private readonly config: Config;
|
|
116
|
+
|
|
117
|
+
constructor(database: Database, config: Config) {
|
|
118
|
+
this.database = database;
|
|
119
|
+
this.config = config;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// métodos públicos que usam as propriedades privadas
|
|
123
|
+
public getUser(id: string): User {
|
|
124
|
+
return this.database.findUser(id);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Métodos de Array
|
|
130
|
+
|
|
131
|
+
Prefira o uso de `find`, `filter`, `map` e `reduce` ao invés de `for` e `while`.
|
|
132
|
+
|
|
133
|
+
**Exemplo:**
|
|
134
|
+
```typescript
|
|
135
|
+
const users = [
|
|
136
|
+
{ id: 1, name: 'John', age: 30 },
|
|
137
|
+
{ id: 2, name: 'Jane', age: 25 },
|
|
138
|
+
{ id: 3, name: 'Bob', age: 35 }
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
// ❌ Evite
|
|
142
|
+
const result = [];
|
|
143
|
+
for (let i = 0; i < users.length; i++) {
|
|
144
|
+
if (users[i].age > 25) {
|
|
145
|
+
result.push(users[i].name);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ✅ Prefira
|
|
150
|
+
const result = users
|
|
151
|
+
.filter(user => user.age > 25)
|
|
152
|
+
.map(user => user.name);
|
|
153
|
+
|
|
154
|
+
// Find
|
|
155
|
+
const user = users.find(u => u.id === 2);
|
|
156
|
+
|
|
157
|
+
// Reduce
|
|
158
|
+
const totalAge = users.reduce((sum, user) => sum + user.age, 0);
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Promises e Async/Await
|
|
162
|
+
|
|
163
|
+
Sempre utilize `async/await` para lidar com promises. Evite o uso de callbacks.
|
|
164
|
+
|
|
165
|
+
**Exemplo:**
|
|
166
|
+
```typescript
|
|
167
|
+
// ❌ Evite
|
|
168
|
+
function getUser(id: string, callback: (user: User) => void) {
|
|
169
|
+
database.query('SELECT * FROM users WHERE id = ?', [id], (err, result) => {
|
|
170
|
+
if (err) {
|
|
171
|
+
console.error(err);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
callback(result);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ✅ Prefira
|
|
179
|
+
async function getUser(id: string): Promise<User> {
|
|
180
|
+
const result = await database.query('SELECT * FROM users WHERE id = ?', [id]);
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Uso
|
|
185
|
+
try {
|
|
186
|
+
const user = await getUser('123');
|
|
187
|
+
console.log(user);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
console.error(error);
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Tipagem Forte
|
|
194
|
+
|
|
195
|
+
Nunca utilize `any`. Sempre utilize types existentes ou crie types para tudo que for implementado.
|
|
196
|
+
|
|
197
|
+
**Exemplo:**
|
|
198
|
+
```typescript
|
|
199
|
+
// ❌ Evite
|
|
200
|
+
function processData(data: any): any {
|
|
201
|
+
return data.map((item: any) => item.value);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ✅ Prefira
|
|
205
|
+
interface DataItem {
|
|
206
|
+
id: string;
|
|
207
|
+
value: number;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function processData(data: DataItem[]): number[] {
|
|
211
|
+
return data.map(item => item.value);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Para casos desconhecidos, use unknown
|
|
215
|
+
function parseJSON(json: string): unknown {
|
|
216
|
+
return JSON.parse(json);
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Imports e Exports
|
|
221
|
+
|
|
222
|
+
Nunca utilize `require` para importar módulos, sempre utilize `import`. Nunca utilize `module.exports` para exportar módulos, sempre `export`.
|
|
223
|
+
|
|
224
|
+
**Exemplo:**
|
|
225
|
+
```typescript
|
|
226
|
+
// ❌ Evite
|
|
227
|
+
const express = require('express');
|
|
228
|
+
const { UserService } = require('./user.service');
|
|
229
|
+
module.exports = { createUser };
|
|
230
|
+
|
|
231
|
+
// ✅ Prefira
|
|
232
|
+
import express from 'express';
|
|
233
|
+
import { UserService } from './user.service';
|
|
234
|
+
export { createUser };
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Default vs Named Exports
|
|
238
|
+
|
|
239
|
+
Se o arquivo tiver apenas uma coisa sendo exportada, utilize `default`, senão named exports.
|
|
240
|
+
|
|
241
|
+
**Exemplo:**
|
|
242
|
+
```typescript
|
|
243
|
+
// user.service.ts - apenas uma classe
|
|
244
|
+
export default class UserService {
|
|
245
|
+
// ...
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// user.utils.ts - múltiplas funções
|
|
249
|
+
export function validateEmail(email: string): boolean {
|
|
250
|
+
// ...
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function formatName(name: string): string {
|
|
254
|
+
// ...
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function calculateAge(birthDate: Date): number {
|
|
258
|
+
// ...
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Dependência Circular
|
|
263
|
+
|
|
264
|
+
Evite dependência circular.
|
|
265
|
+
|
|
266
|
+
**Exemplo:**
|
|
267
|
+
```typescript
|
|
268
|
+
// ❌ Evite
|
|
269
|
+
// user.service.ts
|
|
270
|
+
import { OrderService } from './order.service';
|
|
271
|
+
|
|
272
|
+
export class UserService {
|
|
273
|
+
constructor(private orderService: OrderService) {}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// order.service.ts
|
|
277
|
+
import { UserService } from './user.service';
|
|
278
|
+
|
|
279
|
+
export class OrderService {
|
|
280
|
+
constructor(private userService: UserService) {}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ✅ Prefira
|
|
284
|
+
// user.service.ts
|
|
285
|
+
export class UserService {
|
|
286
|
+
getUser(id: string): User { }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// order.service.ts
|
|
290
|
+
export class OrderService {
|
|
291
|
+
getOrders(userId: string): Order[] { }
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// user-order.service.ts
|
|
295
|
+
import { UserService } from './user.service';
|
|
296
|
+
import { OrderService } from './order.service';
|
|
297
|
+
|
|
298
|
+
export class UserOrderService {
|
|
299
|
+
constructor(
|
|
300
|
+
private userService: UserService,
|
|
301
|
+
private orderService: OrderService
|
|
302
|
+
) {}
|
|
303
|
+
|
|
304
|
+
getUserWithOrders(userId: string) {
|
|
305
|
+
const user = this.userService.getUser(userId);
|
|
306
|
+
const orders = this.orderService.getOrders(userId);
|
|
307
|
+
return { ...user, orders };
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## Tipos Utilitários
|
|
313
|
+
|
|
314
|
+
Utilize os tipos utilitários do TypeScript quando apropriado.
|
|
315
|
+
|
|
316
|
+
**Exemplo:**
|
|
317
|
+
```typescript
|
|
318
|
+
interface User {
|
|
319
|
+
id: string;
|
|
320
|
+
name: string;
|
|
321
|
+
email: string;
|
|
322
|
+
password: string;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Partial - todos os campos opcionais
|
|
326
|
+
type UserUpdate = Partial<User>;
|
|
327
|
+
|
|
328
|
+
// Pick - selecionar campos específicos
|
|
329
|
+
type UserPublic = Pick<User, 'id' | 'name' | 'email'>;
|
|
330
|
+
|
|
331
|
+
// Omit - excluir campos específicos
|
|
332
|
+
type UserCreate = Omit<User, 'id'>;
|
|
333
|
+
|
|
334
|
+
// Readonly - tornar imutável
|
|
335
|
+
type UserReadonly = Readonly<User>;
|
|
336
|
+
|
|
337
|
+
// Record - criar objeto com chaves específicas
|
|
338
|
+
type UserMap = Record<string, User>;
|
|
339
|
+
```
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
# React
|
|
2
|
+
|
|
3
|
+
> **Aplicabilidade:** condicional — vale somente se o projeto usar React, decisão que
|
|
4
|
+
> ainda não foi tomada. Até lá, use este arquivo apenas como referência, nunca como
|
|
5
|
+
> justificativa para escolher a stack. Ver `.claude/rules/README.md`.
|
|
6
|
+
|
|
7
|
+
## Componentes Funcionais
|
|
8
|
+
|
|
9
|
+
Utilize componentes funcionais, nunca classes.
|
|
10
|
+
|
|
11
|
+
**Exemplo:**
|
|
12
|
+
```typescript
|
|
13
|
+
// ❌ Evite
|
|
14
|
+
class UserProfile extends React.Component {
|
|
15
|
+
render() {
|
|
16
|
+
return <div>{this.props.name}</div>;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ✅ Prefira
|
|
21
|
+
function UserProfile({ name }: { name: string }) {
|
|
22
|
+
return <div>{name}</div>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Ou com arrow function
|
|
26
|
+
const UserProfile = ({ name }: { name: string }) => {
|
|
27
|
+
return <div>{name}</div>;
|
|
28
|
+
};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## TypeScript
|
|
32
|
+
|
|
33
|
+
Utilize TypeScript e a extensão `.tsx` para os componentes.
|
|
34
|
+
|
|
35
|
+
**Exemplo:**
|
|
36
|
+
```typescript
|
|
37
|
+
// UserCard.tsx
|
|
38
|
+
interface UserCardProps {
|
|
39
|
+
name: string;
|
|
40
|
+
email: string;
|
|
41
|
+
avatar?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function UserCard({ name, email, avatar }: UserCardProps) {
|
|
45
|
+
return (
|
|
46
|
+
<div className="user-card">
|
|
47
|
+
{avatar && <img src={avatar} alt={name} />}
|
|
48
|
+
<h3>{name}</h3>
|
|
49
|
+
<p>{email}</p>
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Estado Local
|
|
56
|
+
|
|
57
|
+
Mantenha o estado do componente o mais próximo possível de onde ele será usado.
|
|
58
|
+
|
|
59
|
+
**Exemplo:**
|
|
60
|
+
```typescript
|
|
61
|
+
// ❌ Evite - estado no componente pai quando só é usado no filho
|
|
62
|
+
function ParentComponent() {
|
|
63
|
+
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div>
|
|
67
|
+
<UserProfile />
|
|
68
|
+
<Settings isModalOpen={isModalOpen} setIsModalOpen={setIsModalOpen} />
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ✅ Prefira - estado no componente que realmente usa
|
|
74
|
+
function ParentComponent() {
|
|
75
|
+
return (
|
|
76
|
+
<div>
|
|
77
|
+
<UserProfile />
|
|
78
|
+
<Settings />
|
|
79
|
+
</div>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function Settings() {
|
|
84
|
+
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
85
|
+
// usa o estado aqui
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Passagem de Props
|
|
90
|
+
|
|
91
|
+
Passe propriedades de forma explícita entre componentes. Evite spread operator como `<ComponentName {...props} />`.
|
|
92
|
+
|
|
93
|
+
**Exemplo:**
|
|
94
|
+
```typescript
|
|
95
|
+
// ❌ Evite
|
|
96
|
+
function UserProfile(props: any) {
|
|
97
|
+
return <UserCard {...props} />;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ✅ Prefira
|
|
101
|
+
interface UserProfileProps {
|
|
102
|
+
name: string;
|
|
103
|
+
email: string;
|
|
104
|
+
avatar: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function UserProfile({ name, email, avatar }: UserProfileProps) {
|
|
108
|
+
return <UserCard name={name} email={email} avatar={avatar} />;
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Tamanho dos Componentes
|
|
113
|
+
|
|
114
|
+
Evite componentes muito grandes, acima de 300 linhas.
|
|
115
|
+
|
|
116
|
+
**Exemplo:**
|
|
117
|
+
```typescript
|
|
118
|
+
// ❌ Evite - componente monolítico
|
|
119
|
+
function Dashboard() {
|
|
120
|
+
// 400 linhas de código
|
|
121
|
+
return (
|
|
122
|
+
<div>
|
|
123
|
+
{/* header */}
|
|
124
|
+
{/* sidebar */}
|
|
125
|
+
{/* content */}
|
|
126
|
+
{/* footer */}
|
|
127
|
+
</div>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ✅ Prefira - dividir em componentes menores
|
|
132
|
+
function Dashboard() {
|
|
133
|
+
return (
|
|
134
|
+
<div>
|
|
135
|
+
<DashboardHeader />
|
|
136
|
+
<DashboardSidebar />
|
|
137
|
+
<DashboardContent />
|
|
138
|
+
<DashboardFooter />
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Context API
|
|
145
|
+
|
|
146
|
+
Utilize Context API quando precisar se comunicar entre diferentes componentes filhos.
|
|
147
|
+
|
|
148
|
+
**Exemplo:**
|
|
149
|
+
```typescript
|
|
150
|
+
// AuthContext.tsx
|
|
151
|
+
interface AuthContextType {
|
|
152
|
+
user: User | null;
|
|
153
|
+
login: (email: string, password: string) => Promise<void>;
|
|
154
|
+
logout: () => void;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
158
|
+
|
|
159
|
+
export function AuthProvider({ children }: { children: ReactNode }) {
|
|
160
|
+
const [user, setUser] = useState<User | null>(null);
|
|
161
|
+
|
|
162
|
+
const login = async (email: string, password: string) => {
|
|
163
|
+
const user = await authService.login(email, password);
|
|
164
|
+
setUser(user);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const logout = () => setUser(null);
|
|
168
|
+
|
|
169
|
+
return (
|
|
170
|
+
<AuthContext.Provider value={{ user, login, logout }}>
|
|
171
|
+
{children}
|
|
172
|
+
</AuthContext.Provider>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function useAuth() {
|
|
177
|
+
const context = useContext(AuthContext);
|
|
178
|
+
if (!context) throw new Error('useAuth must be used within AuthProvider');
|
|
179
|
+
return context;
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Estilização
|
|
184
|
+
|
|
185
|
+
Utilize Tailwind para fazer a estilização dos componentes. Não utilize styled-components.
|
|
186
|
+
|
|
187
|
+
**Exemplo:**
|
|
188
|
+
```typescript
|
|
189
|
+
// ✅ Prefira
|
|
190
|
+
function Button({ children, variant = 'primary' }: ButtonProps) {
|
|
191
|
+
const baseClasses = 'px-4 py-2 rounded font-medium transition-colors';
|
|
192
|
+
const variantClasses = {
|
|
193
|
+
primary: 'bg-blue-500 text-white hover:bg-blue-600',
|
|
194
|
+
secondary: 'bg-gray-200 text-gray-800 hover:bg-gray-300'
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
return (
|
|
198
|
+
<button className={`${baseClasses} ${variantClasses[variant]}`}>
|
|
199
|
+
{children}
|
|
200
|
+
</button>
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Granularidade de Componentes
|
|
206
|
+
|
|
207
|
+
Evite o excesso de componentes pequenos.
|
|
208
|
+
|
|
209
|
+
**Exemplo:**
|
|
210
|
+
```typescript
|
|
211
|
+
// ❌ Evite - over-engineering
|
|
212
|
+
function UserName({ name }: { name: string }) {
|
|
213
|
+
return <span>{name}</span>;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function UserEmail({ email }: { email: string }) {
|
|
217
|
+
return <span>{email}</span>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function UserCard({ name, email }: UserCardProps) {
|
|
221
|
+
return (
|
|
222
|
+
<div>
|
|
223
|
+
<UserName name={name} />
|
|
224
|
+
<UserEmail email={email} />
|
|
225
|
+
</div>
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ✅ Prefira - simplicidade
|
|
230
|
+
function UserCard({ name, email }: UserCardProps) {
|
|
231
|
+
return (
|
|
232
|
+
<div>
|
|
233
|
+
<span>{name}</span>
|
|
234
|
+
<span>{email}</span>
|
|
235
|
+
</div>
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Performance com useMemo
|
|
241
|
+
|
|
242
|
+
Utilize o hook `useMemo` para evitar o excesso de cálculos e interações desnecessárias entre renderizações.
|
|
243
|
+
|
|
244
|
+
**Exemplo:**
|
|
245
|
+
```typescript
|
|
246
|
+
function ProductList({ products, filter }: ProductListProps) {
|
|
247
|
+
// ✅ Memoriza o resultado do filtro
|
|
248
|
+
const filteredProducts = useMemo(() => {
|
|
249
|
+
return products.filter(product =>
|
|
250
|
+
product.name.toLowerCase().includes(filter.toLowerCase())
|
|
251
|
+
);
|
|
252
|
+
}, [products, filter]);
|
|
253
|
+
|
|
254
|
+
return (
|
|
255
|
+
<div>
|
|
256
|
+
{filteredProducts.map(product => (
|
|
257
|
+
<ProductCard key={product.id} product={product} />
|
|
258
|
+
))}
|
|
259
|
+
</div>
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Nomenclatura de Hooks
|
|
265
|
+
|
|
266
|
+
Nomeie os hooks customizados com o prefixo "use", por exemplo: `useAuth`, `useLocalStorage`, `useUrl`.
|
|
267
|
+
|
|
268
|
+
**Exemplo:**
|
|
269
|
+
```typescript
|
|
270
|
+
// useLocalStorage.ts
|
|
271
|
+
export function useLocalStorage<T>(key: string, initialValue: T) {
|
|
272
|
+
const [storedValue, setStoredValue] = useState<T>(() => {
|
|
273
|
+
try {
|
|
274
|
+
const item = window.localStorage.getItem(key);
|
|
275
|
+
return item ? JSON.parse(item) : initialValue;
|
|
276
|
+
} catch (error) {
|
|
277
|
+
return initialValue;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
const setValue = (value: T) => {
|
|
282
|
+
try {
|
|
283
|
+
setStoredValue(value);
|
|
284
|
+
window.localStorage.setItem(key, JSON.stringify(value));
|
|
285
|
+
} catch (error) {
|
|
286
|
+
console.error(error);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
return [storedValue, setValue] as const;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Uso
|
|
294
|
+
function MyComponent() {
|
|
295
|
+
const [theme, setTheme] = useLocalStorage('theme', 'light');
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## Bibliotecas de Componentes
|
|
300
|
+
|
|
301
|
+
Antes de criar um componente novo complexo, pergunte antes se deve buscar uma biblioteca existente.
|
|
302
|
+
|
|
303
|
+
**Exemplo:**
|
|
304
|
+
```typescript
|
|
305
|
+
// Para componentes complexos como date pickers, data tables, etc
|
|
306
|
+
// Considere bibliotecas como:
|
|
307
|
+
// - shadcn/ui
|
|
308
|
+
// - Radix UI
|
|
309
|
+
// - Headless UI
|
|
310
|
+
// - React Hook Form (formulários)
|
|
311
|
+
// - React Query (data fetching)
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
## Testes
|
|
315
|
+
|
|
316
|
+
Crie testes automatizados para todos os componentes.
|
|
317
|
+
|
|
318
|
+
**Exemplo:**
|
|
319
|
+
```typescript
|
|
320
|
+
// UserCard.test.tsx
|
|
321
|
+
import { render, screen } from '@testing-library/react';
|
|
322
|
+
import { UserCard } from './UserCard';
|
|
323
|
+
|
|
324
|
+
describe('UserCard', () => {
|
|
325
|
+
it('should render user information', () => {
|
|
326
|
+
render(
|
|
327
|
+
<UserCard
|
|
328
|
+
name="John Doe"
|
|
329
|
+
email="john@example.com"
|
|
330
|
+
/>
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
expect(screen.getByText('John Doe')).toBeInTheDocument();
|
|
334
|
+
expect(screen.getByText('john@example.com')).toBeInTheDocument();
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('should render avatar when provided', () => {
|
|
338
|
+
render(
|
|
339
|
+
<UserCard
|
|
340
|
+
name="John Doe"
|
|
341
|
+
email="john@example.com"
|
|
342
|
+
avatar="https://example.com/avatar.jpg"
|
|
343
|
+
/>
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
const avatar = screen.getByRole('img', { name: 'John Doe' });
|
|
347
|
+
expect(avatar).toBeInTheDocument();
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
```
|