Skip to content

Commit 90f36fe

Browse files
authored
Refactor download link creation in renderFile function
Refactored download link creation into a separate function for better code organization and reuse.
1 parent ca32159 commit 90f36fe

1 file changed

Lines changed: 13 additions & 24 deletions

File tree

js/app.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ async function generatePdfThumbnail(pdfDataURL) {
9696
function renderFile(doc) {
9797
if (!doc.file) return;
9898

99+
const createDownloadLink = () => {
100+
const downloadLink = document.createElement("a");
101+
downloadLink.href = doc.file.data;
102+
downloadLink.download = doc.file.name;
103+
downloadLink.textContent = "Download";
104+
downloadLink.className = "file-download-link";
105+
downloadLink.setAttribute("aria-label", `Download ${doc.file.name}`);
106+
return downloadLink;
107+
};
108+
99109
if (doc.file.type.startsWith("image/")) {
100110
const img = document.createElement("img");
101111
img.src = doc.file.data;
@@ -113,14 +123,7 @@ function renderFile(doc) {
113123

114124
chatbox.appendChild(img);
115125

116-
const downloadLink = document.createElement("a");
117-
downloadLink.href = doc.file.data;
118-
downloadLink.download = doc.file.name;
119-
downloadLink.textContent = `Download ${doc.file.name}`;
120-
downloadLink.style.display = "inline-block";
121-
downloadLink.style.marginTop = "6px";
122-
downloadLink.style.color = "#8ab4ff";
123-
chatbox.appendChild(downloadLink);
126+
chatbox.appendChild(createDownloadLink());
124127
return;
125128
}
126129

@@ -169,26 +172,12 @@ function renderFile(doc) {
169172
}
170173

171174
// Download Link
172-
const downloadLink = document.createElement("a");
173-
downloadLink.href = doc.file.data;
174-
downloadLink.download = doc.file.name;
175-
downloadLink.textContent = `Download ${doc.file.name}`;
176-
downloadLink.style.display = "inline-block";
177-
downloadLink.style.marginTop = "6px";
178-
downloadLink.style.color = "#8ab4ff";
179-
chatbox.appendChild(downloadLink);
175+
chatbox.appendChild(createDownloadLink());
180176

181177
return;
182178
}
183179

184-
const link = document.createElement("a");
185-
link.href = doc.file.data;
186-
link.download = doc.file.name;
187-
link.textContent = `Download ${doc.file.name}`;
188-
link.style.display = "inline-block";
189-
link.style.marginTop = "6px";
190-
link.style.color = "#8ab4ff";
191-
chatbox.appendChild(link);
180+
chatbox.appendChild(createDownloadLink());
192181
}
193182

194183
function updateDocument(id, updates) {

0 commit comments

Comments
 (0)