Code Clenup

This commit is contained in:
2026-04-05 02:05:24 +02:00
parent a290ff87dd
commit b135203318
37 changed files with 661 additions and 992 deletions

View File

@@ -3,40 +3,22 @@ using RpgRoller.Contracts;
namespace RpgRoller.Components.Pages;
public sealed class WorkspaceSessionCoordinator
public sealed class WorkspaceSessionCoordinator(WorkspaceState state, WorkspaceFeedbackService feedback, IJSRuntime js, RpgRollerApiClient apiClient, WorkspaceQueryService workspaceQuery, Func<Guid?, Task> reloadCampaignsAsync, Func<Task> reloadCharacterCampaignOptionsAsync, Func<Task> refreshCampaignScopeAsync, Func<Task> syncStateEventsAsync, Func<Task> stopStateEventsAsync, Func<Task> ensureAdminUsersLoadedAsync, Action resetCampaignLogDetailState, Func<Task> requestRefreshAsync, Func<string?, Task> onLoggedOutAsync)
{
public WorkspaceSessionCoordinator(WorkspaceState state, WorkspaceFeedbackService feedback, IJSRuntime js, RpgRollerApiClient apiClient, WorkspaceQueryService workspaceQuery, Func<Guid?, Task> reloadCampaignsAsync, Func<Task> reloadCharacterCampaignOptionsAsync, Func<Task> refreshCampaignScopeAsync, Func<Task> syncStateEventsAsync, Func<Task> stopStateEventsAsync, Func<Task> ensureAdminUsersLoadedAsync, Action resetCampaignLogDetailState, Func<Task> requestRefreshAsync, Func<string?, Task> onLoggedOutAsync)
{
m_State = state;
m_Feedback = feedback;
m_JS = js;
m_ApiClient = apiClient;
m_WorkspaceQuery = workspaceQuery;
m_ReloadCampaignsAsync = reloadCampaignsAsync;
m_ReloadCharacterCampaignOptionsAsync = reloadCharacterCampaignOptionsAsync;
m_RefreshCampaignScopeAsync = refreshCampaignScopeAsync;
m_SyncStateEventsAsync = syncStateEventsAsync;
m_StopStateEventsAsync = stopStateEventsAsync;
m_EnsureAdminUsersLoadedAsync = ensureAdminUsersLoadedAsync;
m_ResetCampaignLogDetailState = resetCampaignLogDetailState;
m_RequestRefreshAsync = requestRefreshAsync;
m_OnLoggedOutAsync = onLoggedOutAsync;
}
public async Task InitializeAsync()
{
var storedScreen = await m_JS.InvokeAsync<string?>("rpgRollerApi.getSessionValue", ScreenSessionKey);
m_State.CurrentScreen = NormalizeRequestedScreen(storedScreen) ?? ScreenPlay;
var storedScreen = await js.InvokeAsync<string?>("rpgRollerApi.getSessionValue", ScreenSessionKey);
state.CurrentScreen = NormalizeRequestedScreen(storedScreen) ?? ScreenPlay;
var storedPanel = await m_JS.InvokeAsync<string?>("rpgRollerApi.getSessionValue", MobilePanelSessionKey);
var storedPanel = await js.InvokeAsync<string?>("rpgRollerApi.getSessionValue", MobilePanelSessionKey);
if (string.Equals(storedPanel, "log", StringComparison.OrdinalIgnoreCase))
m_State.MobilePanel = "log";
state.MobilePanel = "log";
var storedRollVisibility = await m_JS.InvokeAsync<string?>("rpgRollerApi.getSessionValue", RollVisibilitySessionKey);
m_State.RollVisibility = NormalizeRollVisibility(storedRollVisibility);
var storedRollVisibility = await js.InvokeAsync<string?>("rpgRollerApi.getSessionValue", RollVisibilitySessionKey);
state.RollVisibility = NormalizeRollVisibility(storedRollVisibility);
Guid? preferredCampaignId = null;
var storedCampaignId = await m_JS.InvokeAsync<string?>("rpgRollerApi.getSessionValue", CampaignSessionKey);
var storedCampaignId = await js.InvokeAsync<string?>("rpgRollerApi.getSessionValue", CampaignSessionKey);
if (Guid.TryParse(storedCampaignId, out var parsedCampaignId))
preferredCampaignId = parsedCampaignId;
@@ -45,17 +27,17 @@ public sealed class WorkspaceSessionCoordinator
var reloaded = await ReloadAuthenticatedSessionAsync(preferredCampaignId);
if (!reloaded)
await m_OnLoggedOutAsync("Session expired. Please log in again.");
await onLoggedOutAsync("Session expired. Please log in again.");
}
public async Task RetryAfterHealthIssueAsync()
{
await CheckHealthAsync();
if (!m_State.HasHealthIssue && m_State.User is not null)
if (!state.HasHealthIssue && state.User is not null)
{
var reloaded = await ReloadAuthenticatedSessionAsync(m_State.SelectedCampaignId);
var reloaded = await ReloadAuthenticatedSessionAsync(state.SelectedCampaignId);
if (!reloaded)
await m_OnLoggedOutAsync("Session expired. Please log in again.");
await onLoggedOutAsync("Session expired. Please log in again.");
}
}
@@ -63,102 +45,102 @@ public sealed class WorkspaceSessionCoordinator
{
try
{
var usernames = await m_WorkspaceQuery.GetUsernamesAsync();
m_State.KnownUsernames = usernames.OrderBy(username => username, StringComparer.OrdinalIgnoreCase).ToList();
var usernames = await workspaceQuery.GetUsernamesAsync();
state.KnownUsernames = usernames.OrderBy(username => username, StringComparer.OrdinalIgnoreCase).ToList();
}
catch (ApiRequestException ex)
{
m_State.KnownUsernames = [];
m_Feedback.SetStatus(ex.Message, true);
state.KnownUsernames = [];
feedback.SetStatus(ex.Message, true);
}
}
public async Task LogoutAsync()
{
if (m_State.IsMutating)
if (state.IsMutating)
return;
m_State.IsMutating = true;
state.IsMutating = true;
try
{
await m_ApiClient.RequestWithoutPayloadAsync("POST", "/api/auth/logout");
await apiClient.RequestWithoutPayloadAsync("POST", "/api/auth/logout");
}
catch (ApiRequestException)
{
}
finally
{
m_State.IsMutating = false;
state.IsMutating = false;
}
ClearAuthenticatedState();
await m_StopStateEventsAsync();
await m_OnLoggedOutAsync("Logged out.");
await stopStateEventsAsync();
await onLoggedOutAsync("Logged out.");
}
public async Task SwitchScreenAsync(string screen)
{
var targetScreen = NormalizeRequestedScreen(screen) ?? ScreenPlay;
if (string.Equals(targetScreen, ScreenAdmin, StringComparison.OrdinalIgnoreCase) && !m_State.IsCurrentUserAdmin)
if (string.Equals(targetScreen, ScreenAdmin, StringComparison.OrdinalIgnoreCase) && !state.IsCurrentUserAdmin)
targetScreen = ScreenPlay;
m_State.CurrentScreen = targetScreen;
m_State.IsScreenMenuOpen = false;
await PersistScreenPreferenceAsync(m_State.CurrentScreen);
await m_RequestRefreshAsync();
state.CurrentScreen = targetScreen;
state.IsScreenMenuOpen = false;
await PersistScreenPreferenceAsync(state.CurrentScreen);
await requestRefreshAsync();
if (m_State.User is not null)
if (state.User is not null)
{
await m_RefreshCampaignScopeAsync();
await m_SyncStateEventsAsync();
await refreshCampaignScopeAsync();
await syncStateEventsAsync();
}
if (m_State.IsAdminScreen)
if (state.IsAdminScreen)
{
await m_EnsureAdminUsersLoadedAsync();
await m_RequestRefreshAsync();
await ensureAdminUsersLoadedAsync();
await requestRefreshAsync();
}
}
public async Task OnRollVisibilityChangedAsync(string visibility)
{
m_State.RollVisibility = NormalizeRollVisibility(visibility);
await m_JS.InvokeVoidAsync("rpgRollerApi.setSessionValue", RollVisibilitySessionKey, m_State.RollVisibility);
state.RollVisibility = NormalizeRollVisibility(visibility);
await js.InvokeVoidAsync("rpgRollerApi.setSessionValue", RollVisibilitySessionKey, state.RollVisibility);
}
public void ClearAuthenticatedState()
{
m_State.User = null;
m_State.ActiveCharacterId = null;
m_State.SelectedCampaignId = null;
m_State.SelectedCampaign = null;
m_State.Campaigns = [];
m_State.CharacterCampaignOptions = [];
m_State.SelectedCharacterSkills = [];
m_State.SelectedCharacterSkillGroups = [];
m_State.CampaignLog = [];
m_State.CampaignLogCursor = null;
m_ResetCampaignLogDetailState();
m_State.SelectedCharacterId = null;
m_State.LastRoll = null;
m_State.KnownUsernames = [];
m_State.ShowCreateCharacterModal = false;
m_State.ShowEditCharacterModal = false;
m_State.CanEditCharacterOwner = false;
m_State.CreateCharacterInitialModel = new();
m_State.EditCharacterInitialModel = new();
m_State.CreateCharacterFormVersion = 0;
m_State.EditCharacterFormVersion = 0;
m_State.AdminUsers = [];
m_State.HasLoadedAdminUsers = false;
m_State.IsAdminDataLoading = false;
m_Feedback.ClearToasts();
state.User = null;
state.ActiveCharacterId = null;
state.SelectedCampaignId = null;
state.SelectedCampaign = null;
state.Campaigns = [];
state.CharacterCampaignOptions = [];
state.SelectedCharacterSkills = [];
state.SelectedCharacterSkillGroups = [];
state.CampaignLog = [];
state.CampaignLogCursor = null;
resetCampaignLogDetailState();
state.SelectedCharacterId = null;
state.LastRoll = null;
state.KnownUsernames = [];
state.ShowCreateCharacterModal = false;
state.ShowEditCharacterModal = false;
state.CanEditCharacterOwner = false;
state.CreateCharacterInitialModel = new();
state.EditCharacterInitialModel = new();
state.CreateCharacterFormVersion = 0;
state.EditCharacterFormVersion = 0;
state.AdminUsers = [];
state.HasLoadedAdminUsers = false;
state.IsAdminDataLoading = false;
feedback.ClearToasts();
}
private async Task CheckHealthAsync()
{
m_State.HasHealthIssue = false;
m_State.HealthIssueMessage = string.Empty;
state.HasHealthIssue = false;
state.HealthIssueMessage = string.Empty;
await Task.CompletedTask;
}
@@ -166,11 +148,11 @@ public sealed class WorkspaceSessionCoordinator
{
try
{
m_State.Rulesets = (await m_WorkspaceQuery.GetRulesetsAsync()).ToList();
state.Rulesets = (await workspaceQuery.GetRulesetsAsync()).ToList();
}
catch (ApiRequestException ex)
{
m_Feedback.SetStatus(ex.Message, true);
feedback.SetStatus(ex.Message, true);
}
}
@@ -180,21 +162,21 @@ public sealed class WorkspaceSessionCoordinator
if (me is null)
{
ClearAuthenticatedState();
await m_StopStateEventsAsync();
await stopStateEventsAsync();
return false;
}
m_State.User = me.User;
m_State.ActiveCharacterId = me.ActiveCharacterId;
state.User = me.User;
state.ActiveCharacterId = me.ActiveCharacterId;
await EnsureScreenAccessAsync();
await m_ReloadCampaignsAsync(preferredCampaignId ?? me.CurrentCampaignId);
await m_ReloadCharacterCampaignOptionsAsync();
await m_RefreshCampaignScopeAsync();
await m_SyncStateEventsAsync();
await reloadCampaignsAsync(preferredCampaignId ?? me.CurrentCampaignId);
await reloadCharacterCampaignOptionsAsync();
await refreshCampaignScopeAsync();
await syncStateEventsAsync();
if (m_State.IsAdminScreen)
await m_EnsureAdminUsersLoadedAsync();
if (state.IsAdminScreen)
await ensureAdminUsersLoadedAsync();
return true;
}
@@ -203,7 +185,7 @@ public sealed class WorkspaceSessionCoordinator
{
try
{
return await m_WorkspaceQuery.GetMeAsync();
return await workspaceQuery.GetMeAsync();
}
catch (ApiRequestException ex) when (ex.StatusCode == 401)
{
@@ -213,24 +195,24 @@ public sealed class WorkspaceSessionCoordinator
private async Task EnsureScreenAccessAsync()
{
if (m_State.IsCurrentUserAdmin)
if (state.IsCurrentUserAdmin)
return;
m_State.AdminUsers = [];
m_State.HasLoadedAdminUsers = false;
state.AdminUsers = [];
state.HasLoadedAdminUsers = false;
if (!m_State.IsAdminScreen)
if (!state.IsAdminScreen)
return;
m_State.CurrentScreen = ScreenPlay;
await PersistScreenPreferenceAsync(m_State.CurrentScreen);
state.CurrentScreen = ScreenPlay;
await PersistScreenPreferenceAsync(state.CurrentScreen);
}
private async Task PersistScreenPreferenceAsync(string screen)
{
try
{
await m_JS.InvokeVoidAsync("rpgRollerApi.setSessionValue", ScreenSessionKey, screen);
await js.InvokeVoidAsync("rpgRollerApi.setSessionValue", ScreenSessionKey, screen);
}
catch (JSDisconnectedException)
{
@@ -271,19 +253,4 @@ public sealed class WorkspaceSessionCoordinator
private const string CampaignSessionKey = "campaign";
private const string MobilePanelSessionKey = "play-panel";
private const string RollVisibilitySessionKey = "roll-visibility";
private readonly RpgRollerApiClient m_ApiClient;
private readonly Func<Task> m_EnsureAdminUsersLoadedAsync;
private readonly WorkspaceFeedbackService m_Feedback;
private readonly IJSRuntime m_JS;
private readonly Func<string?, Task> m_OnLoggedOutAsync;
private readonly Func<Task> m_RefreshCampaignScopeAsync;
private readonly Func<Guid?, Task> m_ReloadCampaignsAsync;
private readonly Func<Task> m_ReloadCharacterCampaignOptionsAsync;
private readonly Func<Task> m_RequestRefreshAsync;
private readonly Action m_ResetCampaignLogDetailState;
private readonly WorkspaceState m_State;
private readonly Func<Task> m_StopStateEventsAsync;
private readonly Func<Task> m_SyncStateEventsAsync;
private readonly WorkspaceQueryService m_WorkspaceQuery;
}