Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/entrypoints/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CommentEvent, CommentSpot } from '@/lib/enhancer'
import { type DraftStats, statsFor } from '@/lib/enhancers/draft-stats'
import { logger } from '@/lib/logger'
import type { GetTableRowsResponse, ToBackgroundMessage } from '@/lib/messages'
import {
CLOSE_MESSAGE_PORT,
Expand Down Expand Up @@ -36,14 +37,15 @@ export interface CommentTableRow {
export const openSpots = new Map<string, CommentStorage>()

export function handleCommentEvent(message: CommentEvent, sender: any): boolean {
logger.debug('received comment event', message)
if (
(message.type === 'ENHANCED' || message.type === 'DESTROYED') &&
sender.tab?.id &&
sender.tab?.windowId
) {
if (message.type === 'ENHANCED') {
const commentState: CommentStorage = {
drafts: [],
drafts: [[Date.now(), message.draft || '']],
sentOn: null,
spot: message.spot,
tab: {
Expand All @@ -68,6 +70,7 @@ export function handlePopupMessage(
sendResponse: (response: any) => void,
): typeof CLOSE_MESSAGE_PORT | typeof KEEP_PORT_OPEN {
if (isGetOpenSpotsMessage(message)) {
logger.debug('received open spots message', message)
const rows: CommentTableRow[] = Array.from(openSpots.values()).map((storage) => {
const [time, content] = storage.drafts.at(-1)!
const row: CommentTableRow = {
Expand All @@ -87,6 +90,7 @@ export function handlePopupMessage(
sendResponse(response)
return KEEP_PORT_OPEN
} else if (isSwitchToTabMessage(message)) {
logger.debug('received switch tab message', message)
browser.windows
.update(message.windowId, { focused: true })
.then(() => {
Expand All @@ -97,6 +101,7 @@ export function handlePopupMessage(
})
return CLOSE_MESSAGE_PORT
} else {
logger.error('received unknown message', message)
throw new Error(`Unhandled popup message type: ${message?.type || 'unknown'}`)
}
}
Expand All @@ -109,4 +114,16 @@ export default defineBackground(() => {
return handlePopupMessage(message, sender, sendResponse)
}
})

browser.tabs.onRemoved.addListener((tabId: number) => {
logger.debug('tab removed', { tabId })

// Clean up openSpots entries for the closed tab
for (const [key, value] of openSpots) {
if (tabId === value.tab.tabId) {
openSpots.delete(key)
logger.debug('closed tab which contained spot', value.spot.unique_key)
}
}
})
})
1 change: 0 additions & 1 deletion src/entrypoints/popup/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
body {
width: var(--popup-width);
height: var(--popup-height);
padding: 15px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 14px;
line-height: 1.4;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/registries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class EnhancerRegistry {
}, 400)
setTimeout(() => {
overtype.updatePreview()
}, 8000)
}, 800)
}

getEnhancerCount(): number {
Expand Down
14 changes: 12 additions & 2 deletions tests/background.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { handleCommentEvent, openSpots } from '../src/entrypoints/background'
import type { CommentEvent, CommentSpot } from '../src/lib/enhancer'

Expand All @@ -14,8 +14,13 @@ const mockSpot = {
}
describe('Background Event Handler', () => {
beforeEach(() => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2025-09-19T10:00:00.000Z'))
openSpots.clear()
})
afterEach(() => {
vi.useRealTimers()
})
describe('ENHANCED Event', () => {
it('should create new comment state when textarea is enhanced', () => {
handleCommentEvent(
Expand All @@ -30,7 +35,12 @@ describe('Background Event Handler', () => {
[
"test-key",
{
"drafts": [],
"drafts": [
[
1758276000000,
"",
],
],
"sentOn": null,
"spot": {
"type": "TEST_SPOT",
Expand Down