From 957a336349494f4586026a68be2fe11d7e5348e2 Mon Sep 17 00:00:00 2001 From: Luke De Feo Date: Fri, 21 Jul 2023 07:17:31 -0700 Subject: [PATCH] UID refactor 4/ Expose readonly UIState Summary: Currently state writes can either go through a named handler that is easy to find and debug or they can directly modify the state. By exposing readonly atoms only we ensure that all state writes go through a UIACtions. This adds consistency and ease of future debugging. E.g We could add a utility to wrap all ui actions with logging statements Reviewed By: antonk52 Differential Revision: D47547531 fbshipit-source-id: f88651169d8e7c5f7e31068d64f9aa5b6b573647 --- .../public/ui-debugger/DesktopTypes.tsx | 18 +++- .../ui-debugger/components/Controls.tsx | 27 +----- .../ui-debugger/components/PerfStats.tsx | 4 +- .../public/ui-debugger/components/Tree.tsx | 8 +- .../components/Visualization2D.tsx | 9 +- .../components/util/UIDebuggerMenuItem.tsx | 2 +- desktop/plugins/public/ui-debugger/index.tsx | 87 ++++++++++++------- 7 files changed, 86 insertions(+), 69 deletions(-) diff --git a/desktop/plugins/public/ui-debugger/DesktopTypes.tsx b/desktop/plugins/public/ui-debugger/DesktopTypes.tsx index e800b8db7..6c07b650e 100644 --- a/desktop/plugins/public/ui-debugger/DesktopTypes.tsx +++ b/desktop/plugins/public/ui-debugger/DesktopTypes.tsx @@ -7,7 +7,7 @@ * @format */ -import {Atom} from 'flipper-plugin'; +import {Atom, _ReadOnlyAtom} from 'flipper-plugin'; import { Id, FrameworkEventType, @@ -35,6 +35,14 @@ export type UIState = { filterMainThreadMonitoring: Atom; }; +//enumerates the keys of input type and casts each to ReadOnlyAtom, this is so we only expose read only atoms to the UI +//and all writes come through UIActions +type TransformToReadOnly = { + [P in keyof T]: T[P] extends Atom ? _ReadOnlyAtom : T[P]; +}; + +export type ReadOnlyUIState = TransformToReadOnly; + export type StreamFlowState = {paused: boolean}; export type NestedNode = { @@ -62,7 +70,7 @@ export type OnSelectNode = ( ) => void; export type UIActions = { - onHoverNode: (node?: Id) => void; + onHoverNode: (...node: Id[]) => void; onFocusNode: (focused?: Id) => void; onContextMenuOpen: (open: boolean) => void; onSelectNode: OnSelectNode; @@ -71,6 +79,12 @@ export type UIActions = { setVisualiserWidth: (width: number) => void; onSetFilterMainThreadMonitoring: (toggled: boolean) => void; onSetViewMode: (viewMode: ViewMode) => void; + onSetFrameworkEventMonitored: ( + eventType: FrameworkEventType, + monitored: boolean, + ) => void; + onPlayPauseToggled: () => void; + onSearchTermUpdated: (searchTerm: string) => void; }; export type SelectionSource = 'visualiser' | 'tree' | 'keyboard'; diff --git a/desktop/plugins/public/ui-debugger/components/Controls.tsx b/desktop/plugins/public/ui-debugger/components/Controls.tsx index 3a37664ca..3085a4553 100644 --- a/desktop/plugins/public/ui-debugger/components/Controls.tsx +++ b/desktop/plugins/public/ui-debugger/components/Controls.tsx @@ -27,12 +27,6 @@ import { } from '@ant-design/icons'; import {usePlugin, useValue, Layout} from 'flipper-plugin'; import {FrameworkEventType} from '../ClientTypes'; -import {tracker} from '../utils/tracker'; -import {debounce} from 'lodash'; - -const searchTermUpdated = debounce((searchTerm: string) => { - tracker.track('search-term-updated', {searchTerm}); -}, 250); export const Controls: React.FC = () => { const instance = usePlugin(plugin); @@ -49,23 +43,12 @@ export const Controls: React.FC = () => { const [showFrameworkEventsModal, setShowFrameworkEventsModal] = useState(false); - const onSetEventMonitored: ( - eventType: FrameworkEventType, - monitored: boolean, - ) => void = (eventType: FrameworkEventType, monitored: boolean) => { - tracker.track('framework-event-monitored', {eventType, monitored}); - instance.uiState.frameworkEventMonitoring.update((draft) => - draft.set(eventType, monitored), - ); - }; - return ( { - instance.uiState.searchTerm.set(e.target.value); - searchTermUpdated(e.target.value); + instance.uiActions.onSearchTermUpdated(e.target.value); }} prefix={} placeholder="Search" @@ -73,11 +56,7 @@ export const Controls: React.FC = () => {