From 45ab41934a43fd807f7307a415280d8c75d86dce Mon Sep 17 00:00:00 2001
From: Jack <jjbayliss@icloud.com>
Date: Sat, 28 Sep 2024 02:36:27 +0100
Subject: [PATCH 1/8] hide buttons if not in control

prevent saving if not in control
---
 src/Forms/Components/GazeBanner.php | 18 ++-------
 src/Traits/GazeLockControl.php      | 57 +++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 15 deletions(-)
 create mode 100644 src/Traits/GazeLockControl.php

diff --git a/src/Forms/Components/GazeBanner.php b/src/Forms/Components/GazeBanner.php
index 5296533..d73bf82 100644
--- a/src/Forms/Components/GazeBanner.php
+++ b/src/Forms/Components/GazeBanner.php
@@ -4,6 +4,7 @@
 
 use Carbon\Carbon;
 use Closure;
+use DiscoveryDesign\FilamentGaze\Traits\GazeLockControl;
 use Filament\Facades\Filament;
 use Filament\Forms\Components\Component;
 use Illuminate\Support\Facades\Cache;
@@ -18,6 +19,7 @@
  */
 class GazeBanner extends Component
 {
+    use GazeLockControl;
     /**
      * The array of current viewers.
      */
@@ -63,7 +65,7 @@ public static function make(array | Closure $schema = []): static
     public function identifier(string | Closure $fnc = ''): static
     {
         $this->identifier = (string) $this->evaluate($fnc);
-
+        Cache::put('filament-gaze-' . $this->getIdentifier() . '-custom-identfier',$this->identifier);
         return $this;
     }
 
@@ -141,20 +143,6 @@ public function takeControl()
 
     }
 
