mock-block-dock
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -13,15 +13,9 @@ "use strict"; | ||
entityId: "person-".concat(entityId.toString()), | ||
timestamps: { | ||
createdAt: now, | ||
updatedAt: now, | ||
}, | ||
properties: { | ||
age: Math.ceil(Math.random() * 100), | ||
email: { | ||
address: "".concat(name, "@example.com"), | ||
primary: true, | ||
}, | ||
name: name, | ||
username: name.toLowerCase(), | ||
}, | ||
entityTypeId: "Person", | ||
createdAt: now, | ||
updatedAt: now, | ||
age: Math.ceil(Math.random() * 100), | ||
email: "".concat(name, "@example.com"), | ||
name: name, | ||
username: name.toLowerCase(), | ||
}; | ||
@@ -33,11 +27,8 @@ }; | ||
return { | ||
entityId: entityId.toString(), | ||
timestamps: { | ||
createdAt: now, | ||
updatedAt: now, | ||
}, | ||
properties: { | ||
employees: Math.ceil(Math.random() * 10000), | ||
name: name, | ||
}, | ||
entityId: "company-".concat(entityId.toString()), | ||
entityTypeId: "Company", | ||
createdAt: now, | ||
updatedAt: now, | ||
employees: Math.ceil(Math.random() * 10000), | ||
name: name, | ||
}; | ||
@@ -44,0 +35,0 @@ }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.entityTypes = void 0; | ||
exports.entityTypes = []; | ||
exports.entityTypes = [ | ||
{ | ||
$id: "https://example.com/types/Company", | ||
title: "Company", | ||
entityTypeId: "Company", | ||
type: "object", | ||
$schema: "https://json-schema.org/draft/2019-09/schema", | ||
description: "A company or organisation.", | ||
properties: { | ||
employees: { | ||
type: "number", | ||
description: "The number of employees in the company.", | ||
}, | ||
name: { | ||
type: "string", | ||
description: "A display name for the company.", | ||
}, | ||
}, | ||
required: ["name", "employees"], | ||
}, | ||
{ | ||
$id: "https://example.com/types/Person", | ||
title: "Person", | ||
entityTypeId: "Person", | ||
type: "object", | ||
$schema: "https://json-schema.org/draft/2019-09/schema", | ||
description: "A human person.", | ||
properties: { | ||
age: { | ||
type: "number", | ||
description: "The age of the person, in years.", | ||
}, | ||
email: { | ||
type: "string", | ||
description: "An email address.", | ||
format: "email", | ||
}, | ||
name: { | ||
type: "string", | ||
description: "The person's name.", | ||
}, | ||
username: { | ||
description: "The person's username in this application", | ||
type: "string", | ||
minLength: 4, | ||
maxLength: 24, | ||
}, | ||
}, | ||
required: ["age", "email", "name", "username"], | ||
}, | ||
]; | ||
//# sourceMappingURL=entityTypes.js.map |
@@ -1,5 +0,2 @@ | ||
export declare const mockData: { | ||
entities: import("blockprotocol").BlockProtocolEntity[]; | ||
entityTypes: import("blockprotocol").BlockProtocolEntityType[]; | ||
links: never[]; | ||
}; | ||
import { MockData } from "../useMockDatastore"; | ||
export declare const mockData: MockData; |
@@ -13,2 +13,11 @@ "use strict"; | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -22,19 +31,31 @@ exports.MockBlockDock = void 0; | ||
var children = _a.children, blockSchema = _a.blockSchema; | ||
var blockEntityType = __assign({ entityTypeId: "blockType1", title: "BlockType", type: "object", $schema: "https://json-schema.org/draft/2019-09/schema", $id: "http://localhost/blockType1" }, (blockSchema !== null && blockSchema !== void 0 ? blockSchema : {})); | ||
var initialBlockEntity = { | ||
accountId: "account1", | ||
entityId: "block1", | ||
}; | ||
if (children.props && | ||
typeof children.props === "object" && | ||
Object.keys(children.props).length > 0) { | ||
Object.assign(initialBlockEntity, children.props); | ||
} | ||
initialBlockEntity.entityTypeId = blockEntityType.entityTypeId; | ||
data_1.mockData.entities.push(initialBlockEntity); | ||
data_1.mockData.entityTypes.push(blockEntityType); | ||
var _b = (0, useMockDatastore_1.useMockDatastore)(data_1.mockData), entities = _b.entities, entityTypes = _b.entityTypes, links = _b.links, functions = _b.functions; | ||
var mockData = (0, react_1.useMemo)(function () { | ||
var _a; | ||
var blockEntityType = __assign({ entityTypeId: "blockType1", title: "BlockType", type: "object", $schema: "https://json-schema.org/draft/2019-09/schema", $id: "http://localhost/blockType1" }, (blockSchema !== null && blockSchema !== void 0 ? blockSchema : {})); | ||
var accountId = (_a = children.props.accountId) !== null && _a !== void 0 ? _a : "accountId"; | ||
var initialBlockEntity = { | ||
accountId: accountId, | ||
entityId: "block1", | ||
}; | ||
if (children.props && | ||
typeof children.props === "object" && | ||
Object.keys(children.props).length > 0) { | ||
Object.assign(initialBlockEntity, children.props); | ||
} | ||
initialBlockEntity.entityTypeId = blockEntityType.entityTypeId; | ||
var nextMockData = __assign({}, data_1.mockData); | ||
// give the entities/types the same accountId as the root entity | ||
nextMockData.entities = __spreadArray(__spreadArray([], data_1.mockData.entities.map(function (entity) { return (__assign(__assign({}, entity), { accountId: accountId })); }), true), [ | ||
initialBlockEntity, | ||
], false); | ||
nextMockData.entityTypes = __spreadArray(__spreadArray([], data_1.mockData.entityTypes.map(function (entityType) { return (__assign(__assign({}, entityType), { accountId: accountId })); }), true), [ | ||
blockEntityType, | ||
], false); | ||
return nextMockData; | ||
}, [blockSchema, children.props]); | ||
var _b = (0, useMockDatastore_1.useMockDatastore)(mockData), entities = _b.entities, entityTypes = _b.entityTypes, links = _b.links, functions = _b.functions; | ||
var latestBlockEntity = (0, react_1.useMemo)(function () { | ||
return entities.find(function (entity) { return entity.entityId === children.props.entityId; }); | ||
}, [entities, children.props.entityId]); | ||
var _a; | ||
return ((_a = entities.find(function (entity) { return entity.entityId === children.props.entityId; })) !== null && _a !== void 0 ? _a : mockData.entities.find(function (entity) { return entity.entityId === children.props.entityId; })); | ||
}, [entities, children.props.entityId, mockData.entities]); | ||
if (!latestBlockEntity) { | ||
@@ -48,5 +69,5 @@ throw new Error("Cannot find block entity. Did it delete itself?"); | ||
if (JSON.stringify(children.props) !== prevChildPropsString.current) { | ||
updateEntities === null || updateEntities === void 0 ? void 0 : updateEntities([ | ||
void (updateEntities === null || updateEntities === void 0 ? void 0 : updateEntities([ | ||
__assign({ accountId: accountId, entityId: entityId, entityTypeId: entityTypeId }, children.props), | ||
]); | ||
])); | ||
} | ||
@@ -59,3 +80,3 @@ prevChildPropsString.current = JSON.stringify(children.props); | ||
startingEntity: latestBlockEntity, | ||
}), linkGroups = _c.linkGroups, linkedEntities = _c.linkedEntities; | ||
}), linkedAggregations = _c.linkedAggregations, linkedEntities = _c.linkedEntities, linkGroups = _c.linkGroups; | ||
var latestBlockEntityType = (0, react_1.useMemo)(function () { | ||
@@ -69,3 +90,3 @@ return entityTypes.find(function (entityType) { | ||
} | ||
var propsToInject = __assign(__assign(__assign({}, latestBlockEntity), functions), { entityTypes: [latestBlockEntityType], linkGroups: linkGroups, linkedEntities: linkedEntities }); | ||
var propsToInject = __assign(__assign(__assign({}, latestBlockEntity), functions), { entityTypes: entityTypes, linkedAggregations: linkedAggregations, linkedEntities: linkedEntities, linkGroups: linkGroups }); | ||
return (0, react_1.cloneElement)(react_1.Children.only(children), propsToInject); | ||
@@ -72,0 +93,0 @@ }; |
import { BlockProtocolEntity, BlockProtocolLink, BlockProtocolLinkGroup, BlockProtocolProps } from "blockprotocol"; | ||
declare type LinkFields = { | ||
linkedAggregations: BlockProtocolProps["linkedAggregations"]; | ||
linkedEntities: BlockProtocolProps["linkedEntities"]; | ||
linkGroups: BlockProtocolLinkGroup[]; | ||
linkedEntities: BlockProtocolProps["linkedEntities"]; | ||
}; | ||
@@ -6,0 +7,0 @@ export declare const useLinkFields: ({ entities, links, startingEntity, }: { |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -19,2 +30,10 @@ exports.useLinkFields = void 0; | ||
var calculatedLinkGroups = linksFromStartingEntity.reduce(function (linkGroupsAcc, link) { | ||
if ("operation" in link) { | ||
/** | ||
* this is an aggregation link - they don't go in linkGroups, | ||
* because they are in linkedAggregations | ||
* @todo should they be in linkGroups too? | ||
* */ | ||
return linkGroupsAcc; | ||
} | ||
var existingGroup = linkGroupsAcc.find(function (group) { | ||
@@ -55,8 +74,21 @@ return (0, util_1.matchIdentifiers)({ | ||
}); | ||
var calculatedLinkedAggregations = linksFromStartingEntity | ||
.map(function (link) { | ||
if (!("operation" in link)) { | ||
return null; | ||
} | ||
var results = (0, util_1.filterAndSortEntitiesOrTypes)(entities, { | ||
operation: link.operation, | ||
}); | ||
return __assign(__assign({}, link), results); | ||
}) | ||
.filter(function (thing) { return thing !== null; }); | ||
return { | ||
linkedAggregations: calculatedLinkedAggregations, | ||
linkedEntities: calculatedLinkedEntities, | ||
linkGroups: calculatedLinkGroups, | ||
linkedEntities: calculatedLinkedEntities, | ||
}; | ||
}, [entities, links, startingEntity]), linkGroups = _b.linkGroups, linkedEntities = _b.linkedEntities; | ||
}, [entities, links, startingEntity]), linkedAggregations = _b.linkedAggregations, linkedEntities = _b.linkedEntities, linkGroups = _b.linkGroups; | ||
return { | ||
linkedAggregations: linkedAggregations, | ||
linkGroups: linkGroups, | ||
@@ -63,0 +95,0 @@ linkedEntities: linkedEntities, |
import { BlockProtocolEntity, BlockProtocolEntityType, BlockProtocolFunctions, BlockProtocolLink } from "blockprotocol"; | ||
declare type MockData = { | ||
export declare type MockData = { | ||
entities: BlockProtocolEntity[]; | ||
@@ -8,6 +8,5 @@ links: BlockProtocolLink[]; | ||
declare type MockDataStore = MockData & { | ||
functions: Omit<BlockProtocolFunctions, "aggregateEntities" | "aggregateEntityTypes" | "getEntityTypes" | "createEntityTypes" | "updateEntityTypes" | "deleteEntityTypes" | "uploadFile">; | ||
functions: Omit<BlockProtocolFunctions, "getEntityTypes" | "createEntityTypes" | "updateEntityTypes" | "deleteEntityTypes">; | ||
}; | ||
declare type UseMockDataStore = (initialData?: MockData) => MockDataStore; | ||
export declare const useMockDatastore: UseMockDataStore; | ||
export declare const useMockDatastore: (initialData?: MockData) => MockDataStore; | ||
export {}; |
@@ -72,2 +72,13 @@ "use strict"; | ||
var _c = (0, react_1.useState)(initialData.entityTypes), entityTypes = _c[0], _setEntityTypes = _c[1]; | ||
(0, react_1.useEffect)(function () { | ||
setEntities(initialData.entities); | ||
}, [initialData.entities]); | ||
var aggregateEntityTypes = (0, react_1.useCallback)(function (payload) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, (0, util_1.filterAndSortEntitiesOrTypes)(entityTypes, payload)]; | ||
}); | ||
}); }, [entityTypes]); | ||
var aggregateEntities = (0, react_1.useCallback)(function (payload) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2 /*return*/, (0, util_1.filterAndSortEntitiesOrTypes)(entities, payload)]; | ||
}); }); }, [entities]); | ||
var createEntities = (0, react_1.useCallback)(function (actions) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -162,5 +173,13 @@ var _a, newEntities, newLinks; | ||
return currentLinks.map(function (link) { | ||
var actionToApply = actions.find(function (action) { return link.linkId === action.linkId; }); | ||
var actionToApply = actions.find(function (action) { | ||
if ("linkId" in action) { | ||
return link.linkId === action.linkId; | ||
} | ||
var sourceEntityId = action.sourceEntityId, sourceAccountId = action.sourceAccountId, path = action.path; | ||
return (sourceEntityId === link.sourceEntityId && | ||
path === link.path && | ||
(!sourceAccountId || sourceAccountId === link.sourceAccountId)); | ||
}); | ||
if (actionToApply) { | ||
var newLink = __assign(__assign({}, link), actionToApply); | ||
var newLink = __assign(__assign({}, link), { operation: actionToApply.data }); | ||
updatedLinks.push(newLink); | ||
@@ -194,29 +213,15 @@ return newLink; | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var reader; | ||
var result; | ||
return __generator(this, function (_b) { | ||
if (!file && !(url === null || url === void 0 ? void 0 : url.trim())) { | ||
throw new Error("Please enter a valid ".concat(mediaType, " URL or select a file below")); | ||
} | ||
if (url === null || url === void 0 ? void 0 : url.trim()) { | ||
return [2 /*return*/, createEntities([ | ||
{ | ||
accountId: accountId, | ||
data: { | ||
url: url, | ||
mediaType: mediaType, | ||
}, | ||
entityTypeId: "file1", | ||
}, | ||
]).then(function (resp) { return resp[0]; })]; | ||
} | ||
else if (file) { | ||
reader = new FileReader(); | ||
reader.onload = function (event) { | ||
var _a; | ||
if ((_a = event.target) === null || _a === void 0 ? void 0 : _a.result) { | ||
return createEntities([ | ||
switch (_b.label) { | ||
case 0: | ||
if (!file && !(url === null || url === void 0 ? void 0 : url.trim())) { | ||
throw new Error("Please enter a valid ".concat(mediaType, " URL or select a file below")); | ||
} | ||
if (!(url === null || url === void 0 ? void 0 : url.trim())) return [3 /*break*/, 1]; | ||
return [2 /*return*/, createEntities([ | ||
{ | ||
accountId: accountId, | ||
data: { | ||
url: event.target.result.toString(), | ||
url: url, | ||
mediaType: mediaType, | ||
@@ -226,11 +231,33 @@ }, | ||
}, | ||
]).then(function (resp) { return resp[0]; }); | ||
]).then(function (resp) { return resp[0]; })]; | ||
case 1: | ||
if (!file) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, new Promise(function (resolve, reject) { | ||
var reader = new FileReader(); | ||
reader.onload = function (event) { | ||
var _a, _b; | ||
resolve((_b = (_a = event.target) === null || _a === void 0 ? void 0 : _a.result) !== null && _b !== void 0 ? _b : null); | ||
}; | ||
reader.onerror = function (event) { | ||
reject(event); | ||
}; | ||
reader.readAsDataURL(file); | ||
})]; | ||
case 2: | ||
result = _b.sent(); | ||
if (result) { | ||
return [2 /*return*/, createEntities([ | ||
{ | ||
accountId: accountId, | ||
data: { | ||
url: result.toString(), | ||
mediaType: mediaType, | ||
}, | ||
entityTypeId: "file1", | ||
}, | ||
]).then(function (resp) { return resp[0]; })]; | ||
} | ||
else { | ||
throw new Error("Couldn't read your file"); | ||
} | ||
}; | ||
reader.readAsDataURL(file); | ||
throw new Error("Couldn't read your file"); | ||
case 3: throw new Error("Unreachable."); | ||
} | ||
throw new Error("Unreachable."); | ||
}); | ||
@@ -242,4 +269,5 @@ }); | ||
entityTypes: entityTypes, | ||
links: links, | ||
functions: { | ||
aggregateEntities: aggregateEntities, | ||
aggregateEntityTypes: aggregateEntityTypes, | ||
getEntities: getEntities, | ||
@@ -255,2 +283,3 @@ createEntities: createEntities, | ||
}, | ||
links: links, | ||
}; | ||
@@ -257,0 +286,0 @@ }; |
@@ -0,1 +1,2 @@ | ||
import { BlockProtocolAggregateEntitiesFunction, BlockProtocolAggregateEntitiesPayload, BlockProtocolAggregateEntityTypesFunction, BlockProtocolAggregateEntityTypesPayload, BlockProtocolEntity, BlockProtocolEntityType } from "blockprotocol"; | ||
declare type Identifiers = { | ||
@@ -7,2 +8,5 @@ accountId?: string | null; | ||
export declare const matchIdentifiers: (first: Identifiers, second: Identifiers) => boolean; | ||
declare type Unpromise<T extends Promise<any>> = T extends Promise<infer U> ? U : never; | ||
export declare function filterAndSortEntitiesOrTypes(entities: BlockProtocolEntity[], payload: BlockProtocolAggregateEntitiesPayload): Unpromise<ReturnType<BlockProtocolAggregateEntitiesFunction>>; | ||
export declare function filterAndSortEntitiesOrTypes(entities: BlockProtocolEntityType[], payload: BlockProtocolAggregateEntityTypesPayload): Unpromise<ReturnType<BlockProtocolAggregateEntityTypesFunction>>; | ||
export {}; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.matchIdentifiers = void 0; | ||
exports.filterAndSortEntitiesOrTypes = exports.matchIdentifiers = void 0; | ||
var lodash_1 = require("lodash"); | ||
var matchIdentifiers = function (first, second) { | ||
@@ -18,2 +39,110 @@ if (first.entityId !== second.entityId) { | ||
exports.matchIdentifiers = matchIdentifiers; | ||
var filterEntities = function (params) { | ||
var entityTypeId = params.entityTypeId, entityTypeVersionId = params.entityTypeVersionId, entities = params.entities, multiFilter = params.multiFilter; | ||
return entities.filter(function (entity) { | ||
if (entityTypeId && entityTypeId !== entity.entityTypeId) { | ||
return false; | ||
} | ||
if (entityTypeVersionId && | ||
entityTypeVersionId !== entity.entityTypeVersionId) { | ||
return false; | ||
} | ||
var results = multiFilter.filters | ||
.map(function (filterItem) { | ||
var item = (0, lodash_1.get)(entity, filterItem.field); | ||
// @todo support non-string comparison | ||
if (typeof item !== "string") { | ||
return null; | ||
} | ||
switch (filterItem.operator) { | ||
case "CONTAINS": | ||
return item.toLowerCase().includes(filterItem.value.toLowerCase()); | ||
case "DOES_NOT_CONTAIN": | ||
return !item.toLowerCase().includes(filterItem.value.toLowerCase()); | ||
case "STARTS_WITH": | ||
return item | ||
.toLowerCase() | ||
.startsWith(filterItem.value.toLowerCase()); | ||
case "ENDS_WITH": | ||
return item.toLowerCase().endsWith(filterItem.value.toLowerCase()); | ||
case "IS_EMPTY": | ||
return !item; | ||
case "IS_NOT_EMPTY": | ||
return !!item; | ||
case "IS": | ||
return item.toLowerCase() === filterItem.value.toLowerCase(); | ||
case "IS_NOT": | ||
return item.toLowerCase() !== filterItem.value.toLowerCase(); | ||
default: | ||
return null; | ||
} | ||
}) | ||
.filter(function (val) { return val !== null; }); | ||
return multiFilter.operator === "OR" | ||
? results.some(Boolean) | ||
: results.every(Boolean); | ||
}); | ||
}; | ||
var sortEntitiesOrTypes = function (params) { | ||
var entities = params.entities, multiSort = params.multiSort; | ||
return (0, lodash_1.orderBy)(__spreadArray([], entities, true), multiSort.map(function (_a) { | ||
var field = _a.field; | ||
return field; | ||
}), multiSort.map(function (_a) { | ||
var desc = _a.desc; | ||
return (desc ? "desc" : "asc"); | ||
})); | ||
}; | ||
var isEntityTypes = function (entities) { return "$schema" in entities[0]; }; | ||
function filterAndSortEntitiesOrTypes(entities, payload) { | ||
var _a; | ||
var accountId = payload.accountId, operation = payload.operation; | ||
var pageNumber = (operation === null || operation === void 0 ? void 0 : operation.pageNumber) || 1; | ||
var itemsPerPage = (operation === null || operation === void 0 ? void 0 : operation.itemsPerPage) || 10; | ||
var multiSort = (_a = operation === null || operation === void 0 ? void 0 : operation.multiSort) !== null && _a !== void 0 ? _a : [{ field: "updatedAt" }]; | ||
var multiFilter = operation === null || operation === void 0 ? void 0 : operation.multiFilter; | ||
var appliedOperation = { | ||
pageNumber: pageNumber, | ||
itemsPerPage: itemsPerPage, | ||
multiFilter: multiFilter, | ||
multiSort: multiSort, | ||
}; | ||
var startIndex = pageNumber === 1 ? 0 : (pageNumber - 1) * itemsPerPage; | ||
var endIndex = Math.min(startIndex + itemsPerPage, entities.length); | ||
// @todo add filtering to entityTypes, remove duplication below | ||
if (isEntityTypes(entities)) { | ||
var results_1 = __spreadArray([], entities, true); | ||
if (accountId) { | ||
results_1 = results_1.filter(function (entity) { return entity.accountId; }); | ||
} | ||
var totalCount_1 = results_1.length; | ||
var pageCount_1 = Math.ceil(totalCount_1 / itemsPerPage); | ||
results_1 = sortEntitiesOrTypes({ entities: results_1, multiSort: multiSort }).slice(startIndex, endIndex); | ||
return { | ||
results: results_1, | ||
operation: __assign(__assign({}, appliedOperation), { totalCount: totalCount_1, pageCount: pageCount_1 }), | ||
}; | ||
} | ||
var results = __spreadArray([], entities, true); | ||
if (multiFilter) { | ||
results = filterEntities({ | ||
entities: results, | ||
multiFilter: multiFilter, | ||
}); | ||
} | ||
var entityTypeIdFilter = operation && "entityTypeId" in operation | ||
? operation.entityTypeId | ||
: undefined; | ||
if (entityTypeIdFilter) { | ||
results = results.filter(function (entity) { return entity.entityTypeId === entityTypeIdFilter; }); | ||
} | ||
var totalCount = results.length; | ||
var pageCount = Math.ceil(totalCount / itemsPerPage); | ||
results = sortEntitiesOrTypes({ entities: results, multiSort: multiSort }).slice(startIndex, endIndex); | ||
return { | ||
results: results, | ||
operation: __assign(__assign({}, appliedOperation), { entityTypeId: entityTypeIdFilter, totalCount: totalCount, pageCount: pageCount }), | ||
}; | ||
} | ||
exports.filterAndSortEntitiesOrTypes = filterAndSortEntitiesOrTypes; | ||
//# sourceMappingURL=util.js.map |
@@ -9,15 +9,9 @@ import { companyNames, personNames } from "./words"; | ||
entityId: "person-".concat(entityId.toString()), | ||
timestamps: { | ||
createdAt: now, | ||
updatedAt: now, | ||
}, | ||
properties: { | ||
age: Math.ceil(Math.random() * 100), | ||
email: { | ||
address: "".concat(name, "@example.com"), | ||
primary: true, | ||
}, | ||
name: name, | ||
username: name.toLowerCase(), | ||
}, | ||
entityTypeId: "Person", | ||
createdAt: now, | ||
updatedAt: now, | ||
age: Math.ceil(Math.random() * 100), | ||
email: "".concat(name, "@example.com"), | ||
name: name, | ||
username: name.toLowerCase(), | ||
}; | ||
@@ -29,11 +23,8 @@ }; | ||
return { | ||
entityId: entityId.toString(), | ||
timestamps: { | ||
createdAt: now, | ||
updatedAt: now, | ||
}, | ||
properties: { | ||
employees: Math.ceil(Math.random() * 10000), | ||
name: name, | ||
}, | ||
entityId: "company-".concat(entityId.toString()), | ||
entityTypeId: "Company", | ||
createdAt: now, | ||
updatedAt: now, | ||
employees: Math.ceil(Math.random() * 10000), | ||
name: name, | ||
}; | ||
@@ -40,0 +31,0 @@ }; |
@@ -1,2 +0,52 @@ | ||
export var entityTypes = []; | ||
export var entityTypes = [ | ||
{ | ||
$id: "https://example.com/types/Company", | ||
title: "Company", | ||
entityTypeId: "Company", | ||
type: "object", | ||
$schema: "https://json-schema.org/draft/2019-09/schema", | ||
description: "A company or organisation.", | ||
properties: { | ||
employees: { | ||
type: "number", | ||
description: "The number of employees in the company.", | ||
}, | ||
name: { | ||
type: "string", | ||
description: "A display name for the company.", | ||
}, | ||
}, | ||
required: ["name", "employees"], | ||
}, | ||
{ | ||
$id: "https://example.com/types/Person", | ||
title: "Person", | ||
entityTypeId: "Person", | ||
type: "object", | ||
$schema: "https://json-schema.org/draft/2019-09/schema", | ||
description: "A human person.", | ||
properties: { | ||
age: { | ||
type: "number", | ||
description: "The age of the person, in years.", | ||
}, | ||
email: { | ||
type: "string", | ||
description: "An email address.", | ||
format: "email", | ||
}, | ||
name: { | ||
type: "string", | ||
description: "The person's name.", | ||
}, | ||
username: { | ||
description: "The person's username in this application", | ||
type: "string", | ||
minLength: 4, | ||
maxLength: 24, | ||
}, | ||
}, | ||
required: ["age", "email", "name", "username"], | ||
}, | ||
]; | ||
//# sourceMappingURL=entityTypes.js.map |
@@ -1,5 +0,2 @@ | ||
export declare const mockData: { | ||
entities: import("blockprotocol").BlockProtocolEntity[]; | ||
entityTypes: import("blockprotocol").BlockProtocolEntityType[]; | ||
links: never[]; | ||
}; | ||
import { MockData } from "../useMockDatastore"; | ||
export declare const mockData: MockData; |
@@ -12,25 +12,46 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
import { Children, cloneElement, useEffect, useMemo, useRef, } from "react"; | ||
import { useMockDatastore } from "./useMockDatastore"; | ||
import { mockData } from "./data"; | ||
import { mockData as initialMockData } from "./data"; | ||
import { useLinkFields } from "./useLinkFields"; | ||
export var MockBlockDock = function (_a) { | ||
var children = _a.children, blockSchema = _a.blockSchema; | ||
var blockEntityType = __assign({ entityTypeId: "blockType1", title: "BlockType", type: "object", $schema: "https://json-schema.org/draft/2019-09/schema", $id: "http://localhost/blockType1" }, (blockSchema !== null && blockSchema !== void 0 ? blockSchema : {})); | ||
var initialBlockEntity = { | ||
accountId: "account1", | ||
entityId: "block1", | ||
}; | ||
if (children.props && | ||
typeof children.props === "object" && | ||
Object.keys(children.props).length > 0) { | ||
Object.assign(initialBlockEntity, children.props); | ||
} | ||
initialBlockEntity.entityTypeId = blockEntityType.entityTypeId; | ||
mockData.entities.push(initialBlockEntity); | ||
mockData.entityTypes.push(blockEntityType); | ||
var mockData = useMemo(function () { | ||
var _a; | ||
var blockEntityType = __assign({ entityTypeId: "blockType1", title: "BlockType", type: "object", $schema: "https://json-schema.org/draft/2019-09/schema", $id: "http://localhost/blockType1" }, (blockSchema !== null && blockSchema !== void 0 ? blockSchema : {})); | ||
var accountId = (_a = children.props.accountId) !== null && _a !== void 0 ? _a : "accountId"; | ||
var initialBlockEntity = { | ||
accountId: accountId, | ||
entityId: "block1", | ||
}; | ||
if (children.props && | ||
typeof children.props === "object" && | ||
Object.keys(children.props).length > 0) { | ||
Object.assign(initialBlockEntity, children.props); | ||
} | ||
initialBlockEntity.entityTypeId = blockEntityType.entityTypeId; | ||
var nextMockData = __assign({}, initialMockData); | ||
// give the entities/types the same accountId as the root entity | ||
nextMockData.entities = __spreadArray(__spreadArray([], initialMockData.entities.map(function (entity) { return (__assign(__assign({}, entity), { accountId: accountId })); }), true), [ | ||
initialBlockEntity, | ||
], false); | ||
nextMockData.entityTypes = __spreadArray(__spreadArray([], initialMockData.entityTypes.map(function (entityType) { return (__assign(__assign({}, entityType), { accountId: accountId })); }), true), [ | ||
blockEntityType, | ||
], false); | ||
return nextMockData; | ||
}, [blockSchema, children.props]); | ||
var _b = useMockDatastore(mockData), entities = _b.entities, entityTypes = _b.entityTypes, links = _b.links, functions = _b.functions; | ||
var latestBlockEntity = useMemo(function () { | ||
return entities.find(function (entity) { return entity.entityId === children.props.entityId; }); | ||
}, [entities, children.props.entityId]); | ||
var _a; | ||
return ((_a = entities.find(function (entity) { return entity.entityId === children.props.entityId; })) !== null && _a !== void 0 ? _a : mockData.entities.find(function (entity) { return entity.entityId === children.props.entityId; })); | ||
}, [entities, children.props.entityId, mockData.entities]); | ||
if (!latestBlockEntity) { | ||
@@ -44,5 +65,5 @@ throw new Error("Cannot find block entity. Did it delete itself?"); | ||
if (JSON.stringify(children.props) !== prevChildPropsString.current) { | ||
updateEntities === null || updateEntities === void 0 ? void 0 : updateEntities([ | ||
void (updateEntities === null || updateEntities === void 0 ? void 0 : updateEntities([ | ||
__assign({ accountId: accountId, entityId: entityId, entityTypeId: entityTypeId }, children.props), | ||
]); | ||
])); | ||
} | ||
@@ -55,3 +76,3 @@ prevChildPropsString.current = JSON.stringify(children.props); | ||
startingEntity: latestBlockEntity, | ||
}), linkGroups = _c.linkGroups, linkedEntities = _c.linkedEntities; | ||
}), linkedAggregations = _c.linkedAggregations, linkedEntities = _c.linkedEntities, linkGroups = _c.linkGroups; | ||
var latestBlockEntityType = useMemo(function () { | ||
@@ -65,5 +86,5 @@ return entityTypes.find(function (entityType) { | ||
} | ||
var propsToInject = __assign(__assign(__assign({}, latestBlockEntity), functions), { entityTypes: [latestBlockEntityType], linkGroups: linkGroups, linkedEntities: linkedEntities }); | ||
var propsToInject = __assign(__assign(__assign({}, latestBlockEntity), functions), { entityTypes: entityTypes, linkedAggregations: linkedAggregations, linkedEntities: linkedEntities, linkGroups: linkGroups }); | ||
return cloneElement(Children.only(children), propsToInject); | ||
}; | ||
//# sourceMappingURL=MockBlockDock.jsx.map |
import { BlockProtocolEntity, BlockProtocolLink, BlockProtocolLinkGroup, BlockProtocolProps } from "blockprotocol"; | ||
declare type LinkFields = { | ||
linkedAggregations: BlockProtocolProps["linkedAggregations"]; | ||
linkedEntities: BlockProtocolProps["linkedEntities"]; | ||
linkGroups: BlockProtocolLinkGroup[]; | ||
linkedEntities: BlockProtocolProps["linkedEntities"]; | ||
}; | ||
@@ -6,0 +7,0 @@ export declare const useLinkFields: ({ entities, links, startingEntity, }: { |
@@ -0,3 +1,14 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
import { useMemo } from "react"; | ||
import { matchIdentifiers } from "./util"; | ||
import { filterAndSortEntitiesOrTypes, matchIdentifiers } from "./util"; | ||
export var useLinkFields = function (_a) { | ||
@@ -16,2 +27,10 @@ var entities = _a.entities, links = _a.links, startingEntity = _a.startingEntity; | ||
var calculatedLinkGroups = linksFromStartingEntity.reduce(function (linkGroupsAcc, link) { | ||
if ("operation" in link) { | ||
/** | ||
* this is an aggregation link - they don't go in linkGroups, | ||
* because they are in linkedAggregations | ||
* @todo should they be in linkGroups too? | ||
* */ | ||
return linkGroupsAcc; | ||
} | ||
var existingGroup = linkGroupsAcc.find(function (group) { | ||
@@ -52,8 +71,21 @@ return matchIdentifiers({ | ||
}); | ||
var calculatedLinkedAggregations = linksFromStartingEntity | ||
.map(function (link) { | ||
if (!("operation" in link)) { | ||
return null; | ||
} | ||
var results = filterAndSortEntitiesOrTypes(entities, { | ||
operation: link.operation, | ||
}); | ||
return __assign(__assign({}, link), results); | ||
}) | ||
.filter(function (thing) { return thing !== null; }); | ||
return { | ||
linkedAggregations: calculatedLinkedAggregations, | ||
linkedEntities: calculatedLinkedEntities, | ||
linkGroups: calculatedLinkGroups, | ||
linkedEntities: calculatedLinkedEntities, | ||
}; | ||
}, [entities, links, startingEntity]), linkGroups = _b.linkGroups, linkedEntities = _b.linkedEntities; | ||
}, [entities, links, startingEntity]), linkedAggregations = _b.linkedAggregations, linkedEntities = _b.linkedEntities, linkGroups = _b.linkGroups; | ||
return { | ||
linkedAggregations: linkedAggregations, | ||
linkGroups: linkGroups, | ||
@@ -60,0 +92,0 @@ linkedEntities: linkedEntities, |
import { BlockProtocolEntity, BlockProtocolEntityType, BlockProtocolFunctions, BlockProtocolLink } from "blockprotocol"; | ||
declare type MockData = { | ||
export declare type MockData = { | ||
entities: BlockProtocolEntity[]; | ||
@@ -8,6 +8,5 @@ links: BlockProtocolLink[]; | ||
declare type MockDataStore = MockData & { | ||
functions: Omit<BlockProtocolFunctions, "aggregateEntities" | "aggregateEntityTypes" | "getEntityTypes" | "createEntityTypes" | "updateEntityTypes" | "deleteEntityTypes" | "uploadFile">; | ||
functions: Omit<BlockProtocolFunctions, "getEntityTypes" | "createEntityTypes" | "updateEntityTypes" | "deleteEntityTypes">; | ||
}; | ||
declare type UseMockDataStore = (initialData?: MockData) => MockDataStore; | ||
export declare const useMockDatastore: UseMockDataStore; | ||
export declare const useMockDatastore: (initialData?: MockData) => MockDataStore; | ||
export {}; |
@@ -57,5 +57,5 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
import { useCallback, useState } from "react"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { v4 as uuid } from "uuid"; | ||
import { matchIdentifiers } from "./util"; | ||
import { filterAndSortEntitiesOrTypes, matchIdentifiers } from "./util"; | ||
export var useMockDatastore = function (initialData) { | ||
@@ -70,2 +70,13 @@ if (initialData === void 0) { initialData = { | ||
var _c = useState(initialData.entityTypes), entityTypes = _c[0], _setEntityTypes = _c[1]; | ||
useEffect(function () { | ||
setEntities(initialData.entities); | ||
}, [initialData.entities]); | ||
var aggregateEntityTypes = useCallback(function (payload) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, filterAndSortEntitiesOrTypes(entityTypes, payload)]; | ||
}); | ||
}); }, [entityTypes]); | ||
var aggregateEntities = useCallback(function (payload) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2 /*return*/, filterAndSortEntitiesOrTypes(entities, payload)]; | ||
}); }); }, [entities]); | ||
var createEntities = useCallback(function (actions) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -160,5 +171,13 @@ var _a, newEntities, newLinks; | ||
return currentLinks.map(function (link) { | ||
var actionToApply = actions.find(function (action) { return link.linkId === action.linkId; }); | ||
var actionToApply = actions.find(function (action) { | ||
if ("linkId" in action) { | ||
return link.linkId === action.linkId; | ||
} | ||
var sourceEntityId = action.sourceEntityId, sourceAccountId = action.sourceAccountId, path = action.path; | ||
return (sourceEntityId === link.sourceEntityId && | ||
path === link.path && | ||
(!sourceAccountId || sourceAccountId === link.sourceAccountId)); | ||
}); | ||
if (actionToApply) { | ||
var newLink = __assign(__assign({}, link), actionToApply); | ||
var newLink = __assign(__assign({}, link), { operation: actionToApply.data }); | ||
updatedLinks.push(newLink); | ||
@@ -192,29 +211,15 @@ return newLink; | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var reader; | ||
var result; | ||
return __generator(this, function (_b) { | ||
if (!file && !(url === null || url === void 0 ? void 0 : url.trim())) { | ||
throw new Error("Please enter a valid ".concat(mediaType, " URL or select a file below")); | ||
} | ||
if (url === null || url === void 0 ? void 0 : url.trim()) { | ||
return [2 /*return*/, createEntities([ | ||
{ | ||
accountId: accountId, | ||
data: { | ||
url: url, | ||
mediaType: mediaType, | ||
}, | ||
entityTypeId: "file1", | ||
}, | ||
]).then(function (resp) { return resp[0]; })]; | ||
} | ||
else if (file) { | ||
reader = new FileReader(); | ||
reader.onload = function (event) { | ||
var _a; | ||
if ((_a = event.target) === null || _a === void 0 ? void 0 : _a.result) { | ||
return createEntities([ | ||
switch (_b.label) { | ||
case 0: | ||
if (!file && !(url === null || url === void 0 ? void 0 : url.trim())) { | ||
throw new Error("Please enter a valid ".concat(mediaType, " URL or select a file below")); | ||
} | ||
if (!(url === null || url === void 0 ? void 0 : url.trim())) return [3 /*break*/, 1]; | ||
return [2 /*return*/, createEntities([ | ||
{ | ||
accountId: accountId, | ||
data: { | ||
url: event.target.result.toString(), | ||
url: url, | ||
mediaType: mediaType, | ||
@@ -224,11 +229,33 @@ }, | ||
}, | ||
]).then(function (resp) { return resp[0]; }); | ||
]).then(function (resp) { return resp[0]; })]; | ||
case 1: | ||
if (!file) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, new Promise(function (resolve, reject) { | ||
var reader = new FileReader(); | ||
reader.onload = function (event) { | ||
var _a, _b; | ||
resolve((_b = (_a = event.target) === null || _a === void 0 ? void 0 : _a.result) !== null && _b !== void 0 ? _b : null); | ||
}; | ||
reader.onerror = function (event) { | ||
reject(event); | ||
}; | ||
reader.readAsDataURL(file); | ||
})]; | ||
case 2: | ||
result = _b.sent(); | ||
if (result) { | ||
return [2 /*return*/, createEntities([ | ||
{ | ||
accountId: accountId, | ||
data: { | ||
url: result.toString(), | ||
mediaType: mediaType, | ||
}, | ||
entityTypeId: "file1", | ||
}, | ||
]).then(function (resp) { return resp[0]; })]; | ||
} | ||
else { | ||
throw new Error("Couldn't read your file"); | ||
} | ||
}; | ||
reader.readAsDataURL(file); | ||
throw new Error("Couldn't read your file"); | ||
case 3: throw new Error("Unreachable."); | ||
} | ||
throw new Error("Unreachable."); | ||
}); | ||
@@ -240,4 +267,5 @@ }); | ||
entityTypes: entityTypes, | ||
links: links, | ||
functions: { | ||
aggregateEntities: aggregateEntities, | ||
aggregateEntityTypes: aggregateEntityTypes, | ||
getEntities: getEntities, | ||
@@ -253,4 +281,5 @@ createEntities: createEntities, | ||
}, | ||
links: links, | ||
}; | ||
}; | ||
//# sourceMappingURL=useMockDatastore.js.map |
@@ -0,1 +1,2 @@ | ||
import { BlockProtocolAggregateEntitiesFunction, BlockProtocolAggregateEntitiesPayload, BlockProtocolAggregateEntityTypesFunction, BlockProtocolAggregateEntityTypesPayload, BlockProtocolEntity, BlockProtocolEntityType } from "blockprotocol"; | ||
declare type Identifiers = { | ||
@@ -7,2 +8,5 @@ accountId?: string | null; | ||
export declare const matchIdentifiers: (first: Identifiers, second: Identifiers) => boolean; | ||
declare type Unpromise<T extends Promise<any>> = T extends Promise<infer U> ? U : never; | ||
export declare function filterAndSortEntitiesOrTypes(entities: BlockProtocolEntity[], payload: BlockProtocolAggregateEntitiesPayload): Unpromise<ReturnType<BlockProtocolAggregateEntitiesFunction>>; | ||
export declare function filterAndSortEntitiesOrTypes(entities: BlockProtocolEntityType[], payload: BlockProtocolAggregateEntityTypesPayload): Unpromise<ReturnType<BlockProtocolAggregateEntityTypesFunction>>; | ||
export {}; |
@@ -0,1 +1,22 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
import { get, orderBy } from "lodash"; | ||
export var matchIdentifiers = function (first, second) { | ||
@@ -14,2 +35,109 @@ if (first.entityId !== second.entityId) { | ||
}; | ||
var filterEntities = function (params) { | ||
var entityTypeId = params.entityTypeId, entityTypeVersionId = params.entityTypeVersionId, entities = params.entities, multiFilter = params.multiFilter; | ||
return entities.filter(function (entity) { | ||
if (entityTypeId && entityTypeId !== entity.entityTypeId) { | ||
return false; | ||
} | ||
if (entityTypeVersionId && | ||
entityTypeVersionId !== entity.entityTypeVersionId) { | ||
return false; | ||
} | ||
var results = multiFilter.filters | ||
.map(function (filterItem) { | ||
var item = get(entity, filterItem.field); | ||
// @todo support non-string comparison | ||
if (typeof item !== "string") { | ||
return null; | ||
} | ||
switch (filterItem.operator) { | ||
case "CONTAINS": | ||
return item.toLowerCase().includes(filterItem.value.toLowerCase()); | ||
case "DOES_NOT_CONTAIN": | ||
return !item.toLowerCase().includes(filterItem.value.toLowerCase()); | ||
case "STARTS_WITH": | ||
return item | ||
.toLowerCase() | ||
.startsWith(filterItem.value.toLowerCase()); | ||
case "ENDS_WITH": | ||
return item.toLowerCase().endsWith(filterItem.value.toLowerCase()); | ||
case "IS_EMPTY": | ||
return !item; | ||
case "IS_NOT_EMPTY": | ||
return !!item; | ||
case "IS": | ||
return item.toLowerCase() === filterItem.value.toLowerCase(); | ||
case "IS_NOT": | ||
return item.toLowerCase() !== filterItem.value.toLowerCase(); | ||
default: | ||
return null; | ||
} | ||
}) | ||
.filter(function (val) { return val !== null; }); | ||
return multiFilter.operator === "OR" | ||
? results.some(Boolean) | ||
: results.every(Boolean); | ||
}); | ||
}; | ||
var sortEntitiesOrTypes = function (params) { | ||
var entities = params.entities, multiSort = params.multiSort; | ||
return orderBy(__spreadArray([], entities, true), multiSort.map(function (_a) { | ||
var field = _a.field; | ||
return field; | ||
}), multiSort.map(function (_a) { | ||
var desc = _a.desc; | ||
return (desc ? "desc" : "asc"); | ||
})); | ||
}; | ||
var isEntityTypes = function (entities) { return "$schema" in entities[0]; }; | ||
export function filterAndSortEntitiesOrTypes(entities, payload) { | ||
var _a; | ||
var accountId = payload.accountId, operation = payload.operation; | ||
var pageNumber = (operation === null || operation === void 0 ? void 0 : operation.pageNumber) || 1; | ||
var itemsPerPage = (operation === null || operation === void 0 ? void 0 : operation.itemsPerPage) || 10; | ||
var multiSort = (_a = operation === null || operation === void 0 ? void 0 : operation.multiSort) !== null && _a !== void 0 ? _a : [{ field: "updatedAt" }]; | ||
var multiFilter = operation === null || operation === void 0 ? void 0 : operation.multiFilter; | ||
var appliedOperation = { | ||
pageNumber: pageNumber, | ||
itemsPerPage: itemsPerPage, | ||
multiFilter: multiFilter, | ||
multiSort: multiSort, | ||
}; | ||
var startIndex = pageNumber === 1 ? 0 : (pageNumber - 1) * itemsPerPage; | ||
var endIndex = Math.min(startIndex + itemsPerPage, entities.length); | ||
// @todo add filtering to entityTypes, remove duplication below | ||
if (isEntityTypes(entities)) { | ||
var results_1 = __spreadArray([], entities, true); | ||
if (accountId) { | ||
results_1 = results_1.filter(function (entity) { return entity.accountId; }); | ||
} | ||
var totalCount_1 = results_1.length; | ||
var pageCount_1 = Math.ceil(totalCount_1 / itemsPerPage); | ||
results_1 = sortEntitiesOrTypes({ entities: results_1, multiSort: multiSort }).slice(startIndex, endIndex); | ||
return { | ||
results: results_1, | ||
operation: __assign(__assign({}, appliedOperation), { totalCount: totalCount_1, pageCount: pageCount_1 }), | ||
}; | ||
} | ||
var results = __spreadArray([], entities, true); | ||
if (multiFilter) { | ||
results = filterEntities({ | ||
entities: results, | ||
multiFilter: multiFilter, | ||
}); | ||
} | ||
var entityTypeIdFilter = operation && "entityTypeId" in operation | ||
? operation.entityTypeId | ||
: undefined; | ||
if (entityTypeIdFilter) { | ||
results = results.filter(function (entity) { return entity.entityTypeId === entityTypeIdFilter; }); | ||
} | ||
var totalCount = results.length; | ||
var pageCount = Math.ceil(totalCount / itemsPerPage); | ||
results = sortEntitiesOrTypes({ entities: results, multiSort: multiSort }).slice(startIndex, endIndex); | ||
return { | ||
results: results, | ||
operation: __assign(__assign({}, appliedOperation), { entityTypeId: entityTypeIdFilter, totalCount: totalCount, pageCount: pageCount }), | ||
}; | ||
} | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "mock-block-dock", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A mock embedding application for Block Protocol blocks", | ||
@@ -40,2 +40,3 @@ "keywords": [ | ||
"blockprotocol": "0.0.6", | ||
"lodash": "^4.17.21", | ||
"uuid": "^8.3.2" | ||
@@ -42,0 +43,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
118931
1572
5
+ Addedlodash@^4.17.21
+ Addedlodash@4.17.21(transitive)