From 353fcddb918ee9ccaedd69032f9212fc66c04a7f Mon Sep 17 00:00:00 2001
From: Skitty1238 <157652284+Skitty1238@users.noreply.github.com>
Date: Thu, 5 Jun 2025 13:54:09 -0400
Subject: sync button only accessible when a change is made to the task (to
 counter unnecessary syncing)

---
 src/client/views/nodes/TaskBox.tsx | 55 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

(limited to 'src/client/views/nodes/TaskBox.tsx')

diff --git a/src/client/views/nodes/TaskBox.tsx b/src/client/views/nodes/TaskBox.tsx
index 7df3876c3..82c22057e 100644
--- a/src/client/views/nodes/TaskBox.tsx
+++ b/src/client/views/nodes/TaskBox.tsx
@@ -23,6 +23,23 @@ interface TaskBoxProps {
  */
 @observer
 export class TaskBox extends React.Component<TaskBoxProps> {
+    // contains the last synced task information
+    lastSyncedTask: {
+        title: string;
+        text: string;
+        due?: string;
+        completed: boolean;
+    } = {
+        title: '',
+        text: '',
+        due: '',
+        completed: false,
+    };
+
+    state = {
+        needsSync: false,
+    };
+
     /**
      * Method to reuturn the
      * @param fieldStr
@@ -234,6 +251,34 @@ export class TaskBox extends React.Component<TaskBoxProps> {
                 }
             }
         );
+
+        this._googleTaskCreateDisposer = reaction(
+            () => {
+                const { title, text, $task_completed, $task_dateRange, $task_startTime, $task_endTime, $task_allDay } = doc;
+
+                const completed = !!$task_completed;
+                let due: string | undefined;
+
+                if ($task_allDay && typeof $task_dateRange === 'string') {
+                    const datePart = $task_dateRange.split('|')[0];
+                    if (datePart && !isNaN(new Date(datePart).getTime())) {
+                        due = new Date(datePart + 'T00:00:00Z').toISOString();
+                    }
+                } else if ($task_endTime && $task_endTime instanceof DateField && $task_endTime.date) {
+                    due = $task_endTime.date.toISOString();
+                } else if ($task_startTime && $task_startTime instanceof DateField && $task_startTime.date) {
+                    due = $task_startTime.date.toISOString();
+                }
+
+                return { title, text, completed, due };
+            },
+            current => {
+                const hasChanged = current.title !== this.lastSyncedTask.title || current.text !== this.lastSyncedTask.text || current.due !== this.lastSyncedTask.due || current.completed !== this.lastSyncedTask.completed;
+
+                this.setState({ needsSync: hasChanged });
+            },
+            { fireImmediately: true }
+        );
     }
 
     componentWillUnmount() {
@@ -350,6 +395,15 @@ export class TaskBox extends React.Component<TaskBoxProps> {
 
                 if (result?.id) {
                     alert('✅ Task synced with Google Tasks!');
+                    if (result?.id) {
+                        this.lastSyncedTask = {
+                            title: taskTitle,
+                            text: taskDesc,
+                            due,
+                            completed: isCompleted,
+                        };
+                        this.setState({ needsSync: false });
+                    }
                 } else {
                     alert(`❌ Failed: ${result?.error?.message || 'Unknown error'}`);
                 }
@@ -402,6 +456,7 @@ export class TaskBox extends React.Component<TaskBoxProps> {
 
                     <button
                         className="task-manager-google"
+                        disabled={!this.state.needsSync}
                         onClick={event => {
                             event.preventDefault();
                             handleGoogleTaskSync();
-- 
cgit v1.2.3-70-g09d2

