Skip to content

Commit

Permalink
fix(console): fix edge case for home page with redirects, fix #272
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 19, 2023
1 parent f328a68 commit 6b88267
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/client/app/layout/menu-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { LegacyMenuItem, MaybeGetter, useContext } from '@koishijs/client'
import { computed } from 'vue'
const props = defineProps<{
item: LegacyMenuItem
item: any
menuKey?: string
menuData?: any
}>()
Expand Down
20 changes: 10 additions & 10 deletions packages/client/client/activity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, reactive } from 'vue'
import { Component, reactive, ref } from 'vue'
import { MaybeRefOrGetter, toValue } from '@vueuse/core'
import { Context, Dict, Disposable, Field, omit, root, router, store } from '.'
import { Context, Dict, Disposable, omit, root, routeCache, router, store, Store } from '.'

declare module 'vue-router' {
interface RouteMeta {
Expand All @@ -20,7 +20,7 @@ export namespace Activity {
order?: number
authority?: number
position?: 'top' | 'bottom'
fields?: Field[]
fields?: (keyof Store)[]
/** @deprecated */
when?: () => boolean
disabled?: () => boolean
Expand All @@ -35,6 +35,8 @@ function getActivityId(path: string) {
return path.split('/').find(Boolean) ?? ''
}

export const redirectTo = ref<string>()

export class Activity {
id: string
_disposables: Disposable[] = []
Expand All @@ -53,10 +55,10 @@ export class Activity {
}

handleUpdate() {
const { redirect } = router.currentRoute.value.query
if (typeof redirect === 'string') {
const location = router.resolve(redirect)
if (redirectTo.value) {
const location = router.resolve(redirectTo.value)
if (location.matched.length) {
redirectTo.value = null
router.replace(location)
}
}
Expand Down Expand Up @@ -85,10 +87,8 @@ export class Activity {
this._disposables.forEach(dispose => dispose())
const current = router.currentRoute.value
if (current?.meta?.activity === this) {
router.push({
path: '/',
query: { redirect: current.fullPath },
})
redirectTo.value = current.fullPath
router.push(routeCache['home'] || '/')
}
return delete activities[this.id]
}
Expand Down
14 changes: 9 additions & 5 deletions packages/client/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRouter, createWebHistory, START_LOCATION } from 'vue-router'
import { global } from './data'
import install, { Dict } from './components'
import Overlay from './components/chat/overlay.vue'
import { redirectTo } from './activity'
import { config } from './config'
import { initTask } from './loader'
import { Context, routeCache } from './context'
Expand Down Expand Up @@ -78,16 +79,19 @@ root.slot({
root.on('activity', data => !data)

router.beforeEach(async (to, from) => {
if (to.matched.length) return
if (to.matched.length) {
if (to.matched[0].path !== '/') {
redirectTo.value = null
}
return
}

if (from === START_LOCATION) {
await initTask
to = router.resolve(to)
if (to.matched.length) return to
}

return {
path: '/',
query: { redirect: to.fullPath },
}
redirectTo.value = to.fullPath
return routeCache['home'] || '/'
})

0 comments on commit 6b88267

Please sign in to comment.