Skip to content

Commit

Permalink
feat(client): separate settings to subroutes
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 22, 2023
1 parent 61c3e00 commit 5614aaa
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 59 deletions.
2 changes: 0 additions & 2 deletions packages/client/client/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export * from './slot'
export * from 'vue-i18n'
export * from '@koishijs/components'

export { fallbackWithLocaleChain } from '@intlify/core-base'

export { icons, ChatImage }

components.extensions.add({
Expand Down
56 changes: 5 additions & 51 deletions packages/client/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Console } from '@koishijs/plugin-console'
import { defineComponent, h, resolveComponent } from 'vue'
import { createRouter, createWebHistory, START_LOCATION } from 'vue-router'
import { global, Store, store } from './data'
import install, { isNullable } from './components'
import { global } from './data'
import install from './components'
import Overlay from './components/chat/overlay.vue'
import Settings from './extensions/settings.vue'
import Settings from './settings/index.vue'
import { initTask } from './loader'
import { Context } from './context'
import { createI18n } from 'vue-i18n'
Expand All @@ -17,6 +15,7 @@ export * from './config'
export * from './context'
export * from './loader'
export * from './data'
export * from './utils'

export default install

Expand Down Expand Up @@ -44,7 +43,7 @@ root.slot({
})

root.page({
path: '/settings',
path: '/settings/:name*',
name: '用户设置',
icon: 'activity:settings',
position: 'bottom',
Expand All @@ -68,48 +67,3 @@ router.beforeEach(async (to, from) => {
query: { redirect: to.fullPath },
}
})

// component helper

export type Field = keyof Console.Services

export namespace Card {
export function create(render: Function, fields: readonly Field[] = []) {
return defineComponent({
render: () => fields.every(key => store[key]) ? render() : null,
})
}

export interface NumericOptions {
icon: string
title: string
type?: string
fields?: Field[]
content: (store: Store) => any
}

export function numeric({ type, icon, fields, title, content }: NumericOptions) {
if (!type) {
return defineComponent(() => () => {
if (!fields.every(key => store[key])) return
return h(resolveComponent('k-numeric'), { icon, title }, () => content(store))
})
}

return defineComponent(() => () => {
if (!fields.every(key => store[key])) return
let value = content(store)
if (isNullable(value)) return
if (type === 'size') {
if (value >= (1 << 20) * 1000) {
value = (value / (1 << 30)).toFixed(1) + ' GB'
} else if (value >= (1 << 10) * 1000) {
value = (value / (1 << 20)).toFixed(1) + ' MB'
} else {
value = (value / (1 << 10)).toFixed(1) + ' KB'
}
}
return h(resolveComponent('k-numeric'), { icon, title }, () => [value])
})
}
}
19 changes: 19 additions & 0 deletions packages/client/client/settings/general.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<k-content>
<k-schema :schema="schema" :initial="config" v-model="config"></k-schema>
</k-content>
</template>

<script lang="ts" setup>
import { config, Schema } from '..'
const schema = Schema.object({
locale: Schema.union(['zh-CN', 'en-US']).description('语言设置。'),
}).description('通用设置')
</script>

<style lang="scss">
</style>
73 changes: 73 additions & 0 deletions packages/client/client/settings/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<template>
<k-layout main="darker page-settings">
<template #left>
<el-scrollbar>
<el-tree
ref="tree"
:data="data"
:default-expand-all="true"
@node-click="handleClick"
></el-tree>
</el-scrollbar>
</template>
<keep-alive>
<component :is="components[path].component" :key="path" />
</keep-alive>
</k-layout>
</template>

<script lang="ts" setup>
import { useRoute, useRouter } from 'vue-router'
import { computed } from 'vue'
import General from './general.vue'
import Theme from './theme.vue'
const route = useRoute()
const router = useRouter()
const components = {
'': {
label: '通用',
component: General,
},
appearance: {
label: '外观',
component: Theme,
},
}
interface Tree {
id: string
label: string
children?: Tree[]
}
const data = computed(() => {
return Object.entries(components).map<Tree>(([id, { label }]) => ({
id,
label,
}))
})
function handleClick(tree: Tree) {
if (tree.children) return
path.value = tree.id
}
const path = computed({
get() {
const name = route.params.name.toString()
return name in components ? name : ''
},
set(value) {
if (!(value in components)) value = ''
router.replace('/settings/' + value)
},
})
</script>

<style lang="scss">
</style>
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<template>
<k-layout main="darker page-settings">
<k-content>
<k-schema :schema="schema" :initial="config" v-model="config"></k-schema>
</k-content>
</k-layout>
<k-content>
<k-schema :schema="schema" :initial="config" v-model="config"></k-schema>
</k-content>
</template>

<script lang="ts" setup>
Expand All @@ -12,7 +10,6 @@ import { config, Schema } from '..'
const schema = Schema.object({
isDark: Schema.boolean().description('暗色模式。'),
locale: Schema.union(['zh-CN', 'en-US']).description('语言设置。'),
}).description('外观设置')
</script>
Expand Down
47 changes: 47 additions & 0 deletions packages/client/client/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { defineComponent, h, resolveComponent } from 'vue'
import { Console } from '@koishijs/plugin-console'
import { isNullable } from 'cosmokit'
import { Store, store } from './data'

export type Field = keyof Console.Services

export namespace Card {
export function create(render: Function, fields: readonly Field[] = []) {
return defineComponent({
render: () => fields.every(key => store[key]) ? render() : null,
})
}

export interface NumericOptions {
icon: string
title: string
type?: string
fields?: Field[]
content: (store: Store) => any
}

export function numeric({ type, icon, fields, title, content }: NumericOptions) {
if (!type) {
return defineComponent(() => () => {
if (!fields.every(key => store[key])) return
return h(resolveComponent('k-numeric'), { icon, title }, () => content(store))
})
}

return defineComponent(() => () => {
if (!fields.every(key => store[key])) return
let value = content(store)
if (isNullable(value)) return
if (type === 'size') {
if (value >= (1 << 20) * 1000) {
value = (value / (1 << 30)).toFixed(1) + ' GB'
} else if (value >= (1 << 10) * 1000) {
value = (value / (1 << 20)).toFixed(1) + ' MB'
} else {
value = (value / (1 << 10)).toFixed(1) + ' KB'
}
}
return h(resolveComponent('k-numeric'), { icon, title }, () => [value])
})
}
}

0 comments on commit 5614aaa

Please sign in to comment.