-    public function getIdentifier()
-    {
-        if (! $this->identifier) {
-            $record = $this->getRecord();
-            if (! $record) {
-                $this->identifier = (string) $this->getModel();
-            } else {
-                $this->identifier = get_class($record) . '-' . $record->id;
-            }
-        }
-
-        return $this->identifier;
-    }
-
     public function refreshForm()
     {
         // Very hacky, maybe a better solution for this?
diff --git a/src/Traits/GazeLockControl.php b/src/Traits/GazeLockControl.php
new file mode 100644
index 0000000..725077d
--- /dev/null
+++ b/src/Traits/GazeLockControl.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace DiscoveryDesign\FilamentGaze\Traits;
+
+
+use Illuminate\Support\Facades\Cache;
+/**
+ * Trait GazeLockControl
+ *
+ * This Trait can be used by pages etc to control button access, and is used to contain the identifier logic.
+ */
+trait GazeLockControl {
+    /**
+     * The custom identifier for the GazeBanner component.
+     */
+    public ?string $identifier = null;
+
+    public function getIdentifier(): ?string
+    {
+        // Try to get the custom identifier first, if one has been set.
+        if (! $this->identifier) {
+            $record = $this->getRecord();
+            if (! $record) {
+                $this->identifier = (string) $this->getModel();
+            } else {
+                $this->identifier = get_class($record) . '-' . $record->id;
+            }
+        }
+        $customIdentifier = Cache::get('filament-gaze-' . $this->identifier . '-custom-identfier');
+        if($customIdentifier) {
+            $this->identifier = $customIdentifier;
+        }
+        return $this->identifier;
+    }
+
+    public function setControllingUser(string|int $userId): void
+    {
+        Cache::put('filament-gaze-controller-' .$this->getIdentifier(), $userId);
+
+    }
+
+    public function hasControl(): bool
+    {
+        $currentViewers = Cache::get('filament-gaze-' . $this->getIdentifier());
+        if( is_null($currentViewers) ) {
+            return false;
+        }
+        foreach ($currentViewers as $viewer) {
+            if($viewer['id'] === request()->user()->id && $viewer['has_control']) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+}
\ No newline at end of file

From 4460a34fff16ff2e07240d57964e64bf11fd251b Mon Sep 17 00:00:00 2001
From: Jack <jjbayliss@icloud.com>
Date: Sat, 28 Sep 2024 15:54:48 +0100
Subject: [PATCH 2/8] Update GazeLockControl.php

change method, add cache timeout.
---
 src/Traits/GazeLockControl.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/Traits/GazeLockControl.php b/src/Traits/GazeLockControl.php
index 725077d..cda8008 100644
--- a/src/Traits/GazeLockControl.php
+++ b/src/Traits/GazeLockControl.php
@@ -35,11 +35,11 @@ public function getIdentifier(): ?string
 
     public function setControllingUser(string|int $userId): void
     {
-        Cache::put('filament-gaze-controller-' .$this->getIdentifier(), $userId);
+        Cache::put('filament-gaze-controller-' .$this->getIdentifier(), $userId,now()->addSeconds(max([5, $this->pollTimer * 2])));
 
     }
 
-    public function hasControl(): bool
+    public function hasGazeControl(): bool
     {
         $currentViewers = Cache::get('filament-gaze-' . $this->getIdentifier());
         if( is_null($currentViewers) ) {

From a952cdfc6951b13f45ea5e4c5eaf175ddf7cf974 Mon Sep 17 00:00:00 2001
From: Jack <jjbayliss@icloud.com>
Date: Sat, 28 Sep 2024 16:25:35 +0100
Subject: [PATCH 3/8] remove redundant method and comments.

---
 src/Forms/Components/GazeBanner.php |  2 +-
 src/Traits/GazeLockControl.php      | 14 ++++++++------
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/Forms/Components/GazeBanner.php b/src/Forms/Components/GazeBanner.php
index d73bf82..425e000 100644
--- a/src/Forms/Components/GazeBanner.php
+++ b/src/Forms/Components/GazeBanner.php
@@ -4,10 +4,10 @@
 
 use Carbon\Carbon;
 use Closure;
-use DiscoveryDesign\FilamentGaze\Traits\GazeLockControl;
 use Filament\Facades\Filament;
 use Filament\Forms\Components\Component;
 use Illuminate\Support\Facades\Cache;
+use DiscoveryDesign\FilamentGaze\Traits\GazeLockControl;
 
 /**
  * Class GazeBanner
diff --git a/src/Traits/GazeLockControl.php b/src/Traits/GazeLockControl.php
index cda8008..0ff4261 100644
--- a/src/Traits/GazeLockControl.php
+++ b/src/Traits/GazeLockControl.php
@@ -26,6 +26,7 @@ public function getIdentifier(): ?string
                 $this->identifier = get_class($record) . '-' . $record->id;
             }
         }
+        // Ensure there's not a custom identifier set.
         $customIdentifier = Cache::get('filament-gaze-' . $this->identifier . '-custom-identfier');
         if($customIdentifier) {
             $this->identifier = $customIdentifier;
@@ -33,14 +34,15 @@ public function getIdentifier(): ?string
         return $this->identifier;
     }
 
-    public function setControllingUser(string|int $userId): void
-    {
-        Cache::put('filament-gaze-controller-' .$this->getIdentifier(), $userId,now()->addSeconds(max([5, $this->pollTimer * 2])));
-
-    }
-
+    /**
+     * See if current user has gaze control access.
+     * @return bool
+     */
     public function hasGazeControl(): bool
     {
+        if(!isset(request()->user()?->id)){
+            return false;
+        }
         $currentViewers = Cache::get('filament-gaze-' . $this->getIdentifier());
         if( is_null($currentViewers) ) {
             return false;

From 377d595cede27e4555acce826c1c388fd29d6652 Mon Sep 17 00:00:00 2001
From: Jack <jjbayliss@icloud.com>
Date: Sat, 28 Sep 2024 16:36:33 +0100
Subject: [PATCH 4/8] Update GazeLockControl.php

ensure if it has no viewers, its true-- as the only viewer is the current user?
---
 src/Traits/GazeLockControl.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/Traits/GazeLockControl.php b/src/Traits/GazeLockControl.php
index 0ff4261..aa2eac4 100644
--- a/src/Traits/GazeLockControl.php
+++ b/src/Traits/GazeLockControl.php
@@ -44,8 +44,9 @@ public function hasGazeControl(): bool
             return false;
         }
         $currentViewers = Cache::get('filament-gaze-' . $this->getIdentifier());
+
         if( is_null($currentViewers) ) {
-            return false;
+            return true;
         }
         foreach ($currentViewers as $viewer) {
             if($viewer['id'] === request()->user()->id && $viewer['has_control']) {

From 9276b73961890c863944aedda9af8d6c815c4995 Mon Sep 17 00:00:00 2001
From: Jack <jjbayliss@icloud.com>
Date: Sat, 28 Sep 2024 16:49:06 +0100
Subject: [PATCH 5/8] Update GazeLockControl.php

match spacing
---
 src/Traits/GazeLockControl.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/Traits/GazeLockControl.php b/src/Traits/GazeLockControl.php
index aa2eac4..3682d1d 100644
--- a/src/Traits/GazeLockControl.php
+++ b/src/Traits/GazeLockControl.php
@@ -2,8 +2,9 @@
 
 namespace DiscoveryDesign\FilamentGaze\Traits;
 
-
 use Illuminate\Support\Facades\Cache;
+
+
 /**
  * Trait GazeLockControl
  *

From c2e20b4823270243074d5e0810d5fe8bf79b82dd Mon Sep 17 00:00:00 2001
From: Jack <jjbayliss@icloud.com>
Date: Sat, 28 Sep 2024 16:50:44 +0100
Subject: [PATCH 6/8] Update GazeBanner.php

add return types
---
 src/Forms/Components/GazeBanner.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/Forms/Components/GazeBanner.php b/src/Forms/Components/GazeBanner.php
index 425e000..cd903b9 100644
--- a/src/Forms/Components/GazeBanner.php
+++ b/src/Forms/Components/GazeBanner.php
@@ -143,7 +143,7 @@ public function takeControl()
 
     }
 
-    public function refreshForm()
+    public function refreshForm(): void
     {
         // Very hacky, maybe a better solution for this?
 	    $record = $this->getRecord();
@@ -161,7 +161,7 @@ public function refreshForm()
      *
      * @return void
      */
-    public function refreshViewers()
+    public function refreshViewers(): void
     {
         $this->registerListeners([
             'FilamentGaze::takeControl' => [

From 4e9fd7f75c9f8495e30a755841e685284d194f28 Mon Sep 17 00:00:00 2001
From: Jack <jjbayliss@icloud.com>
Date: Sat, 28 Sep 2024 17:02:06 +0100
Subject: [PATCH 7/8] Update README.md

---
 README.md | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/README.md b/README.md
index 73631cc..2b70160 100644
--- a/README.md
+++ b/README.md
@@ -97,7 +97,29 @@ There is also a helper function
 GazeBanner::make()
     ->hideOnCreate(),
 ```
+### Hide actions based on the controlling user (if you've implemented the take control logic)
+```php
+namespace App\Filament\Resources\CustomerResource\Pages;
+
+use App\Filament\Resources\CustomerResource;
+use DiscoveryDesign\FilamentGaze\Traits\GazeLockControl;
+use Filament\Actions;
+use Filament\Resources\Pages\EditRecord;
+
+class EditCustomer extends EditRecord
+{
+    use GazeLockControl;
 
+    protected static string $resource = CustomerResource::class;
+
+    protected function getHeaderActions(): array
+    {
+        return [
+            Actions\DeleteAction::make()->hidden(!$this->hasGazeControl()),
+        ];
+    }
+
+```
 
 ## Docs
 
@@ -138,6 +160,12 @@ GazeBanner::make()
 #### Description
 `hideOnCreate` is a helper function that can be used to hide the banner on create forms.
 
+### `->hasGazeControl()`
+
+#### Description
+`hasGazeControl` is a helper function that can be used to hide actions, and is usable from the GazeLocKControl trait.
+
+
 ## Customization
 
 ### Change icons

From fc64c12d8e7880da5b30b7c7458a147cb6723057 Mon Sep 17 00:00:00 2001
From: Jack <jjbayliss@icloud.com>
Date: Sat, 28 Sep 2024 17:03:40 +0100
Subject: [PATCH 8/8] Update README.md

---
 README.md | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 2b70160..176a62b 100644
--- a/README.md
+++ b/README.md
@@ -98,6 +98,8 @@ GazeBanner::make()
     ->hideOnCreate(),
 ```
 ### Hide actions based on the controlling user (if you've implemented the take control logic)
+
+You will need to use the `DiscoveryDesign\FilamentGaze\Traits\GazeLockControl` trait, in order to access the method.
 ```php
 namespace App\Filament\Resources\CustomerResource\Pages;
 
@@ -163,7 +165,7 @@ class EditCustomer extends EditRecord
 ### `->hasGazeControl()`
 
 #### Description
-`hasGazeControl` is a helper function that can be used to hide actions, and is usable from the GazeLocKControl trait.
+`hasGazeControl` is a helper function that can be used to hide actions, and is usable from the GazeLockControl trait.
 
 
 ## Customization