Skip to content

Commit 92d7696

Browse files
committed
Review edits, improvements and tweaks
1 parent afb406f commit 92d7696

File tree

4 files changed

+146
-115
lines changed

4 files changed

+146
-115
lines changed

.repomix/instructions/plan.md

Lines changed: 0 additions & 76 deletions
This file was deleted.

.repomix/plan.config.json

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<div class="relative mt-2 pt-2 border-t border-gray-200 dark:border-gray-700 flex items-center gap-x-1 flex-wrap">
2+
{{-- Inline buttons for existing reactions --}}
3+
@foreach ($this->reactionSummary as $reactionData)
4+
<span wire:key="inline-reaction-button-{{ $reactionData['reaction'] }}-{{ $comment->getId() }}">
5+
<button
6+
x-cloak
7+
wire:click="handleReactionToggle('{{ $reactionData['reaction'] }}')"
8+
type="button"
9+
class="inline-flex items-center justify-center gap-1 rounded-full border px-2 h-8 text-sm font-medium transition hover:bg-gray-100 dark:hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed
10+
{{ $reactionData['reacted_by_current_user']
11+
? 'bg-primary-100 dark:bg-primary-800 border-primary-300 dark:border-primary-600 text-primary-700 dark:text-primary-200'
12+
: 'bg-white dark:bg-gray-900 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-200' }}"
13+
title="{{ $reactionData['reaction'] }}"
14+
15+
>
16+
<span>{{ $reactionData['reaction'] }}</span>
17+
<span wire:key="inline-reaction-count-{{ $reactionData['reaction'] }}-{{ $comment->getId() }}">{{ $reactionData['count'] }}</span>
18+
</button>
19+
</span>
20+
@endforeach
21+
22+
{{-- Add Reaction Button --}}
23+
<div class="relative" x-data="{ open: false }" wire:ignore.self>
24+
<button
25+
x-on:click="open = !open"
26+
type="button"
27+
@disabled(! auth()->check())
28+
class="inline-flex items-center justify-center gap-1 rounded-full border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-900 w-8 h-8 text-sm font-medium text-gray-700 dark:text-gray-200 transition hover:bg-gray-100 dark:hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed"
29+
title="Add Reaction"
30+
wire:key="add-reaction-button-{{ $comment->getId() }}"
31+
>
32+
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
33+
<path stroke-linecap="round" stroke-linejoin="round" d="M14.828 14.828a4 4 0 01-5.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
34+
</svg>
35+
</button>
36+
37+
{{-- Reaction Popup --}}
38+
<div
39+
x-show="open"
40+
x-cloak
41+
x-on:click.away="open = false"
42+
class="absolute bottom-full mb-2 z-10 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg shadow-lg p-2 flex flex-wrap gap-1 w-max max-w-xs"
43+
>
44+
@foreach ($allowedReactions as $reactionEmoji)
45+
@php
46+
$reactionData = $this->reactionSummary[$reactionEmoji] ?? ['count' => 0, 'reacted_by_current_user' => false];
47+
@endphp
48+
49+
<button
50+
wire:click="handleReactionToggle('{{ $reactionEmoji }}')"
51+
x-on:click="open = false"
52+
type="button"
53+
@disabled(! auth()->check())
54+
class="inline-flex items-center justify-center gap-1 rounded-full border w-8 h-8 text-sm font-medium transition hover:bg-gray-100 dark:hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed
55+
{{ $reactionData['reacted_by_current_user']
56+
? 'bg-primary-100 dark:bg-primary-800 border-primary-300 dark:border-primary-600 text-primary-700 dark:text-primary-200'
57+
: 'bg-white dark:bg-gray-900 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-200' }}"
58+
title="{{ $reactionEmoji }}"
59+
wire:key="popup-reaction-button-{{ $reactionEmoji }}-{{ $comment->getId() }}"
60+
>
61+
<span>{{ $reactionEmoji }}</span>
62+
</button>
63+
@endforeach
64+
</div>
65+
</div>
66+
67+
{{-- Display summary of reactions not explicitly in the allowed list --}}
68+
@foreach ($this->reactionSummary as $reactionEmoji => $data)
69+
@if (! in_array($reactionEmoji, $allowedReactions) && $data['count'] > 0)
70+
<span
71+
wire:key="reaction-extra-{{ $reactionEmoji }}-{{ $comment->getId() }}"
72+
class="inline-flex items-center justify-center gap-1 rounded-full border border-gray-300 dark:border-gray-600 bg-gray-100 dark:bg-gray-800 w-8 h-8 text-sm font-medium text-gray-600 dark:text-gray-300"
73+
title="{{ $reactionEmoji }}"
74+
>
75+
<span>{{ $reactionEmoji }}</span>
76+
<span>{{ $data['count'] }}</span>
77+
</span>
78+
@endif
79+
@endforeach
80+
</div>

src/Livewire/Reactions.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Kirschbaum\Commentions\Livewire;
4+
5+
use Livewire\Component;
6+
use Livewire\Attributes\On;
7+
use Livewire\Attributes\Computed;
8+
use Kirschbaum\Commentions\Config;
9+
use Illuminate\Contracts\View\View;
10+
use Kirschbaum\Commentions\Comment as CommentModel;
11+
use Kirschbaum\Commentions\Contracts\RenderableComment;
12+
13+
class Reactions extends Component
14+
{
15+
public RenderableComment $comment;
16+
17+
public function handleReactionToggle(string $reaction): void
18+
{
19+
$this->dispatch(
20+
'comment:reaction:toggled',
21+
reaction: $reaction,
22+
commentId: $this->comment->getId()
23+
)->to(Comment::class);
24+
25+
unset($this->reactionSummary);
26+
}
27+
28+
public function render(): View
29+
{
30+
return view('commentions::reactions', [
31+
'allowedReactions' => Config::getAllowedReactions(),
32+
]);
33+
}
34+
35+
#[On('comment:reaction:saved')]
36+
public function refreshReactionSummary()
37+
{
38+
unset($this->reactionSummary);
39+
}
40+
41+
#[Computed]
42+
public function reactionSummary()
43+
{
44+
if (! $this->comment instanceof CommentModel) {
45+
return [];
46+
}
47+
48+
if (! $this->comment->relationLoaded('reactions')) {
49+
$this->comment->load('reactions.reactor');
50+
}
51+
52+
return $this->comment->reactions
53+
->groupBy('reaction')
54+
->map(function ($group) {
55+
$user = Config::resolveAuthenticatedUser();
56+
57+
return [
58+
'count' => $group->count(),
59+
'reaction' => $group->first()->reaction,
60+
'reacted_by_current_user' => $user && $group->contains(fn ($reaction) => $reaction->reactor_id == $user->getKey() && $reaction->reactor_type == $user->getMorphClass()),
61+
];
62+
})
63+
->sortByDesc('count')
64+
->toArray();
65+
}
66+
}

0 commit comments

Comments
 (0)