Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { UmbBlockWorkspaceElementManagerNames } from '../../block-workspace.context.js';

Check warning on line 1 in src/Umbraco.Web.UI.Client/src/packages/block/block/workspace/views/edit/block-workspace-view-edit.element.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (release/17.2)

❌ New issue: Overall Code Complexity

This module has a mean cyclomatic complexity of 4.57 across 7 functions. The mean complexity threshold is 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
import { UMB_BLOCK_WORKSPACE_CONTEXT } from '../../block-workspace.context-token.js';
import type { UmbBlockWorkspaceViewEditTabElement } from './block-workspace-view-edit-tab.element.js';
import { css, html, customElement, state, repeat, property } from '@umbraco-cms/backoffice/external/lit';
import { css, html, customElement, state, repeat, property, nothing } from '@umbraco-cms/backoffice/external/lit';
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import type { UmbContentTypeModel, UmbPropertyTypeContainerMergedModel } from '@umbraco-cms/backoffice/content-type';
import { UmbContentTypeContainerStructureHelper } from '@umbraco-cms/backoffice/content-type';
Expand Down Expand Up @@ -101,39 +101,38 @@
if (!this._tabs || !this.#blockWorkspace) return;
const routes: UmbRoute[] = [];

if (this._hasRootGroups || this._hasRootProperties) {
routes.push({
path: 'root',
component: () => import('./block-workspace-view-edit-tab.element.js'),
setup: (component) => {
(component as UmbBlockWorkspaceViewEditTabElement).managerName = this.#managerName;
(component as UmbBlockWorkspaceViewEditTabElement).containerId = null;
},
});
}

if (this._tabs.length > 0) {
this._tabs?.forEach((tab) => {
const tabName = tab.name ?? '';
routes.push({
path: `tab/${encodeFolderName(tabName)}`,
component: () => import('./block-workspace-view-edit-tab.element.js'),
setup: (component) => {
(component as UmbBlockWorkspaceViewEditTabElement).managerName = this.#managerName;
(component as UmbBlockWorkspaceViewEditTabElement).containerId = tab.ids[0];
},
});
});
}

if (this._hasRootGroups || this._hasRootProperties) {
if (routes.length !== 0) {
routes.push({
...routes[0],
unique: 'emptyPathFor_' + routes[0].path,
path: '',
component: () => import('./block-workspace-view-edit-tab.element.js'),
setup: (component) => {
(component as UmbBlockWorkspaceViewEditTabElement).managerName = this.#managerName;
(component as UmbBlockWorkspaceViewEditTabElement).containerId = null;
},
});
}

Check notice on line 135 in src/Umbraco.Web.UI.Client/src/packages/block/block/workspace/views/edit/block-workspace-view-edit.element.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (release/17.2)

✅ No longer an issue: Complex Method

UmbBlockWorkspaceViewEditElement.createRoutes is no longer above the threshold for cyclomatic complexity. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
if (routes.length !== 0) {
if (!this._hasRootGroups) {
routes.push({
path: '',
pathMatch: 'full',
redirectTo: routes[0]?.path,
});
}
routes.push({
path: `**`,
component: async () => (await import('@umbraco-cms/backoffice/router')).UmbRouteNotFoundElement,
Expand All @@ -151,24 +150,14 @@
(this._tabs.length > 1 || (this._tabs.length === 1 && (this._hasRootGroups || this._hasRootProperties)))
? html` <uui-tab-group slot="header">
${(this._hasRootGroups || this._hasRootProperties) && this._tabs.length > 0
? html`
<uui-tab
label=${this.localize.term('general_generic')}
.active=${this._routerPath + '/' === this._activePath}
href=${this._routerPath + '/'}></uui-tab>
`
: ''}
? this.#renderTab(null, '#general_generic')
: nothing}
${repeat(
this._tabs,
(tab) => tab.name,
(tab) => {
const path = this._routerPath + '/tab/' + encodeFolderName(tab.name || '');
return html`<uui-tab
label=${this.localize.string(tab.name ?? '#general_unknown')}
.active=${path === this._activePath}
href=${path}>
${this.localize.string(tab.name)}
</uui-tab>`;
(tab, index) => {
const path = 'tab/' + encodeFolderName(tab.name || '');
return this.#renderTab(path, tab.name, index);
},
)}
</uui-tab-group>`
Expand All @@ -187,6 +176,25 @@
`;
}

#renderTab(path: string | null, name: string, index = 0) {
const isRootTab = path === null;
const hasRootItems = this._hasRootGroups || this._hasRootProperties;
const basePath = this._routerPath + '/';
const fullPath = basePath + (path ? path : 'root');

const active =
fullPath === this._activePath ||
// When there are no root items, the first tab should be active on the alias path.
(!hasRootItems && index === 0 && basePath === this._activePath) ||
// When there are root items, the root tab should be active on both the canonical `/root` and alias `/` paths.
(hasRootItems && isRootTab && basePath === this._activePath);
return html`<uui-tab
label=${this.localize.string(name ?? '#general_unnamed')}
.active=${active}
href=${isRootTab ? basePath : fullPath}
data-mark="content-tab:${path ?? 'root'}"></uui-tab>`;
}

static override readonly styles = [
UmbTextStyles,
css`
Expand Down
Loading