|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Filament\Resources; |
| 6 | + |
| 7 | +use App\Enums\PluginReportCategory; |
| 8 | +use App\Enums\PluginReportStatus; |
| 9 | +use App\Filament\Resources\PluginReportResource\Pages; |
| 10 | +use App\Models\PluginReport; |
| 11 | +use Filament\Actions; |
| 12 | +use Filament\Forms; |
| 13 | +use Filament\Infolists; |
| 14 | +use Filament\Notifications\Notification; |
| 15 | +use Filament\Resources\Resource; |
| 16 | +use Filament\Schemas; |
| 17 | +use Filament\Schemas\Schema; |
| 18 | +use Filament\Tables; |
| 19 | +use Filament\Tables\Table; |
| 20 | +use Illuminate\Support\Facades\Auth; |
| 21 | + |
| 22 | +final class PluginReportResource extends Resource |
| 23 | +{ |
| 24 | + protected static ?string $model = PluginReport::class; |
| 25 | + |
| 26 | + protected static \BackedEnum|string|null $navigationIcon = 'heroicon-o-flag'; |
| 27 | + |
| 28 | + protected static ?string $navigationLabel = 'Plugin Reports'; |
| 29 | + |
| 30 | + protected static \UnitEnum|string|null $navigationGroup = 'Products'; |
| 31 | + |
| 32 | + protected static ?int $navigationSort = 5; |
| 33 | + |
| 34 | + protected static ?string $modelLabel = 'Plugin Report'; |
| 35 | + |
| 36 | + protected static ?string $pluralModelLabel = 'Plugin Reports'; |
| 37 | + |
| 38 | + protected static ?string $slug = 'plugin-reports'; |
| 39 | + |
| 40 | + public static function canCreate(): bool |
| 41 | + { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + public static function getNavigationBadge(): ?string |
| 46 | + { |
| 47 | + $count = PluginReport::query()->open()->count(); |
| 48 | + |
| 49 | + return $count > 0 ? (string) $count : null; |
| 50 | + } |
| 51 | + |
| 52 | + public static function getNavigationBadgeColor(): ?string |
| 53 | + { |
| 54 | + return 'danger'; |
| 55 | + } |
| 56 | + |
| 57 | + public static function form(Schema $schema): Schema |
| 58 | + { |
| 59 | + return $schema->schema([]); |
| 60 | + } |
| 61 | + |
| 62 | + private static function resolutionActions(): array |
| 63 | + { |
| 64 | + return [ |
| 65 | + Actions\Action::make('resolve') |
| 66 | + ->label('Mark Resolved') |
| 67 | + ->icon('heroicon-o-check-circle') |
| 68 | + ->color('success') |
| 69 | + ->visible(fn (PluginReport $record): bool => $record->isOpen()) |
| 70 | + ->form([ |
| 71 | + Forms\Components\Textarea::make('resolution_note') |
| 72 | + ->label('Resolution Note') |
| 73 | + ->helperText('Optional internal note — not visible to the reporter or the plugin author.') |
| 74 | + ->rows(3), |
| 75 | + ]) |
| 76 | + ->action(function (PluginReport $record, array $data): void { |
| 77 | + $record->resolve(Auth::user(), $data['resolution_note'] ?: null); |
| 78 | + |
| 79 | + Notification::make() |
| 80 | + ->title('Report marked resolved') |
| 81 | + ->success() |
| 82 | + ->send(); |
| 83 | + }), |
| 84 | + |
| 85 | + Actions\Action::make('dismiss') |
| 86 | + ->label('Dismiss') |
| 87 | + ->icon('heroicon-o-x-circle') |
| 88 | + ->color('gray') |
| 89 | + ->visible(fn (PluginReport $record): bool => $record->isOpen()) |
| 90 | + ->requiresConfirmation() |
| 91 | + ->modalDescription('Dismiss this report as not actionable? It will be removed from the open reports count.') |
| 92 | + ->form([ |
| 93 | + Forms\Components\Textarea::make('resolution_note') |
| 94 | + ->label('Resolution Note') |
| 95 | + ->helperText('Optional internal note — not visible to the reporter or the plugin author.') |
| 96 | + ->rows(3), |
| 97 | + ]) |
| 98 | + ->action(function (PluginReport $record, array $data): void { |
| 99 | + $record->dismiss(Auth::user(), $data['resolution_note'] ?: null); |
| 100 | + |
| 101 | + Notification::make() |
| 102 | + ->title('Report dismissed') |
| 103 | + ->success() |
| 104 | + ->send(); |
| 105 | + }), |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + public static function infolist(Schema $schema): Schema |
| 110 | + { |
| 111 | + return $schema |
| 112 | + ->inlineLabel() |
| 113 | + ->columns(1) |
| 114 | + ->schema([ |
| 115 | + Schemas\Components\Section::make('Report Details') |
| 116 | + ->inlineLabel() |
| 117 | + ->columns(1) |
| 118 | + ->schema([ |
| 119 | + Infolists\Components\TextEntry::make('plugin.name') |
| 120 | + ->label('Plugin'), |
| 121 | + Infolists\Components\TextEntry::make('user.email') |
| 122 | + ->label('Reported By') |
| 123 | + ->copyable(), |
| 124 | + Infolists\Components\TextEntry::make('category') |
| 125 | + ->badge() |
| 126 | + ->formatStateUsing(fn (PluginReportCategory $state): string => $state->label()), |
| 127 | + Infolists\Components\TextEntry::make('status') |
| 128 | + ->badge() |
| 129 | + ->color(fn (PluginReportStatus $state): string => $state->color()) |
| 130 | + ->formatStateUsing(fn (PluginReportStatus $state): string => $state->label()), |
| 131 | + Infolists\Components\TextEntry::make('message') |
| 132 | + ->label('Report Message') |
| 133 | + ->columnSpanFull(), |
| 134 | + Infolists\Components\TextEntry::make('created_at') |
| 135 | + ->label('Filed At') |
| 136 | + ->dateTime(), |
| 137 | + ]), |
| 138 | + |
| 139 | + Schemas\Components\Section::make('Resolution') |
| 140 | + ->inlineLabel() |
| 141 | + ->columns(1) |
| 142 | + ->schema([ |
| 143 | + Infolists\Components\TextEntry::make('resolvedBy.email') |
| 144 | + ->label('Resolved By') |
| 145 | + ->placeholder('—'), |
| 146 | + Infolists\Components\TextEntry::make('resolved_at') |
| 147 | + ->label('Resolved At') |
| 148 | + ->dateTime() |
| 149 | + ->placeholder('—'), |
| 150 | + Infolists\Components\TextEntry::make('resolution_note') |
| 151 | + ->label('Note') |
| 152 | + ->placeholder('—') |
| 153 | + ->columnSpanFull(), |
| 154 | + ]) |
| 155 | + ->visible(fn (PluginReport $record): bool => ! $record->isOpen()), |
| 156 | + ]); |
| 157 | + } |
| 158 | + |
| 159 | + public static function table(Table $table): Table |
| 160 | + { |
| 161 | + return $table |
| 162 | + ->columns([ |
| 163 | + Tables\Columns\TextColumn::make('plugin.name') |
| 164 | + ->label('Plugin') |
| 165 | + ->searchable() |
| 166 | + ->sortable(), |
| 167 | + |
| 168 | + Tables\Columns\TextColumn::make('user.email') |
| 169 | + ->label('Reported By') |
| 170 | + ->searchable() |
| 171 | + ->sortable(), |
| 172 | + |
| 173 | + Tables\Columns\TextColumn::make('category') |
| 174 | + ->badge() |
| 175 | + ->formatStateUsing(fn (PluginReportCategory $state): string => $state->label()) |
| 176 | + ->sortable(), |
| 177 | + |
| 178 | + Tables\Columns\TextColumn::make('status') |
| 179 | + ->badge() |
| 180 | + ->color(fn (PluginReportStatus $state): string => $state->color()) |
| 181 | + ->formatStateUsing(fn (PluginReportStatus $state): string => $state->label()) |
| 182 | + ->sortable(), |
| 183 | + |
| 184 | + Tables\Columns\TextColumn::make('message') |
| 185 | + ->limit(80) |
| 186 | + ->wrap() |
| 187 | + ->tooltip(fn (PluginReport $record): string => $record->message), |
| 188 | + |
| 189 | + Tables\Columns\TextColumn::make('created_at') |
| 190 | + ->label('Filed') |
| 191 | + ->dateTime() |
| 192 | + ->sortable(), |
| 193 | + ]) |
| 194 | + ->filters([ |
| 195 | + Tables\Filters\SelectFilter::make('status') |
| 196 | + ->options(PluginReportStatus::class), |
| 197 | + Tables\Filters\SelectFilter::make('category') |
| 198 | + ->options(PluginReportCategory::class), |
| 199 | + ]) |
| 200 | + ->actions([ |
| 201 | + Actions\ViewAction::make(), |
| 202 | + ...self::resolutionActions(), |
| 203 | + ]) |
| 204 | + ->bulkActions([]) |
| 205 | + ->defaultSort('created_at', 'desc') |
| 206 | + ->recordUrl( |
| 207 | + fn ($record) => self::getUrl('view', ['record' => $record]) |
| 208 | + ); |
| 209 | + } |
| 210 | + |
| 211 | + public static function getPages(): array |
| 212 | + { |
| 213 | + return [ |
| 214 | + 'index' => Pages\ListPluginReports::route('/'), |
| 215 | + 'view' => Pages\ViewPluginReport::route('/{record}'), |
| 216 | + ]; |
| 217 | + } |
| 218 | +} |
0 commit comments