mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-24 18:50:56 +08:00
- Introduced a new TabRouter component to manage routing for each tab independently, enhancing the tab management system. - Updated the AppShell component to utilize the new TabRouter, allowing for true KeepAlive behavior with isolated history. - Refactored the Sidebar component for improved navigation and tab creation, now supporting internal app routes. - Enhanced the useTabs hook to ensure at least one default tab exists, improving user experience on initial load. - Updated cacheSchemas to reflect changes in tab types and metadata structure. These changes significantly improve the architecture and functionality of the tab system, providing a more robust user experience.
137 lines
3.3 KiB
TypeScript
137 lines
3.3 KiB
TypeScript
import type * as CacheValueTypes from './cacheValueTypes'
|
|
|
|
/**
|
|
* Cache Schema Definitions
|
|
*
|
|
* ## Key Naming Convention
|
|
*
|
|
* All cache keys MUST follow the format: `namespace.sub.key_name`
|
|
*
|
|
* Rules:
|
|
* - At least 2 segments separated by dots (.)
|
|
* - Each segment uses lowercase letters, numbers, and underscores only
|
|
* - Pattern: /^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/
|
|
*
|
|
* Examples:
|
|
* - 'app.user.avatar' (valid)
|
|
* - 'chat.multi_select_mode' (valid)
|
|
* - 'minapp.opened_keep_alive' (valid)
|
|
* - 'userAvatar' (invalid - missing dot separator)
|
|
* - 'App.user' (invalid - uppercase not allowed)
|
|
*
|
|
* This convention is enforced by ESLint rule: data-schema-key/valid-key
|
|
*/
|
|
|
|
/**
|
|
* Use cache schema for renderer hook
|
|
*/
|
|
|
|
export type UseCacheSchema = {
|
|
// App state
|
|
'app.dist.update_state': CacheValueTypes.CacheAppUpdateState
|
|
'app.user.avatar': string
|
|
|
|
// Chat context
|
|
'chat.multi_select_mode': boolean
|
|
'chat.selected_message_ids': string[]
|
|
'chat.generating': boolean
|
|
'chat.websearch.searching': boolean
|
|
'chat.websearch.active_searches': CacheValueTypes.CacheActiveSearches
|
|
|
|
// Minapp management
|
|
'minapp.opened_keep_alive': CacheValueTypes.CacheMinAppType[]
|
|
'minapp.current_id': string
|
|
'minapp.show': boolean
|
|
'minapp.opened_oneoff': CacheValueTypes.CacheMinAppType | null
|
|
|
|
// Topic management
|
|
'topic.active': CacheValueTypes.CacheTopic | null
|
|
'topic.renaming': string[]
|
|
'topic.newly_renamed': string[]
|
|
}
|
|
|
|
export const DefaultUseCache: UseCacheSchema = {
|
|
// App state
|
|
'app.dist.update_state': {
|
|
info: null,
|
|
checking: false,
|
|
downloading: false,
|
|
downloaded: false,
|
|
downloadProgress: 0,
|
|
available: false
|
|
},
|
|
'app.user.avatar': '',
|
|
|
|
// Chat context
|
|
'chat.multi_select_mode': false,
|
|
'chat.selected_message_ids': [],
|
|
'chat.generating': false,
|
|
'chat.websearch.searching': false,
|
|
'chat.websearch.active_searches': {},
|
|
|
|
// Minapp management
|
|
'minapp.opened_keep_alive': [],
|
|
'minapp.current_id': '',
|
|
'minapp.show': false,
|
|
'minapp.opened_oneoff': null,
|
|
|
|
// Topic management
|
|
'topic.active': null,
|
|
'topic.renaming': [],
|
|
'topic.newly_renamed': []
|
|
}
|
|
|
|
/**
|
|
* Use shared cache schema for renderer hook
|
|
*/
|
|
export type UseSharedCacheSchema = {
|
|
'example_scope.example_key': string
|
|
}
|
|
|
|
export const DefaultUseSharedCache: UseSharedCacheSchema = {
|
|
'example_scope.example_key': 'example default value'
|
|
}
|
|
|
|
/**
|
|
* Tab type for browser-like tabs
|
|
*
|
|
* - 'route': Internal app routes rendered via MemoryRouter
|
|
* - 'webview': External web content rendered via Electron webview
|
|
*/
|
|
export type TabType = 'route' | 'webview'
|
|
|
|
export interface Tab {
|
|
id: string
|
|
type: TabType
|
|
url: string
|
|
title: string
|
|
icon?: string
|
|
metadata?: Record<string, unknown>
|
|
// TODO: LRU 优化字段,后续添加
|
|
// lastAccessTime?: number
|
|
}
|
|
|
|
export interface TabsState {
|
|
tabs: Tab[]
|
|
activeTabId: string
|
|
}
|
|
|
|
/**
|
|
* Persist cache schema defining allowed keys and their value types
|
|
* This ensures type safety and prevents key conflicts
|
|
*/
|
|
export type RendererPersistCacheSchema = {
|
|
'ui.tab.state': TabsState
|
|
}
|
|
|
|
export const DefaultRendererPersistCache: RendererPersistCacheSchema = {
|
|
'ui.tab.state': { tabs: [], activeTabId: '' }
|
|
}
|
|
|
|
/**
|
|
* Type-safe cache key
|
|
*/
|
|
export type RendererPersistCacheKey = keyof RendererPersistCacheSchema
|
|
export type UseCacheKey = keyof UseCacheSchema
|
|
export type UseSharedCacheKey = keyof UseSharedCacheSchema
|