Skip to content

Commit

Permalink
Added context menu action to exclude dates
Browse files Browse the repository at this point in the history
  • Loading branch information
jschick04 committed Dec 5, 2024
1 parent 3e892b8 commit a24bc20
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 45 deletions.
6 changes: 2 additions & 4 deletions src/EventLogExpert.UI/Models/FilterDateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ namespace EventLogExpert.UI.Models;

public sealed record FilterDateModel
{
public DateTime After { get; set; }
public DateTime? After { get; set; }

public DateTime Before { get; set; }

public TimeZoneInfo TimeZoneInfo { get; set; } = null!;
public DateTime? Before { get; set; }

public bool IsEnabled { get; set; } = true;
}
2 changes: 2 additions & 0 deletions src/EventLogExpert.UI/Store/FilterPane/FilterPaneAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public sealed record SetFilter(FilterModel FilterModel);

public sealed record SetFilterDateRange(FilterDateModel? FilterDateModel);

public sealed record SetFilterDateRangeSuccess(FilterDateModel? FilterDateModel);

public sealed record ToggleFilterEditing(FilterId Id);

public sealed record ToggleFilterEnabled(FilterId Id);
Expand Down
58 changes: 55 additions & 3 deletions src/EventLogExpert.UI/Store/FilterPane/FilterPaneEffects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

namespace EventLogExpert.UI.Store.FilterPane;

public sealed class FilterPaneEffects(IState<FilterPaneState> filterPaneState)
public sealed class FilterPaneEffects(
IState<EventLogState> eventLogState,
IState<FilterPaneState> filterPaneState)
{
[EffectMethod]
public async Task HandleAddFilter(FilterPaneAction.AddFilter action, IDispatcher dispatcher)
Expand Down Expand Up @@ -74,8 +76,58 @@ public async Task HandleSetFilter(FilterPaneAction.SetFilter action, IDispatcher
}
}

[EffectMethod(typeof(FilterPaneAction.SetFilterDateRange))]
public async Task HandleSetFilterDateRange(IDispatcher dispatcher) =>
[EffectMethod]
public Task HandleSetFilterDateRange(FilterPaneAction.SetFilterDateRange action, IDispatcher dispatcher)
{
if (action.FilterDateModel is null)
{
dispatcher.Dispatch(new FilterPaneAction.SetFilterDateRangeSuccess(action.FilterDateModel));

return Task.CompletedTask;
}

DateTime? updatedAfter = action.FilterDateModel?.After ?? filterPaneState.Value.FilteredDateRange?.After;
DateTime? updatedBefore = action.FilterDateModel?.Before ?? filterPaneState.Value.FilteredDateRange?.Before;

long ticksPerHour = TimeSpan.FromHours(1).Ticks;

if (updatedAfter is null)
{
long ticks =
(eventLogState.Value.ActiveLogs.Values.Select(log => log.Events.LastOrDefault()?.TimeCreated)
.Order()
.LastOrDefault() ??
DateTime.UtcNow)
.Ticks;

updatedAfter = new DateTime(ticks / ticksPerHour * ticksPerHour, DateTimeKind.Utc);
}

if (updatedBefore is null)
{
long ticks =
(eventLogState.Value.ActiveLogs.Values.Select(log => log.Events.FirstOrDefault()?.TimeCreated)
.Order()
.FirstOrDefault() ??
DateTime.UtcNow)
.Ticks;

updatedBefore = new DateTime((ticks + ticksPerHour - 1) / ticksPerHour * ticksPerHour, DateTimeKind.Utc);
}

dispatcher.Dispatch(
new FilterPaneAction.SetFilterDateRangeSuccess(
new FilterDateModel
{
After = updatedAfter,
Before = updatedBefore
}));

return Task.CompletedTask;
}

[EffectMethod(typeof(FilterPaneAction.SetFilterDateRangeSuccess))]
public async Task HandleSetFilterDateRangeSuccess(IDispatcher dispatcher) =>
await UpdateEventTableFiltersAsync(filterPaneState.Value, dispatcher);

