From abcc2a9c2c8922c310f5b830c540dd8b3e8ed440 Mon Sep 17 00:00:00 2001
From: Samed <sbalcioglu@graphpolaris.com>
Date: Thu, 7 Nov 2024 14:13:35 +0100
Subject: [PATCH] fix: sharebutton

---
 .../security/useAuthentication.tsx            | 58 +++++++++++++------
 1 file changed, 41 insertions(+), 17 deletions(-)

diff --git a/libs/shared/lib/data-access/security/useAuthentication.tsx b/libs/shared/lib/data-access/security/useAuthentication.tsx
index fcb507b86..a14824d96 100644
--- a/libs/shared/lib/data-access/security/useAuthentication.tsx
+++ b/libs/shared/lib/data-access/security/useAuthentication.tsx
@@ -1,5 +1,6 @@
 import { useAppDispatch, useAuthCache } from '../store';
 import { authenticated, changeRoom, UserAuthenticationHeader } from '../store/authSlice';
+import { addInfo, addError } from '../store/configSlice';
 
 const domain = import.meta.env.BACKEND_URL;
 const userURI = import.meta.env.BACKEND_USER;
@@ -16,15 +17,15 @@ export const useAuthentication = () => {
 
   const handleError = (err: any) => {
     console.error(err);
+    dispatch(addError('Failed to copy: ' + (err.message || 'Unknown error occurred')));
   };
 
   const login = () => {
     fetch(`${domain}${userURI}/headers`, fetchSettings)
-      .then((res) => {
+      .then((res) =>
         res
           .json()
           .then((res: UserAuthenticationHeader) => {
-            console.log(res, 'headers');
             dispatch(
               authenticated({
                 username: res.username,
@@ -36,24 +37,47 @@ export const useAuthentication = () => {
               }),
             );
           })
-          .catch(handleError);
-      })
-      .catch(handleError);
-  };
-
-  const newShareRoom = () => {
-    fetch(`${domain}${userURI}/share`, { ...fetchSettings, method: 'POST' })
-      .then((res) =>
-        res
-          .json()
-          .then((res: { Roomid: string; Sessionid: string }) => {
-            // TODO: send to backend current state and make redux accordingly
-            dispatch(changeRoom(res.Roomid));
-          })
           .catch(handleError),
       )
       .catch(handleError);
   };
 
+  const copyToClipboard = async (text: string) => {
+    try {
+      await navigator.clipboard.writeText(text);
+      return true;
+    } catch (err) {
+      console.error('Failed to copy:', err);
+      return false;
+    }
+  };
+
+  const newShareRoom = async () => {
+    try {
+      // TODO: Implement share room functionality when backend is ready
+      // fetch(`${domain}${userURI}/share`, { ...fetchSettings, method: 'POST' })
+      //   .then((res) =>
+      //     res
+      //       .json()
+      //       .then((res: { Roomid: string; Sessionid: string }) => {
+      //         dispatch(changeRoom(res.Roomid));
+      //       })
+      //       .catch(handleError),
+      //   )
+      //   .catch(handleError);
+
+      const shareUrl = window.location.href;
+      const copied = await copyToClipboard(shareUrl);
+      
+      if (copied) {
+        dispatch(addInfo('Link copied to clipboard!'));
+      } else {
+        throw new Error('Failed to copy to clipboard');
+      }
+    } catch (error: any) {
+      handleError(error);
+    }
+  };
+
   return { login, newShareRoom };
-};
+};
\ No newline at end of file
-- 
GitLab