Skip to content

Commit

Permalink
#172 Loads custom default config from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
victrme committed Dec 27, 2023
1 parent 82e5367 commit d2a9e9d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/scripts/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ export const SYNC_DEFAULT: Sync.Storage = {
type: 'classic',
frequency: 'day',
last: 1650516688,
userlist: [],
},
font: {
family: '',
Expand Down
24 changes: 14 additions & 10 deletions src/scripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { settingsInit } from './settings'

import storage from './storage'
import clock from './features/clock'
import notes from './features/notes'
import quotes, { oldJSONToCSV } from './features/quotes'
import clock from './features/clock'
import weather from './features/weather'
import searchbar from './features/searchbar'
import customFont from './features/fonts'
Expand All @@ -12,6 +10,8 @@ import moveElements from './features/move'
import hideElements from './features/hide'
import localBackgrounds from './features/localbackgrounds'
import unsplashBackgrounds from './features/unsplash'
import quotes, { oldJSONToCSV } from './features/quotes'
import storage, { getSyncDefaults } from './storage'

import { SYSTEM_OS, BROWSER, PLATFORM, IS_MOBILE, SYNC_DEFAULT, CURRENT_VERSION } from './defaults'
import { traduction, tradThis, setTranslationCache } from './utils/translations'
Expand Down Expand Up @@ -445,24 +445,28 @@ function startup(data: Sync.Storage, local: Local.Storage) {
onlineAndMobileHandler()

try {
const { sync, local } = await storage.init()
let { sync, local } = await storage.init()
const version_old = sync?.about?.version
const isUpdate = version_old !== CURRENT_VERSION

if (isUpdate) {
console.log(`Version change: ${version_old} => ${CURRENT_VERSION}`)
const hasChanged = version_old !== CURRENT_VERSION

if (hasChanged) {
if (version_old === undefined && Object.keys(sync).length === 0) {
console.log(`First install: ${CURRENT_VERSION}`)
sync = await getSyncDefaults()
} else {
console.log(`Version change: ${version_old} => ${CURRENT_VERSION}`)
}

if (Array.isArray(sync?.quotes?.userlist)) {
const newuserlist = oldJSONToCSV(sync?.quotes?.userlist as unknown as Quotes.UserInput)
sync.quotes.userlist = newuserlist
}

sync.about = SYNC_DEFAULT.about

storage.sync.set(sync)
}

await setTranslationCache(sync.lang, local, isUpdate)
await setTranslationCache(sync.lang, local, hasChanged)

startup(sync, local)
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions src/scripts/services/webext-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ chrome.storage.sync.get(null, (sync) => {
startupStorage.sync = sync
document.dispatchEvent(new Event('webextstorage'))
})

chrome.storage.local.get(null, (local) => {
startupStorage.local = local
document.dispatchEvent(new Event('webextstorage'))
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import storage from './storage'
import clock from './features/clock'
import notes from './features/notes'
import quotes from './features/quotes'
Expand All @@ -11,6 +10,7 @@ import hideElements from './features/hide'
import moveElements from './features/move'
import localBackgrounds from './features/localbackgrounds'
import unsplashBackgrounds from './features/unsplash'
import storage, { getSyncDefaults } from './storage'

import langList from './langs'
import parse from './utils/parse'
Expand Down Expand Up @@ -1186,7 +1186,7 @@ function paramsReset(action: 'yes' | 'no' | 'conf') {
storage.local.clear()

setTimeout(() => {
storage.sync.set({ ...SYNC_DEFAULT })
storage.sync.set({ ...getSyncDefaults() })
storage.local.set({ ...LOCAL_DEFAULT })
fadeOut()
}, 50)
Expand Down
13 changes: 12 additions & 1 deletion src/scripts/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ function verifyDataAsSync(data: Keyval) {
return data as Sync.Storage
}

export async function getSyncDefaults(): Promise<Sync.Storage> {
try {
const json = await (await fetch('defaults.json')).json()
return verifyDataAsSync(json)
} catch (error) {
console.log('No defaults.json settings found')
}

return SYNC_DEFAULT
}

function online(): Storage {
const sync = {
set: function (value: Keyval) {
Expand Down Expand Up @@ -178,7 +189,7 @@ function webext(): Storage {
//@ts-ignore
startupStorage = undefined

return { sync: verifyDataAsSync(sync), local }
return { sync, local }
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MAIN_API, FALLBACK_API } from './defaults'
import { MAIN_API, FALLBACK_API, SYNC_DEFAULT } from './defaults'
import suntime from './utils/suntime'

function shuffledAPIUrls(): string[] {
Expand Down

0 comments on commit d2a9e9d

Please sign in to comment.