Skip to content

Commit b27e3fe

Browse files
committed
Fixes the UX issue of download buttons showing before the report is complete. Still kinda ugly, but functional
1 parent 9e80602 commit b27e3fe

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

client/index.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ <h2>Agent Output</h2>
9393
<div class="margin-div">
9494
<h2>Research Report</h2>
9595
<div id="reportContainer"></div>
96-
<button onclick="GPTResearcher.copyToClipboard()" class="btn btn-secondary mt-3">Copy to clipboard</button>
97-
<a id="downloadLink" href="#" class="btn btn-secondary mt-3" target="_blank">Download as PDF</a>
96+
<div id="reportActions">
97+
<div class="alert alert-info" role="alert" id="status"></div>
98+
<a onclick="GPTResearcher.copyToClipboard()" class="btn btn-secondary mt-3">Copy to clipboard</button>
99+
<a id="downloadLink" href="#" class="btn btn-secondary mt-3" target="_blank">Download as PDF</a>
100+
</div>
98101
</div>
99102
</main>
100103

@@ -106,17 +109,19 @@ <h2>Research Report</h2>
106109
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js"></script>
107110
<script src="/site/scripts.js"></script>
108111
<script>
109-
const btnChoose = document.getElementById('btnChoose');
112+
// const btnChoose = document.getElementById('btnChoose');
110113
const btnShowAuto = document.getElementById('btnShowAuto');
111114
const autoAgentDiv = document.getElementById('autoAgentDiv');
112115
const agentChoices = document.getElementsByClassName('agent-choices');
113116

117+
/**
114118
btnChoose.addEventListener('click', function () {
115119
btnShowAuto.style.display = 'inline-block';
116120
btnChoose.style.display = 'none';
117121
autoAgentDiv.style.display = 'none';
118122
agentChoices[0].style.display = 'flex';
119123
});
124+
**/
120125

121126
btnShowAuto.addEventListener('click', function () {
122127
btnShowAuto.style.display = 'none';

client/scripts.js

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
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+
210
const startResearch = () => {
311
document.getElementById("output").innerHTML = "";
412
document.getElementById("reportContainer").innerHTML = "";
13+
updateState("in_progress")
514

615
addAgentResponse({ output: "🤔 Thinking about research questions for the task..." });
716

@@ -21,7 +30,9 @@ const GPTResearcher = (() => {
2130
} else if (data.type === 'report') {
2231
writeReport(data, converter);
2332
} else if (data.type === 'path') {
33+
updateState("finished")
2434
updateDownloadLink(data);
35+
2536
}
2637
};
2738

@@ -57,8 +68,7 @@ const GPTResearcher = (() => {
5768

5869
const updateDownloadLink = (data) => {
5970
const path = data.output;
60-
const downloadLink = document.getElementById("downloadLink");
61-
downloadLink.href = path;
71+
document.getElementById("downloadLink").setAttribute("href", path);
6272
};
6373

6474
const updateScroll = () => {
@@ -76,9 +86,65 @@ const GPTResearcher = (() => {
7686
document.execCommand('copy');
7787
document.body.removeChild(textarea);
7888
};
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);
80146
return {
81147
startResearch,
82148
copyToClipboard,
83149
};
84-
})();
150+
})();

0 commit comments

Comments
 (0)