Skip to content

Commit 37a9f49

Browse files
committed
Pipe the data to the "Open" badge.
1 parent 9869872 commit 37a9f49

File tree

6 files changed

+46
-38
lines changed

6 files changed

+46
-38
lines changed

src/components/Badge.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ const typePopups = {
1717
open: OpenTabPopup,
1818
text: TextPreview,
1919
time: TimePreview,
20-
} satisfies Partial<Record<keyof typeof typeIcons, () => JSX.Element>>
20+
} satisfies Partial<Record<keyof typeof typeIcons, (props?: any) => JSX.Element>>
2121

2222
export type BadgeProps = VariantProps<typeof badgeCVA> & {
2323
type: keyof typeof typeIcons
2424
text?: number | string
25+
data?: any
2526
}
2627

27-
const Badge = ({ text, type }: BadgeProps) => {
28+
const Badge = ({ text, type, data }: BadgeProps) => {
2829
const Icon = typeIcons[type]
2930
const [showTooltip, setShowTooltip] = useState(false)
3031
const PopupComponent =
@@ -54,7 +55,7 @@ const Badge = ({ text, type }: BadgeProps) => {
5455
typeColors[type],
5556
)}
5657
>
57-
<PopupComponent />
58+
<PopupComponent {...data} />
5859
</div>
5960
)}
6061
</button>
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
export function OpenTabPopup() {
1+
import { switchToTab } from '@/entrypoints/popup/popup'
2+
3+
interface OpenTabPopupProps {
4+
uniqueKey: string
5+
}
6+
7+
export function OpenTabPopup({ uniqueKey }: OpenTabPopupProps) {
8+
const handleClick = () => {
9+
switchToTab(uniqueKey)
10+
}
11+
212
return (
3-
<>
13+
<button
14+
onClick={handleClick}
15+
className='w-full cursor-pointer text-left hover:bg-opacity-80'
16+
type='button'
17+
>
418
<p>Tab is already open.</p>
519
<p>Click to activate.</p>
6-
</>
20+
</button>
721
)
822
}

src/components/CommentRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function CommentRow({ row, selectedIds, toggleSelection }: CommentRowProp
4444
)}
4545
<Badge type='text' text={row.latestDraft.stats.charCount} />
4646
<Badge type='time' text={timeAgo(row.latestDraft.time)} />
47-
{row.isOpenTab && <Badge type='open' />}
47+
{row.isOpenTab && <Badge type='open' data={{ uniqueKey: row.spot.unique_key }} />}
4848
</div>
4949
</div>
5050

src/entrypoints/background.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,19 @@ export function handlePopupMessage(
108108
return KEEP_PORT_OPEN
109109
} else if (isSwitchToTabMessage(message)) {
110110
logger.debug('received switch tab message', message)
111-
browser.windows
112-
.update(message.windowId, { focused: true })
113-
.then(() => {
114-
return browser.tabs.update(message.tabId, { active: true })
115-
})
116-
.catch((error) => {
117-
console.error('Error switching to tab:', error)
118-
})
111+
const storage = openSpots.get(message.uniqueKey)
112+
if (storage) {
113+
browser.windows
114+
.update(storage.tab.windowId, { focused: true })
115+
.then(() => {
116+
return browser.tabs.update(storage.tab.tabId, { active: true })
117+
})
118+
.catch((error) => {
119+
console.error('Error switching to tab:', error)
120+
})
121+
} else {
122+
console.error('No tab found for unique key:', message.uniqueKey)
123+
}
119124
return CLOSE_MESSAGE_PORT
120125
} else {
121126
logger.error('received unknown message', message)

src/entrypoints/popup/popup.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createRoot } from 'react-dom/client'
33
import { PopupRoot } from '@/components/PopupRoot'
44
import type { CommentTableRow } from '@/entrypoints/background'
55
import { logger } from '@/lib/logger'
6-
import type { GetOpenSpotsMessage, GetTableRowsResponse } from '@/lib/messages'
6+
import type { GetOpenSpotsMessage, GetTableRowsResponse, SwitchToTabMessage } from '@/lib/messages'
77

88
export interface FilterState {
99
sentFilter: 'both' | 'sent' | 'unsent'
@@ -24,20 +24,14 @@ async function getOpenSpots(): Promise<CommentTableRow[]> {
2424
}
2525
}
2626

27-
// function switchToTab(tabId: number, windowId: number): void {
28-
// const message: SwitchToTabMessage = {
29-
// tabId,
30-
// type: 'SWITCH_TO_TAB',
31-
// windowId,
32-
// }
33-
// browser.runtime.sendMessage(message)
34-
// window.close()
35-
// }
36-
37-
// const handleSpotClick = (spot: CommentTableRow) => {
38-
// console.log('TODO: switchToTab')
39-
// //switchToTab(spot.tab.tabId, spot.tab.windowId)
40-
// }
27+
export function switchToTab(uniqueKey: string): void {
28+
const message: SwitchToTabMessage = {
29+
type: 'SWITCH_TO_TAB',
30+
uniqueKey,
31+
}
32+
browser.runtime.sendMessage(message)
33+
window.close()
34+
}
4135

4236
const app = document.getElementById('app')
4337
if (app) {

src/lib/messages.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ export interface GetOpenSpotsMessage {
1515

1616
export interface SwitchToTabMessage {
1717
type: 'SWITCH_TO_TAB'
18-
tabId: number
19-
windowId: number
18+
uniqueKey: string
2019
}
2120

2221
export type PopupToBackgroundMessage = GetOpenSpotsMessage | SwitchToTabMessage
@@ -64,12 +63,7 @@ export function isGetOpenSpotsMessage(message: any): message is GetOpenSpotsMess
6463
}
6564

6665
export function isSwitchToTabMessage(message: any): message is SwitchToTabMessage {
67-
return (
68-
message &&
69-
message.type === 'SWITCH_TO_TAB' &&
70-
typeof message.tabId === 'number' &&
71-
typeof message.windowId === 'number'
72-
)
66+
return message && message.type === 'SWITCH_TO_TAB' && typeof message.uniqueKey === 'string'
7367
}
7468

7569
// Message handler types

0 commit comments

Comments
 (0)