@@ -30,12 +30,162 @@ export type EventBusEvents = {
3030 'settings-updated' : void
3131 'settings-group-updated' : { groupId : string }
3232 'settings-connection-tested' : { groupId : string ; success : boolean ; message : string }
33+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34+ 'storage-changed' : { key : string ; oldValue : any ; newValue : any }
35+ }
36+
37+ // Storage configuration
38+ const STORAGE_CONFIG = {
39+ prefix : 'deploystack_' ,
40+ keys : {
41+ SELECTED_TEAM_ID : 'selected_team_id' ,
42+ }
43+ }
44+
45+ // Enhanced event bus with storage capabilities
46+ interface EnhancedEventBus extends Emitter < EventBusEvents > {
47+ setState < T > ( key : string , value : T ) : void
48+ getState < T > ( key : string , defaultValue ?: T ) : T | null
49+ clearState ( key : string ) : void
50+ hasState ( key : string ) : boolean
51+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
52+ getAllState ( ) : Record < string , any >
53+ clearAllState ( ) : void
54+ }
55+
56+ // Storage utility functions
57+ function getStorageKey ( key : string ) : string {
58+ return `${ STORAGE_CONFIG . prefix } ${ key } `
59+ }
60+
61+ function safeJsonParse < T > ( value : string | null , defaultValue ?: T ) : T | null {
62+ if ( value === null ) return defaultValue ?? null
63+ try {
64+ return JSON . parse ( value )
65+ } catch {
66+ return defaultValue ?? null
67+ }
68+ }
69+
70+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
71+ function safeJsonStringify ( value : any ) : string {
72+ try {
73+ return JSON . stringify ( value )
74+ } catch {
75+ return String ( value )
76+ }
3377}
3478
35- export function useEventBus ( ) {
79+ export function useEventBus ( ) : EnhancedEventBus {
3680 const emitter = inject < Emitter < EventBusEvents > > ( 'emitter' )
3781 if ( ! emitter ) {
3882 throw new Error ( 'Event bus not provided. Make sure to provide the emitter in main.ts' )
3983 }
40- return emitter
84+
85+ // Add storage methods to the emitter
86+ const enhancedEmitter = emitter as EnhancedEventBus
87+
88+ // Set state in localStorage
89+ enhancedEmitter . setState = function < T > ( key : string , value : T ) : void {
90+ const storageKey = getStorageKey ( key )
91+ const oldValue = safeJsonParse ( localStorage . getItem ( storageKey ) )
92+
93+ try {
94+ localStorage . setItem ( storageKey , safeJsonStringify ( value ) )
95+
96+ // Emit storage change event
97+ this . emit ( 'storage-changed' , { key, oldValue, newValue : value } )
98+ } catch ( error ) {
99+ console . error ( `Failed to set storage for key "${ key } ":` , error )
100+ }
101+ }
102+
103+ // Get state from localStorage
104+ enhancedEmitter . getState = function < T > ( key : string , defaultValue ?: T ) : T | null {
105+ const storageKey = getStorageKey ( key )
106+
107+ try {
108+ const value = localStorage . getItem ( storageKey )
109+ return safeJsonParse < T > ( value , defaultValue )
110+ } catch ( error ) {
111+ console . error ( `Failed to get storage for key "${ key } ":` , error )
112+ return defaultValue ?? null
113+ }
114+ }
115+
116+ // Clear specific state
117+ enhancedEmitter . clearState = function ( key : string ) : void {
118+ const storageKey = getStorageKey ( key )
119+ const oldValue = safeJsonParse ( localStorage . getItem ( storageKey ) )
120+
121+ try {
122+ localStorage . removeItem ( storageKey )
123+
124+ // Emit storage change event
125+ this . emit ( 'storage-changed' , { key, oldValue, newValue : null } )
126+ } catch ( error ) {
127+ console . error ( `Failed to clear storage for key "${ key } ":` , error )
128+ }
129+ }
130+
131+ // Check if state exists
132+ enhancedEmitter . hasState = function ( key : string ) : boolean {
133+ const storageKey = getStorageKey ( key )
134+
135+ try {
136+ return localStorage . getItem ( storageKey ) !== null
137+ } catch ( error ) {
138+ console . error ( `Failed to check storage for key "${ key } ":` , error )
139+ return false
140+ }
141+ }
142+
143+ // Get all stored state
144+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
145+ enhancedEmitter . getAllState = function ( ) : Record < string , any > {
146+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
147+ const result : Record < string , any > = { }
148+
149+ try {
150+ for ( let i = 0 ; i < localStorage . length ; i ++ ) {
151+ const key = localStorage . key ( i )
152+ if ( key && key . startsWith ( STORAGE_CONFIG . prefix ) ) {
153+ const cleanKey = key . replace ( STORAGE_CONFIG . prefix , '' )
154+ const value = localStorage . getItem ( key )
155+ result [ cleanKey ] = safeJsonParse ( value )
156+ }
157+ }
158+ } catch ( error ) {
159+ console . error ( 'Failed to get all storage:' , error )
160+ }
161+
162+ return result
163+ }
164+
165+ // Clear all stored state
166+ enhancedEmitter . clearAllState = function ( ) : void {
167+ try {
168+ const keysToRemove : string [ ] = [ ]
169+
170+ for ( let i = 0 ; i < localStorage . length ; i ++ ) {
171+ const key = localStorage . key ( i )
172+ if ( key && key . startsWith ( STORAGE_CONFIG . prefix ) ) {
173+ keysToRemove . push ( key )
174+ }
175+ }
176+
177+ keysToRemove . forEach ( key => {
178+ const cleanKey = key . replace ( STORAGE_CONFIG . prefix , '' )
179+ const oldValue = safeJsonParse ( localStorage . getItem ( key ) )
180+ localStorage . removeItem ( key )
181+
182+ // Emit storage change event for each cleared key
183+ this . emit ( 'storage-changed' , { key : cleanKey , oldValue, newValue : null } )
184+ } )
185+ } catch ( error ) {
186+ console . error ( 'Failed to clear all storage:' , error )
187+ }
188+ }
189+
190+ return enhancedEmitter
41191}
0 commit comments