about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Rooms/Index2Components/RoomsIndex2MainTab.razor
blob: b163a52a99a57f3bbf3e611d0482553934ca2070 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
@using MatrixUtils.Abstractions
@using System.Security.Cryptography
@using ArcaneLibs.Extensions
@using System.ComponentModel
@using System.Diagnostics
@using LibMatrix.EventTypes.Spec.State
@using MatrixUtils.Web.Pages.Rooms.Index2Components.MainTabComponents
@using Microsoft.AspNetCore.Components.Rendering
<h3>RoomsIndex2MainTab</h3>

@* <div> *@
@*     <div class="row"> *@
@*         <div class="col-3" style="background-color: #ffffff66;"> *@
@*             <LinkButton>Uncategorised rooms</LinkButton> *@
@*             @foreach (var space in GetTopLevelSpaces()) { *@
@*                 <a style="@("display:block; width: 100%; height: 50px; background-color: #" + RandomNumberGenerator.GetBytes(3).Append((byte)0x11).ToArray().AsHexString().Replace(" ", ""))"> *@
@*                     <div style="vertical-align: middle;"> *@
@*                         <div style="overflow:hidden; text-overflow: ellipsis; white-space: nowrap; ">@space.RoomName</div> *@
@*                     </div> *@
@*                 </a> *@
@*             } *@
@*         </div> *@
@*         <div class="col-9" style="background-color: #ff00ff66;"> *@
@*             <p>Placeholder for rooms list...</p> *@
@*         </div> *@
@*     </div> *@
@* </div> *@

<div>
    <div class="row">
        <div class="col-3" style="background-color: #ffffff22;">
            <LinkButton>Uncategorised rooms</LinkButton>
            @foreach (var space in GetTopLevelSpaces()) {
                @* @RecursingSpaceChildren(space) *@
                <MainTabSpaceItem Space="space" OpenedSpaces="OpenedSpaces" @bind-SelectedSpace="SelectedSpace" />
            }
        </div>
        <div class="col-9" style="background-color: #ff00ff66;">
            <p>Placeholder for rooms list...</p>
            @if (SelectedSpace != null) {
                foreach (var room in GetSpaceChildRooms(SelectedSpace)) {
                    <p>@room.RoomName</p>
                }
            }
        </div>
    </div>
</div>


