1
0
mirror of synced 2024-11-25 07:20:15 +01:00
TaikoLocalServer/TaikoWebUI/Components/MainLayout.razor

107 lines
3.6 KiB
Plaintext

@inherits LayoutComponentBase
@inject Blazored.LocalStorage.ILocalStorageService LocalStorage
@inject HttpClient Client
@inject LoginService LoginService
<MudThemeProvider IsDarkMode="@isDarkMode" Theme="@taikoWebUiTheme" />
<MudDialogProvider />
<MudSnackbarProvider />
<MudLayout>
<MudAppBar Elevation="0">
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="DrawerToggle" Size="Size.Small" />
<MudSpacer />
<MudStack Spacing="2" Row="true">
<MudIconButton Icon="@DarkModeIcon" Size="Size.Small" Color="Color.Inherit" OnClick="ToggleDarkMode" />
<ChooseLanguage />
</MudStack>
</MudAppBar>
<MudDrawer Elevation="0" Style="border-right:1px solid #ededf0" @bind-Open="drawerOpen">
<MudDrawerHeader>
<MudText Typo="Typo.h6">TaikoWebUI</MudText>
</MudDrawerHeader>
<NavMenu />
</MudDrawer>
<MudMainContent>
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="my-8 pb-8">
@Body
<MudScrollToTop>
<MudFab Color="Color.Secondary" StartIcon="@Icons.Material.Filled.ArrowCircleUp" />
</MudScrollToTop>
</MudContainer>
</MudMainContent>
</MudLayout>
@code {
bool drawerOpen = true;
bool isDarkMode = false;
protected override async Task OnInitializedAsync()
{
var hasDrawerOpenEntry = await LocalStorage.ContainKeyAsync("drawerOpen");
if (hasDrawerOpenEntry)
{
drawerOpen = await LocalStorage.GetItemAsync<bool>("drawerOpen");
}
var hasDarkModeEntry = await LocalStorage.ContainKeyAsync("isDarkMode");
if (hasDarkModeEntry)
{
isDarkMode = await LocalStorage.GetItemAsync<bool>("isDarkMode");
}
if (LoginService.LoginRequired)
{
// If not logged in, attempt to use JwtToken from local storage to log in
var hasJwtToken = await LocalStorage.ContainKeyAsync("authToken");
if (hasJwtToken)
{
var authToken = await LocalStorage.GetItemAsync<string>("authToken");
if (!string.IsNullOrWhiteSpace(authToken))
{
// Attempt to log in with the token
var loginResult = await LoginService.LoginWithAuthToken(authToken, Client);
if (!loginResult)
{
// Failed to log in with the token, remove it
await LocalStorage.RemoveItemAsync("authToken");
}
}
}
}
}
private async Task DrawerToggle()
{
drawerOpen = !drawerOpen;
await LocalStorage.SetItemAsync("drawerOpen", drawerOpen);
}
private async Task ToggleDarkMode()
{
isDarkMode = !isDarkMode;
await LocalStorage.SetItemAsync("isDarkMode", isDarkMode);
}
private string DarkModeIcon => isDarkMode ? Icons.Material.Filled.BrightnessLow : Icons.Material.Filled.Brightness2;
readonly MudTheme taikoWebUiTheme = new()
{
Palette = new PaletteLight()
{
Primary = Colors.Indigo.Default,
PrimaryLighten = Colors.Indigo.Lighten2,
PrimaryDarken = Colors.Indigo.Darken2,
AppbarBackground = Colors.Indigo.Darken3,
},
PaletteDark = new PaletteDark()
{
Primary = Colors.Indigo.Accent1,
PrimaryLighten = Colors.Indigo.Lighten4,
PrimaryDarken = Colors.Indigo.Accent1,
},
};
}