2021-11-05 13:38:33 +01:00
|
|
|
import { get, writable } from "svelte/store"
|
2020-11-13 16:42:32 +01:00
|
|
|
import { push } from "svelte-spa-router"
|
2022-01-25 20:22:43 +01:00
|
|
|
import { API } from "api"
|
2021-11-05 13:38:33 +01:00
|
|
|
import { peekStore } from "./peek"
|
|
|
|
import { builderStore } from "./builder"
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
const createRouteStore = () => {
|
2020-11-17 13:08:24 +01:00
|
|
|
const initialState = {
|
|
|
|
routes: [],
|
|
|
|
routeParams: {},
|
|
|
|
activeRoute: null,
|
2020-12-11 15:24:19 +01:00
|
|
|
routeSessionId: Math.random(),
|
2021-05-20 15:47:17 +02:00
|
|
|
routerLoaded: false,
|
2021-07-30 15:01:01 +02:00
|
|
|
queryParams: {},
|
2020-11-17 13:08:24 +01:00
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
const store = writable(initialState)
|
|
|
|
|
2020-11-19 18:55:40 +01:00
|
|
|
const fetchRoutes = async () => {
|
2022-01-20 10:40:53 +01:00
|
|
|
let routeConfig
|
|
|
|
try {
|
2022-01-20 12:30:51 +01:00
|
|
|
routeConfig = await API.fetchClientAppRoutes()
|
2022-01-20 10:40:53 +01:00
|
|
|
} catch (error) {
|
|
|
|
routeConfig = null
|
|
|
|
}
|
2020-11-19 19:39:22 +01:00
|
|
|
let routes = []
|
2022-01-20 10:40:53 +01:00
|
|
|
Object.values(routeConfig?.routes || {}).forEach(route => {
|
|
|
|
Object.entries(route.subpaths || {}).forEach(([path, config]) => {
|
2020-11-19 19:39:22 +01:00
|
|
|
routes.push({
|
|
|
|
path,
|
|
|
|
screenId: config.screenId,
|
|
|
|
})
|
|
|
|
})
|
2020-11-19 18:55:40 +01:00
|
|
|
})
|
2020-12-08 16:44:35 +01:00
|
|
|
|
|
|
|
// Sort route by paths so that the router matches correctly
|
|
|
|
routes.sort((a, b) => {
|
|
|
|
return a.path > b.path ? -1 : 1
|
|
|
|
})
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
store.update(state => {
|
2020-11-17 13:08:24 +01:00
|
|
|
state.routes = routes
|
2020-12-11 15:24:19 +01:00
|
|
|
state.routeSessionId = Math.random()
|
2020-11-17 13:08:24 +01:00
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
2021-05-04 12:32:22 +02:00
|
|
|
const setRouteParams = routeParams => {
|
|
|
|
store.update(state => {
|
2020-11-17 13:08:24 +01:00
|
|
|
state.routeParams = routeParams
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
2021-07-30 15:01:01 +02:00
|
|
|
const setQueryParams = queryParams => {
|
|
|
|
store.update(state => {
|
2021-08-02 16:12:38 +02:00
|
|
|
state.queryParams = {
|
|
|
|
...queryParams,
|
|
|
|
// Never unset the peek param - screen peek modals should always be
|
|
|
|
// in a peek state, even if they navigate to a different page
|
|
|
|
peek: queryParams.peek || state.queryParams?.peek,
|
|
|
|
}
|
2021-07-30 15:01:01 +02:00
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
2021-05-04 12:32:22 +02:00
|
|
|
const setActiveRoute = route => {
|
|
|
|
store.update(state => {
|
|
|
|
state.activeRoute = state.routes.find(x => x.path === route)
|
2020-11-17 13:08:24 +01:00
|
|
|
return state
|
|
|
|
})
|
2020-11-13 16:42:32 +01:00
|
|
|
}
|
2021-11-05 13:38:33 +01:00
|
|
|
const navigate = (url, peek) => {
|
|
|
|
if (get(builderStore).inBuilder) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (url) {
|
|
|
|
// If we're already peeking, don't peek again
|
|
|
|
const isPeeking = get(store).queryParams?.peek
|
|
|
|
if (peek && !isPeeking) {
|
|
|
|
peekStore.actions.showPeek(url)
|
|
|
|
} else {
|
|
|
|
const external = !url.startsWith("/")
|
|
|
|
if (external) {
|
|
|
|
window.location.href = url
|
|
|
|
} else {
|
|
|
|
push(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 15:47:17 +02:00
|
|
|
const setRouterLoaded = () => {
|
|
|
|
store.update(state => ({ ...state, routerLoaded: true }))
|
|
|
|
}
|
2022-01-19 12:22:27 +01:00
|
|
|
const createFullURL = relativeURL => {
|
|
|
|
if (!relativeURL?.startsWith("/")) {
|
|
|
|
return relativeURL
|
|
|
|
}
|
|
|
|
if (!window.location.href.includes("#")) {
|
|
|
|
return `${window.location.href}#${relativeURL}`
|
|
|
|
}
|
|
|
|
const base = window.location.href.split("#")[0]
|
|
|
|
return `${base}#${relativeURL}`
|
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
2021-05-20 15:47:17 +02:00
|
|
|
actions: {
|
|
|
|
fetchRoutes,
|
|
|
|
navigate,
|
2022-01-19 12:22:27 +01:00
|
|
|
createFullURL,
|
2021-05-20 15:47:17 +02:00
|
|
|
setRouteParams,
|
2021-07-30 15:01:01 +02:00
|
|
|
setQueryParams,
|
2021-05-20 15:47:17 +02:00
|
|
|
setActiveRoute,
|
|
|
|
setRouterLoaded,
|
|
|
|
},
|
2020-11-17 13:08:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:18:18 +01:00
|
|
|
export const routeStore = createRouteStore()
|