1- import type { CommentEvent , CommentSpot } from '@/lib/enhancer'
1+ import type { CommentEvent , CommentEventType , CommentSpot } from '@/lib/enhancer'
22import { type DraftStats , statsFor } from '@/lib/enhancers/draft-stats'
33import { logger } from '@/lib/logger'
44import type { GetTableRowsResponse , ToBackgroundMessage } from '@/lib/messages'
@@ -38,12 +38,14 @@ export const openSpots = new Map<string, CommentStorage>()
3838
3939export function handleCommentEvent ( message : CommentEvent , sender : any ) : boolean {
4040 logger . debug ( 'received comment event' , message )
41- if (
42- ( message . type === 'ENHANCED' || message . type === 'DESTROYED' ) &&
43- sender . tab ?. id &&
44- sender . tab ?. windowId
45- ) {
46- if ( message . type === 'ENHANCED' ) {
41+
42+ // Only process events with valid tab information
43+ if ( ! sender . tab ?. id || ! sender . tab ?. windowId ) {
44+ return CLOSE_MESSAGE_PORT
45+ }
46+
47+ switch ( message . type ) {
48+ case 'ENHANCED' : {
4749 const commentState : CommentStorage = {
4850 drafts : [ [ Date . now ( ) , message . draft || '' ] ] ,
4951 sentOn : null ,
@@ -55,12 +57,27 @@ export function handleCommentEvent(message: CommentEvent, sender: any): boolean
5557 trashedOn : null ,
5658 }
5759 openSpots . set ( message . spot . unique_key , commentState )
58- } else if ( message . type === 'DESTROYED' ) {
60+ break
61+ }
62+ case 'DESTROYED' : {
5963 openSpots . delete ( message . spot . unique_key )
60- } else {
61- throw new Error ( `Unhandled comment event type: ${ message . type } ` )
64+ break
65+ }
66+ case 'LOST_FOCUS' : {
67+ // Update the draft content for existing comment state
68+ const existingState = openSpots . get ( message . spot . unique_key )
69+ if ( existingState ) {
70+ existingState . drafts . push ( [ Date . now ( ) , message . draft || '' ] )
71+ }
72+ break
73+ }
74+ default : {
75+ // TypeScript exhaustiveness check - will error if we miss any CommentEventType
76+ const exhaustiveCheck : never = message . type satisfies CommentEventType
77+ throw new Error ( `Unhandled comment event type: ${ exhaustiveCheck } ` )
6278 }
6379 }
80+
6481 return CLOSE_MESSAGE_PORT
6582}
6683
0 commit comments