LANCommander/LANCommander.Server/UI/Pages/Games/Components/SavePathEditor.razor

148 lines
5.3 KiB
Text

@inject SavePathService SavePathService
@inject ArchiveService ArchiveService
@inject ModalService ModalService
@inject IMessageService MessageService
@inject ILogger<SavePathEditor> Logger
<Flex Direction="FlexDirection.Vertical" Gap="FlexGap.Large">
<Table TItem="SavePath" DataSource="@_savePaths" HidePagination="true" Responsive>
<PropertyColumn Property="p => p.Type">
<Select @bind-Value="context.Type" TItem="SavePathType" TItemValue="SavePathType" DataSource="Enum.GetValues<SavePathType>()">
<LabelTemplate Context="Value">@Value.GetDisplayName()</LabelTemplate>
<ItemTemplate Context="Value">@Value.GetDisplayName()</ItemTemplate>
</Select>
</PropertyColumn>
<PropertyColumn Property="p => p.Path">
@if (context.Type == SavePathType.Registry)
{
<InputRegistry @bind-Value="context.Path" />
}
else
{
<Flex Gap="FlexGap.Small" Align="FlexAlign.Center">
<FilePicker @bind-Value="context.Path" ArchiveId="@(_archive?.Id ?? Guid.Empty)" AllowDirectories="true" />
<Button Icon="@IconType.Outline.Code" Type="ButtonType.Text" OnClick="() => OpenVariablePicker(context, nameof(SavePath.Path))" Size="ButtonSize.Small" />
</Flex>
}
</PropertyColumn>
<PropertyColumn Property="p => p.WorkingDirectory" Title="Working Directory">
<Flex Gap="FlexGap.Small" Align="FlexAlign.Center">
<FilePicker @bind-Value="context.WorkingDirectory" Disabled="@(context.Type == SavePathType.Registry)" ArchiveId="@(_archive?.Id ?? Guid.Empty)" AllowDirectories Title="Select Working Directory" />
<Button Icon="@IconType.Outline.Code" Type="ButtonType.Text" OnClick="() => OpenVariablePicker(context, nameof(SavePath.WorkingDirectory))" Disabled="@(context.Type == SavePathType.Registry)" Size="ButtonSize.Small" />
</Flex>
</PropertyColumn>
<PropertyColumn Property="p => p.IsRegex" Title="Regex">
<Checkbox @bind-Checked="context.IsRegex" Disabled="@(context.Type == SavePathType.Registry)" />
</PropertyColumn>
<PropertyColumn Property="p => p.Platforms" Title="Platforms">
<RuntimePlatformSelect @bind-Value="context.Platforms" />
</PropertyColumn>
<ActionColumn>
<Flex Gap="FlexGap.Small" Justify="FlexJustify.End">
<Button OnClick="() => RemovePath(context)" Type="@ButtonType.Text" Danger Icon="@IconType.Outline.Close" />
</Flex>
</ActionColumn>
</Table>
<Flex Justify="FlexJustify.End">
<Button OnClick="AddPath" Type="@ButtonType.Primary">Add Path</Button>
</Flex>
</Flex>
@code {
[CascadingParameter(Name = "GameId")] public Guid GameId { get; set; }
Archive? _archive;
ICollection<SavePath> _savePaths = new List<SavePath>();
protected override async Task OnInitializedAsync()
{
_archive = await ArchiveService.GetLatestArchiveAsync(a => a.GameId == GameId);
await LoadPaths();
}
async Task LoadPaths()
=> _savePaths = await SavePathService.GetAsync(p => p.GameId == GameId);
void AddPath()
{
_savePaths.Add(new SavePath
{
GameId = GameId,
WorkingDirectory = "{InstallDir}"
});
}
async Task RemovePath(SavePath path)
{
await SavePathService.DeleteAsync(path);
await LoadPaths();
}
async Task OpenVariablePicker(SavePath savePath, string fieldName)
{
var currentValue = fieldName switch
{
nameof(SavePath.Path) => savePath.Path ?? "",
nameof(SavePath.WorkingDirectory) => savePath.WorkingDirectory ?? "",
_ => ""
};
var modalOptions = new ModalOptions
{
Title = $"Edit {fieldName}",
Maximizable = false,
Closable = true,
OkText = "Apply"
};
var pickerOptions = new VariablePickerOptions
{
Value = currentValue,
FieldName = fieldName
};
var modalRef = await ModalService.CreateModalAsync<VariablePickerDialog, VariablePickerOptions, string>(modalOptions, pickerOptions);
modalRef.OnOk = (result) =>
{
switch (fieldName)
{
case nameof(SavePath.Path):
savePath.Path = result;
break;
case nameof(SavePath.WorkingDirectory):
savePath.WorkingDirectory = result;
break;
}
StateHasChanged();
return Task.CompletedTask;
};
}
public async Task Save()
{
try
{
foreach (var savePath in _savePaths)
{
if (savePath.Id == Guid.Empty)
await SavePathService.AddAsync(savePath);
else
await SavePathService.UpdateAsync(savePath);
}
MessageService.SuccessAsync("Save paths updated!");
}
catch (Exception ex)
{
MessageService.ErrorAsync("Could not update save paths!");
Logger.LogError(ex, "Could not update save paths!");
}
await LoadPaths();
}
}