1
0
Fork 0
mirror of synced 2024-09-20 03:08:18 +12:00
budibase/packages/client/src/stores/app.js

55 lines
1.3 KiB
JavaScript

import { API } from "api"
import { get, writable, derived } from "svelte/store"
const initialState = {
appId: null,
isDevApp: false,
clientLoadTime: window.INIT_TIME ? Date.now() - window.INIT_TIME : null,
}
const createAppStore = () => {
const store = writable(initialState)
const derivedStore = derived(store, $store => {
return {
...$store,
isDevApp: $store.appId?.startsWith("app_dev"),
}
})
// Fetches the app definition including screens, layouts and theme
const fetchAppDefinition = async () => {
const appId = get(store)?.appId
if (!appId) {
throw "Cannot fetch app definition without app ID set"
}
try {
const appDefinition = await API.fetchAppPackage(appId)
store.set({
...initialState,
...appDefinition,
appId: appDefinition?.application?.appId,
})
} catch (error) {
store.set(initialState)
}
}
// Sets the initial app ID
const setAppId = id => {
store.update(state => {
if (state) {
state.appId = id
} else {
state = { appId: id }
}
return state
})
}
return {
subscribe: derivedStore.subscribe,
actions: { setAppId, fetchAppDefinition },
}
}
export const appStore = createAppStore()