128 lines
3.1 KiB
TypeScript
128 lines
3.1 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { api } from '@/services/api'
|
|
|
|
export interface Topic {
|
|
id: string
|
|
name: string
|
|
description: string
|
|
category: string
|
|
}
|
|
|
|
export interface SourceReference {
|
|
type?: 'TEXT' | 'FIGURE'
|
|
refLabel?: string
|
|
bookId: string | null
|
|
bookTitle: string
|
|
page: number | null
|
|
chunkText?: string
|
|
figureId?: string
|
|
label?: string
|
|
caption?: string
|
|
figureType?: string
|
|
imageUrl?: string
|
|
}
|
|
|
|
export interface TopicSummary {
|
|
id: string
|
|
summaryNumber: number
|
|
topicId: string
|
|
topicName: string
|
|
summary: string
|
|
sources: SourceReference[]
|
|
generatedAt: string
|
|
}
|
|
|
|
export interface SavedSummaryItem {
|
|
id: string
|
|
summaryNumber: number
|
|
generatedAt: string
|
|
}
|
|
|
|
export const useTopicStore = defineStore('topics', () => {
|
|
const topics = ref<Topic[]>([])
|
|
const activeSummary = ref<TopicSummary | null>(null)
|
|
const activeSummaryTopicId = ref<string | null>(null)
|
|
const summaryList = ref<SavedSummaryItem[]>([])
|
|
const loading = ref(false)
|
|
const summaryLoading = ref(false)
|
|
const summaryListLoading = ref(false)
|
|
const error = ref<string | null>(null)
|
|
|
|
async function fetchTopics() {
|
|
loading.value = true
|
|
error.value = null
|
|
try {
|
|
const response = await api.get<Topic[]>('/topics')
|
|
topics.value = response.data
|
|
} catch (err: any) {
|
|
error.value = err.message
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function fetchSummaries(topicId: string) {
|
|
summaryListLoading.value = true
|
|
summaryList.value = []
|
|
error.value = null
|
|
try {
|
|
const response = await api.get<SavedSummaryItem[]>(`/topics/${topicId}/summaries`)
|
|
summaryList.value = response.data
|
|
} catch (err: any) {
|
|
error.value = err.message
|
|
} finally {
|
|
summaryListLoading.value = false
|
|
}
|
|
}
|
|
|
|
async function fetchSummaryDetail(topicId: string, summaryId: string): Promise<TopicSummary | null> {
|
|
summaryLoading.value = true
|
|
activeSummary.value = null
|
|
error.value = null
|
|
try {
|
|
const response = await api.get<TopicSummary>(`/topics/${topicId}/summaries/${summaryId}`)
|
|
activeSummary.value = response.data
|
|
return response.data
|
|
} catch (err: any) {
|
|
error.value = err.message
|
|
return null
|
|
} finally {
|
|
summaryLoading.value = false
|
|
}
|
|
}
|
|
|
|
async function generateSummary(topicId: string): Promise<TopicSummary | null> {
|
|
summaryLoading.value = true
|
|
activeSummaryTopicId.value = topicId
|
|
activeSummary.value = null
|
|
error.value = null
|
|
try {
|
|
const response = await api.post<TopicSummary>(`/topics/${topicId}/summary`)
|
|
activeSummary.value = response.data
|
|
return response.data
|
|
} catch (err: any) {
|
|
error.value = err.message
|
|
return null
|
|
} finally {
|
|
summaryLoading.value = false
|
|
activeSummaryTopicId.value = null
|
|
}
|
|
}
|
|
|
|
return {
|
|
topics,
|
|
activeSummary,
|
|
activeSummaryTopicId,
|
|
summaryList,
|
|
loading,
|
|
summaryLoading,
|
|
summaryListLoading,
|
|
error,
|
|
fetchTopics,
|
|
fetchSummaries,
|
|
fetchSummaryDetail,
|
|
generateSummary
|
|
}
|
|
})
|