about summary refs log tree commit diff
path: root/MatrixUtils.Web/Shared/MxcImage.razor
blob: e651c3f568cc418513865673558c386a4f69517b (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
<img src="@ResolvedUri" style="@StyleString"/>
@code {
    private string _mxcUri;
    private string _style;
    private string _resolvedUri;

    [Parameter]
    public string MxcUri {
        get => _mxcUri ?? "";
        set {
            Console.WriteLine($"New MXC uri: {value}");
            _mxcUri = value;
            UriHasChanged(value);
        }
    }
    [Parameter]
    public bool Circular { get; set; }
    
    [Parameter]
    public int? Width { get; set; }
    
    [Parameter]
    public int? Height { get; set; }
    
    [Parameter]
    public string Style {
        get => _style;
        set {
            _style = value;
            StateHasChanged();
        }
    }
    
    [Parameter]
    public RemoteHomeserver? Homeserver { get; set; }

    private string ResolvedUri {
        get => _resolvedUri;
        set {
            _resolvedUri = value;
            StateHasChanged();
        }
    }

    private string StyleString => $"{Style} {(Circular ? "border-radius: 50%;" : "")} {(Width.HasValue ? $"width: {Width}px;" : "")} {(Height.HasValue ? $"height: {Height}px;" : "")} object-fit: cover;";
    
    private static readonly string Prefix = "mxc://";
    private static readonly int PrefixLength = Prefix.Length;

    private async Task UriHasChanged(string value) {
        if (!value.StartsWith(Prefix)) {
            Console.WriteLine($"UriHasChanged: {value} does not start with {Prefix}, passing as resolved URI!!!");
            ResolvedUri = value;
            return;
        }
        var uri = value[PrefixLength..].Split('/');
        Console.WriteLine($"UriHasChanged: {value} {uri[0]}");
        if (Homeserver is null) {
            Console.WriteLine($"Homeserver is null, creating new remotehomeserver for {uri[0]}");
            Homeserver = await hsProvider.GetRemoteHomeserver(uri[0]);
        }
        ResolvedUri = Homeserver.ResolveMediaUri(value);
        Console.WriteLine($"ResolvedUri: {ResolvedUri}");
    }

    // [Parameter]
    // public string Class { get; set; }

}