Skip to content

Commit 3ec2141

Browse files
authored
Fix some things which broke about the popup (#67)
2 parents 43aabe2 + cdc8a15 commit 3ec2141

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/entrypoints/background.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CommentEvent, CommentSpot } from '@/lib/enhancer'
22
import { type DraftStats, statsFor } from '@/lib/enhancers/draft-stats'
3+
import { logger } from '@/lib/logger'
34
import type { GetTableRowsResponse, ToBackgroundMessage } from '@/lib/messages'
45
import {
56
CLOSE_MESSAGE_PORT,
@@ -36,14 +37,15 @@ export interface CommentTableRow {
3637
export const openSpots = new Map<string, CommentStorage>()
3738

3839
export function handleCommentEvent(message: CommentEvent, sender: any): boolean {
40+
logger.debug('received comment event', message)
3941
if (
4042
(message.type === 'ENHANCED' || message.type === 'DESTROYED') &&
4143
sender.tab?.id &&
4244
sender.tab?.windowId
4345
) {
4446
if (message.type === 'ENHANCED') {
4547
const commentState: CommentStorage = {
46-
drafts: [],
48+
drafts: [[Date.now(), message.draft || '']],
4749
sentOn: null,
4850
spot: message.spot,
4951
tab: {
@@ -68,6 +70,7 @@ export function handlePopupMessage(
6870
sendResponse: (response: any) => void,
6971
): typeof CLOSE_MESSAGE_PORT | typeof KEEP_PORT_OPEN {
7072
if (isGetOpenSpotsMessage(message)) {
73+
logger.debug('received open spots message', message)
7174
const rows: CommentTableRow[] = Array.from(openSpots.values()).map((storage) => {
7275
const [time, content] = storage.drafts.at(-1)!
7376
const row: CommentTableRow = {
@@ -87,6 +90,7 @@ export function handlePopupMessage(
8790
sendResponse(response)
8891
return KEEP_PORT_OPEN
8992
} else if (isSwitchToTabMessage(message)) {
93+
logger.debug('received switch tab message', message)
9094
browser.windows
9195
.update(message.windowId, { focused: true })
9296
.then(() => {
@@ -97,6 +101,7 @@ export function handlePopupMessage(
97101
})
98102
return CLOSE_MESSAGE_PORT
99103
} else {
104+
logger.error('received unknown message', message)
100105
throw new Error(`Unhandled popup message type: ${message?.type || 'unknown'}`)
101106
}
102107
}
@@ -109,4 +114,16 @@ export default defineBackground(() => {
109114
return handlePopupMessage(message, sender, sendResponse)
110115
}
111116
})
117+
118+
browser.tabs.onRemoved.addListener((tabId: number) => {
119+
logger.debug('tab removed', { tabId })
120+
121+
// Clean up openSpots entries for the closed tab
122+
for (const [key, value] of openSpots) {
123+
if (tabId === value.tab.tabId) {
124+
openSpots.delete(key)
125+
logger.debug('closed tab which contained spot', value.spot.unique_key)
126+
}
127+
}
128+
})
112129
})

src/entrypoints/popup/style.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
body {
1010
width: var(--popup-width);
1111
height: var(--popup-height);
12-
padding: 15px;
1312
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
1413
font-size: 14px;
1514
line-height: 1.4;

src/lib/registries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class EnhancerRegistry {
9191
}, 400)
9292
setTimeout(() => {
9393
overtype.updatePreview()
94-
}, 8000)
94+
}, 800)
9595
}
9696

9797
getEnhancerCount(): number {

tests/background.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { handleCommentEvent, openSpots } from '../src/entrypoints/background'
33
import type { CommentEvent, CommentSpot } from '../src/lib/enhancer'
44

@@ -14,8 +14,13 @@ const mockSpot = {
1414
}
1515
describe('Background Event Handler', () => {
1616
beforeEach(() => {
17+
vi.useFakeTimers()
18+
vi.setSystemTime(new Date('2025-09-19T10:00:00.000Z'))
1719
openSpots.clear()
1820
})
21+
afterEach(() => {
22+
vi.useRealTimers()
23+
})
1924
describe('ENHANCED Event', () => {
2025
it('should create new comment state when textarea is enhanced', () => {
2126
handleCommentEvent(
@@ -30,7 +35,12 @@ describe('Background Event Handler', () => {
3035
[
3136
"test-key",
3237
{
33-
"drafts": [],
38+
"drafts": [
39+
[
40+
1758276000000,
41+
"",
42+
],
43+
],
3444
"sentOn": null,
3545
"spot": {
3646
"type": "TEST_SPOT",

0 commit comments

Comments
 (0)