Skip to content

Commit 3516cb0

Browse files
committed
Update content diff for propertyData
1 parent 7fe459a commit 3516cb0

1 file changed

Lines changed: 61 additions & 26 deletions

File tree

src/Livewire/ContentVersionHistory.php

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class ContentVersionHistory extends RelationManager implements HasActions, HasFo
3636
protected $queryString = [
3737
'page' => ['except' => 1, 'as' => self::PAGE_NAME],
3838
];
39+
40+
public function getTableRecordTitle(Model $record): ?string
41+
{
42+
return '#' . $record->getKey();
43+
}
3944

4045
public function table(Table $table): Table
4146
{
@@ -52,6 +57,10 @@ public function table(Table $table): Table
5257
Stack::make([
5358
Split::make([
5459
Stack::make([
60+
TextColumn::make('id')
61+
->label(__('inspirecms::inspirecms.id'))
62+
->prefix('#')
63+
->weight('semibold'),
5564
TextColumn::make('created_at')
5665
->label(__('inspirecms::inspirecms.created_at'))
5766
->dateTime('Y-m-d H:i:s')
@@ -114,9 +123,6 @@ public function table(Table $table): Table
114123
])
115124
->actions([
116125
TableAction::make('toggleAvoidToClean')
117-
->label(fn (Model | ContentVersion $record) => $this->getAvoidToCleanActionConfigFromRecord($record)['label'] ?? null)
118-
->color(fn (Model | ContentVersion $record): mixed => $this->getAvoidToCleanActionConfigFromRecord($record)['color'] ?? null)
119-
->icon(fn (Model | ContentVersion $record) => $this->getAvoidToCleanActionConfigFromRecord($record)['icon'] ?? null)
120126
->action(function (Model | ContentVersion $record, TableAction $action) {
121127

122128
$record->avoid_to_clean = ! $record->avoid_to_clean;
@@ -139,29 +145,7 @@ public function table(Table $table): Table
139145
->after(fn () => $this->dispatch('refresh')),
140146
TableAction::make('viewDifferences')
141147
->label(__('inspirecms::resources/content-version.buttons.view_differences.label'))
142-
->icon('heroicon-o-eye')
143-
->disabledForm()
144-
->modalFooterActions(fn () => []) // Disable footer actions
145-
->slideOver()
146-
->modalWidth(MaxWidth::ScreenTwoExtraLarge)
147-
->modalHeading(__('inspirecms::resources/content-version.buttons.view_differences.heading'))
148-
->modalDescription(fn ($record) => __('inspirecms::resources/content-version.buttons.view_differences.description', [
149-
'author' => $record->author?->name ?? __('inspirecms::inspirecms.unknown_user'),
150-
'date' => $record->created_at?->format('Y-m-d H:i:s') ?? __('inspirecms::inspirecms.n/a'),
151-
]))
152-
->modalContent(function (Model | ContentVersion $record) {
153-
return view('inspirecms::filament.actions.content-history-detail', [
154-
'record' => $record,
155-
'diff' => collect($this->getDiffKeysFromRecord($record))
156-
->mapWithKeys(fn ($key) => [
157-
$key => $this->computeDiff(
158-
originalContent: $record->from_data[$key] ?? null,
159-
newContent: $record->to_data[$key] ?? null
160-
),
161-
])
162-
->all(),
163-
]);
164-
}),
148+
->icon('heroicon-o-eye'),
165149
])
166150
->bulkActions([
167151
TableBulkAction::make('bulkUpdateState')
@@ -223,6 +207,41 @@ protected function configureTableAction(TableAction $action): void
223207
}
224208
return ! $record->exists && ! $record instanceof ContentVersion;
225209
});
210+
211+
switch ($action->getName()) {
212+
case 'toggleAvoidToClean':
213+
$action
214+
->label(fn (Model | ContentVersion $record) => $this->getAvoidToCleanActionConfigFromRecord($record)['label'] ?? null)
215+
->color(fn (Model | ContentVersion $record): mixed => $this->getAvoidToCleanActionConfigFromRecord($record)['color'] ?? null)
216+
->icon(fn (Model | ContentVersion $record) => $this->getAvoidToCleanActionConfigFromRecord($record)['icon'] ?? null);
217+
break;
218+
case 'viewDifferences':
219+
$action
220+
->color('gray')
221+
->disabledForm()
222+
// Disable footer actions
223+
->modalSubmitAction(false)->modalCancelAction(false)
224+
->slideOver()
225+
->modalWidth(MaxWidth::ScreenTwoExtraLarge)
226+
->modalHeading(fn (TableAction $action) => str(__('inspirecms::resources/content-version.buttons.view_differences.heading'))->when($action->getRecordTitle(), fn ($str, $value) => $str->finish(' - ' . $value)))
227+
->modalDescription(fn ($record) => __('inspirecms::resources/content-version.buttons.view_differences.description', [
228+
'author' => $record->author?->name ?? __('inspirecms::inspirecms.unknown_user'),
229+
'date' => $record->created_at?->format('Y-m-d H:i:s') ?? __('inspirecms::inspirecms.n/a'),
230+
]))
231+
->modalContent(function (Model | ContentVersion $record) {
232+
$diff = collect($this->getDiffKeysFromRecord($record))
233+
->mapWithKeys(fn ($key) => $this->computeDiffItem(
234+
key: $key,
235+
fromData: $record?->from_data ?? [],
236+
toData: $record?->to_data ?? [])
237+
)
238+
->all();
239+
return view('inspirecms::filament.actions.content-history-detail', [
240+
'diff' => $diff,
241+
]);
242+
});
243+
break;
244+
}
226245
}
227246

228247
protected function configureTableBulkAction(TableBulkAction $action): void
@@ -235,6 +254,22 @@ protected function configureTableBulkAction(TableBulkAction $action): void
235254
});
236255
}
237256

257+
protected function computeDiffItem(string $key, array $fromData, array $toData): array
258+
{
259+
$originalContent = data_get($fromData, $key, null);
260+
$newContent = data_get($toData, $key, null);
261+
if ($key == 'propertyData') {
262+
// Convert to array if it is a JSON string
263+
foreach (['originalContent', 'newContent'] as $var) {
264+
if (is_string($$var) && is_array(json_decode($$var, true))) {
265+
$$var = json_decode($$var, true);
266+
}
267+
}
268+
}
269+
$diff = $this->computeDiff($originalContent, $newContent);
270+
return [$key => $diff];
271+
}
272+
238273
protected function computeDiff($originalContent, $newContent)
239274
{
240275
try {

0 commit comments

Comments
 (0)