<template><p></p></template>

<script setup lang="ts">
import { onBeforeRouteUpdate, useRouter } from 'vue-router';
import { inject, onBeforeMount } from 'vue';
import { FS, check_token, isErrorResponse, TokenInjectType } from '@/api';

const router = useRouter();
const jwt = inject<TokenInjectType>('jwt') as TokenInjectType;

async function start_redirect() {
	const token = await check_token(jwt);
	if (!token) return;
	const root = await FS.get_root(token);
	if (isErrorResponse(root)) return jwt.logout();
	await router.push({
		name: 'fs',
		params: { node_id: root.rootId }
	});
}

onBeforeRouteUpdate(async () => {
	await start_redirect();
});
onBeforeMount(async () => {
	await start_redirect();
});
</script>
