feat(core-blocks): CB-2420 add DBObjectParentKey

This commit is contained in:
Wroud
2022-08-23 14:19:29 +03:00
parent f1e37e20ed
commit 51911f5793
3 changed files with 57 additions and 52 deletions
@@ -8,13 +8,19 @@
import { injectable } from '@cloudbeaver/core-di';
import {
GraphQLService, CachedMapResource, ResourceKey, isResourceKeyList, resourceKeyList, ResourceKeyUtils
GraphQLService, CachedMapResource, ResourceKey, isResourceKeyList, resourceKeyList, ResourceKeyUtils, ResourceKeyList
} from '@cloudbeaver/core-sdk';
import type { DBObject } from './EntityTypes';
import { NavNodeInfoResource } from './NavNodeInfoResource';
import { NavTreeResource } from './NavTreeResource';
const dbObjectParentKeySymbol = Symbol('@connection-folder/project') as unknown as string;
export const DBObjectParentKey = (parentId: string) => resourceKeyList<string>(
[dbObjectParentKeySymbol],
parentId
);
@injectable()
export class DBObjectResource extends CachedMapResource<string, DBObject> {
constructor(
@@ -24,24 +30,25 @@ export class DBObjectResource extends CachedMapResource<string, DBObject> {
) {
super();
this.addAlias(
isDBObjectParentKey,
param => resourceKeyList(navTreeResource.get(param.mark) || []),
true
);
// this.preloadResource(this.navNodeInfoResource);
this.navNodeInfoResource.outdateResource(this);
this.navNodeInfoResource.deleteInResource(this);
this.navNodeInfoResource.onDataOutdated.addHandler(this.outdateChildren.bind(this));
}
async loadChildren(parentId: string, key: ResourceKey<string>): Promise<Map<string, DBObject>> {
await this.performUpdate(
key,
[],
() => this.loadFromChildren(parentId, 0, this.navTreeResource.childrenLimit + 1),
() => this.isLoaded(key) && !this.isOutdated(key)
);
protected async loader(originalKey: ResourceKey<string>): Promise<Map<string, DBObject>> {
const key = this.transformParam(originalKey);
return this.data;
}
if (isDBObjectParentKey(originalKey)) {
await this.loadFromChildren(originalKey.mark, 0, this.navTreeResource.childrenLimit + 1);
return this.data;
}
protected async loader(key: ResourceKey<string>): Promise<Map<string, DBObject>> {
if (isResourceKeyList(key)) {
const values: DBObject[] = [];
for (const navNodeId of key.list) {
@@ -87,3 +94,9 @@ export class DBObjectResource extends CachedMapResource<string, DBObject> {
// }
}
}
function isDBObjectParentKey(
param: ResourceKey<string>
): param is ResourceKeyList<string> {
return isResourceKeyList(param) && param.list.includes(dbObjectParentKeySymbol);
}
@@ -6,14 +6,15 @@
* you may not use this file except in compliance with the License.
*/
import { untracked } from 'mobx';
import { observer } from 'mobx-react-lite';
import { useEffect } from 'react';
import styled, { css } from 'reshadow';
import { Loader, TextPlaceholder, useMapResource } from '@cloudbeaver/core-blocks';
import { useService } from '@cloudbeaver/core-di';
import { useTranslate } from '@cloudbeaver/core-localization';
import { NavNodeInfoResource, NavTreeResource, DBObjectResource, type DBObject } from '@cloudbeaver/core-navigation-tree';
import { resourceKeyList } from '@cloudbeaver/core-sdk';
import { NavNodeInfoResource, NavTreeResource, DBObjectResource, type DBObject, DBObjectParentKey } from '@cloudbeaver/core-navigation-tree';
import { type NavNodeTransformViewComponent, NavNodeViewService } from '@cloudbeaver/plugin-navigation-tree';
import { TableLoader } from '../../ObjectPropertyTable/Table/TableLoader';
@@ -42,24 +43,26 @@ export const VirtualFolderPanel: NavNodeTransformViewComponent = observer(functi
onLoad: async resource => !(await resource.preloadNodeParents(parents, nodeId)),
});
const { nodes, truncated } = navNodeViewService.limit(tree.data || []);
const key = resourceKeyList(nodes);
const dbObject = useMapResource(VirtualFolderPanel, DBObjectResource, key, {
async onLoad(resource: DBObjectResource) {
const preloaded = await tree.resource.preloadNodeParents(parents, nodeId);
const limited = navNodeViewService.limit(tree.data || []);
if (!preloaded) {
return true;
}
const { nodes, duplicates } = navNodeViewService.filterDuplicates(limited.nodes);
await resource.loadChildren(nodeId, key);
return true;
},
const dbObject = useMapResource(VirtualFolderPanel, DBObjectResource, DBObjectParentKey(nodeId), {
preload: [tree],
});
useEffect(() => {
untracked(() => {
navNodeViewService.logDuplicates(nodeId, duplicates);
});
});
const objects = dbObject.data
.filter(object => object && navNodeInfoResource.get(object.id)?.nodeType === nodeType) as DBObject[];
.filter(object => (
object
&& nodes.includes(object.id)
&& navNodeInfoResource.get(object.id)?.nodeType === nodeType
)) as DBObject[];
return styled(style)(
<Loader state={[tree, dbObject]}>{() => styled(style)(
@@ -68,7 +71,7 @@ export const VirtualFolderPanel: NavNodeTransformViewComponent = observer(functi
<TextPlaceholder>{translate('plugin_object_viewer_table_no_items')}</TextPlaceholder>
) : (
<tab-wrapper>
<TableLoader objects={objects} truncated={truncated > 0} />
<TableLoader objects={objects} truncated={limited.truncated > 0} />
</tab-wrapper>
)}
</>
@@ -14,8 +14,7 @@ import styled, { css } from 'reshadow';
import { Loader, TextPlaceholder, useMapResource } from '@cloudbeaver/core-blocks';
import { useService } from '@cloudbeaver/core-di';
import { useTranslate } from '@cloudbeaver/core-localization';
import { NavTreeResource, DBObjectResource, type DBObject } from '@cloudbeaver/core-navigation-tree';
import { resourceKeyList } from '@cloudbeaver/core-sdk';
import { NavTreeResource, DBObjectResource, type DBObject, DBObjectParentKey } from '@cloudbeaver/core-navigation-tree';
import { NavNodeViewService } from '@cloudbeaver/plugin-navigation-tree';
import { TableLoader } from './Table/TableLoader';
@@ -54,18 +53,7 @@ export const ObjectPropertyTable = observer<ObjectPropertyTableProps>(function O
const { nodes, duplicates } = navNodeViewService.filterDuplicates(limited.nodes);
const key = resourceKeyList(nodes);
const dbObject = useMapResource(ObjectPropertyTable, DBObjectResource, key, {
async onLoad(resource: DBObjectResource) {
const preloaded = await tree.resource.preloadNodeParents(parents, objectId);
if (!preloaded) {
return true;
}
await resource.loadChildren(objectId, key);
return true;
},
const dbObject = useMapResource(ObjectPropertyTable, DBObjectResource, DBObjectParentKey(objectId), {
preload: [tree],
});
@@ -75,20 +63,21 @@ export const ObjectPropertyTable = observer<ObjectPropertyTableProps>(function O
});
});
const objects = dbObject.data as DBObject[];
const objects = dbObject.data.filter(object => nodes.includes(object?.id ?? '')) as DBObject[];
return styled(styles)(
<Loader state={[tree, dbObject]} style={styles}>{() => styled(styles)(
<>
{nodes.length === 0 ? (
<TextPlaceholder>{translate('plugin_object_viewer_table_no_items')}</TextPlaceholder>
) : (
<div>
<TableLoader objects={objects} truncated={limited.truncated > 0} />
</div>
)}
</>
)}
<Loader state={[tree, dbObject]} style={styles}>
{() => styled(styles)(
<>
{nodes.length === 0 ? (
<TextPlaceholder>{translate('plugin_object_viewer_table_no_items')}</TextPlaceholder>
) : (
<div>
<TableLoader objects={objects} truncated={limited.truncated > 0} />
</div>
)}
</>
)}
</Loader>
);
});