Skip to content

Commit

Permalink
fix(api): better findUid
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Feb 16, 2022
1 parent c8403a2 commit 78a0ec7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
24 changes: 14 additions & 10 deletions packages/unigraph-dev-common/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ function getPath(obj: any, path: string | string[]): any {
}
}

export function findUid(object: any, uid: string, path?: any[]) {
export function findUid(object: any, uid: string, path?: any[], seenPaths?: string[]) {
const currentPath = [...(path || [])];
if (
object?.['dgraph.type'] === 'Entity' ||
object?.['dgraph.type']?.includes('Entity') ||
object?.type?.['unigraph.id']?.startsWith('$/schema')
) {
if (object?.uid) {
currentPath.push(object);
}
if (seenPaths?.includes(currentPath.map((el) => el.uid).join('/')) && object?.uid === uid) {
console.log(seenPaths, currentPath.map((el) => el.uid).join('/'));
return [undefined, currentPath];
}
if (object?.uid === uid) return [object, currentPath];
if (typeof object === 'object' && object) {
if (Array.isArray(object)) {
for (const el of object) {
const result: any = findUid(el, uid, currentPath);
const result: any = findUid(el, uid, currentPath, seenPaths);
if (result[0]) return result;
}
} else {
const keys = Object.keys(object);
for (let i = 0; i < keys.length; i += 1) {
const result: any = findUid(object[keys[i]], uid, currentPath);
const result: any = findUid(object[keys[i]], uid, currentPath, seenPaths);
if (result[0]) return result;
}
}
Expand All @@ -48,10 +48,14 @@ export function findAllUids(object: any, uid: string) {
let currentResult = true;
const allResults: any[] = [];
while (currentResult !== undefined && currentResult !== null) {
const res = findUid(object, uid);
const res = findUid(
object,
uid,
[],
allResults.map((el) => el[2].map((ell: any) => ell.uid).join('/')),
);
if (res[0] !== undefined && res[0] !== null && res[0].uid) {
allResults.push([res[0].uid, res[0], res[1]]);
delete res[0].uid;
}
[currentResult] = res;
}
Expand Down
17 changes: 5 additions & 12 deletions packages/unigraph-dev-explorer/src/examples/notes/NoteBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Typography } from '@mui/material';
import React from 'react';
import _ from 'lodash';
import { buildGraph, findUid, UnigraphObject } from 'unigraph-dev-common/lib/utils/utils';
import { buildGraph, findAllUids, findUid, UnigraphObject } from 'unigraph-dev-common/lib/utils/utils';
import { FiberManualRecord, MoreVert } from '@mui/icons-material';
import stringify from 'json-stable-stringify';
import { mdiClockOutline, mdiNoteOutline } from '@mdi/js';
Expand Down Expand Up @@ -911,21 +911,14 @@ export const ReferenceNoteView = ({ data, callbacks, noChildren }: any) => {

React.useEffect(() => {
removeAllPropsFromObj(data, ['~_value', '~unigraph.origin', 'unigraph.origin']);
let targetObj = data;
const paths = [];
let its = 0;
while (its < 1000) {
let path;
its += 1;
[targetObj, path] = findUid(data, callbacks?.context?.uid);
if (targetObj?.uid) delete targetObj.uid;
else break;
paths.push(path);
}
const targetObj = data;
const paths: any[] = findAllUids(data, callbacks?.context?.uid).map((el) => el[1]);
console.log(paths);
const refinedPaths = paths
.map((path) =>
path.filter(
(el: any) =>
el?.type?.['unigraph.id'] &&
!['$/schema/subentity', '$/schema/interface/semantic'].includes(el?.type?.['unigraph.id']),
),
)
Expand Down

0 comments on commit 78a0ec7

Please sign in to comment.