Files
webstudio/app/shared/tree-utils/find-instance.ts
T
2022-06-13 15:01:39 +02:00

20 lines
574 B
TypeScript

import { type Instance } from "@webstudio-is/sdk";
// @todo should be optimized because it will be a frequent operation on a potentially large tree.
export const findInstanceById = (
instance: Instance,
id: Instance["id"]
): Instance | undefined => {
if (instance.id === id) {
return instance;
}
if (instance.children !== undefined) {
for (const child of instance.children) {
if (typeof child === "string") continue;
const foundInstance = findInstanceById(child, id);
if (foundInstance !== undefined) return foundInstance;
}
}
};