Skip to content

Commit 9afe1f5

Browse files
committed
refactor: enhance type safety and improve code clarity in AI analysis and viewport components
1 parent 9840e2a commit 9afe1f5

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

src/lib/components/panels/Viewport.svelte

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
<script>
1+
<script lang="ts">
22
import { onMount } from 'svelte';
33
import { selectedPetStore, petHelpers, selectedPetHelpers } from '$lib/stores/pets';
44
import { aiAnalysisHelpers, isAnalyzing } from '$lib/stores/ai-analysis';
55
import { PenTool, Brain, Calendar, Heart, Activity } from 'lucide-svelte';
66
import AIInsightsCard from '../ui/AIInsightsCard.svelte';
7+
import type { PetPanelData } from '$lib/types/Pet';
8+
import type { JournalEntry } from '$lib/types/JournalEntry';
79
8-
let selectedPet = null;
10+
let selectedPet: PetPanelData | null = null;
911
let currentView = 'dashboard'; // dashboard, journal, history
1012
let journalInput = '';
1113
let selectedMood = '';
@@ -28,13 +30,17 @@
2830
isSubmitting = true;
2931
3032
try {
31-
const entry = {
33+
const now = new Date();
34+
const entry: JournalEntry = {
3235
id: Date.now().toString(),
3336
petId: selectedPet.id,
37+
type: 'general',
38+
title: `Journal Entry - ${now.toLocaleDateString()}`,
3439
content: journalInput.trim(),
35-
date: new Date().toISOString(),
36-
mood: selectedMood || 'unknown',
37-
activityLevel: selectedActivity || 'normal',
40+
date: now,
41+
mood: (selectedMood as JournalEntry['mood']) || 'neutral',
42+
createdAt: now,
43+
updatedAt: now,
3844
};
3945
4046
// Add entry to pet

src/lib/components/ui/AIInsightsCard.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
<script>
1+
<script lang="ts">
22
import { Brain, AlertTriangle, CheckCircle, TrendingUp } from 'lucide-svelte';
33
import { aiAnalysisHelpers } from '$lib/stores/ai-analysis';
4+
import type { AnalysisResult } from '$lib/utils/ai-analysis';
45
56
export let entryId = '';
6-
export const petId = '';
7+
// eslint-disable-next-line svelte/valid-compile
8+
export let petId = '';
79
export let compact = false;
810
9-
let analysis = null;
11+
let analysis: AnalysisResult | null = null;
1012
1113
$: if (entryId) {
1214
analysis = aiAnalysisHelpers.getAnalysis(entryId);

src/lib/stores/ai-analysis.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { writable } from 'svelte/store';
22
import { AIAnalyzer, type AnalysisResult } from '$lib/utils/ai-analysis';
33
import { guardianStore } from './guardian';
4-
import type { Pet } from '../types/Pet';
4+
import type { PetPanelData } from '../types/Pet';
55
import type { JournalEntry } from '../types/JournalEntry';
66

77
export const analysisStore = writable<Record<string, AnalysisResult>>({});
@@ -19,7 +19,7 @@ guardianStore.subscribe((guardian) => {
1919
});
2020

2121
export const aiAnalysisHelpers = {
22-
async analyzeEntry(pet: Pet, entry: JournalEntry): Promise<AnalysisResult | null> {
22+
async analyzeEntry(pet: PetPanelData, entry: JournalEntry): Promise<AnalysisResult | null> {
2323
if (!analyzer) {
2424
throw new Error('API key not configured');
2525
}

src/lib/utils/ai-analysis.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Pet } from '../types/Pet.js';
1+
import type { PetPanelData } from '../types/Pet.js';
22
import type { JournalEntry } from '../types/JournalEntry.js';
33

44
export interface AnalysisResult {
@@ -18,7 +18,7 @@ export class AIAnalyzer {
1818
this.apiKey = apiKey;
1919
}
2020

21-
async analyzeJournalEntry(pet: Pet, entry: JournalEntry): Promise<AnalysisResult> {
21+
async analyzeJournalEntry(pet: PetPanelData, entry: JournalEntry): Promise<AnalysisResult> {
2222
const prompt = this.buildAnalysisPrompt(pet, entry);
2323

2424
try {
@@ -60,24 +60,24 @@ export class AIAnalyzer {
6060
}
6161
}
6262

63-
private buildAnalysisPrompt(pet: Pet, entry: JournalEntry): string {
63+
private buildAnalysisPrompt(pet: PetPanelData, entry: JournalEntry): string {
6464
const recentEntries =
6565
pet.journalEntries
6666
?.slice(-5)
67-
.map((e: JournalEntry) => `${e.date}: ${e.content}`)
67+
.map((e) => `${e.date}: ${e.content}`)
6868
.join('\n') || 'No previous entries available';
6969

7070
return `
71-
Analyze this journal entry for ${pet.name}, a ${pet.age || 'unknown age'}-year-old ${pet.breed || pet.species}:
71+
Analyze this journal entry for ${pet.name}, a ${pet.age || 'unknown age'}-year-old ${pet.breed}:
7272
7373
LATEST ENTRY: "${entry.content}"
7474
7575
RECENT HISTORY:
7676
${recentEntries}
7777
7878
PET INFO:
79-
- Breed: ${pet.breed || pet.species}
80-
- Age: ${pet.age || 'unknown'} years
79+
- Breed: ${pet.breed}
80+
- Age: ${pet.age || 'unknown'} years
8181
- Gender: ${pet.gender || 'unknown'}
8282
8383
Please provide analysis in this JSON format:

0 commit comments

Comments
 (0)