controls/job-historical-grid.ps1

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

    New-UDGrid -Id "JobHistoricalGrid" -Title "Historical Jobs" -Headers @("Id", "Script", "Status", "Started", "Duration", "Executed By","Actions") -Properties @("Id", "Script", "Status", "StartTime", "Duration", "ExecutedBy","Actions") -Endpoint {
        if ($null -ne $Identity)
        {
            $Jobs = Get-UAJob -Identity $Identity 
        }
        elseif ($null -ne $Script)
        {
            $Jobs = Get-UAJob -Script $Script
        }
        else 
        {
            $Jobs = Get-UAJob
        }
        
        $Jobs | ForEach-Object {
            $Job = $_
            $Identity = Get-UAIdentity -Id $_.Identity.Id
            $Script = Get-UAScript -Id $_.Script.Id
            $JobStatus = $_.Status
            $JobOutput = Get-UAJobOutput -JobId $_.Id
            
            if(($JobStatus -ne "Running") -and ($JobStatus -ne "WaitingOnFeedback") -and ($JobStatus -ne "Queued"))
            {
                $OutputButton = if($null -ne $JobOutput)
                {
                    New-UDButton -Text "" -Icon envelope_open -OnClick {
                        New-UDJobOutputModal -Job $Job
                    }
                }
                else {
                    "N/A"
                }
                
                $ViewJobButton = New-UDButton -Text "" -Icon folder -OnClick {
                    Invoke-UDRedirect -Url "/job/$($Job.Id)"
                }



                $ActionControls =  New-UDElement -Tag div -Content{
                    New-UDTooltip -Content{$ViewJobButton} -TooltipContent {"Open Job Page"}
                    " "
                    New-UDTooltip -Content{$OutputButton} -TooltipContent {"View Job Output"}
                }


                [PSCustomObject]@{
                    Id = New-UDLink -Text $_.Id -Url "/job/$($_.Id)"
                    Script = New-UDLink -Text $Script.Name -Url "/script/$($Script.Id)"
                    Status = New-UDJobIcon -Job $_ -NoSpan
                    StartTime = $_.StartTime
                    Duration = Format-UASecondsToReadableString -Seconds(($_.EndTime - $_.StartTime).TotalSeconds)
                    ExecutedBy = $Identity.Name
                    Actions = $ActionControls
                }
            }
            
        } | Out-UDGridData
    } -AutoRefresh -DefaultSortColumn "Id" -DefaultSortDescending
}