Files
tgCrewUser/src/router/index.ts
2025-06-26 11:06:48 +03:00

97 lines
2.7 KiB
TypeScript

import { defineRouter } from '#q-app/wrappers'
import {
createMemoryHistory,
createRouter,
createWebHashHistory,
createWebHistory,
} from 'vue-router'
import routes from './routes'
import { useProjectsStore } from 'stores/projects'
export default defineRouter(function (/* { store, ssrContext } */) {
const createHistory = process.env.SERVER
? createMemoryHistory
: (process.env.VUE_ROUTER_MODE === 'history' ? createWebHistory : createWebHashHistory)
const Router = createRouter({
scrollBehavior: () => ({ left: 0, top: 0 }),
routes,
// Leave this as is and make changes in quasar.conf.js instead!
// quasar.conf.js -> build -> vueRouterMode
// quasar.conf.js -> build -> publicPath
history: createHistory(process.env.VUE_ROUTER_BASE),
})
Router.beforeEach(async (to) => {
if (to.name === 'settings') return
if (to.name === '404') return
const projectsStore = useProjectsStore()
console.log('router mount', projectsStore.startRouteInfo)
if (projectsStore.startRouteInfo && to.path === '/') {
const { id, taskId, meetingId } = projectsStore.startRouteInfo
projectsStore.setStartRouteInfo(null)
if (!projectsStore.isInit) await projectsStore.init()
const project = projectsStore.projectById(id)
if (!project) return { name: '404' }
return taskId
? { name: 'task_info', params: { id, taskId } }
: meetingId
? { name: 'meeting_info', params: { id, meetingId } }
: { name: 'files', params: { id } }
}
if (to.params.id) {
const projectId = Number(to.params.id)
if (!projectsStore.isInit) await projectsStore.init()
const project = projectsStore.projectById(projectId)
if (!project) return { name: '404' }
if (projectsStore.currentProjectId !== projectId) {
projectsStore.setCurrentProjectId(projectId)
}
} else {
return { name: '404' }
}
})
const handleBackButton = async () => {
const currentRoute = Router.currentRoute.value
if (currentRoute.meta.backRoute) {
await Router.push(currentRoute.meta.backRoute);
} else {
if (window.history.length > 1) {
Router.go(-1)
} else {
await Router.push({ name: 'main'})
}
}
}
Router.afterEach((to) => {
const BackButton = window.Telegram?.WebApp?.BackButton
if (BackButton) {
// Управление видимостью
if (to.meta.hideBackButton) {
BackButton.hide()
} else {
BackButton.show()
}
// Обновляем обработчик клика
BackButton.offClick(handleBackButton as () => void)
BackButton.onClick(handleBackButton as () => void)
}
})
return Router
})