|
| 1 | +@using MaintenanceLog.Common.Contracts |
| 2 | +@using MaintenanceLog.Data.Entities |
| 3 | + |
| 4 | +@inject ISmartTaskDefinitionStepService SmartTaskDefinitionStepService |
| 5 | + |
| 6 | +@if (isLoading) |
| 7 | +{ |
| 8 | + <div class="spinner-border text-primary" role="status"> |
| 9 | + <span class="visually-hidden">Loading...</span> |
| 10 | + </div> |
| 11 | +} |
| 12 | +else |
| 13 | +{ |
| 14 | + <button type="button" class="btn btn-primary" @onclick="@OnSmartAssist"> |
| 15 | + ✨ Recommend Steps |
| 16 | + </button> |
| 17 | +} |
| 18 | + |
| 19 | +@if (aiResultSteps is not null) |
| 20 | +{ |
| 21 | + <div class="modal fade show" style="display: block;" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> |
| 22 | + <div class="modal-dialog modal-lg"> |
| 23 | + <div class="modal-content"> |
| 24 | + <div class="modal-header"> |
| 25 | + <h5 class="modal-title" id="exampleModalLabel">Smart Assist</h5> |
| 26 | + <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" @onclick="@Cancel"></button> |
| 27 | + </div> |
| 28 | + <div class="modal-body p-0"> |
| 29 | + <span> |
| 30 | + Add the following steps to your task: |
| 31 | + </span> |
| 32 | + <table class="table table-striped"> |
| 33 | + <thead> |
| 34 | + <tr> |
| 35 | + <th scope="col">Name</th> |
| 36 | + <th scope="col">Is Optional</th> |
| 37 | + <th scope="col">Is Selected</th> |
| 38 | + </tr> |
| 39 | + </thead> |
| 40 | + <tbody> |
| 41 | + @foreach (var step in aiResultSteps) |
| 42 | + { |
| 43 | + <tr> |
| 44 | + <td>@step.Name</td> |
| 45 | + <td> |
| 46 | + <div class="form-check form-switch"> |
| 47 | + @* <InputCheckbox class="form-check-input" Value="@step.IsOptional" /> shouldn't be nullable *@ |
| 48 | + </div> |
| 49 | + </td> |
| 50 | + <td> |
| 51 | + <div class="form-check form-switch"> |
| 52 | + <InputCheckbox class="form-check-input" @bind-Value="@step.IsSelected" /> |
| 53 | + </div> |
| 54 | + </td> |
| 55 | + </tr> |
| 56 | + } |
| 57 | + </tbody> |
| 58 | + </table> |
| 59 | + </div> |
| 60 | + <div class="modal-footer"> |
| 61 | + <button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @onclick="@Cancel">Close</button> |
| 62 | + <button type="button" class="btn btn-primary" @onclick="@SaveSteps" disabled="@(aiResultSteps.Any(s => s.IsSelected) == false)">Add steps</button> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | +} |
| 68 | + |
| 69 | +@code |
| 70 | +{ |
| 71 | + [Parameter] |
| 72 | + public TaskDefinition? TaskDefinition { get; set; } |
| 73 | + [Parameter] |
| 74 | + public EventCallback<TaskDefinition> DataChangedAsync { get; set; } |
| 75 | + |
| 76 | + private List<SuggestedTaskDefinitionStep>? aiResultSteps; |
| 77 | + private bool isLoading = false; |
| 78 | + |
| 79 | + protected override async Task OnParametersSetAsync() |
| 80 | + { |
| 81 | + await base.OnParametersSetAsync(); |
| 82 | + if (TaskDefinition is null) |
| 83 | + { |
| 84 | + throw new ArgumentNullException(nameof(TaskDefinition)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + protected override async Task OnInitializedAsync() |
| 89 | + { |
| 90 | + await base.OnInitializedAsync(); |
| 91 | + } |
| 92 | + |
| 93 | + private async Task OnSmartAssist() |
| 94 | + { |
| 95 | + isLoading = true; |
| 96 | + |
| 97 | + string taskDescriptor = string.Empty; |
| 98 | + if (!string.IsNullOrWhiteSpace(TaskDefinition!.Area?.Name)) |
| 99 | + { |
| 100 | + taskDescriptor += $"in {TaskDefinition.Area.Name}"; |
| 101 | + } |
| 102 | + if (!string.IsNullOrWhiteSpace(TaskDefinition!.Asset?.Name)) |
| 103 | + { |
| 104 | + taskDescriptor += $" for {TaskDefinition.Asset.Name}"; |
| 105 | + } |
| 106 | + |
| 107 | + var steps = await SmartTaskDefinitionStepService.SuggestStepsForTaskDefinition(TaskDefinition!.Name, taskDescriptor, TaskDefinition!.TaskDefinitionSteps?.Select(s => s.Name)); |
| 108 | + |
| 109 | + // show modal with steps that user can check to include (default check them all) |
| 110 | + // then save the steps to the task definition |
| 111 | + // if user cancels, do nothing |
| 112 | + // if user saves, update the task definition with the new steps |
| 113 | + // if user modifies the steps, update the task definition with the new steps |
| 114 | +
|
| 115 | + var suggestedSteps = steps?.Select(s => new SuggestedTaskDefinitionStep(s) { Name = s, IsSelected = false })?.ToList(); |
| 116 | + |
| 117 | + isLoading = false; |
| 118 | + |
| 119 | + if (suggestedSteps is null) |
| 120 | + { |
| 121 | + // do something about this? |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + // populating aiResultSteps will show the modal |
| 126 | + aiResultSteps = suggestedSteps; |
| 127 | + |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + private void Cancel() |
| 132 | + { |
| 133 | + aiResultSteps = null; |
| 134 | + } |
| 135 | + |
| 136 | + private async Task SaveSteps() |
| 137 | + { |
| 138 | + if (aiResultSteps is null) |
| 139 | + { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + var selectedSteps = aiResultSteps.Where(s => s.IsSelected).ToList(); |
| 144 | + TaskDefinition!.TaskDefinitionSteps ??= new List<TaskDefinitionStep>(); |
| 145 | + |
| 146 | + foreach (var step in selectedSteps) |
| 147 | + { |
| 148 | + TaskDefinition.TaskDefinitionSteps.Add(new TaskDefinitionStep |
| 149 | + { |
| 150 | + Name = step.Name, |
| 151 | + IsOptional = step.IsOptional |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + aiResultSteps = null; |
| 156 | + await DataChangedAsync.InvokeAsync(TaskDefinition); |
| 157 | + } |
| 158 | + |
| 159 | + // AI response will populate into this object |
| 160 | + // and allow user to modify IsSelected to determine what is |
| 161 | + // added to the TaskDefinition |
| 162 | + class SuggestedTaskDefinitionStep : TaskDefinitionStep |
| 163 | + { |
| 164 | + public SuggestedTaskDefinitionStep(string taskDefinitionStepName) |
| 165 | + { |
| 166 | + this.Name = taskDefinitionStepName; |
| 167 | + this.IsSelected = true; |
| 168 | + } |
| 169 | + public bool IsSelected { get; set; } = false; |
| 170 | + } |
| 171 | +} |
0 commit comments