1
1
import { EXT_ACTIONS } from '../actions'
2
2
import { POCKET_SAVES , POCKET_HOME } from '../constants'
3
+ import { getErrorMessage } from '../utilities/error'
3
4
import { isSystemPage } from '../utilities/is-system'
4
5
import { sendMessage } from '../utilities/send-message'
5
6
import { getSetting } from '../utilities/storage'
7
+ import { addNote } from './api/add-note'
6
8
import { upsertItem } from './api/upsert'
7
9
import { setAuth , logIn } from './auth'
8
10
import { setDefaultIcon , setToolbarIcon } from './icon-updates'
9
11
10
12
// Types
11
- import { getErrorMessage } from '../utilities/error'
12
13
import type { ExtMessage , ExtSave } from '../types'
14
+ import type { ExtNote } from '../types/note'
13
15
14
16
let saveQueue : ExtSave | null = null
15
17
@@ -38,7 +40,6 @@ chrome.runtime.onMessage.addListener(messageHandler)
38
40
async function messageHandler ( message : ExtMessage , sender : chrome . runtime . MessageSender ) {
39
41
const { action = EXT_ACTIONS . UNKNOWN_ACTION } = message || { }
40
42
41
- console . log ( message )
42
43
// Messages from `popup action` don't have a sender so we need to query the active tab
43
44
const activeTab = await chrome . tabs . query ( { active : true , lastFocusedWindow : true } )
44
45
@@ -76,6 +77,12 @@ async function messageHandler(message: ExtMessage, sender: chrome.runtime.Messag
76
77
return true
77
78
}
78
79
80
+ case EXT_ACTIONS . ADD_NOTE_REQUEST : {
81
+ const { noteData } = message
82
+ if ( noteData && tab . url ) await saveNote ( noteData , tab . url )
83
+ return true
84
+ }
85
+
79
86
case EXT_ACTIONS . AUTH_CODE_RECEIVED : {
80
87
try {
81
88
const { auth } = message
@@ -140,11 +147,6 @@ async function save(saveData: ExtSave) {
140
147
141
148
// send a message so the popup can display the preview
142
149
sendMessage ( { action : EXT_ACTIONS . SAVE_TO_POCKET_SUCCESS , item } )
143
-
144
- // Set the toolbar icon to saved
145
- // ?? Since we generally can't track this, it feels problematic to
146
- // ?? maintain it in a reasonable fashion. However, it is the way at the moment.
147
- await setToolbarIcon ( id , true )
148
150
} catch ( error ) {
149
151
// Things have gone awry. Let's send the error along
150
152
const message = getErrorMessage ( error )
@@ -154,6 +156,21 @@ async function save(saveData: ExtSave) {
154
156
}
155
157
}
156
158
159
+ async function saveNote ( noteData : ExtNote , source ?: string ) {
160
+ try {
161
+ // Let's add our note
162
+ const item = await addNote ( { ...noteData , source } )
163
+
164
+ // send a message so the popup can display the preview
165
+ sendMessage ( { action : EXT_ACTIONS . ADD_NOTE_SUCCESS , item } )
166
+ } catch ( error ) {
167
+ // Things have gone awry. Let's send the error along
168
+ const message = getErrorMessage ( error )
169
+ sendMessage ( { action : EXT_ACTIONS . ADD_NOTE_FAILURE , error : message } )
170
+ return false
171
+ }
172
+ }
173
+
157
174
export function openPocket ( ) {
158
175
void chrome . tabs . create ( { url : POCKET_SAVES } )
159
176
}
0 commit comments