Skip to content

Commit

Permalink
feat(templates): initial implementation for Boilerplate's isOnline wi…
Browse files Browse the repository at this point in the history
…thout signalR #9202 (#9210)
  • Loading branch information
ysmoradi authored Nov 12, 2024
1 parent e484cf7 commit 42040e6
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class AppComponentBase : ComponentBase, IAsyncDisposable
/// <summary>
/// <inheritdoc cref="Parameters.IsOnline"/>
/// </summary>
[CascadingParameter(Name = Parameters.IsOnline)] protected bool IsOnline { get; set; }
[CascadingParameter(Name = Parameters.IsOnline)] protected bool? IsOnline { get; set; }
[CascadingParameter] protected Task<AuthenticationState> AuthenticationStateTask { get; set; } = default!;

[AutoInject] protected IJSRuntime JSRuntime = default!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,12 @@ private async Task ConnectSignalR()

private async Task HubConnectionConnected(string? connectionId)
{
TelemetryContext.IsOnline = true;
PubSubService.Publish(ClientPubSubMessages.IS_ONLINE_CHANGED, true);
logger.LogInformation("SignalR connection {ConnectionId} established.", connectionId);
}

private async Task HubConnectionDisconnected(Exception? exception)
{
TelemetryContext.IsOnline = false;
PubSubService.Publish(ClientPubSubMessages.IS_ONLINE_CHANGED, false);

if (exception is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class RootContainer
/// <summary>
/// <inheritdoc cref="Parameters.IsOnline"/>
/// </summary>
[Parameter] public bool IsOnline { get; set; }
[Parameter] public bool? IsOnline { get; set; }
[Parameter] public BitDir? CurrentDir { get; set; }
[Parameter] public string? CurrentUrl { get; set; }
[Parameter] public bool? IsAuthenticated { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@ public partial class RootLayout : IDisposable
/// <summary>
/// <inheritdoc cref="Parameters.IsOnline"/>
/// </summary>
private bool isOnline
//#if (signalR == true)
= false;
//#else
//#if (IsInsideProjectTemplate == true)
/*
//#endif
= true;
//#if (IsInsideProjectTemplate == true)
*/
//#endif
//#endif
private bool? isOnline = null;
private bool? isAuthenticated;

/// <summary>
Expand All @@ -39,6 +28,7 @@ private bool isOnline
[AutoInject] private PubSubService pubSubService = default!;
[AutoInject] private AuthenticationManager authManager = default!;
[AutoInject] private IExceptionHandler exceptionHandler = default!;
[AutoInject] private ITelemetryContext telemetryContext = default!;
[AutoInject] private NavigationManager navigationManager = default!;
[AutoInject] private IPrerenderStateService prerenderStateService = default!;

Expand Down Expand Up @@ -70,13 +60,12 @@ protected override async Task OnInitializedAsync()
StateHasChanged();
}));

//#if (signalR == true)
unsubscribers.Add(pubSubService.Subscribe(ClientPubSubMessages.IS_ONLINE_CHANGED, async payload =>
{
isOnline = (bool)payload!;
telemetryContext.IsOnline = isOnline is true;
await InvokeAsync(StateHasChanged);
}));
//#endif

isAuthenticated = await prerenderStateService.GetValue(async () => (await AuthenticationStateTask).User.IsAuthenticated());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Size=@BitPersonaSize.Size32
PrimaryText="@user.DisplayName"
Classes="@(new() { DetailsContainer="persona-details" })"
Presence="@(IsOnline ? BitPersonaPresence.Online : BitPersonaPresence.Offline)" />
Presence="@(IsOnline is null ? BitPersonaPresence.None : IsOnline is true ? BitPersonaPresence.Online : BitPersonaPresence.Offline)" />
<BitIcon IconName="@BitIconName.ChevronDown" Size="BitSize.Small" Color="BitColor.Info" Class="menu-chevron" />
</Template>
<Body>
Expand All @@ -28,7 +28,7 @@
Size="BitPersonaSize.Size48"
PrimaryText="@user.DisplayName"
SecondaryText="@(user.Email ?? user.PhoneNumber)"
Presence="@(IsOnline ? BitPersonaPresence.Online : BitPersonaPresence.Offline)">
Presence="@(IsOnline is null ? BitPersonaPresence.None : IsOnline is true ? BitPersonaPresence.Online : BitPersonaPresence.Offline)">
<ImageOverlayTemplate>
<span>@Localizer[nameof(AppStrings.Edit)]</span>
</ImageOverlayTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
ImageUrl="@imageUrl"
PrimaryText="@User?.FullName"
Size=@BitPersonaSize.Size72
Presence="@(IsOnline ? BitPersonaPresence.Online : BitPersonaPresence.Offline)" />
Presence="@(IsOnline is null ? BitPersonaPresence.None : IsOnline is true ? BitPersonaPresence.Online : BitPersonaPresence.Offline)" />
<BitText Color="BitColor.Primary">
@Localizer[nameof(AppStrings.UploadNewProfileImage)]
</BitText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Size="BitPersonaSize.Size48"
Styles="@(new() { Image = "width:50%;height:50%" })"
ImageInitials=""
Presence="@(IsOnline ? BitPersonaPresence.Online : BitPersonaPresence.Offline)"
Presence="@(IsOnline is null ? BitPersonaPresence.None : IsOnline is true ? BitPersonaPresence.Online : BitPersonaPresence.Offline)"
ImageUrl="@($"/_content/Boilerplate.Client.Core/images/os/{GetImageUrl(currentSession.Device)}")" />
</BitCard>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public class Parameters

