54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
import { inject, provide } from 'vue';
|
|
import { useMapGetter } from 'dashboard/composables/store';
|
|
import { usePolicy } from 'dashboard/composables/usePolicy';
|
|
import { useRouter } from 'vue-router';
|
|
|
|
const SidebarControl = Symbol('SidebarControl');
|
|
|
|
export function useSidebarContext() {
|
|
const context = inject(SidebarControl, null);
|
|
if (context === null) {
|
|
throw new Error(`Component is missing a parent <Sidebar /> component.`);
|
|
}
|
|
|
|
const router = useRouter();
|
|
const isOnChatwootCloud = useMapGetter('globalConfig/isOnChatwootCloud');
|
|
|
|
const { checkFeatureAllowed, checkPermissions } = usePolicy();
|
|
|
|
const resolvePath = to => {
|
|
if (to) return router.resolve(to)?.path || '/';
|
|
return '/';
|
|
};
|
|
|
|
const resolvePermissions = to => {
|
|
if (to) return router.resolve(to)?.meta?.permissions ?? [];
|
|
return [];
|
|
};
|
|
|
|
const resolveFeatureFlag = to => {
|
|
if (to) return router.resolve(to)?.meta?.featureFlag || '';
|
|
return '';
|
|
};
|
|
|
|
const isAllowed = to => {
|
|
const permissions = resolvePermissions(to);
|
|
const featureFlag = resolveFeatureFlag(to);
|
|
|
|
return checkPermissions(permissions) && checkFeatureAllowed(featureFlag);
|
|
};
|
|
|
|
return {
|
|
...context,
|
|
resolvePath,
|
|
resolvePermissions,
|
|
resolveFeatureFlag,
|
|
isAllowed,
|
|
isOnChatwootCloud,
|
|
};
|
|
}
|
|
|
|
export function provideSidebarContext(context) {
|
|
provide(SidebarControl, context);
|
|
}
|