Skip to content

Commit 63e9149

Browse files
committed
Refactor from CommentState to CommentTableRow
1 parent 937a564 commit 63e9149

File tree

7 files changed

+63
-62
lines changed

7 files changed

+63
-62
lines changed

browser-extension/src/components/SpotRow.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { TableCell, TableRow } from '@/components/ui/table'
2-
import type { CommentState } from '@/entrypoints/background'
2+
import type { CommentStorage } from '@/entrypoints/background'
33
import type { EnhancerRegistry } from '@/lib/registries'
44
import { cn } from '@/lib/utils'
55

66
interface SpotRowProps {
7-
commentState: CommentState
7+
commentState: CommentStorage
88
enhancerRegistry: EnhancerRegistry
99
onClick: () => void
1010
className?: string

browser-extension/src/components/SpotTable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Table, TableBody, TableHead, TableHeader, TableRow } from '@/components/ui/table'
2-
import type { CommentState } from '@/entrypoints/background'
2+
import type { CommentStorage } from '@/entrypoints/background'
33
import type { EnhancerRegistry } from '@/lib/registries'
44
import { SpotRow } from './SpotRow'
55

66
interface SpotTableProps {
7-
spots: CommentState[]
7+
spots: CommentStorage[]
88
enhancerRegistry: EnhancerRegistry
9-
onSpotClick: (spot: CommentState) => void
9+
onSpotClick: (spot: CommentStorage) => void
1010
title?: string
1111
description?: string
1212
headerText?: string

browser-extension/src/entrypoints/background.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CommentEvent, CommentSpot } from '@/lib/enhancer'
2-
import type { DraftStats } from '@/lib/enhancers/draftStats'
3-
import type { GetOpenSpotsResponse, ToBackgroundMessage } from '@/lib/messages'
2+
import { type DraftStats, statsFor } from '@/lib/enhancers/draftStats'
3+
import type { GetTableRowsResponse, ToBackgroundMessage } from '@/lib/messages'
44
import {
55
CLOSE_MESSAGE_PORT,
66
isContentToBackgroundMessage,
@@ -13,10 +13,12 @@ export interface Tab {
1313
tabId: number
1414
windowId: number
1515
}
16-
export interface CommentState {
16+
export interface CommentStorage {
1717
tab: Tab
1818
spot: CommentSpot
1919
drafts: [number, string][]
20+
sentOn: number | null
21+
trashedOn: number | null
2022
}
2123
interface Draft {
2224
content: string
@@ -31,7 +33,7 @@ export interface CommentTableRow {
3133
isTrashed: boolean
3234
}
3335

34-
export const openSpots = new Map<string, CommentState>()
36+
export const openSpots = new Map<string, CommentStorage>()
3537

3638
export function handleCommentEvent(message: CommentEvent, sender: any): boolean {
3739
if (
@@ -40,13 +42,15 @@ export function handleCommentEvent(message: CommentEvent, sender: any): boolean
4042
sender.tab?.windowId
4143
) {
4244
if (message.type === 'ENHANCED') {
43-
const commentState: CommentState = {
45+
const commentState: CommentStorage = {
4446
drafts: [],
47+
sentOn: null,
4548
spot: message.spot,
4649
tab: {
4750
tabId: sender.tab.id,
4851
windowId: sender.tab.windowId,
4952
},
53+
trashedOn: null,
5054
}
5155
openSpots.set(message.spot.unique_key, commentState)
5256
} else if (message.type === 'DESTROYED') {
@@ -64,9 +68,22 @@ export function handlePopupMessage(
6468
sendResponse: (response: any) => void,
6569
): typeof CLOSE_MESSAGE_PORT | typeof KEEP_PORT_OPEN {
6670
if (isGetOpenSpotsMessage(message)) {
67-
const spots: CommentState[] = Array.from(openSpots.values())
68-
69-
const response: GetOpenSpotsResponse = { spots }
71+
const rows: CommentTableRow[] = Array.from(openSpots.values()).map((storage) => {
72+
const [time, content] = storage.drafts.at(-1)!
73+
const row: CommentTableRow = {
74+
isOpenTab: true,
75+
isSent: storage.sentOn != null,
76+
isTrashed: storage.trashedOn != null,
77+
latestDraft: {
78+
content,
79+
stats: statsFor(content),
80+
time,
81+
},
82+
spot: storage.spot,
83+
}
84+
return row
85+
})
86+
const response: GetTableRowsResponse = { rows }
7087
sendResponse(response)
7188
return KEEP_PORT_OPEN
7289
} else if (isSwitchToTabMessage(message)) {

browser-extension/src/entrypoints/popup/popup.tsx

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
import './style.css'
22
import React from 'react'
33
import { createRoot } from 'react-dom/client'
4-
import { SpotTable } from '@/components/SpotTable'
5-
import type { CommentState } from '@/entrypoints/background'
4+
import type { CommentTableRow } from '@/entrypoints/background'
65
import { logger } from '@/lib/logger'
7-
import type { GetOpenSpotsMessage, GetOpenSpotsResponse, SwitchToTabMessage } from '@/lib/messages'
8-
import { EnhancerRegistry } from '@/lib/registries'
6+
import type { GetOpenSpotsMessage, GetTableRowsResponse } from '@/lib/messages'
97

10-
async function getOpenSpots(): Promise<CommentState[]> {
8+
async function getOpenSpots(): Promise<CommentTableRow[]> {
119
logger.debug('Sending message to background script...')
1210
try {
1311
const message: GetOpenSpotsMessage = { type: 'GET_OPEN_SPOTS' }
14-
const response = (await browser.runtime.sendMessage(message)) as GetOpenSpotsResponse
12+
const response = (await browser.runtime.sendMessage(message)) as GetTableRowsResponse
1513
logger.debug('Received response:', response)
16-
return response.spots || []
14+
return response.rows || []
1715
} catch (error) {
1816
logger.error('Error sending message to background:', error)
1917
return []
2018
}
2119
}
2220

23-
function switchToTab(tabId: number, windowId: number): void {
24-
const message: SwitchToTabMessage = {
25-
tabId,
26-
type: 'SWITCH_TO_TAB',
27-
windowId,
28-
}
29-
browser.runtime.sendMessage(message)
30-
window.close()
31-
}
32-
33-
const enhancers = new EnhancerRegistry()
21+
// function switchToTab(tabId: number, windowId: number): void {
22+
// const message: SwitchToTabMessage = {
23+
// tabId,
24+
// type: 'SWITCH_TO_TAB',
25+
// windowId,
26+
// }
27+
// browser.runtime.sendMessage(message)
28+
// window.close()
29+
// }
3430

3531
export interface FilterState {
3632
sentFilter: 'both' | 'sent' | 'unsent'
@@ -39,7 +35,7 @@ export interface FilterState {
3935
}
4036

4137
function PopupApp() {
42-
const [spots, setSpots] = React.useState<CommentState[]>([])
38+
const [_spots, setSpots] = React.useState<CommentTableRow[]>([])
4339
const [isLoading, setIsLoading] = React.useState(true)
4440

4541
React.useEffect(() => {
@@ -61,26 +57,16 @@ function PopupApp() {
6157
return <div className='p-4 text-center text-muted-foreground'>Loading...</div>
6258
}
6359

64-
const handleSpotClick = (spot: CommentState) => {
65-
switchToTab(spot.tab.tabId, spot.tab.windowId)
66-
}
60+
// const handleSpotClick = (spot: CommentTableRow) => {
61+
// console.log('TODO: switchToTab')
62+
// //switchToTab(spot.tab.tabId, spot.tab.windowId)
63+
// }
6764

6865
return (
6966
<div className='w-full'>
7067
<h2 className='mb-4 text-lg font-semibold text-foreground'>Open Comment Spots</h2>
7168

72-
<div className='border rounded-md'>
73-
<SpotTable
74-
spots={spots}
75-
enhancerRegistry={enhancers}
76-
onSpotClick={handleSpotClick}
77-
headerClassName='p-3 font-medium text-muted-foreground'
78-
rowClassName='transition-colors hover:bg-muted/50 border-b border-border/40'
79-
cellClassName='p-3'
80-
emptyStateMessage='No open comment spots'
81-
showHeader={true}
82-
/>
83-
</div>
69+
<div className='border rounded-md'></div>
8470
</div>
8571
)
8672
}

browser-extension/src/lib/messages.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { CommentEvent, CommentSpot } from './enhancer'
1+
import type { CommentTableRow } from '@/entrypoints/background'
2+
import type { CommentEvent } from './enhancer'
23

34
// Message handler response types
45
export const CLOSE_MESSAGE_PORT = false as const // No response will be sent
@@ -24,15 +25,8 @@ export type PopupToBackgroundMessage = GetOpenSpotsMessage | SwitchToTabMessage
2425
export type ToBackgroundMessage = ContentToBackgroundMessage | PopupToBackgroundMessage
2526

2627
// Background -> Popup responses
27-
export interface GetOpenSpotsResponse {
28-
spots: Array<{
29-
tab: {
30-
tabId: number
31-
windowId: number
32-
}
33-
spot: CommentSpot
34-
drafts: Array<[number, string]>
35-
}>
28+
export interface GetTableRowsResponse {
29+
rows: CommentTableRow[]
3630
}
3731

3832
// Type guard functions
@@ -76,5 +70,5 @@ export type BackgroundMessageHandler = (
7670
export type PopupMessageSender = {
7771
sendMessage<T extends PopupToBackgroundMessage>(
7872
message: T,
79-
): Promise<T extends GetOpenSpotsMessage ? GetOpenSpotsResponse : void>
73+
): Promise<T extends GetOpenSpotsMessage ? GetTableRowsResponse : void>
8074
}

browser-extension/tests/background.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('Background Event Handler', () => {
3131
"test-key",
3232
{
3333
"drafts": [],
34+
"sentOn": null,
3435
"spot": {
3536
"type": "TEST_SPOT",
3637
"unique_key": "test-key",
@@ -39,6 +40,7 @@ describe('Background Event Handler', () => {
3940
"tabId": 123,
4041
"windowId": 456,
4142
},
43+
"trashedOn": null,
4244
},
4345
],
4446
]

browser-extension/tests/playground/replica.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SpotTable } from '@/components/SpotTable'
2-
import type { CommentState } from '@/entrypoints/background'
2+
import type { CommentStorage } from '@/entrypoints/background'
33
import type { CommentSpot } from '@/lib/enhancer'
44
import type { GitHubIssueAddCommentSpot } from '@/lib/enhancers/github/githubIssueAddComment'
55
import type { GitHubPRAddCommentSpot } from '@/lib/enhancers/github/githubPRAddComment'
@@ -23,20 +23,22 @@ const gh_issue: GitHubIssueAddCommentSpot = {
2323
}
2424

2525
const spots: CommentSpot[] = [gh_pr, gh_issue]
26-
const sampleSpots: CommentState[] = spots.map((spot) => {
27-
const state: CommentState = {
26+
const sampleSpots: CommentStorage[] = spots.map((spot) => {
27+
const state: CommentStorage = {
2828
drafts: [[0, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.']],
29+
sentOn: null,
2930
spot,
3031
tab: {
3132
tabId: 123,
3233
windowId: 456,
3334
},
35+
trashedOn: null,
3436
}
3537
return state
3638
})
3739

3840
export function Replica() {
39-
const handleSpotClick = (spot: CommentState) => {
41+
const handleSpotClick = (spot: CommentStorage) => {
4042
alert(`Clicked: ${spot.spot.type}\nTab: ${spot.tab.tabId}`)
4143
}
4244

0 commit comments

Comments
 (0)