Refactor admin/index to use Composition API (#8662)

* refactor(client): refactor admin/index to use Composition API

* fix(client): fix navigation to initial admin pages

* Apply review suggestions from @Johann150

Co-authored-by: Johann150 <johann@qwertqwefsday.eu>

* fix(client): re-add abuses page to admin/index

Co-authored-by: Johann150 <johann@qwertqwefsday.eu>
This commit is contained in:
Andy 2022-05-17 18:32:21 +02:00 committed by GitHub
parent bf6cc34961
commit f03390f0b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,6 @@
<template> <template>
<div ref="el" class="hiyeyicy" :class="{ wide: !narrow }"> <div ref="el" class="hiyeyicy" :class="{ wide: !narrow }">
<div v-if="!narrow || page == null" class="nav"> <div v-if="!narrow || initialPage == null" class="nav">
<MkHeader :info="header"></MkHeader> <MkHeader :info="header"></MkHeader>
<MkSpacer :content-max="700" :margin-min="16"> <MkSpacer :content-max="700" :margin-min="16">
@ -12,21 +12,21 @@
<MkInfo v-if="noMaintainerInformation" warn class="info">{{ $ts.noMaintainerInformationWarning }} <MkA to="/admin/settings" class="_link">{{ $ts.configure }}</MkA></MkInfo> <MkInfo v-if="noMaintainerInformation" warn class="info">{{ $ts.noMaintainerInformationWarning }} <MkA to="/admin/settings" class="_link">{{ $ts.configure }}</MkA></MkInfo>
<MkInfo v-if="noBotProtection" warn class="info">{{ $ts.noBotProtectionWarning }} <MkA to="/admin/security" class="_link">{{ $ts.configure }}</MkA></MkInfo> <MkInfo v-if="noBotProtection" warn class="info">{{ $ts.noBotProtectionWarning }} <MkA to="/admin/security" class="_link">{{ $ts.configure }}</MkA></MkInfo>
<MkSuperMenu :def="menuDef" :grid="page == null"></MkSuperMenu> <MkSuperMenu :def="menuDef" :grid="initialPage == null"></MkSuperMenu>
</div> </div>
</MkSpacer> </MkSpacer>
</div> </div>
<div class="main"> <div v-if="!(narrow && initialPage == null)" class="main">
<MkStickyContainer> <MkStickyContainer>
<template #header><MkHeader v-if="childInfo && !childInfo.hideHeader" :info="childInfo"/></template> <template #header><MkHeader v-if="childInfo && !childInfo.hideHeader" :info="childInfo"/></template>
<component :is="component" :ref="el => pageChanged(el)" :key="page" v-bind="pageProps"/> <component :is="component" :ref="el => pageChanged(el)" :key="initialPage" v-bind="pageProps"/>
</MkStickyContainer> </MkStickyContainer>
</div> </div>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { computed, defineAsyncComponent, defineComponent, isRef, nextTick, onMounted, reactive, ref, watch } from 'vue'; import { defineAsyncComponent, nextTick, onMounted, onUnmounted, provide, watch } from 'vue';
import { i18n } from '@/i18n'; import { i18n } from '@/i18n';
import MkSuperMenu from '@/components/ui/super-menu.vue'; import MkSuperMenu from '@/components/ui/super-menu.vue';
import MkInfo from '@/components/ui/info.vue'; import MkInfo from '@/components/ui/info.vue';
@ -35,62 +35,42 @@ import { instance } from '@/instance';
import * as symbols from '@/symbols'; import * as symbols from '@/symbols';
import * as os from '@/os'; import * as os from '@/os';
import { lookupUser } from '@/scripts/lookup-user'; import { lookupUser } from '@/scripts/lookup-user';
import { MisskeyNavigator } from '@/scripts/navigate';
export default defineComponent({ const isEmpty = (x: string | null) => x == null || x === '';
components: {
MkSuperMenu,
MkInfo,
},
provide: { const nav = new MisskeyNavigator();
shouldOmitHeaderTitle: false,
},
props: {
initialPage: {
type: String,
required: false
}
},
setup(props, context) {
const indexInfo = { const indexInfo = {
title: i18n.ts.controlPanel, title: i18n.ts.controlPanel,
icon: 'fas fa-cog', icon: 'fas fa-cog',
bg: 'var(--bg)', bg: 'var(--bg)',
hideHeader: true, hideHeader: true,
}; };
const INFO = ref(indexInfo);
const childInfo = ref(null);
const page = ref(props.initialPage);
const narrow = ref(false);
const view = ref(null);
const el = ref(null);
const pageChanged = (page) => {
if (page == null) return;
const viewInfo = page[symbols.PAGE_INFO];
if (isRef(viewInfo)) {
watch(viewInfo, () => {
childInfo.value = viewInfo.value;
}, { immediate: true });
} else {
childInfo.value = viewInfo;
}
};
const pageProps = ref({});
const isEmpty = (x: any) => x == null || x == ''; const props = defineProps<{
initialPage?: string,
}>();
const noMaintainerInformation = ref(false); provide('shouldOmitHeaderTitle', false);
const noBotProtection = ref(false);
os.api('meta', { detail: true }).then(meta => { let INFO = $ref(indexInfo);
// TODO: meta let childInfo = $ref(null);
noMaintainerInformation.value = isEmpty(meta.maintainerName) || isEmpty(meta.maintainerEmail); let page = $ref(props.initialPage);
noBotProtection.value = !meta.enableHcaptcha && !meta.enableRecaptcha; let narrow = $ref(false);
let view = $ref(null);
let el = $ref(null);
let pageProps = $ref({});
let noMaintainerInformation = isEmpty(instance.maintainerName) || isEmpty(instance.maintainerEmail);
let noBotProtection = !instance.enableHcaptcha && !instance.enableRecaptcha;
const NARROW_THRESHOLD = 600;
const ro = new ResizeObserver((entries, observer) => {
if (entries.length === 0) return;
narrow = entries[0].borderBoxSize[0].inlineSize < NARROW_THRESHOLD;
}); });
const menuDef = computed(() => [{ const menuDef = $computed(() => [{
title: i18n.ts.quickAction, title: i18n.ts.quickAction,
items: [{ items: [{
type: 'button', type: 'button',
@ -109,47 +89,47 @@ export default defineComponent({
icon: 'fas fa-tachometer-alt', icon: 'fas fa-tachometer-alt',
text: i18n.ts.dashboard, text: i18n.ts.dashboard,
to: '/admin/overview', to: '/admin/overview',
active: page.value === 'overview', active: props.initialPage === 'overview',
}, { }, {
icon: 'fas fa-users', icon: 'fas fa-users',
text: i18n.ts.users, text: i18n.ts.users,
to: '/admin/users', to: '/admin/users',
active: page.value === 'users', active: props.initialPage === 'users',
}, { }, {
icon: 'fas fa-laugh', icon: 'fas fa-laugh',
text: i18n.ts.customEmojis, text: i18n.ts.customEmojis,
to: '/admin/emojis', to: '/admin/emojis',
active: page.value === 'emojis', active: props.initialPage === 'emojis',
}, { }, {
icon: 'fas fa-globe', icon: 'fas fa-globe',
text: i18n.ts.federation, text: i18n.ts.federation,
to: '/admin/federation', to: '/admin/federation',
active: page.value === 'federation', active: props.initialPage === 'federation',
}, { }, {
icon: 'fas fa-clipboard-list', icon: 'fas fa-clipboard-list',
text: i18n.ts.jobQueue, text: i18n.ts.jobQueue,
to: '/admin/queue', to: '/admin/queue',
active: page.value === 'queue', active: props.initialPage === 'queue',
}, { }, {
icon: 'fas fa-cloud', icon: 'fas fa-cloud',
text: i18n.ts.files, text: i18n.ts.files,
to: '/admin/files', to: '/admin/files',
active: page.value === 'files', active: props.initialPage === 'files',
}, { }, {
icon: 'fas fa-broadcast-tower', icon: 'fas fa-broadcast-tower',
text: i18n.ts.announcements, text: i18n.ts.announcements,
to: '/admin/announcements', to: '/admin/announcements',
active: page.value === 'announcements', active: props.initialPage === 'announcements',
}, { }, {
icon: 'fas fa-audio-description', icon: 'fas fa-audio-description',
text: i18n.ts.ads, text: i18n.ts.ads,
to: '/admin/ads', to: '/admin/ads',
active: page.value === 'ads', active: props.initialPage === 'ads',
}, { }, {
icon: 'fas fa-exclamation-circle', icon: 'fas fa-exclamation-circle',
text: i18n.ts.abuseReports, text: i18n.ts.abuseReports,
to: '/admin/abuses', to: '/admin/abuses',
active: page.value === 'abuses', active: props.initialPage === 'abuses',
}], }],
}, { }, {
title: i18n.ts.settings, title: i18n.ts.settings,
@ -157,47 +137,47 @@ export default defineComponent({
icon: 'fas fa-cog', icon: 'fas fa-cog',
text: i18n.ts.general, text: i18n.ts.general,
to: '/admin/settings', to: '/admin/settings',
active: page.value === 'settings', active: props.initialPage === 'settings',
}, { }, {
icon: 'fas fa-envelope', icon: 'fas fa-envelope',
text: i18n.ts.emailServer, text: i18n.ts.emailServer,
to: '/admin/email-settings', to: '/admin/email-settings',
active: page.value === 'email-settings', active: props.initialPage === 'email-settings',
}, { }, {
icon: 'fas fa-cloud', icon: 'fas fa-cloud',
text: i18n.ts.objectStorage, text: i18n.ts.objectStorage,
to: '/admin/object-storage', to: '/admin/object-storage',
active: page.value === 'object-storage', active: props.initialPage === 'object-storage',
}, { }, {
icon: 'fas fa-lock', icon: 'fas fa-lock',
text: i18n.ts.security, text: i18n.ts.security,
to: '/admin/security', to: '/admin/security',
active: page.value === 'security', active: props.initialPage === 'security',
}, { }, {
icon: 'fas fa-globe', icon: 'fas fa-globe',
text: i18n.ts.relays, text: i18n.ts.relays,
to: '/admin/relays', to: '/admin/relays',
active: page.value === 'relays', active: props.initialPage === 'relays',
}, { }, {
icon: 'fas fa-share-alt', icon: 'fas fa-share-alt',
text: i18n.ts.integration, text: i18n.ts.integration,
to: '/admin/integrations', to: '/admin/integrations',
active: page.value === 'integrations', active: props.initialPage === 'integrations',
}, { }, {
icon: 'fas fa-ban', icon: 'fas fa-ban',
text: i18n.ts.instanceBlocking, text: i18n.ts.instanceBlocking,
to: '/admin/instance-block', to: '/admin/instance-block',
active: page.value === 'instance-block', active: props.initialPage === 'instance-block',
}, { }, {
icon: 'fas fa-ghost', icon: 'fas fa-ghost',
text: i18n.ts.proxyAccount, text: i18n.ts.proxyAccount,
to: '/admin/proxy-account', to: '/admin/proxy-account',
active: page.value === 'proxy-account', active: props.initialPage === 'proxy-account',
}, { }, {
icon: 'fas fa-cogs', icon: 'fas fa-cogs',
text: i18n.ts.other, text: i18n.ts.other,
to: '/admin/other-settings', to: '/admin/other-settings',
active: page.value === 'other-settings', active: props.initialPage === 'other-settings',
}], }],
}, { }, {
title: i18n.ts.info, title: i18n.ts.info,
@ -205,12 +185,13 @@ export default defineComponent({
icon: 'fas fa-database', icon: 'fas fa-database',
text: i18n.ts.database, text: i18n.ts.database,
to: '/admin/database', to: '/admin/database',
active: page.value === 'database', active: props.initialPage === 'database',
}], }],
}]); }]);
const component = computed(() => {
if (page.value == null) return null; const component = $computed(() => {
switch (page.value) { if (props.initialPage == null) return null;
switch (props.initialPage) {
case 'overview': return defineAsyncComponent(() => import('./overview.vue')); case 'overview': return defineAsyncComponent(() => import('./overview.vue'));
case 'users': return defineAsyncComponent(() => import('./users.vue')); case 'users': return defineAsyncComponent(() => import('./users.vue'));
case 'emojis': return defineAsyncComponent(() => import('./emojis.vue')); case 'emojis': return defineAsyncComponent(() => import('./emojis.vue'));
@ -234,31 +215,50 @@ export default defineComponent({
}); });
watch(component, () => { watch(component, () => {
pageProps.value = {}; pageProps = {};
nextTick(() => { nextTick(() => {
scroll(el.value, { top: 0 }); scroll(el, { top: 0 });
}); });
}, { immediate: true }); }, { immediate: true });
watch(() => props.initialPage, () => { watch(() => props.initialPage, () => {
if (props.initialPage == null && !narrow.value) { if (props.initialPage == null && !narrow) {
page.value = 'overview'; nav.push('/admin/overview');
} else { } else {
page.value = props.initialPage;
if (props.initialPage == null) { if (props.initialPage == null) {
INFO.value = indexInfo; INFO = indexInfo;
} }
} }
}); });
onMounted(() => { watch(narrow, () => {
narrow.value = el.value.offsetWidth < 800; if (props.initialPage == null && !narrow) {
if (!narrow.value) { nav.push('/admin/overview');
page.value = 'overview';
} }
}); });
onMounted(() => {
ro.observe(el);
narrow = el.offsetWidth < NARROW_THRESHOLD;
if (props.initialPage == null && !narrow) {
nav.push('/admin/overview');
}
});
onUnmounted(() => {
ro.disconnect();
});
const pageChanged = (page) => {
if (page == null) {
childInfo = null;
} else {
childInfo = page[symbols.PAGE_INFO];
}
};
const invite = () => { const invite = () => {
os.api('admin/invite').then(x => { os.api('admin/invite').then(x => {
os.alert({ os.alert({
@ -301,26 +301,11 @@ export default defineComponent({
}], ev.currentTarget ?? ev.target); }], ev.currentTarget ?? ev.target);
}; };
return { defineExpose({
[symbols.PAGE_INFO]: INFO, [symbols.PAGE_INFO]: INFO,
menuDef,
header: { header: {
title: i18n.ts.controlPanel, title: i18n.ts.controlPanel,
}, }
noMaintainerInformation,
noBotProtection,
page,
narrow,
view,
el,
pageChanged,
childInfo,
pageProps,
component,
invite,
lookup,
};
},
}); });
</script> </script>