11<script setup lang="ts">
2- import { ref , onMounted , computed } from ' vue'
2+ import { ref , onMounted , computed , watch } from ' vue'
33import { useI18n } from ' vue-i18n'
44import { DynamicIcon } from ' @/components/ui/dynamic-icon'
55import { McpCatalogService } from ' @/services/mcpCatalogService'
6+ import { useEventBus } from ' @/composables/useEventBus'
67import type { McpCategory } from ' @/services/mcpCategoriesService'
78
89const { t } = useI18n ()
10+ const eventBus = useEventBus ()
11+
12+ // Storage key for categories cache
13+ const CATEGORIES_STORAGE_KEY = ' mcp_categories_cache'
914
1015interface Props {
1116 categoryId? : string | null
@@ -29,25 +34,56 @@ const category = ref<McpCategory | null>(props.category)
2934const isLoading = ref (false )
3035const error = ref <string | null >(null )
3136
32- // Fetch category by ID if not provided as prop
33- async function fetchCategory(categoryId : string ): Promise <McpCategory | null > {
37+ // Find category from cached categories in storage
38+ function findCategoryFromCache(categoryId : string ): McpCategory | null {
39+ const cached = eventBus .getState <McpCategory []>(CATEGORIES_STORAGE_KEY )
40+ if (cached && Array .isArray (cached )) {
41+ return cached .find (cat => cat .id === categoryId ) || null
42+ }
43+ return null
44+ }
45+
46+ // Fetch categories and cache them in storage
47+ async function fetchAndCacheCategories(): Promise <McpCategory []> {
48+ const categories = await McpCatalogService .getCategories ()
49+ eventBus .setState (CATEGORIES_STORAGE_KEY , categories )
50+ return categories
51+ }
52+
53+ // Load category - check storage cache first, then fetch if needed
54+ async function loadCategory(categoryId : string ): Promise <void > {
55+ // First, try to find in storage cache (prevents redundant API calls)
56+ const cachedCategory = findCategoryFromCache (categoryId )
57+ if (cachedCategory ) {
58+ category .value = cachedCategory
59+ return
60+ }
61+
62+ // Not in cache, fetch from API
3463 try {
3564 isLoading .value = true
36- const categories = await McpCatalogService .getCategories ()
37- return categories .find (cat => cat .id === categoryId ) || null
65+ error .value = null
66+ const categories = await fetchAndCacheCategories ()
67+ category .value = categories .find (cat => cat .id === categoryId ) || null
3868 } catch (err ) {
3969 error .value = err instanceof Error ? err .message : ' Failed to fetch category'
4070 console .error (' Failed to fetch category:' , err )
41- return null
4271 } finally {
4372 isLoading .value = false
4473 }
4574}
4675
76+ // Watch for categoryId changes (handles re-renders without remount)
77+ watch (() => props .categoryId , (newId ) => {
78+ if (newId && ! props .category ) {
79+ loadCategory (newId )
80+ }
81+ }, { immediate: false })
82+
4783// Load category on mount if categoryId is provided but category is not
48- onMounted (async () => {
84+ onMounted (() => {
4985 if (props .categoryId && ! props .category ) {
50- category . value = await fetchCategory (props .categoryId )
86+ loadCategory (props .categoryId )
5187 }
5288})
5389
0 commit comments