@code {

    [CascadingParameter]
    public Index2.RoomListViewData Data { get; set; } = null!;

    protected override async Task OnInitializedAsync() {
        Data.Rooms.CollectionChanged += (sender, args) => {
            DebouncedStateHasChanged();
            if (args.NewItems is { Count: > 0 })
                foreach (var newItem in args.NewItems) {
                    (newItem as RoomInfo).PropertyChanged += OnRoomListChanged;
                    (newItem as RoomInfo).StateEvents.CollectionChanged += (sender, args) => { DebouncedStateHasChanged(); };
                }
        };
        foreach (var newItem in Data.Rooms) {
            newItem.PropertyChanged += OnRoomListChanged;
            newItem.StateEvents.CollectionChanged += (sender, args) => { DebouncedStateHasChanged(); };
        }

        await base.OnInitializedAsync();
        StateHasChanged();
    }

    private void OnRoomListChanged(object? sender, PropertyChangedEventArgs e) {
        if (e.PropertyName == "RoomName" || e.PropertyName == "RoomType")
            DebouncedStateHasChanged();
    }

    private CancellationTokenSource _debounceCts = new CancellationTokenSource();

    private async Task DebouncedStateHasChanged() {
        _debounceCts.Cancel();
        _debounceCts = new CancellationTokenSource();
        try {
            Console.WriteLine("DebouncedStateHasChanged - Waiting 50ms...");
            await Task.Delay(50, _debounceCts.Token);
            Console.WriteLine("DebouncedStateHasChanged - Calling StateHasChanged!");
            StateHasChanged();
        }
        catch (TaskCanceledException) { }
    }

    private List<RoomInfo> GetTopLevelSpaces() {
        var spaces = Data.Rooms.Where(x => x.RoomType == "m.space").OrderBy(x => x.RoomName).ToList();
        var allSpaceChildEvents = spaces.SelectMany(x => x.StateEvents.Where(y =>
            y.Type == SpaceChildEventContent.EventId &&
            y.RawContent!.Count > 0
        )).ToList();

        Console.WriteLine($"Child count: {allSpaceChildEvents.Count}");

        spaces.RemoveAll(x => allSpaceChildEvents.Any(y => y.StateKey == x.Room.RoomId));

        if (allSpaceChildEvents.Count == 0) {
            Console.WriteLine("No space children found, returning nothing...");
            return [];
        }

        return spaces.ToList();
    }

    private List<RoomInfo> GetSpaceChildren(RoomInfo space) {
        var childEvents = space.StateEvents.Where(x =>
            x.Type == SpaceChildEventContent.EventId &&
            x.RawContent!.Count > 0
        ).ToList();
        var children = childEvents.Select(x => Data.Rooms.FirstOrDefault(y => y.Room.RoomId == x.StateKey)).Where(x => x is not null).ToList();
        return children;
    }

    private List<RoomInfo> GetSpaceChildSpaces(RoomInfo space) {
        var children = GetSpaceChildren(space);
        var childSpaces = children.Where(x => x.RoomType == "m.space").ToList();
        return childSpaces;
    }
    
    private List<RoomInfo> GetSpaceChildRooms(RoomInfo space) {
        var children = GetSpaceChildren(space);
        var childRooms = children.Where(x => x.RoomType != "m.space").ToList();
        return childRooms;
    }

    private RoomInfo? SelectedSpace { get; set; }
    private List<RoomInfo> OpenedSpaces { get; set; } = new List<RoomInfo>();

    // private RenderFragment RecursingSpaceChildren(RoomInfo space, List<RoomInfo>? parents = null, int depth = 0) {
    //     parents ??= [];
    //     var totalSw = Stopwatch.StartNew();
    //     var children = GetSpaceChildSpaces(space);
    //
    //     var randomColor = RandomNumberGenerator.GetBytes(3).Append((byte)0x33).ToArray().AsHexString().Replace(" ", "");
    //     var isExpanded = OpenedSpaces.Contains(space);
    //
    //     // Console.WriteLine($"RecursingSpaceChildren::FetchData - Depth: {depth}, Space: {space.RoomName}, Children: {children.Count} - {totalSw.Elapsed}");
    //
    //     // var renderSw = Stopwatch.StartNew();
    //     var rf = new RenderFragment(builder => {
    //         builder.OpenElement(0, "div");
    //         //space list entry render fragment
    //         // builder.AddContent(1, SpaceListEntry(space));
    //         builder.OpenComponent<MainTabSpaceItem>(1);
    //         builder.AddAttribute(2, "Space", space);
    //         builder.AddAttribute(2, "OpenedSpaces", OpenedSpaces);
    //         builder.CloseComponent();
    //         builder.CloseElement();
    //         //space children render fragment
    //         if (isExpanded) {
    //             builder.OpenElement(2, "div");
    //             builder.AddAttribute(3, "style", "padding-left: 10px;");
    //             foreach (var child in children) {
    //                 builder.AddContent(4, RecursingSpaceChildren(child, parents.Append(space).ToList(), depth + 1));
    //             }
    //
    //             builder.CloseElement();
    //         }
    //     });
    //
    //     // Console.WriteLine($"RecursingSpaceChildren::Render - Depth: {depth}, Space: {space.RoomName}, Children: {children.Count} - {renderSw.Elapsed}");
    //     if (totalSw.ElapsedMilliseconds > 20)
    //         Console.WriteLine($"RecursingSpaceChildren::Total - Depth: {depth}, Space: {space.RoomName}, Children: {children.Count} - {totalSw.Elapsed}");
    //     // Console.WriteLine($"RecursingSpaceChildren::Total - Depth: {depth}, Space: {space.RoomName}, Children: {children.Count} - {totalSw.Elapsed}");
    //     return rf;
    // }

    // private RenderFragment SpaceListEntry(RoomInfo space) {
    //     return builder => {
    //         {
    //             builder.OpenElement(0, "div");
    //             builder.AddAttribute(1, "style", "display: block; width: 100%; height: 50px;");
    //             builder.AddAttribute(2, "onclick", EventCallback.Factory.Create(this, () => {
    //                 if (OpenedSpaces.Contains(space)) {
    //                     OpenedSpaces.Remove(space);
    //                 }
    //                 else {
    //                     OpenedSpaces.Add(space);
    //                 }
    //
    //                 StateHasChanged();
    //             }));
    //             {
    //                 builder.OpenComponent<MxcImage>(5);
    //                 builder.AddAttribute(6, "Homeserver", Data.Homeserver);
    //                 builder.AddAttribute(7, "MxcUri", space.RoomIcon);
    //                 builder.AddAttribute(8, "Circular", true);
    //                 builder.AddAttribute(9, "Width", 32);
    //                 builder.AddAttribute(10, "Height", 32);
    //                 builder.CloseComponent();
    //             }
    //             {
    //                 // room name, ellipsized
    //                 builder.OpenElement(11, "span");
    //                 builder.AddAttribute(12, "class", "spaceNameEllipsis");
    //                 builder.AddContent(13, space.RoomName);
    //                 builder.CloseElement();
    //             }
    //             builder.CloseElement();
    //         }
    //     };
    // }

}