@php $diff = $getState() ?? ['summary' => [], 'added' => [], 'removed' => [], 'changed' => []]; $summary = $diff['summary'] ?? []; $policyType = $diff['policy_type'] ?? null; $groupByBlock = static function (array $items): array { $groups = []; foreach ($items as $path => $value) { if (! is_string($path) || $path === '') { continue; } $parts = explode(' > ', $path, 2); if (count($parts) === 2) { [$group, $label] = $parts; } else { $group = 'Other'; $label = $path; } $groups[$group][$label] = $value; } ksort($groups); return $groups; }; $stringify = static function (mixed $value): string { if ($value === null) { return '—'; } if (is_bool($value)) { return $value ? 'Enabled' : 'Disabled'; } if (is_scalar($value)) { return (string) $value; } return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ?: ''; }; $isExpandable = static function (mixed $value): bool { if (is_array($value)) { return true; } return is_string($value) && strlen($value) > 160; }; $isScriptKey = static function (mixed $name): bool { return in_array((string) $name, ['scriptContent', 'detectionScriptContent', 'remediationScriptContent'], true); }; $canHighlightScripts = static function (?string $policyType): bool { return (bool) config('tenantpilot.display.show_script_content', false) && in_array($policyType, ['deviceManagementScript', 'deviceShellScript', 'deviceHealthScript'], true); }; $selectGrammar = static function (?string $policyType, string $code): string { if ($policyType === 'deviceShellScript') { $firstLine = strtok($code, "\n") ?: ''; $shebang = trim($firstLine); if (str_starts_with($shebang, '#!')) { if (str_contains($shebang, 'zsh')) { return 'zsh'; } if (str_contains($shebang, 'bash')) { return 'bash'; } return 'sh'; } return 'sh'; } return 'powershell'; }; $highlight = static function (?string $policyType, string $code, string $fallbackClass = '') use ($selectGrammar): ?string { if (! class_exists(\Torchlight\Engine\Engine::class)) { return null; } try { return (new \Torchlight\Engine\Engine())->codeToHtml( code: $code, grammar: $selectGrammar($policyType, $code), theme: [ 'light' => 'github-light', 'dark' => 'github-dark', ], withGutter: false, withWrapper: true, ); } catch (\Throwable $e) { return null; } }; @endphp
{{ (int) ($summary['added'] ?? 0) }} added {{ (int) ($summary['removed'] ?? 0) }} removed {{ (int) ($summary['changed'] ?? 0) }} changed
@foreach (['changed' => ['label' => 'Changed', 'collapsed' => false], 'added' => ['label' => 'Added', 'collapsed' => true], 'removed' => ['label' => 'Removed', 'collapsed' => true]] as $key => $meta) @php $items = $diff[$key] ?? []; $groups = $groupByBlock(is_array($items) ? $items : []); @endphp @if ($groups !== [])
@foreach ($groups as $group => $groupItems)
{{ $group }}
{{ count($groupItems) }}
@foreach ($groupItems as $name => $value)
@if ($key === 'changed' && is_array($value) && array_key_exists('from', $value) && array_key_exists('to', $value)) @php $from = $value['from']; $to = $value['to']; $fromText = $stringify($from); $toText = $stringify($to); $isScriptContent = $canHighlightScripts($policyType) && $isScriptKey($name); $fromHighlight = $isScriptContent ? $highlight($policyType, (string) $fromText) : null; $toHighlight = $isScriptContent ? $highlight($policyType, (string) $toText) : null; @endphp
{{ (string) $name }}
From @if ($isExpandable($from))
View @if (is_string($fromHighlight) && $fromHighlight !== '')
{!! $fromHighlight !!}
@else
{{ $fromText }}
@endif
@else
{{ $fromText }}
@endif
To @if ($isExpandable($to))
View @if (is_string($toHighlight) && $toHighlight !== '')
{!! $toHighlight !!}
@else
{{ $toText }}
@endif
@else
{{ $toText }}
@endif
@else @php $text = $stringify($value); @endphp
{{ (string) $name }}
@if ($isExpandable($value))
View @php $isScriptContent = $canHighlightScripts($policyType) && $isScriptKey($name); $highlighted = $isScriptContent ? $highlight($policyType, (string) $text) : null; @endphp @if (is_string($highlighted) && $highlighted !== '')
{!! $highlighted !!}
@else
{{ $text }}
@endif
@else
{{ $text }}
@endif
@endif
@endforeach
@endforeach
@endif @endforeach