File size: 2,731 Bytes
75fefa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"use client";

import { useEffect } from "react";
import Script from "next/script";

const pylon_app_id = "da167302-89d0-41fa-b794-0f8d19c70b68";

interface PylonChatProps {
  user: {
    email?: string | undefined;
  };
  nonce: string;
}

const PylonChat: React.FC<PylonChatProps> = ({ user, nonce }) => {
  useEffect(() => {
    // If there's no user.email, do nothing.
    if (!user?.email) return;

    // Mount/initialize Pylon
    (window as any).pylon = {
      chat_settings: {
        app_id: pylon_app_id,
        email: user.email,
        name: user.email,
      },
    };

    // The cleanup function to run when the user logs out
    // or when this component unmounts.
    return () => {
      try {
        console.log("Unmounting Pylon script...");

        // If Pylon provides a remove/destroy function, call it:
        if (
          (window as any).Pylon &&
          typeof (window as any).Pylon.remove === "function"
        ) {
          (window as any).Pylon.remove();
        }

        // Remove the script tag from the DOM, if you wish:
        const pylonScript = document.getElementById("pylon-script");
        if (pylonScript) {
          pylonScript.remove();
        }

        // Also remove the global Pylon variables, if desired:
        delete (window as any).Pylon;
        delete (window as any).pylon;
      } catch (error) {
        console.error("Error unmounting Pylon script:", error);
      }
    };
  }, [user]);

  // Only render the script if we have a user email
  if (!user?.email) {
    console.log("No user email found, not rendering Pylon chat");
    return null;
  }

  return (
    <Script
      id="pylon-script"
      nonce={nonce}
      strategy="afterInteractive"
      onLoad={() => {}}
      onError={(e) => {
        console.error("Error loading Pylon script:", e);
      }}
      dangerouslySetInnerHTML={{
        __html: `
          (function(){
            var e=window;
            var t=document;
            var n=function(){n.e(arguments)};
            n.q=[];
            n.e=function(e){n.q.push(e)};
            e.Pylon=n;
            var r=function(){
              var s=t.createElement("script");
              s.setAttribute("type","text/javascript");
              s.setAttribute("async","true");
              s.setAttribute("src","https://widget.usepylon.com/widget/${pylon_app_id}");
              var o=t.getElementsByTagName("script")[0];
              o.parentNode.insertBefore(s,o)
            };
            if(t.readyState==="complete"){ 
              r()
            } else if(e.addEventListener){
              e.addEventListener("load",r,false)
            }
          })();
        `,
      }}
    />
  );
};

export default PylonChat;