[EffectMethod(typeof(FilterPaneAction.ToggleFilterDate))]
Expand Down
5 changes: 3 additions & 2 deletions src/EventLogExpert.UI/Store/FilterPane/FilterPaneReducers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ .. state.Filters
};

[ReducerMethod]
public static FilterPaneState
ReduceSetFilterDateRange(FilterPaneState state, FilterPaneAction.SetFilterDateRange action) =>
public static FilterPaneState ReduceSetFilterDateRangeSuccess(
FilterPaneState state,
FilterPaneAction.SetFilterDateRangeSuccess action) =>
state with { FilteredDateRange = action.FilterDateModel };

[ReducerMethod(typeof(FilterPaneAction.ToggleFilterDate))]
Expand Down
85 changes: 50 additions & 35 deletions src/EventLogExpert/Components/FilterPane.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ namespace EventLogExpert.Components;

public sealed partial class FilterPane
{
private readonly FilterDateModel _model = new() { TimeZoneInfo = TimeZoneInfo.Utc };
private readonly FilterDateModel _model = new();

private bool _canEditDate;
private TimeZoneInfo _currentTimeZone = TimeZoneInfo.Utc;
private bool _isFilterListVisible;

[Inject] private IDispatcher Dispatcher { get; init; } = null!;
Expand All @@ -34,36 +35,40 @@ public sealed partial class FilterPane

[Inject] private IState<SettingsState> SettingsState { get; init; } = null!;

[Inject] private IStateSelection<SettingsState, string> TimeZoneState { get; init; } = null!;
[Inject] private IStateSelection<SettingsState, TimeZoneInfo> TimeZoneState { get; init; } = null!;

protected override void OnInitialized()
{
SubscribeToAction<FilterPaneAction.ClearAllFilters>(action => { _canEditDate = false; });
SubscribeToAction<FilterPaneAction.SetFilterDateRangeSuccess>(action => { UpdateFilterDate(action.FilterDateModel); });

TimeZoneState.Select(x => x.Config.TimeZoneId);
TimeZoneState.SelectedValueChanged += (sender, args) => { UpdateFilterDateModel(); };
TimeZoneState.Select(x => x.Config.TimeZoneInfo, selectedValueChanged: UpdateFilterDateTimeZone);

base.OnInitialized();
}

private void AddAdvancedFilter()
{
Dispatcher.Dispatch(new FilterPaneAction.AddFilter(new FilterModel
{
FilterType = FilterType.Advanced,
IsEditing = true
}));
Dispatcher.Dispatch(
new FilterPaneAction.AddFilter(
new FilterModel
{
FilterType = FilterType.Advanced,
IsEditing = true
}));

_isFilterListVisible = true;
}

private void AddBasicFilter()
{
Dispatcher.Dispatch(new FilterPaneAction.AddFilter(new FilterModel
{
FilterType = FilterType.Basic,
IsEditing = true
}));
Dispatcher.Dispatch(
new FilterPaneAction.AddFilter(
new FilterModel
{
FilterType = FilterType.Basic,
IsEditing = true
}));

_isFilterListVisible = true;
}
Expand All @@ -72,7 +77,7 @@ private void AddBasicFilter()

private void AddDateFilter()
{
_model.TimeZoneInfo = SettingsState.Value.Config.TimeZoneInfo;
_currentTimeZone = SettingsState.Value.Config.TimeZoneInfo;

long ticksPerHour = TimeSpan.FromHours(1).Ticks;

Expand All @@ -92,37 +97,39 @@ private void AddDateFilter()

// Round down to the nearest hour for the earliest event
_model.After = new DateTime(oldestEventTicks / ticksPerHour * ticksPerHour, DateTimeKind.Utc)
.ConvertTimeZone(SettingsState.Value.Config.TimeZoneInfo);
.ConvertTimeZone(_currentTimeZone);

// Round up to the nearest hour for the latest event
_model.Before = new DateTime(((mostRecentEventTicks + ticksPerHour - 1) / ticksPerHour) * ticksPerHour, DateTimeKind.Utc)
.ConvertTimeZone(SettingsState.Value.Config.TimeZoneInfo);
.ConvertTimeZone(_currentTimeZone);

_isFilterListVisible = true;
_canEditDate = true;
}

private void AddExclusion()
{
Dispatcher.Dispatch(new FilterPaneAction.AddFilter(new FilterModel
{
FilterType = FilterType.Basic,
IsEditing = true,
IsExcluded = true
}));
Dispatcher.Dispatch(
new FilterPaneAction.AddFilter(
new FilterModel
{
FilterType = FilterType.Basic,
IsEditing = true,
IsExcluded = true
}));

_isFilterListVisible = true;
}

private void ApplyDateFilter()
{
FilterDateModel model = new()
{
After = _model.After.ConvertTimeZoneToUtc(SettingsState.Value.Config.TimeZoneInfo),
Before = _model.Before.ConvertTimeZoneToUtc(SettingsState.Value.Config.TimeZoneInfo)
};

Dispatcher.Dispatch(new FilterPaneAction.SetFilterDateRange(model));
Dispatcher.Dispatch(
new FilterPaneAction.SetFilterDateRange(
new FilterDateModel
{
After = _model.After?.ConvertTimeZoneToUtc(_currentTimeZone),
Before = _model.Before?.ConvertTimeZoneToUtc(_currentTimeZone)
}));

_canEditDate = false;
}
Expand All @@ -149,12 +156,20 @@ private void RemoveDateFilter()

private void ToggleMenu() => _isFilterListVisible = !_isFilterListVisible;

private void UpdateFilterDateModel()
private void UpdateFilterDate(FilterDateModel? updatedDate)
{
var temp = _model.TimeZoneInfo;
_model.TimeZoneInfo = SettingsState.Value.Config.TimeZoneInfo;
_model.Before = updatedDate?.Before?.ConvertTimeZone(_currentTimeZone);
_model.After = updatedDate?.After?.ConvertTimeZone(_currentTimeZone);
}

private void UpdateFilterDateTimeZone(TimeZoneInfo timeZoneInfo)
{
_model.Before = _model.Before is not null ?
TimeZoneInfo.ConvertTime(_model.Before.Value, _currentTimeZone, timeZoneInfo) : null;

_model.After = _model.After is not null ?
TimeZoneInfo.ConvertTime(_model.After.Value, _currentTimeZone, timeZoneInfo) : null;

_model.Before = TimeZoneInfo.ConvertTime(_model.Before, temp, _model.TimeZoneInfo);
_model.After = TimeZoneInfo.ConvertTime(_model.After, temp, _model.TimeZoneInfo);
_currentTimeZone = timeZoneInfo;
}
}
5 changes: 5 additions & 0 deletions src/EventLogExpert/Shared/Components/ContextMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

