From 55b852f90c41552dd2e9cf2ae704384d21c74d89 Mon Sep 17 00:00:00 2001 From: Luke De Feo Date: Wed, 7 Sep 2022 04:37:17 -0700 Subject: [PATCH] Basic tree UI Summary: A very basic tree in Antd to visualise the UI Reviewed By: lblasa Differential Revision: D38977035 fbshipit-source-id: f9bbf765ea8027eeb263cad86407502c6a5779dd --- .../public/ui-debugger/components/main.tsx | 46 +++++++++++++++++- desktop/plugins/public/ui-debugger/index.tsx | 48 ++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/desktop/plugins/public/ui-debugger/components/main.tsx b/desktop/plugins/public/ui-debugger/components/main.tsx index 754d69149..755a21625 100644 --- a/desktop/plugins/public/ui-debugger/components/main.tsx +++ b/desktop/plugins/public/ui-debugger/components/main.tsx @@ -8,12 +8,56 @@ */ import React from 'react'; -import {plugin} from '../index'; +import {Id, plugin, UINode} from '../index'; import {usePlugin, useValue} from 'flipper-plugin'; +import {Tree} from 'antd'; +import type {DataNode} from 'antd/es/tree'; +import {DownOutlined} from '@ant-design/icons'; + +function treeToAntTree(uiNode: UINode): DataNode { + return { + key: uiNode.id, + title: uiNode.name, + children: uiNode.children ? uiNode.children.map(treeToAntTree) : [], + }; +} + +function treeToMap(uiNode: UINode): Map { + const result = new Map(); + + function treeToMapRec(node: UINode): void { + result.set(node.id, node); + for (const child of node.children) { + treeToMapRec(child); + } + } + + treeToMapRec(uiNode); + + return result; +} export function Component() { const instance = usePlugin(plugin); const rootId = useValue(instance.rootId); + const tree = useValue(instance.tree); + + if (tree) { + const nodeMap = treeToMap(tree); + const antTree = treeToAntTree(tree); + return ( + { + console.log(nodeMap.get(selected[0] as string)); + }} + defaultExpandAll + switcherIcon={} + treeData={[antTree]} + /> + ); + } return
{rootId}
; } diff --git a/desktop/plugins/public/ui-debugger/index.tsx b/desktop/plugins/public/ui-debugger/index.tsx index 1684cbb86..e26fa2b74 100644 --- a/desktop/plugins/public/ui-debugger/index.tsx +++ b/desktop/plugins/public/ui-debugger/index.tsx @@ -9,14 +9,60 @@ import {PluginClient, createState} from 'flipper-plugin'; +export type Inspectable = + | InspectableObject + | InspectableText + | InspectableNumber + | InspectableColor; + +export type InspectableText = { + type: 'text'; + value: string; + mutable: boolean; +}; + +export type InspectableNumber = { + type: 'number'; + value: number; + mutable: boolean; +}; + +export type InspectableColor = { + type: 'number'; + value: number; + mutable: boolean; +}; + +export type InspectableObject = { + type: 'object'; + fields: Record; +}; + +export type Id = string; +export type UINode = { + id: Id; + name: string; + attributes: Record; + children: UINode[]; +}; + type Events = { init: {rootId: string}; + + nativeScan: {root: UINode}; }; export function plugin(client: PluginClient) { const rootId = createState(undefined); + + const tree = createState(undefined); client.onMessage('init', (root) => rootId.set(root.rootId)); - return {rootId}; + + client.onMessage('nativeScan', ({root}) => { + tree.set(root as UINode); + }); + + return {rootId, tree}; } export {Component} from './components/main';