-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.cursorrules
More file actions
576 lines (446 loc) · 14.7 KB
/
.cursorrules
File metadata and controls
576 lines (446 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# Angular 20 VibeCoding Project - Cursor Rules
## Project Overview
This is a modern Angular 20 application using standalone components, signals, TailwindCSS, and DaisyUI. The project is optimized for AI-assisted development and follows strict best practices.
## Core Technologies
- **Angular 20+** with standalone architecture (no NgModules)
- **TypeScript** with strict type checking
- **Angular Signals** for reactive state management
- **TailwindCSS 4** + **DaisyUI** for styling
- **SCSS** as the primary styling engine
- **RxJS** for complex asynchronous operations only
- **Angular Material** for additional UI components
## Critical Naming Conventions
⚠️ **IMPORTANT**: This project does NOT use standard Angular file suffixes
- ✅ Use: `login.ts`, `auth.ts`, `notification.ts`
- ❌ Never use: `login.component.ts`, `auth.service.ts`, `notification.service.ts`
- Components, services, guards, and pipes all use simple `.ts` extension
- Each feature has its own folder with `.ts`, `.html`, `.scss`, and `.spec.ts` files
## File Structure Pattern
```
feature-name/
├── feature-name.ts # Component or service
├── feature-name.html # Template (if component)
├── feature-name.scss # Styles (if component)
└── feature-name.spec.ts # Tests
```
## TypeScript Best Practices
### Always Use Strict Typing
- ❌ NEVER use `any` type
- ✅ Define proper interfaces for all data structures
- ✅ Use type inference where obvious
- ✅ Use `unknown` for uncertain types (rare cases)
```typescript
// ✅ Good
interface LoginCredentials {
email: string;
password: string;
}
// ❌ Bad
const credentials: any = { email: "", password: "" };
```
## Angular Component Best Practices
### Component Structure
```typescript
import {
Component,
signal,
computed,
inject,
input,
output,
} from "@angular/core";
import { ChangeDetectionStrategy } from "@angular/core";
@Component({
selector: "app-example",
templateUrl: "./example.html",
styleUrls: ["./example.scss"],
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [
/* standalone imports */
],
})
export class Example {
// Use inject() instead of constructor injection
private router = inject(Router);
private authService = inject(Auth);
// Use input() and output() functions instead of decorators
title = input<string>("Default Title");
users = input<User[]>([]);
formSubmitted = output<FormData>();
// Use signals for local state
isLoading = signal(false);
isActive = signal(false);
// Use computed() for derived state
isFormValid = computed(() => !this.nameError() && !this.emailError());
backgroundColor = computed(() => (this.isActive() ? "#e3f2fd" : "#ffffff"));
}
```
### Key Component Rules
1. ✅ All components use `ChangeDetectionStrategy.OnPush`
2. ✅ Use `inject()` function instead of constructor injection
3. ✅ Use `input()` and `output()` functions instead of `@Input()` and `@Output()` decorators
4. ✅ Use signals for all local state
5. ✅ Use `computed()` for all derived state
6. ✅ Keep components small and focused (single responsibility)
7. ✅ All components are standalone (no NgModules)
## Template Best Practices
### Use Native Control Flow (NOT *ngIf, *ngFor, \*ngSwitch)
```html
<!-- ✅ Good: Use native control flow -->
@if (isVisible()) {
<h2>{{ title() }}</h2>
} @for (user of users(); track user.id) {
<div class="user-item">
<span>{{ user.name }}</span>
</div>
} @switch (status()) { @case ('loading') {
<div class="loading">Loading...</div>
} @case ('success') {
<div class="success">Success!</div>
} @default {
<div class="default">Default state</div>
} }
<!-- ❌ Bad: Don't use old syntax -->
<div *ngIf="isVisible()">...</div>
<div *ngFor="let user of users()">...</div>
```
### Use Class and Style Bindings (NOT ngClass/ngStyle)
```html
<!-- ✅ Good: Use direct bindings -->
<div [class.active]="isActive()" [class.disabled]="isDisabled()">
Dynamic classes
</div>
<div [style.background-color]="backgroundColor()" [style.color]="textColor()">
Dynamic styles
</div>
<!-- ❌ Bad: Don't use ngClass/ngStyle -->
<div [ngClass]="{'active': isActive()}">...</div>
<div [ngStyle]="{'background-color': backgroundColor()}">...</div>
```
### Always Use Async Pipe for Observables
```html
<!-- ✅ Good -->
@if (user$ | async; as user) {
<div>{{ user.name }}</div>
}
<!-- ❌ Bad: Don't manually subscribe in templates -->
```
## Service Best Practices
### Service Structure
```typescript
import { Injectable, inject, signal } from "@angular/core";
@Injectable({
providedIn: "root", // Always use root provider
})
export class Auth {
// Use inject() instead of constructor injection
private router = inject(Router);
private notification = inject(NotificationService);
// Use signals for service state
currentUser = signal<User | null>(null);
isAuthenticated = computed(() => this.currentUser() !== null);
// Service methods...
}
```
### Key Service Rules
1. ✅ All services use `providedIn: 'root'`
2. ✅ Use `inject()` function instead of constructor injection
3. ✅ Single responsibility principle
4. ✅ Use signals for state management
## State Management
### Use Signals for State
```typescript
// ✅ Local component state
isSubmitting = signal(false);
errorMessage = signal("");
// ✅ Computed state
isFormValid = computed(
() => this.email().length > 0 && this.password().length > 0
);
// ✅ Effects for side effects
effect(() => {
if (this.isAuthenticated()) {
this.router.navigate(["/dashboard"]);
}
});
```
### State Rules
1. ✅ Use signals for all local state
2. ✅ Use computed() for all derived state
3. ✅ Use effect() for side effects
4. ✅ Keep state transformations pure and predictable
5. ✅ Use RxJS only for complex asynchronous operations
## Styling Best Practices
### Use TailwindCSS + DaisyUI in Templates
```html
<!-- ✅ Use Tailwind and DaisyUI classes directly -->
<div class="hero min-h-screen bg-base-200">
<button class="btn btn-primary">Click me</button>
</div>
```
### SCSS for Component-Specific Styles
```scss
// Use SCSS for complex component-specific styling
.custom-component {
@apply flex items-center gap-4;
&__title {
@apply text-2xl font-bold;
}
}
```
## Forms Best Practices
### Use Reactive Forms with Signals
```typescript
import { FormControl, FormGroup, Validators } from "@angular/forms";
// ✅ Reactive forms
loginForm = new FormGroup({
email: new FormControl("", [Validators.required, Validators.email]),
password: new FormControl("", [Validators.required, Validators.minLength(6)]),
});
// Use signals for form state
isSubmitting = signal(false);
```
## Project Structure
```
src/app/
├── auth/ # Authentication feature
│ ├── guards/ # Route guards (auth-guard.ts)
│ ├── services/ # Auth services (auth.ts)
│ ├── login/ # Login component
│ ├── register/ # Register component
│ └── user-profile/ # User profile component
├── core/ # Core application components
│ ├── layout/ # Main layout
│ ├── header/ # Application header
│ ├── footer/ # Application footer
│ ├── sidenav/ # Navigation sidebar
│ └── home/ # Home page
└── shared/ # Shared components and services
├── services/ # Shared services
├── notification/ # Notification components
└── spinner/ # Loading spinner
```
## Migration Checklist for New Components
When creating new components or services, ensure:
- [ ] Use `inject()` instead of constructor injection
- [ ] Set `changeDetection: ChangeDetectionStrategy.OnPush`
- [ ] Use signals for local state
- [ ] Use `computed()` for derived state
- [ ] Use `input()` and `output()` functions
- [ ] Use native control flow (`@if`, `@for`, `@switch`)
- [ ] Use class and style bindings instead of `ngClass`/`ngStyle`
- [ ] Define proper TypeScript interfaces
- [ ] Avoid `any` types
- [ ] Use reactive forms with signals
- [ ] Keep components small and focused
- [ ] Use `providedIn: 'root'` for services
- [ ] No `.component.ts` or `.service.ts` suffixes
## Performance Optimizations
- ✅ OnPush change detection on all components
- ✅ Signal-based reactivity
- ✅ Lazy loading for feature routes
- ✅ Track functions in @for loops
- ✅ @defer for lazy loading templates
## Error Handling
- ✅ Proper error handling in async operations
- ✅ User-friendly error messages via notification service
- ✅ Type-safe error interfaces
## Code Generation Commands
```bash
# Generate new component (remember: no .component.ts suffix!)
ng generate component path/component-name
# Generate new service (remember: no .service.ts suffix!)
ng generate service path/service-name
# Generate new guard
ng generate guard path/guard-name
# Clean the template (remove demo/example content)
npm run clean-template
```
## Clean Template Script - Two Modes
This project includes scripts to remove demo content with TWO modes:
### Dashboard Mode (Recommended)
```bash
npm run clean-template:dashboard
```
**Perfect for:** Dashboards, admin panels, full-featured apps
**Keeps:**
- Complete layout structure (header, footer, sidenav, layout)
- Dashboard-style welcome page
- Collapsible navigation
- Professional structure
### Blank Mode (Minimal)
```bash
npm run clean-template:blank
```
**Perfect for:** Custom designs, landing pages, absolute scratch
**Removes:**
- ALL layout components (header, footer, sidenav, layout)
- Gives minimal single-page starter
### Interactive Selection
```bash
npm run clean-template
```
Prompts you to choose between modes.
**Both modes remove:**
- Authentication pages and services
- Demo data pages
- Example components
**Both modes keep:**
- Shared utilities (notification, spinner, services)
- All configuration files
- TailwindCSS and DaisyUI setup
- Documentation and .cursorrules
- Best practices structure
📖 See [docs/clean-template-modes.md](docs/clean-template-modes.md) for detailed comparison.
## Additional Context
- This project is designed for AI-assisted development
- Code should be clean, scalable, and maintainable
- Follow the example component at `src/app/shared/example-component/example-component.ts`
- Refer to `docs/best-practices.md` for detailed examples
- Use `docs/setup.md` for AI context setup guidelines
## AI/LLM Integration Patterns
When building AI-powered features with Angular, follow these patterns from [Angular.dev AI Design Patterns](https://angular.dev/ai/design-patterns):
### Triggering AI Requests with Signals
**Pattern: Separate input from submission**
```typescript
// Store user's raw input
userInput = signal('');
// Store submitted value that triggers API
submittedPrompt = signal('');
// Resource triggered only on submission
aiResource = resource({
params: () => this.submittedPrompt(),
loader: async ({params}) => {
return await this.aiService.generateContent(params);
}
});
// On submit button click
onSubmit() {
this.submittedPrompt.set(this.userInput());
}
```
### Managing LLM Response Data
**Pattern: Use linkedSignal for accumulating responses**
```typescript
// Accumulate chat history or streaming responses
chatHistory = linkedSignal<Message[], Message[]>({
source: () => this.aiResource.value().messages,
computation: (newMessages, previous) => {
const existing = previous?.value || [];
return [...existing, ...newMessages];
},
});
```
### Streaming AI Responses
**Pattern: Use resource stream for real-time updates**
```typescript
streamingResponse = resource({
stream: async () => {
const data = signal<ResourceStreamItem<string>>({ value: "" });
const response = await this.aiService.streamContent(prompt);
(async () => {
for await (const chunk of response.stream) {
data.update((prev) => {
if ("value" in prev) {
return { value: `${prev.value}${chunk}` };
}
return prev;
});
}
})();
return data;
},
});
```
### AI-Friendly Templates
```html
<!-- Loading state -->
@if (aiResource.isLoading()) {
<div class="loading">
<span class="loading loading-spinner"></span>
<p>AI is thinking...</p>
</div>
}
<!-- Success state with streaming content -->
@else if (aiResource.hasValue()) {
<div class="ai-response">{{ aiResource.value() }}</div>
}
<!-- Error state with retry -->
@else if (aiResource.error()) {
<div class="alert alert-error">
<p>{{ aiResource.error() }}</p>
<button class="btn btn-primary" (click)="aiResource.reload()">Retry</button>
</div>
}
```
### Performance Best Practices for AI Features
1. **Scoped Resources**: Place AI resources in components that directly use the data
2. **SSR with Hydration**: Show placeholders, defer AI content until hydration
3. **Loading Indicators**: Always show loading state for LLM operations
4. **Error Handling**: Provide retry mechanisms (use `resource.reload()`)
5. **Zoneless Mode**: Use signals to avoid unnecessary change detection
### Type Safety for AI Responses
```typescript
// Define strong types for LLM responses
interface AIStoryResponse {
storyParts: string[];
metadata: {
tokensUsed: number;
model: string;
};
}
// Typed resource
storyResource = resource<AIStoryResponse>({
params: () => this.storyPrompt(),
loader: async ({ params }) => {
return await this.aiService.generateStory(params);
},
});
```
### AI Service Pattern
```typescript
@Injectable({
providedIn: "root",
})
export class AIService {
private http = inject(HttpClient);
// Session management with signals
sessionId = signal<string | null>(null);
generateContent(prompt: string) {
return this.http.post<AIResponse>("/api/ai/generate", {
prompt,
sessionId: this.sessionId(),
});
}
async *streamContent(prompt: string) {
// Streaming implementation
const response = await fetch("/api/ai/stream", {
method: "POST",
body: JSON.stringify({ prompt }),
});
const reader = response.body?.getReader();
if (!reader) return;
while (true) {
const { done, value } = await reader.read();
if (done) break;
yield new TextDecoder().decode(value);
}
}
}
```
## Remember
- 🚫 NO `.component.ts` or `.service.ts` suffixes
- 🚫 NO `*ngIf`, `*ngFor`, or `*ngSwitch`
- 🚫 NO `ngClass` or `ngStyle`
- 🚫 NO `any` types
- 🚫 NO constructor injection
- 🚫 NO `@Input()` or `@Output()` decorators
- ✅ USE signals, computed, and effect
- ✅ USE inject() function
- ✅ USE input() and output() functions
- ✅ USE native control flow (@if, @for, @switch)
- ✅ USE OnPush change detection
- ✅ USE strict TypeScript
- ✅ USE resource API for AI/async operations
- ✅ USE linkedSignal for accumulating AI responses
- ✅ USE proper loading/error states for AI features