Skip to content

Commit

Permalink
Merge branch 'bitfoundation:develop' into 7761-dotnet9-rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi authored Nov 8, 2024
2 parents 788d788 + 14f0b1b commit 3f506ed
Show file tree
Hide file tree
Showing 42 changed files with 433 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public partial class ClientAppCoordinator : AppComponentBase
{
//#if (signalr == true)
private HubConnection? hubConnection;
[AutoInject] private IServiceProvider serviceProvider = default!;
//#endif
//#if (notification == true)
[AutoInject] private IPushNotificationService pushNotificationService = default!;
Expand Down Expand Up @@ -237,27 +238,4 @@ protected override async ValueTask DisposeAsync(bool disposing)

await base.DisposeAsync(disposing);
}

[AutoInject]
private IServiceProvider serviceProvider
{
set => currentServiceProvider = value;
get => currentServiceProvider!;
}

private static IServiceProvider? currentServiceProvider;
public static IServiceProvider? CurrentServiceProvider
{
get
{
if (AppPlatform.IsBlazorHybridOrBrowser is false)
throw new InvalidOperationException($"{nameof(CurrentServiceProvider)} is only available in Blazor Hybrid or blazor web assembly.");

return currentServiceProvider;
}
private set
{
currentServiceProvider = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
</BitStack>

<BitSpacer />
<DiagnosticSpacer />

<UserMenu />
</BitStack>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@inherits AppComponentBase

<div>
<BitModal @bind-IsOpen="isOpen" FullSize>
<BitStack FillContent Class="container">
<BitStack Horizontal AutoHeight VerticalAlign="BitAlignment.Center">
<BitText Typography="BitTypography.H3">Diagnostic</BitText>
<BitSpacer />
<BitButton IconOnly
Color="BitColor.Info"
Variant="BitVariant.Text"
OnClick="() => isOpen = false"
IconName="@BitIconName.ChromeClose" />
</BitStack>

<BitStack Horizontal FitHeight Wrap>
<BitSearchBox Immediate DebounceTime="500" OnChange="HandleOnSearchChange" />
<BitDropdown FitWidth IsMultiSelect
Items="logLevelItems"
DefaultValues="defaultFilterLogLevels"
OnValuesChange="HandleOnLogLevelFilter"
TItem="BitDropdownItem<LogLevel>" TValue="LogLevel" />
<BitButton IconOnly Color="BitColor.SecondaryBackground" Style="height:32px;"
OnClick="HandleOnSortClick"
IconName="@(isDescendingSort ? BitIconName.SortDown : BitIconName.SortUp)" />
</BitStack>

<BitBasicList @ref="logStackRef"
Style="height:unset"
EnableVirtualization
Items="filteredLogs.Indexed().ToArray()">
<EmptyContent>Nothing to show!</EmptyContent>
<RowTemplate Context="logIndex">
<BitStack @key="logIndex.item.CreatedOn" Horizontal AutoHeight Gap="0">
<BitText Style="min-width:7rem">@($"{logIndex.index + 1}. [{logIndex.item.CreatedOn.ToString("HH:mm:ss")}]")</BitText>
<BitButton IconOnly
Title="Copy"
Color="BitColor.Info"
Variant="BitVariant.Text"
IconName="@BitIconName.Copy"
OnClick="() => CopyException(logIndex.item)" />
<BitText Style="white-space:nowrap" Color="GetColor(logIndex.item.Level)">@logIndex.item.Message</BitText>
</BitStack>
</RowTemplate>
</BitBasicList>
</BitStack>

<BitButton IconOnly
OnClick="GoTop"
Color="BitColor.Info"
Class="go-top-button"
Variant="BitVariant.Text"
IconName="@BitIconName.Up" />
</BitModal>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Boilerplate.Client.Core.Services.DiagnosticLog;
using Microsoft.Extensions.Logging;

namespace Boilerplate.Client.Core.Components.Layout;

/// <summary>
/// This modal can be opened by clicking 7 times on the spacer of the header or by pressing Ctrl+Shift+X.
/// Also by calling `App.showDiagnostic` function using the dev-tools console.
/// </summary>
public partial class DiagnosticModal : IDisposable
{
private bool isOpen;
private string? searchText;
private bool isDescendingSort;
private Action unsubscribe = default!;
private IEnumerable<LogLevel> filterLogLevels = [];
private IEnumerable<DiagnosticLog> allLogs = default!;
private IEnumerable<DiagnosticLog> filteredLogs = default!;
private BitBasicList<(DiagnosticLog, int)> logStackRef = default!;
private readonly LogLevel[] defaultFilterLogLevels = [LogLevel.Warning, LogLevel.Error, LogLevel.Critical];
private readonly BitDropdownItem<LogLevel>[] logLevelItems = Enum.GetValues<LogLevel>().Select(v => new BitDropdownItem<LogLevel>() { Value = v, Text = v.ToString() }).ToArray();


[AutoInject] private Clipboard clipboard = default!;


protected override Task OnInitAsync()
{
unsubscribe = PubSubService.Subscribe(PubSubMessages.SHOW_DIAGNOSTIC_MODAL, async _ =>
{
isOpen = true;
allLogs = [.. DiagnosticLogger.Store];
HandleOnLogLevelFilter(defaultFilterLogLevels);
await InvokeAsync(StateHasChanged);
});

return base.OnInitAsync();
}


private void HandleOnSearchChange(string? text)
{
searchText = text;
FilterLogs();
}

private void HandleOnLogLevelFilter(BitDropdownItem<LogLevel>[] items)
{
HandleOnLogLevelFilter(items.Select(i => i.Value));
}

private void HandleOnLogLevelFilter(IEnumerable<LogLevel> logLevels)
{
filterLogLevels = logLevels;
FilterLogs();
}

private void HandleOnSortClick()
{
isDescendingSort = !isDescendingSort;
FilterLogs();
}

private void FilterLogs()
{
filteredLogs = allLogs.WhereIf(string.IsNullOrEmpty(searchText) is false, l => l.Message?.Contains(searchText!, StringComparison.InvariantCultureIgnoreCase) is true)
.Where(l => filterLogLevels.Contains(l.Level));
if (isDescendingSort)
{
filteredLogs = filteredLogs.OrderByDescending(l => l.CreatedOn);
}
else
{
filteredLogs = filteredLogs.OrderBy(l => l.CreatedOn);
}
}

private async Task CopyException(DiagnosticLog log)
{
var stateToCopy = string.Join(Environment.NewLine, log.State?.Select(i => $"{i.Key}: {i.Value}") ?? []);

await clipboard.WriteText($"{log.Message}{Environment.NewLine}{log.Exception?.ToString()}{Environment.NewLine}{stateToCopy}");
}

private async Task GoTop()
{
await logStackRef.RootElement.Scroll(0, 0);
}


private static BitColor GetColor(LogLevel level)
{
return level switch
{
LogLevel.Trace => BitColor.PrimaryForeground,
LogLevel.Debug => BitColor.PrimaryForeground,
LogLevel.Information => BitColor.Primary,
LogLevel.Warning => BitColor.Warning,
LogLevel.Error => BitColor.Error,
LogLevel.Critical => BitColor.Error,
LogLevel.None => BitColor.SecondaryForeground,
_ => BitColor.TertiaryForeground
};
}


public void Dispose()
{
unsubscribe?.Invoke();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@import '../../Styles/abstracts/_media-queries.scss';

section {
width: 100%;
height: 100%;
}

::deep {
.container {
padding: 1rem;
}

.stack {
overflow: auto;
}

.go-top-button {
left: 50%;
position: fixed;
transform: translateX(-50%);
bottom: calc(var(--app-inset-bottom) + 1rem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@inherits AppComponentBase

<BitSpacer @onclick="HandleOnClick" Style="height:100%" />

@code {
private int clickCount = 0;
private async Task HandleOnClick()
{
if (++clickCount == 7)
{
clickCount = 0;
PubSubService.Publish(PubSubMessages.SHOW_DIAGNOSTIC_MODAL);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</BitLink>
}

<BitSpacer />
<DiagnosticSpacer />

<BitButton IconOnly
FixedColor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@inherits AppComponentBase

@* this component creates a bridge between the js code (app.ts) and the .net code
that can easily be used to call any .net code from js without any hassle. *@
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Boilerplate.Client.Core.Components.Layout;

public partial class JsBridge : IDisposable
{
private DotNetObjectReference<JsBridge>? dotnetObj;
/// <summary>
/// at the rendering time of this component (the component is added to the `RootLayout`)
/// it registers an instance of the `DotNetObjectReference` into the js code (look at the `app.ts` file),
/// so we can later use it to call .net methods.
/// </summary>
protected override async Task OnAfterFirstRenderAsync()
{
dotnetObj = DotNetObjectReference.Create(this);

await JSRuntime.InvokeVoidAsync("App.registerJsBridge", dotnetObj);

await base.OnAfterFirstRenderAsync();
}


/// <summary>
/// you can add any other method like this to utilize the bridge between js and .net code.
/// </summary>
[JSInvokable(nameof(ShowDiagnostic))]
public async Task ShowDiagnostic()
{
PubSubService.Publish(PubSubMessages.SHOW_DIAGNOSTIC_MODAL);
}

public void Dispose()
{
dotnetObj?.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
section {
padding: 1rem;
min-width: 20rem;
max-height: var(--app-max-height-vh);
max-height: var(--app-height);

@include lt-md {
min-width: unset;
Expand All @@ -12,8 +12,8 @@ section {

::deep {
.root {
width: var(--app-max-width);
height: var(--app-max-height);
width: var(--app-width);
height: var(--app-height);
top: var(--app-inset-top);
left: var(--app-inset-left);
right: var(--app-inset-right);
Expand All @@ -28,7 +28,7 @@ section {
}

.stack {
max-height: calc(var(--app-max-height-vh) - 3rem);
max-height: calc(var(--app-height) - 3rem);
}

.body {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
@import '../../Styles/abstracts/_bit-css-variables.scss';

section {
bottom: 0;
width: 100%;
display: flex;
height: 3.5rem;
position: sticky;
max-width: 100vw;
position: fixed;
align-items: center;
max-width: var(--app-width);
justify-content: space-around;
bottom: var(--app-inset-bottom);
background-color: $bit-color-background-primary;
border-top: 1px solid $bit-color-border-tertiary;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ section {
padding: 1rem;
position: sticky;
overflow: hidden auto;
height: var(--app-max-height-vh);
height: var(--app-height);
min-width: var(--nav-menu-width);
max-width: var(--nav-menu-width);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="inset-top"></div>
<div class="inset-center" dir="@CurrentDir?.ToString().ToLower()">
<div class="inset-left"></div>
<div class="inset-main">
<div class="inset-main" id="bit-inset-main">
<AppErrorBoundary>
@ChildContent
</AppErrorBoundary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
height: 100%;
display: flex;
overflow: auto;
min-height: 100%;
min-height: unset;
position: relative;
scroll-behavior: smooth;
overscroll-behavior: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@

<SnackBar />
<MessageBox />
<DiagnosticModal />

<JsBridge />
Loading

0 comments on commit 3f506ed

Please sign in to comment.