Skip to content

Commit

Permalink
fix: handle no active tab scenarios (#164)
Browse files Browse the repository at this point in the history
* fix: improve getActiveWindowTab function to handle no active tab scenarios

* fix: handle undefined active tab in heartbeat

* Lint :)
  • Loading branch information
BelKed authored Feb 28, 2025
1 parent c9d4645 commit b3dfcb1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/background/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import deepEqual from 'deep-equal'

async function heartbeat(
client: AWClient,
tab: browser.Tabs.Tab,
tab: browser.Tabs.Tab | undefined,
tabCount: number,
) {
const enabled = await getEnabled()
Expand All @@ -17,10 +17,13 @@ async function heartbeat(
return
}

if (!tab) {
console.warn('Ignoring heartbeat because no active tab was found')
return
}

if (!tab.url || !tab.title) {
console.warn(
'Ignoring heartbeat because tab is missing required properties',
)
console.warn('Ignoring heartbeat because tab is missing URL or title')
return
}

Expand Down
26 changes: 22 additions & 4 deletions src/background/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,33 @@ export const getTab = (id: number) => browser.tabs.get(id)
export const getTabs = (query: browser.Tabs.QueryQueryInfoType = {}) =>
browser.tabs.query(query)

export const getActiveWindowTab = () =>
getTabs({
export const getActiveWindowTab = async (): Promise<
browser.Tabs.Tab | undefined
> => {
const tabs = await getTabs({
active: true,
currentWindow: true,
}).then((tabs) => {
if (tabs.length === 0) throw Error('No active window tab found')
})

if (tabs.length > 0) {
return tabs[0]
}

console.debug('No active tab found in current window')

const allTabs = await getTabs({
active: true,
})

if (allTabs.length > 0) {
return allTabs[0]
}

console.debug('No active tab found in any window')

return undefined
}

export function emitNotification(title: string, message: string) {
browser.notifications.create({
type: 'basic',
Expand Down

0 comments on commit b3dfcb1

Please sign in to comment.