LANCommander/LANCommander.UI/Components/SafeImage.razor
Pat Hartl 8c6cc7fcd1 Display depot images safely
Images in the depot should show a loading indicator, then display the image. If the image fails to load, it should fallback safely without showing a broken image icon.
2025-08-07 17:46:15 -05:00

49 lines
No EOL
1.1 KiB
Text

@using LANCommander.SDK.Models
@inject SDK.Client Client
<Spin Spinning="_loading">
@if (!_error)
{
if (!String.IsNullOrWhiteSpace(Source))
{
<img src="@Source" @onload="OnLoad" @onerror="OnError" class="@Class" style="@Style" />
}
else if (Media != null)
{
<img src="@(Client.Media.GetAbsoluteThumbnailUrl(Media))" @onload="OnLoad" @onerror="OnError" class="@Class" style="@Style" />
}
else
{
@ChildContent
}
}
else
{
@ChildContent
}
</Spin>
@code {
[Parameter] public string Source { get; set; }
[Parameter] public Media Media { get; set; }
[Parameter] public string Class { get; set; }
[Parameter] public string Style { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
bool _loading = true;
bool _error = false;
void OnLoad()
{
_loading = false;
Console.WriteLine("Image error " + Source);
StateHasChanged();
}
void OnError()
{
_error = true;
_loading = false;
StateHasChanged();
}
}