1
1
import { customRef , ref , toRaw , watch } from 'vue'
2
2
import { storage } from 'wxt/utils/storage'
3
3
4
+ import { TAB_STORE_STORAGE_KEY_PREFIX } from '../constants'
4
5
import { debounce } from '../debounce'
5
6
import { Base64ImageData } from '../image'
6
7
import { lazyInitialize } from '../memo'
@@ -10,12 +11,15 @@ import type { HistoryItemV1 } from './history'
10
11
11
12
export { HistoryItemV1 }
12
13
13
- async function defineTabValue < T > ( key : string , defaultValue : T ) {
14
- let sessionKey = `session:tab-store-${ key } ` as `${'local' | 'session' } :tab-store-${string } `
15
- if ( import . meta. env . FIREFOX ) {
16
- // Firefox does not support session storage in content scripts
17
- sessionKey = `local:tab-store-${ key } ` as `local:tab-store-${string } `
18
- }
14
+ function constructStorageKey ( tabId : number , key : string ) {
15
+ const scope = import . meta. env . FIREFOX
16
+ ? 'local'
17
+ : 'session'
18
+ return `${ scope } :${ TAB_STORE_STORAGE_KEY_PREFIX } ${ tabId } -${ key } ` as `${'local' | 'session' } :${string } `
19
+ }
20
+
21
+ async function defineTabValue < T > ( tabId : number , key : string , defaultValue : T ) {
22
+ const sessionKey = constructStorageKey ( tabId , key )
19
23
const valueInStorageStr = await storage . getItem < string > ( sessionKey )
20
24
const valueInStorage = valueInStorageStr ? JSON . parse ( valueInStorageStr ) : defaultValue
21
25
const v = ref ( valueInStorage )
@@ -59,14 +63,14 @@ async function _getTabStore() {
59
63
const { tabId, faviconUrl, url, title } = await c2bRpc . getTabInfo ( )
60
64
if ( ! tabId ) throw new Error ( 'no tab id' )
61
65
return {
62
- currentTabId : await defineTabValue ( 'currentTabId' , tabId ) ,
63
- showContainer : await defineTabValue ( `showContainer- ${ tabId } ` , false ) ,
64
- showSetting : await defineTabValue < ShowSettingsParams > ( `showSetting- ${ tabId } ` , { show : false } ) ,
65
- chatHistory : await defineTabValue ( `chatHistory- ${ tabId } ` , [ ] as ChatHistory ) ,
66
- pageSummary : await defineTabValue ( `summary- ${ tabId } ` , { content : '' , summary : '' } as PageSummary ) ,
67
- contextTabIds : await defineTabValue ( `contextTabs- ${ tabId } ` , [ tabId ] as number [ ] ) ,
68
- contextImages : await defineTabValue ( `contextImages- ${ tabId } ` , [ ] as Base64ImageData [ ] ) ,
69
- tabInfo : await defineTabValue ( `tabInfo- ${ tabId } ` , {
66
+ currentTabId : await defineTabValue ( tabId , 'currentTabId' , tabId ) ,
67
+ showContainer : await defineTabValue ( tabId , `showContainer` , false ) ,
68
+ showSetting : await defineTabValue < ShowSettingsParams > ( tabId , `showSetting` , { show : false } ) ,
69
+ chatHistory : await defineTabValue ( tabId , `chatHistory` , [ ] as ChatHistory ) ,
70
+ pageSummary : await defineTabValue ( tabId , `summary` , { content : '' , summary : '' } as PageSummary ) ,
71
+ contextTabIds : await defineTabValue ( tabId , `contextTabs` , [ tabId ] as number [ ] ) ,
72
+ contextImages : await defineTabValue ( tabId , `contextImages` , [ ] as Base64ImageData [ ] ) ,
73
+ tabInfo : await defineTabValue ( tabId , `tabInfo` , {
70
74
url,
71
75
title,
72
76
faviconUrl,
@@ -75,3 +79,18 @@ async function _getTabStore() {
75
79
}
76
80
77
81
export const getTabStore = lazyInitialize ( _getTabStore )
82
+
83
+ // this is a workaround for firefox to clear the tab store when the tab is closed
84
+ export const getTabKeys = ( tabId : number ) => {
85
+ const keys = [
86
+ 'currentTabId' ,
87
+ 'showContainer' ,
88
+ 'showSetting' ,
89
+ 'chatHistory' ,
90
+ 'summary' ,
91
+ 'contextTabs' ,
92
+ 'contextImages' ,
93
+ 'tabInfo' ,
94
+ ]
95
+ return keys . map ( ( key ) => constructStorageKey ( tabId , key ) )
96
+ }
0 commit comments