Skip to content

Commit

Permalink
fix: Improve failed messages
Browse files Browse the repository at this point in the history
  • Loading branch information
brunojm committed Sep 9, 2022
1 parent 6c5a5be commit c30af2c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function App() {
checkForCurrentGraph()
// @ts-ignore
if (window.onAnotherGraph) {
logseq.App.showMsg(`Readwise is connected to your other graph ${logseq.settings!.currentGraph.name}. Please switch to that to sync your latest highlights`, "warning")
logseq.App.showMsg(`Readwise is connected to your other graph "${logseq.settings!.currentGraph.name}". Please switch to that to sync your latest highlights`, "warning")
return
}
if (isSyncing || logseq.settings!.currentSyncStatusID !== 0) {
Expand All @@ -57,7 +57,7 @@ function App() {
async function clearInstallation() {
// @ts-ignore
if (window.onAnotherGraph) {
logseq.App.showMsg(`Readwise is connected to your other graph ${logseq.settings!.currentGraph.name}. Please switch to that to reset your installation`, "warning")
logseq.App.showMsg(`Readwise is connected to your other graph "${logseq.settings!.currentGraph.name}". Please switch to that to reset your installation`, "warning")
return
}

Expand Down
33 changes: 24 additions & 9 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ function getAuthHeaders() {
}
}

function handleSyncError(msg: string) {
function handleSyncError(notificationCallback: () => void) {
clearSettingsAfterRun()
logseq.updateSettings({
lastSyncFailed: true
})
logseq.App.showMsg(msg, "error")
notificationCallback()
}

function clearSettingsAfterRun() {
Expand Down Expand Up @@ -280,7 +280,7 @@ export async function removeDocuments(documentsToRemove: Array<string>, setNotif
}

// @ts-ignore
async function downloadArchive(exportID: number, setNotification?, setIsSyncing?): Promise<void> {
async function downloadArchive(exportID: number, setNotification?, setIsSyncing?, auto?): Promise<void> {
const artifactURL = `${baseURL}/api/download_artifact/${exportID}`
if (exportID <= logseq.settings!.lastSavedStatusID) {
console.log(`Readwise Official plugin: Already saved data from export ${exportID}`)
Expand Down Expand Up @@ -316,7 +316,14 @@ async function downloadArchive(exportID: number, setNotification?, setIsSyncing?
if (window.onAnotherGraph) {
setIsSyncing(false)
setNotification(null)
handleSyncError(`Graph changed during sync, please return to ${logseq.settings!.currentGraph.name} to complete the sync`)
handleSyncError(() => {
const msg = `Graph changed during sync, please return to graph "${logseq.settings!.currentGraph.name}" to complete the sync`
if (!auto) {
logseq.App.showMsg(msg, "error")
} else {
console.log(msg)
}
})
return
}
if (page !== null) {
Expand Down Expand Up @@ -377,7 +384,7 @@ async function downloadArchive(exportID: number, setNotification?, setIsSyncing?
}

// @ts-ignore
async function getExportStatus(statusID?: number, setNotification?, setIsSyncing?) {
async function getExportStatus(statusID?: number, setNotification?, setIsSyncing?, auto?) {
const statusId = statusID || logseq.settings!.currentSyncStatusID
const url = `${baseURL}/api/get_export_status?exportStatusId=${statusId}`
let response, data: ExportStatusResponse
Expand Down Expand Up @@ -408,14 +415,22 @@ async function getExportStatus(statusID?: number, setNotification?, setIsSyncing
}
// re-try in 2 secs
await new Promise(r => setTimeout(r, 2000))
await getExportStatus(statusId, setNotification, setIsSyncing)
await getExportStatus(statusId, setNotification, setIsSyncing, auto)
} else if (SUCCESS_STATUSES.includes(data.taskStatus)) {
setNotification(null)
return downloadArchive(statusId, setNotification, setIsSyncing)
return downloadArchive(statusId, setNotification, setIsSyncing, auto)
} else {
setNotification(null)
setIsSyncing(false)
handleSyncError("Sync failed")
handleSyncError(() => {
const msg = 'Sync failed'
if (!auto) {
logseq.App.showMsg(msg, "error")
} else {
console.log(msg)
}
}
)
}
setNotification(null)
setIsSyncing(false)
Expand Down Expand Up @@ -520,7 +535,7 @@ export async function syncHighlights(auto?: boolean, setNotification?, setIsSync
})
if (response.status === 201) {
logseq.App.showMsg("Syncing Readwise data")
return getExportStatus(data.latest_id, setNotification, setIsSyncing)
return getExportStatus(data.latest_id, setNotification, setIsSyncing, auto)
} else {
setIsSyncing(false)
setNotification(null)
Expand Down

0 comments on commit c30af2c

Please sign in to comment.