/// <summary>
/// Determines the connection status, with default behavior based on SignalR connection status.
/// If SignalR is not added to the project during initial project creation, this value will always be true by default.
/// If SignalR is not added to the project during initial project creation, this value will always be null by default.
/// Alternatively, you can implement custom logic to control this value.
/// true => Online, false => Offline, null => Unknown
/// </summary>
public const string IsOnline = nameof(IsOnline);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ public class AppTelemetryContext : ITelemetryContext

public string? Environment { get; set; } = AppEnvironment.Current;

//#if (signalR == true)
public bool IsOnline { get; set; }
//#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ public static partial class ClientPubSubMessages
public const string OPEN_NAV_PANEL = nameof(OPEN_NAV_PANEL);
public const string CULTURE_CHANGED = nameof(CULTURE_CHANGED);
public const string PROFILE_UPDATED = nameof(PROFILE_UPDATED);
public const string IS_ONLINE_CHANGED = nameof(IS_ONLINE_CHANGED);
public const string PAGE_TITLE_CHANGED = nameof(PAGE_TITLE_CHANGED);
public const string ROUTE_DATA_UPDATED = nameof(ROUTE_DATA_UPDATED);
public const string UPDATE_IDENTITY_HEADER_BACK_LINK = nameof(UPDATE_IDENTITY_HEADER_BACK_LINK);
public const string IDENTITY_HEADER_BACK_LINK_CLICKED = nameof(IDENTITY_HEADER_BACK_LINK_CLICKED);

//#if (signalR == true)
public const string IS_ONLINE_CHANGED = nameof(IS_ONLINE_CHANGED);
//#endif

public const string SHOW_DIAGNOSTIC_MODAL = nameof(SHOW_DIAGNOSTIC_MODAL);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public static ITelemetryContext? Current

public string? Environment { get; set; }

//#if (signalR == true)
public bool IsOnline { get; set; }
//#endif

public Dictionary<string, object?> ToDictionary(Dictionary<string, object?>? additionalParameters = null)
{
Expand All @@ -61,9 +59,7 @@ public static ITelemetryContext? Current
{ nameof(TimeZone), TimeZone },
{ nameof(Culture), Culture },
{ nameof(Environment), Environment },
//#if (signalR == true)
{ nameof(IsOnline), IsOnline }
//#endif
};

if (AppPlatform.IsBlazorHybrid)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Net;
//+:cnd:noEmit
using System.Net;

namespace Boilerplate.Client.Core.Services.HttpMessageHandlers;

public partial class ExceptionDelegatingHandler(IStringLocalizer<AppStrings> localizer,
//#if (signalR != true)
PubSubService pubSubService,
//#endif
JsonSerializerOptions jsonSerializerOptions,
HttpMessageHandler handler)
: DelegatingHandler(handler)
Expand Down Expand Up @@ -57,5 +61,11 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
throw new ServerConnectionException(localizer[nameof(AppStrings.ServerConnectionException)], exp);
}
//#if (signalR != true)
finally
{
pubSubService.Publish(ClientPubSubMessages.IS_ONLINE_CHANGED, serverCommunicationSuccess);
}
//#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public void Initialize(ITelemetry telemetry)
telemetry.Context.GlobalProperties[nameof(ITelemetryContext.UserAgent)] = ITelemetryContext.Current.UserAgent;
telemetry.Context.GlobalProperties[nameof(ITelemetryContext.TimeZone)] = ITelemetryContext.Current.TimeZone;
telemetry.Context.GlobalProperties[nameof(ITelemetryContext.Culture)] = ITelemetryContext.Current.Culture;
//#if (signalR == true)
telemetry.Context.GlobalProperties[nameof(ITelemetryContext.IsOnline)] = ITelemetryContext.Current.IsOnline.ToString().ToLowerInvariant();
//#endif
}

telemetry.Context.Session.IsFirst = VersionTracking.IsFirstLaunchEver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public void Initialize(ITelemetry telemetry)
telemetry.Context.GlobalProperties[nameof(ITelemetryContext.UserAgent)] = ITelemetryContext.Current.UserAgent;
telemetry.Context.GlobalProperties[nameof(ITelemetryContext.TimeZone)] = ITelemetryContext.Current.TimeZone;
telemetry.Context.GlobalProperties[nameof(ITelemetryContext.Culture)] = ITelemetryContext.Current.Culture;
//#if (signalR == true)
telemetry.Context.GlobalProperties[nameof(ITelemetryContext.IsOnline)] = ITelemetryContext.Current.IsOnline.ToString().ToLowerInvariant();
//#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<!--/-:msbuild-conditional:noEmit -->
<PackageVersion Include="Humanizer" Version="2.14.1" />
<PackageVersion Include="QRCoder" Version="1.6.0" />
<PackageVersion Include="Magick.NET-Q16-AnyCPU" Version="14.0.0" />
<PackageVersion Include="Magick.NET-Q16-AnyCPU" Version="14.1.0" />
<PackageVersion Include="FluentEmail.Smtp" Version="3.0.2" />
<PackageVersion Include="FluentStorage" Version="5.6.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
Expand Down

0 comments on commit 42040e6

Please sign in to comment.