@@ -4,7 +4,9 @@ import { IconPlayerPlay, IconPlayerPause, IconPencil, IconQuestionMark, IconExte
4
4
import useStore from "~store/useStore" ;
5
5
import { updateLocalStorageValue } from "~handlers/localStorageHandlers" ;
6
6
import { Storage } from "@plasmohq/storage" ;
7
- import type { HistoryItems } from '~types/types' ;
7
+ import type { HistoryItems } from "~types/types" ;
8
+
9
+ const broadcastChannel = new BroadcastChannel ( "broadcastChannel" ) ;
8
10
9
11
const useStyles = createStyles ( ( theme ) => ( {
10
12
card : {
@@ -29,7 +31,7 @@ interface SearchItemProps {
29
31
} ;
30
32
}
31
33
32
- const storage = new Storage ( )
34
+ const storage = new Storage ( ) ;
33
35
34
36
const SearchItem = ( { experiment } : SearchItemProps ) => {
35
37
const { classes } = useStyles ( ) ;
@@ -66,23 +68,26 @@ const SearchItem = ({ experiment }: SearchItemProps) => {
66
68
const addHistoryItem = async ( newItem : HistoryItems ) => {
67
69
const maxHistoryItems = 3 ;
68
70
const historyItemsLocalStorage = await storage . get ( "history" ) ;
69
- const newHistoryItems = historyItemsLocalStorage ? JSON . parse ( historyItemsLocalStorage ) : [ ]
71
+ const newHistoryItems = historyItemsLocalStorage ? JSON . parse ( historyItemsLocalStorage ) : [ ] ;
70
72
71
- if ( newHistoryItems . find ( ( item : { key : string ; } ) => item . key === newItem . key ) ) {
73
+ if ( newHistoryItems . find ( ( item : { key : string ; } ) => item . key === newItem . key ) ) {
72
74
return ;
73
75
}
74
76
75
- if ( newHistoryItems . length == = maxHistoryItems ) {
77
+ if ( newHistoryItems . length > = maxHistoryItems ) {
76
78
newHistoryItems . shift ( ) ;
77
79
}
78
80
79
81
newHistoryItems . push ( newItem ) ;
80
82
setHistoryItems ( newHistoryItems ) ;
81
- }
83
+
84
+ // Broadcast history to service worker
85
+ broadcastChannel . postMessage ( { history : newHistoryItems } ) ;
86
+ } ;
82
87
83
88
const saveToLocalStorage = ( value , experimentName ) => {
84
89
setLocalStorageValue ( value ) ;
85
- addHistoryItem ( { name : experimentName , key : value } ) ;
90
+ addHistoryItem ( { name : experimentName , key : value } ) ;
86
91
87
92
chrome . tabs . query ( { active : true , currentWindow : true } , ( tabs ) => {
88
93
updateLocalStorageValue ( tabs [ 0 ] . id , localStorageKey , value ) ;
@@ -141,7 +146,9 @@ const SearchItem = ({ experiment }: SearchItemProps) => {
141
146
</ Text >
142
147
</ Group >
143
148
< Text >
144
- < Anchor href = { `https://app.optimizely.com/v2/projects/${ experiment . project_id } /experiments/${ experiment . id } ` } target = "_blank" >
149
+ < Anchor
150
+ href = { `https://app.optimizely.com/v2/projects/${ experiment . project_id } /experiments/${ experiment . id } ` }
151
+ target = "_blank" >
145
152
< IconExternalLink size = { 18 } stroke = { 1.5 } style = { { marginBottom : - 4 } } /> Optimizely
146
153
</ Anchor >
147
154
</ Text >
0 commit comments