about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/DebugTools.razor
blob: 4e4cec871d382b471ea90d9db33db767ea5a734e (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
@page "/Debug"
@using System.Reflection
@using MatrixRoomUtils.Core.Interfaces
@inject ILocalStorageService LocalStorage
@inject NavigationManager NavigationManager
<h3>Debug Tools</h3>
<hr/>
@if (Rooms.Count == 0) {
    <p>You are not in any rooms!</p>
    @* <p>Loading progress: @checkedRoomCount/@totalRoomCount</p> *@
}
else {
    <details>
        <summary>Room List</summary>
        @foreach (var room in Rooms) {
            <a style="color: unset; text-decoration: unset;" href="/RoomStateViewer/@room.Replace('.', '~')">
                <RoomListItem RoomId="@room"></RoomListItem>
            </a>
        }
    </details>
}

<details open>
    <summary>Send GET request to URL</summary>
    <div class="input-group">
        <input type="text" class="form-control" @bind-value="get_request_url" placeholder="URL">
        <button class="btn btn-outline-secondary" type="button" @onclick="SendGetRequest">Send</button>
    </div>
    <br/>
    <pre>@get_request_result</pre>
</details>

<div style="margin-bottom: 4em;"></div>
<LogView></LogView>

@code {
    public List<string> Rooms { get; set; } = new();

    protected override async Task OnInitializedAsync() {
        await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
        await base.OnInitializedAsync();
        if (RuntimeCache.CurrentHomeServer == null) {
            NavigationManager.NavigateTo("/Login");
            return;
        }
        Rooms = (await RuntimeCache.CurrentHomeServer.GetJoinedRooms()).Select(x => x.RoomId).ToList();
        Console.WriteLine("Fetched joined rooms!");
    }

    //send req
    string get_request_url { get; set; } = "";
    string get_request_result { get; set; } = "";

    private async Task SendGetRequest() {
        var field = typeof(IHomeServer).GetRuntimeFields().First(x => x.ToString().Contains("<_httpClient>k__BackingField"));
        var httpClient = field.GetValue(RuntimeCache.CurrentHomeServer) as HttpClient;
        try {
            var res = await httpClient.GetAsync(get_request_url);
            if (res.IsSuccessStatusCode) {
                if (res.Content.Headers.ContentType.MediaType == "application/json")
                    get_request_result = (await res.Content.ReadFromJsonAsync<object>()).ToJson();
                else
                    get_request_result = await res.Content.ReadAsStringAsync();
                StateHasChanged();
                return;
            }
            if (res.Content.Headers.ContentType.MediaType == "application/json")
                get_request_result = $"Error: {res.StatusCode}\n" + (await res.Content.ReadFromJsonAsync<object>()).ToJson();
            else
                get_request_result = $"Error: {res.StatusCode}\n" + await res.Content.ReadAsStringAsync();
        }
        catch (Exception e) {
            get_request_result = $"Error: {e}";
        }
        StateHasChanged();
    }

}