1
1
const GPTResearcher = ( ( ) => {
2
+ const init = ( ) => {
3
+ // Not sure, but I think it would be better to add event handlers here instead of in the HTML
4
+ //document.getElementById("startResearch").addEventListener("click", startResearch);
5
+ //document.getElementById("copyToClipboard").addEventListener("click", copyToClipboard);
6
+
7
+ updateState ( "initial" ) ;
8
+ }
9
+
2
10
const startResearch = ( ) => {
3
11
document . getElementById ( "output" ) . innerHTML = "" ;
4
12
document . getElementById ( "reportContainer" ) . innerHTML = "" ;
13
+ updateState ( "in_progress" )
5
14
6
15
addAgentResponse ( { output : "🤔 Thinking about research questions for the task..." } ) ;
7
16
@@ -21,7 +30,9 @@ const GPTResearcher = (() => {
21
30
} else if ( data . type === 'report' ) {
22
31
writeReport ( data , converter ) ;
23
32
} else if ( data . type === 'path' ) {
33
+ updateState ( "finished" )
24
34
updateDownloadLink ( data ) ;
35
+
25
36
}
26
37
} ;
27
38
@@ -57,8 +68,7 @@ const GPTResearcher = (() => {
57
68
58
69
const updateDownloadLink = ( data ) => {
59
70
const path = data . output ;
60
- const downloadLink = document . getElementById ( "downloadLink" ) ;
61
- downloadLink . href = path ;
71
+ document . getElementById ( "downloadLink" ) . setAttribute ( "href" , path ) ;
62
72
} ;
63
73
64
74
const updateScroll = ( ) => {
@@ -76,9 +86,65 @@ const GPTResearcher = (() => {
76
86
document . execCommand ( 'copy' ) ;
77
87
document . body . removeChild ( textarea ) ;
78
88
} ;
79
-
89
+
90
+ const updateState = ( state ) => {
91
+ var status = "" ;
92
+ switch ( state ) {
93
+ case "in_progress" :
94
+ status = "Research in progress..."
95
+ setReportActionsStatus ( "disabled" ) ;
96
+ break ;
97
+ case "finished" :
98
+ status = "Research finished!"
99
+ setReportActionsStatus ( "enabled" ) ;
100
+ break ;
101
+ case "error" :
102
+ status = "Research failed!"
103
+ setReportActionsStatus ( "disabled" ) ;
104
+ break ;
105
+ case "initial" :
106
+ status = ""
107
+ setReportActionsStatus ( "hidden" ) ;
108
+ break ;
109
+ default :
110
+ setReportActionsStatus ( "disabled" ) ;
111
+ }
112
+ document . getElementById ( "status" ) . innerHTML = status ;
113
+ if ( document . getElementById ( "status" ) . innerHTML == "" ) {
114
+ document . getElementById ( "status" ) . style . display = "none" ;
115
+ } else {
116
+ document . getElementById ( "status" ) . style . display = "block" ;
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Shows or hides the download and copy buttons
122
+ * @param {str } status Kind of hacky. Takes "enabled", "disabled", or "hidden". "Hidden is same as disabled but also hides the div"
123
+ */
124
+ const setReportActionsStatus = ( status ) => {
125
+ const reportActions = document . getElementById ( "reportActions" ) ;
126
+ // Disable everything in reportActions until research is finished
127
+
128
+ if ( status == "enabled" ) {
129
+ reportActions . querySelectorAll ( "a" ) . forEach ( ( link ) => {
130
+ link . classList . remove ( "disabled" ) ;
131
+ link . removeAttribute ( 'onclick' ) ;
132
+ reportActions . style . display = "block" ;
133
+ } ) ;
134
+ } else {
135
+ reportActions . querySelectorAll ( "a" ) . forEach ( ( link ) => {
136
+ link . classList . add ( "disabled" ) ;
137
+ link . setAttribute ( 'onclick' , "return false;" ) ;
138
+ } ) ;
139
+ if ( status == "hidden" ) {
140
+ reportActions . style . display = "none" ;
141
+ }
142
+ }
143
+ }
144
+
145
+ document . addEventListener ( "DOMContentLoaded" , init ) ;
80
146
return {
81
147
startResearch,
82
148
copyToClipboard,
83
149
} ;
84
- } ) ( ) ;
150
+ } ) ( ) ;
0 commit comments