aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/PropertiesDocContextSelector.tsx
blob: d157e7b1ce7714ed80ec6dd386ae6a5da7d50aee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { computed } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc, DocListCast } from '../../fields/Doc';
import { Id } from '../../fields/FieldSymbols';
import { Cast, StrCast } from '../../fields/Types';
import { DocFocusOrOpen } from '../util/DocumentManager';
import { CollectionDockingView } from './collections/CollectionDockingView';
import { DocumentView, OpenWhere } from './nodes/DocumentView';
import './PropertiesDocContextSelector.scss';

type PropertiesDocContextSelectorProps = {
    DocView?: DocumentView;
    Stack?: any;
    hideTitle?: boolean;
    addDocTab(doc: Doc, location: OpenWhere): void;
};

@observer
export class PropertiesDocContextSelector extends React.Component<PropertiesDocContextSelectorProps> {
    @computed get _docs() {
        if (!this.props.DocView) return [];
        const target = this.props.DocView.props.Document;
        const targetContext = this.props.DocView.props.docViewPath().lastElement()?.rootDoc;
        const embeddings = DocListCast(target.proto_embeddings);
        const containerProtos = embeddings.filter(embedding => embedding.embedContainer && embedding.embedContainer instanceof Doc).reduce((set, embedding) => set.add(Cast(embedding.embedContainer, Doc, null)), new Set<Doc>());
        const containerSets = Array.from(containerProtos.keys()).map(container => DocListCast(container.proto_embeddings));
        const containers = containerSets.reduce((p, set) => {
            set.map(s => p.add(s));
            return p;
        }, new Set<Doc>());
        const doclayoutSets = Array.from(containers.keys()).map(dp => DocListCast(dp.proto_embeddings));
        const doclayouts = Array.from(
            doclayoutSets
                .reduce((p, set) => {
                    set.map(s => p.add(s));
                    return p;
                }, new Set<Doc>())
                .keys()
        );

        return doclayouts
            .filter(doc => !Doc.AreProtosEqual(doc, CollectionDockingView.Instance?.props.Document))
            .filter(doc => !Doc.IsSystem(doc))
            .filter(doc => doc !== targetContext)
            .map(doc => ({ col: doc, target }));
    }

    getOnClick = (col: Doc, target: Doc) => {
        if (!this.props.DocView) return;
        col = Doc.IsDataProto(col) ? Doc.MakeDelegate(col) : col;
        DocFocusOrOpen(Doc.GetProto(this.props.DocView.props.Document), undefined, col);
    };

    render() {
        if (this._docs.length < 1) return undefined;
        return (
            <div>
                {this.props.hideTitle ? null : <p key="contexts">Contexts:</p>}
                {this._docs.map(doc => (
                    <p key={doc.col[Id] + doc.target[Id]}>
                        <a onClick={() => this.getOnClick(doc.col, doc.target)}>{StrCast(doc.col.title)}</a>
                    </p>
                ))}
            </div>
        );
    }
}