import { reactive } from 'vue'; import { ApiError, api, boot } from './api'; import { t } from './i18n'; import Dashboard from './pages/Dashboard.vue'; import Fallback from './pages/Fallback.vue'; import Login from './pages/Login.vue'; import PostForm from './pages/PostForm.vue'; import PostsIndex from './pages/PostsIndex.vue'; import Profile from './pages/Profile.vue'; import AccountsIndex from './pages/AccountsIndex.vue'; import GroupsIndex from './pages/GroupsIndex.vue'; import LabelsIndex from './pages/LabelsIndex.vue'; import MediaLibrary from './pages/MediaLibrary.vue'; import AuditLog from './pages/AuditLog.vue'; import StatisticsReport from './pages/StatisticsReport.vue'; import ListPage from './pages/ListPage.vue'; import RecordForm from './pages/RecordForm.vue'; import RecordsIndex from './pages/RecordsIndex.vue'; import PageForm from './pages/PageForm.vue'; import PagesIndex from './pages/PagesIndex.vue'; /** The screens that exist. Every other manager address shows `Fallback` until its module gets a screen. */ const routes: { pattern: RegExp; component: any; guest?: boolean; data?: boolean; source?: string }[] = [ { pattern: /^\/?$/, component: Dashboard, data: true }, { pattern: /^\/login$/, component: Login, guest: true }, { pattern: /^\/profile$/, component: Profile, data: true }, { pattern: /^\/posts$/, component: PostsIndex, data: true }, { pattern: /^\/posts\/create$/, component: PostForm, data: true }, { pattern: /^\/posts\/\d+\/edit$/, component: PostForm, data: true }, { pattern: /^\/media$/, component: MediaLibrary, data: true, source: '/media/items' }, { pattern: /^\/audit$/, component: AuditLog, data: true }, { pattern: /^\/statistics$/, component: StatisticsReport, data: true }, { pattern: /^\/accounts$/, component: AccountsIndex, data: true }, { pattern: /^\/groups$/, component: GroupsIndex, data: true }, { pattern: /^\/labels$/, component: LabelsIndex, data: true }, { pattern: /^\/pages$/, component: PagesIndex, data: true }, { pattern: /^\/pages\/[a-z0-9]+(?:-[a-z0-9]+)*$/, component: PageForm, data: true }, { pattern: /^\/records\/[a-z][a-z0-9_]*$/, component: RecordsIndex, data: true }, { pattern: /^\/records\/[a-z][a-z0-9_]*\/(create|\d+\/edit)$/, component: RecordForm, data: true }, { pattern: /^\/reviews$/, component: ListPage, data: true }, ]; /** * A screen of a site's own bundle (config `manager.extensions`): `pattern` is a regular expression for the path inside the * manager, `data` fetches the same address (or `source`) as JSON and passes it as props. Its screens win over the built in ones. */ export function register(pattern: string, component: any, options: { data?: boolean; source?: string } = {}): void { routes.unshift({ pattern: new RegExp(pattern), component, data: options.data, source: options.source }); } export const state = reactive({ path: '', component: null as any, props: {} as Record, loading: true, forbidden: false, toast: null as { text: string; error: boolean } | null, }); let timer: number | undefined; export function flash(text: string, error = false): void { state.toast = { text, error }; window.clearTimeout(timer); timer = window.setTimeout(() => (state.toast = null), 4000); } /** The path inside the manager: without the base, without the query. */ function inside(url: string): string { return url.split('?')[0].slice(boot.base.length) || '/'; } export async function load(url: string): Promise { const route = routes.find((r) => r.pattern.test(inside(url))); if (!boot.user && !route?.guest) { window.location.assign(boot.base + '/login'); return; } if (boot.user && route?.guest) { return navigate(boot.base, true); } state.loading = true; state.forbidden = false; state.path = url; state.component = route?.component ?? Fallback; state.props = {}; try { state.props = route?.data ? await api.get(route.source ? boot.base + route.source : url) : {}; } catch (error) { if (error instanceof ApiError && error.status === 401) { window.location.assign(boot.base + '/login'); } else if (error instanceof ApiError && error.status === 403) { state.forbidden = true; } else { flash(t('Something went wrong. Try again.'), true); } } finally { state.loading = false; } } export function navigate(url: string, replace = false): Promise { window.history[replace ? 'replaceState' : 'pushState']({}, '', url); window.scrollTo(0, 0); return load(url); } export function reload(): Promise { return load(state.path); } export function start(): void { window.addEventListener('popstate', () => load(window.location.pathname + window.location.search)); void load(window.location.pathname + window.location.search); }