From 380ee1acac1c0b7972d7d423cf804af146dc0edf Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Sun, 10 Dec 2023 20:19:27 -0500
Subject: massive changes to use mobx 6 which means not accessing props
 directly in @computed functions.

---
 src/client/views/collections/TabDocView.tsx | 91 +++++++++++++++++------------
 1 file changed, 53 insertions(+), 38 deletions(-)

(limited to 'src/client/views/collections/TabDocView.tsx')

diff --git a/src/client/views/collections/TabDocView.tsx b/src/client/views/collections/TabDocView.tsx
index 4a1d702b8..dc7ee206c 100644
--- a/src/client/views/collections/TabDocView.tsx
+++ b/src/client/views/collections/TabDocView.tsx
@@ -2,7 +2,7 @@ import { IconProp } from '@fortawesome/fontawesome-svg-core';
 import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
 import { Popup, Type } from 'browndash-components';
 import { clamp } from 'lodash';
-import { action, computed, IReactionDisposer, observable, ObservableSet, reaction } from 'mobx';
+import { action, computed, IReactionDisposer, makeObservable, observable, ObservableSet, reaction, runInAction } from 'mobx';
 import { observer } from 'mobx-react';
 import * as ReactDOM from 'react-dom/client';
 import { Doc, Opt } from '../../../fields/Doc';
@@ -11,7 +11,7 @@ import { Id } from '../../../fields/FieldSymbols';
 import { List } from '../../../fields/List';
 import { FieldId } from '../../../fields/RefField';
 import { Cast, DocCast, NumCast, StrCast } from '../../../fields/Types';
-import { DashColor, emptyFunction, lightOrDark, returnEmptyDoclist, returnFalse, returnTrue, setupMoveUpEvents, simulateMouseClick, Utils } from '../../../Utils';
+import { copyProps, DashColor, emptyFunction, lightOrDark, returnEmptyDoclist, returnFalse, returnTrue, setupMoveUpEvents, simulateMouseClick, Utils } from '../../../Utils';
 import { DocServer } from '../../DocServer';
 import { CollectionViewType, DocumentType } from '../../documents/DocumentTypes';
 import { DocumentManager } from '../../util/DocumentManager';