<hr />

<div @onclick="ExcludeBefore">Exclude Events Before</div>
<div @onclick="ExcludeAfter">Exclude Events After</div>

<hr />

<div class="sub-menu">
<div>Include <i class="bi bi-caret-right"></i></div>
<ul>
Expand Down
10 changes: 10 additions & 0 deletions src/EventLogExpert/Shared/Components/ContextMenu.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ protected override void OnInitialized()

private void CopySelected(CopyType? copyType) => ClipboardService.CopySelectedEvent(copyType);

private void ExcludeAfter() =>
Dispatcher.Dispatch(
new FilterPaneAction.SetFilterDateRange(
new FilterDateModel { After = SelectedEventState.Value.FirstOrDefault()?.TimeCreated }));

private void ExcludeBefore() =>
Dispatcher.Dispatch(
new FilterPaneAction.SetFilterDateRange(
new FilterDateModel { Before = SelectedEventState.Value.FirstOrDefault()?.TimeCreated }));

private void FilterEvent(FilterCategory filterType, bool shouldExclude = false)
{
if (SelectedEventState.Value.IsEmpty) { return; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed partial class FilterGroupSection
{
private bool _menuState = true;

[Parameter] public FilterGroupData Data { get; set; }
[Parameter] public required FilterGroupData Data { get; set; }

[Parameter] public required string Name { get; set; }

Expand Down

0 comments on commit a24bc20

Please sign in to comment.