controls/job-historical-script-grid.ps1

function New-JobHistoricalScriptGrid {
    param($Identity, $Script)

    New-UDGrid -Id ("JobGrid"+$($Script.Id)+"Historical") -Title "Jobs" -Headers @("Id","Script","", "Started", "Duration", "Executed By", "Actions") -Properties @("Id","Script", "Status", "StartTime", "Duration", "ExecutedBy", "Actions") -Endpoint {

        if ($null -ne $Script)
        {
            $Jobs = Get-UAJob -Script $Script -Status "Historical" -OrderBy $SortColumn -OrderDirection "Descending"
        }
        else 
        {
            $Jobs = Get-UAJob -Status "Historical"
        }
        
        $Jobs | ForEach-Object {
            $Job = $_
            $Identity = Get-UAIdentity -Id $_.Identity.Id
            $Script = Get-UAScript -Id $_.Script.Id
            $JobStatus = $Job.Status
            $JobOutput = Get-UAJobOutput -JobId $_.Id #TODO - PERFORMANCE

            $Feedback = (Get-UAJobFeedback -JobId $Job.id)
                   
            $CancelJobButton = New-UDButton -Text "" -Icon ban -OnClick {
                 New-JobCancelModal -Job $Job
            }

            $ViewJobButton = New-UDButton -Text "" -Icon folder -OnClick {
                Invoke-UDRedirect -Url "/job/$($Job.Id)"
            }

            $ViewFeedBackButton = New-UDButton -Text "" -Icon question -OnClick {
                New-JobFeedbackModal -Job $Job
            } 

            $ScriptHelpButton = New-UDButton -Text "" -Icon code -OnClick {
                New-PowerShellHelpModal -Object $Job
            }

            $OutputButton = if($null -ne $JobOutput)
            {
                New-UDButton -Text "" -Icon envelope_open -OnClick {
                    New-UDJobOutputModal -Job $Job
                }
            }
            else {
                " "
            }


            $ActionControls =  New-UDElement -Tag div -Content{
                New-UDTooltip -Content{$ViewJobButton} -TooltipContent {"Open Job Page"}
                " "
                if(($JobStatus -eq "Running") -or ($JobStatus -eq "WaitingOnFeedback") -or ($JobStatus -eq "Queued"))
                {
                    New-UDTooltip -Content{$CancelJobButton} -TooltipContent {"Cancel the running Job"}
                    " "
                }
                else
                {
                    New-UDTooltip -Content{$OutputButton} -TooltipContent {"View Job Output"}
                    " "
                }

                if ($null -ne $Feedback)
                {
                     New-UDTooltip -Content{$ViewFeedBackButton} -TooltipContent {"View Pending Job Feedback"}
                     " "
                }

                New-UDTooltip -Content{$ScriptHelpButton} -TooltipContent {"Open PowerShell Job Help"}
            }
           
            if(($JobStatus -eq "Running") -or ($JobStatus -eq "WaitingOnFeedback") -or ($JobStatus -eq "Queued"))
            {
                $Duration = Format-UASecondsToReadableString -Seconds(((Get-Date) - $_.StartTime).TotalSeconds)
            }
            else {
                $Duration = Format-UASecondsToReadableString -Seconds(($_.EndTime - $_.StartTime).TotalSeconds)
            }
            
            [PSCustomObject]@{
                Id = New-UDLink -Text $Job.Id -Url "/job/$($Job.Id)"
                Script = New-UDLink -Text $Script.Name -Url "/script/$($Script.Id)"
                Status = New-UDJobIcon -Job $_ -NoSpan
                StartTime = $_.StartTime #todo don't show jan 0 0 0 0 when not started
                Duration = $Duration
                ExecutedBy = $Identity.Name
                Actions = $ActionControls
            }
        } | Out-UDGridData
    } -AutoRefresh -DefaultSortColumn "Id" -DefaultSortDescending -ServerSideProcessing
}