@@ -48,6 +48,15 @@ export class TabDocView extends React.Component<TabDocViewProps> {
     static _allTabs = new ObservableSet<TabDocView>();
     _mainCont: HTMLDivElement | null = null;
     _tabReaction: IReactionDisposer | undefined;
+
+    _prevProps: React.PropsWithChildren<TabDocViewProps>;
+    @observable _props: React.PropsWithChildren<TabDocViewProps>;
+    constructor(props: React.PropsWithChildren<TabDocViewProps>) {
+        super(props);
+        this._props = this._prevProps = props;
+        makeObservable(this);
+    }
+
     @observable _activated: boolean = false;
     @observable _panelWidth = 0;
     @observable _panelHeight = 0;
@@ -57,21 +66,21 @@ export class TabDocView extends React.Component<TabDocViewProps> {
     @computed get _isUserActivated() {
         return SelectionManager.IsSelected(this._document) || this._isAnyChildContentActive;
     }
-    @computed get _isContentActive() {
+    get _isContentActive() {
         return this._isUserActivated || this._hovering;
     }
-    @observable _document: Doc | undefined;
-    @observable _view: DocumentView | undefined;
+    @observable _document: Doc | undefined = undefined;
+    @observable _view: DocumentView | undefined = undefined;
 
     @computed get layoutDoc() {
         return this._document && Doc.Layout(this._document);
     }
 
     get stack() {
-        return (this.props as any).glContainer.parent.parent;
+        return this._props.glContainer.parent.parent;
     }
     get tab() {
-        return (this.props as any).glContainer.tab;
+        return this._props.glContainer.tab;
     }
     get view() {
         return this._view;
@@ -162,12 +171,12 @@ export class TabDocView extends React.Component<TabDocViewProps> {
                                           this._isUserActivated
                                               ? 0
                                               : this._hovering
-                                              ? 0.25
-                                              : degree === Doc.DocBrushStatus.selfBrushed
-                                              ? 0.5
-                                              : degree === Doc.DocBrushStatus.protoBrushed //
-                                              ? 0.7
-                                              : 0.9
+                                                ? 0.25
+                                                : degree === Doc.DocBrushStatus.selfBrushed
+                                                  ? 0.5
+                                                  : degree === Doc.DocBrushStatus.protoBrushed //
+                                                    ? 0.7
+                                                    : 0.9
                                       )
                                       .rgb()
                                       .toString()
@@ -316,7 +325,6 @@ export class TabDocView extends React.Component<TabDocViewProps> {
         setTimeout(batch.end, 500); // need to wait until dockingview (goldenlayout) updates all its structurs
     }
 
-    @action
     componentDidMount() {
         new _global.ResizeObserver(
             action((entries: any) => {
@@ -325,25 +333,25 @@ export class TabDocView extends React.Component<TabDocViewProps> {
                     this._panelHeight = entry.contentRect.height;
                 }
             })
-        ).observe(this.props.glContainer._element[0]);
-        this.props.glContainer.layoutManager.on('activeContentItemChanged', this.onActiveContentItemChanged);
-        this.props.glContainer.tab?.isActive && this.onActiveContentItemChanged(undefined);
+        ).observe(this._props.glContainer._element[0]);
+        this._props.glContainer.layoutManager.on('activeContentItemChanged', this.onActiveContentItemChanged);
+        this._props.glContainer.tab?.isActive && this.onActiveContentItemChanged(undefined);
         // this._tabReaction = reaction(() => ({ selected: this.active(), title: this.tab?.titleElement[0] }),
         //     ({ selected, title }) => title && (title.style.backgroundColor = selected ? "white" : ""),
         //     { fireImmediately: true });
-        TabDocView._allTabs.add(this);
+        runInAction(() => TabDocView._allTabs.add(this));
     }
     componentDidUpdate() {
         this._view && DocumentManager.Instance.AddView(this._view);
+        copyProps(this);
     }
 
-    @action
     componentWillUnmount() {
         this._tabReaction?.();
         this._view && DocumentManager.Instance.RemoveView(this._view);
-        TabDocView._allTabs.delete(this);
+        runInAction(() => TabDocView._allTabs.delete(this));
 
-        this.props.glContainer.layoutManager.off('activeContentItemChanged', this.onActiveContentItemChanged);
+        this._props.glContainer.layoutManager.off('activeContentItemChanged', this.onActiveContentItemChanged);
     }
 
     // Flag indicating that when a tab is activated, it should not select it's document.
@@ -420,7 +428,7 @@ export class TabDocView extends React.Component<TabDocViewProps> {
     ScreenToLocalTransform = () => {
         this._forceInvalidateScreenToLocal;
         const { translateX, translateY } = Utils.GetScreenTransform(this._mainCont?.children?.[0] as HTMLElement);
-        return CollectionDockingView.Instance?.props.ScreenToLocalTransform().translate(-translateX, -translateY) ?? Transform.Identity();
+        return CollectionDockingView.Instance?._props.ScreenToLocalTransform().translate(-translateX, -translateY) ?? Transform.Identity();
     };
     PanelWidth = () => this._panelWidth;
     PanelHeight = () => this._panelHeight;
@@ -441,8 +449,8 @@ export class TabDocView extends React.Component<TabDocViewProps> {
                         this._lastView = this._view;
                     })}
                     renderDepth={0}
-                    LayoutTemplateString={this.props.keyValue ? KeyValueBox.LayoutString() : undefined}
-                    hideTitle={this.props.keyValue}
+                    LayoutTemplateString={this._props.keyValue ? KeyValueBox.LayoutString() : undefined}
+                    hideTitle={this._props.keyValue}
                     Document={this._document}
                     TemplateDataDocument={!Doc.AreProtosEqual(this._document[DocData], this._document) ? this._document[DocData] : undefined}
                     onBrowseClick={DocumentView.exploreMode}
@@ -489,7 +497,7 @@ export class TabDocView extends React.Component<TabDocViewProps> {
                         }
                         this._lastTab = this.tab;
                         (this._mainCont as any).InitTab = (tab: any) => this.init(tab, this._document);
-                        DocServer.GetRefField(this.props.documentId).then(action(doc => doc instanceof Doc && (this._document = doc) && this.tab && this.init(this.tab, this._document)));
+                        DocServer.GetRefField(this._props.documentId).then(action(doc => doc instanceof Doc && (this._document = doc) && this.tab && this.init(this.tab, this._document)));
                         new _global.ResizeObserver(action((entries: any) => this._forceInvalidateScreenToLocal++)).observe(ref);
                     }
                 }}>
@@ -547,8 +555,15 @@ export class TabMinimapView extends React.Component<TabMinimapViewProps> {
             }
         }
     };
