106 lines
3.9 KiB
TypeScript
106 lines
3.9 KiB
TypeScript
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';
|
|
|
|
/** 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|reviews)$/, component: ListPage, data: true },
|
|
];
|
|
|
|
export const state = reactive({
|
|
path: '',
|
|
component: null as any,
|
|
props: {} as Record<string, any>,
|
|
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<void> {
|
|
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<void> {
|
|
window.history[replace ? 'replaceState' : 'pushState']({}, '', url);
|
|
window.scrollTo(0, 0);
|
|
|
|
return load(url);
|
|
}
|
|
|
|
export function reload(): Promise<void> {
|
|
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);
|
|
}
|