-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move non-BG specific PKM util fns into own module
- This fixes a problem where linkedom's service worker was being included in the options UI script bundle. - It stemmed from certain PKM util fns being imported into the options UI code, but living in the same module which (indirectly) imported linkedom/worker. As they lived together, it all got brought in together even though that linkedom/worker stuff wasn't being imported in the UI specifically. - Those non-BG specific PKM util fns have now been moved to their own `utils.ts` module
- Loading branch information
Showing
12 changed files
with
97 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { LOCAL_SERVER_ROOT } from 'src/backup-restore/ui/backup-pane/constants' | ||
import type { Storage } from 'webextension-polyfill' | ||
|
||
export async function getPkmSyncKey(deps: { storageAPI: Storage.Static }) { | ||
let data = await deps.storageAPI.local.get('PKMSYNCpkmSyncKey') | ||
|
||
let pkmSyncKey = data.PKMSYNCpkmSyncKey | ||
|
||
// If pkmSyncKey does not exist, create a new one and store it in local storage | ||
if (!pkmSyncKey) { | ||
// Generate a random string for pkmSyncKey | ||
pkmSyncKey = | ||
Math.random().toString(36).substring(2, 15) + | ||
Math.random().toString(36).substring(2, 15) | ||
await deps.storageAPI.local.set({ PKMSYNCpkmSyncKey: pkmSyncKey }) | ||
} | ||
|
||
return pkmSyncKey | ||
} | ||
|
||
export async function isPkmSyncEnabled(deps: { storageAPI: Storage.Static }) { | ||
try { | ||
const data = await deps.storageAPI.local.get('PKMSYNCpkmFolders') | ||
if ( | ||
data.PKMSYNCpkmFolders && | ||
(data.PKMSYNCpkmFolders.obsidianFolder?.length > 0 || | ||
data.PKMSYNCpkmFolders.logSeqFolder?.length > 0) | ||
) { | ||
return true | ||
} | ||
|
||
return false | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
export async function getFolder( | ||
pkmToSync: string, | ||
deps: { storageAPI: Storage.Static }, | ||
) { | ||
const pkmSyncKey = await getPkmSyncKey(deps) | ||
|
||
const serverToTalkTo = LOCAL_SERVER_ROOT | ||
|
||
const getFolderPath = async (pkmToSync: string) => { | ||
const response = await fetch(`${serverToTalkTo}/set-directory`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
pkmSyncType: pkmToSync, | ||
syncKey: pkmSyncKey, | ||
}), | ||
}) | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`) | ||
} | ||
const directoryPath = await response.text() | ||
|
||
return directoryPath | ||
} | ||
|
||
const folderPath = await getFolderPath(pkmToSync) | ||
|
||
// Fetch the existing "PKMSYNCpkmFolders" from local storage | ||
let data = await deps.storageAPI.local.get('PKMSYNCpkmFolders') | ||
data = data.PKMSYNCpkmFolders || {} | ||
|
||
// Update the value in it that corresponds to the pkmToSync | ||
if (pkmToSync === 'logseq') { | ||
data['logSeqFolder'] = folderPath | ||
} else if (pkmToSync === 'obsidian') { | ||
data['obsidianFolder'] = folderPath | ||
} else if (pkmToSync === 'backup') { | ||
data['backupFolder'] = folderPath | ||
} | ||
|
||
// Write the update to local storage | ||
await deps.storageAPI.local.set({ PKMSYNCpkmFolders: data }) | ||
|
||
return data | ||
} |