+
+    @observable _props: React.PropsWithChildren<TabMinimapViewProps>;
+    constructor(props: React.PropsWithChildren<TabMinimapViewProps>) {
+        super(props);
+        this._props = props;
+        makeObservable(this);
+    }
     @computed get renderBounds() {
-        const compView = this.props.tabView()?.ComponentView as CollectionFreeFormView;
+        const compView = this._props.tabView()?.ComponentView as CollectionFreeFormView;
         const bounds = compView?.freeformData?.(true)?.bounds;
         if (!bounds) return undefined;
         const xbounds = bounds.r - bounds.x;
@@ -556,10 +571,10 @@ export class TabMinimapView extends React.Component<TabMinimapViewProps> {
         const dim = Math.max(xbounds, ybounds);
         return { l: bounds.x + xbounds / 2 - dim / 2, t: bounds.y + ybounds / 2 - dim / 2, cx: bounds.x + xbounds / 2, cy: bounds.y + ybounds / 2, dim };
     }
-    childLayoutTemplate = () => Cast(this.props.document.childLayoutTemplate, Doc, null);
-    returnMiniSize = () => NumCast(this.props.document._miniMapSize, 150);
+    childLayoutTemplate = () => Cast(this._props.document.childLayoutTemplate, Doc, null);
+    returnMiniSize = () => NumCast(this._props.document._miniMapSize, 150);
     miniDown = (e: React.PointerEvent) => {
-        const doc = this.props.document;
+        const doc = this._props.document;
         const miniSize = this.returnMiniSize();
         doc &&
             setupMoveUpEvents(
@@ -578,15 +593,15 @@ export class TabMinimapView extends React.Component<TabMinimapViewProps> {
     popup = () => {
         if (!this.renderBounds) return <></>;
         const renderBounds = this.renderBounds;
-        const miniWidth = () => (this.props.PanelWidth() / NumCast(this.props.document._freeform_scale, 1) / renderBounds.dim) * 100;
-        const miniHeight = () => (this.props.PanelHeight() / NumCast(this.props.document._freeform_scale, 1) / renderBounds.dim) * 100;
-        const miniLeft = () => 50 + ((NumCast(this.props.document._freeform_panX) - renderBounds.cx) / renderBounds.dim) * 100 - miniWidth() / 2;
-        const miniTop = () => 50 + ((NumCast(this.props.document._freeform_panY) - renderBounds.cy) / renderBounds.dim) * 100 - miniHeight() / 2;
+        const miniWidth = () => (this._props.PanelWidth() / NumCast(this._props.document._freeform_scale, 1) / renderBounds.dim) * 100;
+        const miniHeight = () => (this._props.PanelHeight() / NumCast(this._props.document._freeform_scale, 1) / renderBounds.dim) * 100;
+        const miniLeft = () => 50 + ((NumCast(this._props.document._freeform_panX) - renderBounds.cx) / renderBounds.dim) * 100 - miniWidth() / 2;
+        const miniTop = () => 50 + ((NumCast(this._props.document._freeform_panY) - renderBounds.cy) / renderBounds.dim) * 100 - miniHeight() / 2;
         const miniSize = this.returnMiniSize();
         return (
-            <div className="miniMap" style={{ width: miniSize, height: miniSize, background: this.props.background() }}>
+            <div className="miniMap" style={{ width: miniSize, height: miniSize, background: this._props.background() }}>
                 <CollectionFreeFormView
-                    Document={this.props.document}
+                    Document={this._props.document}
                     docViewPath={returnEmptyDoclist}
                     childLayoutTemplate={this.childLayoutTemplate} // bcz: Ugh .. should probably be rendering a CollectionView or the minimap should be part of the collectionFreeFormView to avoid having to set stuff like this.
                     noOverlay={true} // don't render overlay Docs since they won't scale
@@ -596,7 +611,7 @@ export class TabMinimapView extends React.Component<TabMinimapViewProps> {
                     select={emptyFunction}
                     isSelected={returnFalse}
                     dontRegisterView={true}
-                    fieldKey={Doc.LayoutFieldKey(this.props.document)}
+                    fieldKey={Doc.LayoutFieldKey(this._props.document)}
                     bringToFront={emptyFunction}
                     addDocument={returnFalse}
                     moveDocument={returnFalse}
@@ -608,7 +623,7 @@ export class TabMinimapView extends React.Component<TabMinimapViewProps> {
                     whenChildContentsActiveChanged={emptyFunction}
                     focus={emptyFunction}
                     styleProvider={TabMinimapView.miniStyleProvider}
-                    addDocTab={this.props.addDocTab}
+                    addDocTab={this._props.addDocTab}
                     pinToPres={TabDocView.PinDoc}
                     childFilters={CollectionDockingView.Instance?.childDocFilters ?? returnEmptyDoclist}
                     childFiltersByRanges={CollectionDockingView.Instance?.childDocRangeFilters ?? returnEmptyDoclist}
@@ -622,7 +637,7 @@ export class TabMinimapView extends React.Component<TabMinimapViewProps> {
         );
     };
     render() {
-        return this.props.document.layout !== CollectionView.LayoutString(Doc.LayoutFieldKey(this.props.document)) || this.props.document?._type_collection !== CollectionViewType.Freeform ? null : (
+        return this._props.document.layout !== CollectionView.LayoutString(Doc.LayoutFieldKey(this._props.document)) || this._props.document?._type_collection !== CollectionViewType.Freeform ? null : (
             <div className="miniMap-hidden">
                 <Popup icon={<FontAwesomeIcon icon="globe-asia" size="lg" />} color={SettingsManager.userVariantColor} type={Type.TERT} onPointerDown={e => e.stopPropagation()} placement={'top-end'} popup={this.popup} />
             </div>
-- 
cgit v1.2.3-70-g09d2

