Skip to content

Commit 04c244e

Browse files
authored
🐛 Set filenames for task details downloads (#2031)
Resolves: #2030 On the Analysis details / Task details page, the log viewer allows downloading of the file being displayed. Set a filename that matches the attachment name when doing the download. File names used: - Base log view -> "log-${taskId}" - Merged log view -> "log-merged-${taskId}" - Task attachment -> attachment name Due to the way the `CodeEditor` PF5 component implements the download action, only file extensions of known file types can be used. Any log or attachment files will have file extensions of `.yaml` or `.json` or `.txt` depending on the detected file type. File type detection is only using attachment name file extensions currently, and only differentiates between yaml and json with a default to plain text. For example, given an attachment name, the download file name will be: - `git.output` -> `git.output.txt` - `addon.log` -> `addon.log.txt` - `settings.yaml` -> `settings.yaml` - `data.json` -> `data.json` Signed-off-by: Scott J Dickerson <sdickers@redhat.com>
1 parent 1c5e994 commit 04c244e

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

client/src/app/components/simple-document-viewer/SimpleDocumentViewer.tsx

+19-15
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,22 @@ export interface Document {
3131
name: string;
3232
description?: string;
3333
languages: Language[];
34+
downloadFilename: string;
3435
}
3536

3637
export interface ISimpleDocumentViewerProps {
3738
/** The id of the task to display, or `undefined` to display the empty state. */
3839
taskId: number | undefined;
3940

40-
/** The document ID to display - number for task attachment ID, string values for other viewing modes.
41-
* Defaults to basic task view (LOG_VIEW) .
41+
/**
42+
* The document ID to display - number for task attachment ID, string values for
43+
* other viewing modes. Defaults to basic task view (LOG_VIEW).
4244
*/
4345
documentId: DocumentId | undefined;
4446

4547
/** Task attachments */
4648
attachments: TaskAttachment[];
4749

48-
/** Filename, without extension, to use with the download file action. */
49-
downloadFilename?: string;
50-
5150
/**
5251
* Height of the document viewer, or `"full"` to take up all of the available
5352
* vertical space. Defaults to "450px".
@@ -98,7 +97,6 @@ export const SimpleDocumentViewer = ({
9897
taskId,
9998
documentId = "LOG_VIEW",
10099
attachments,
101-
downloadFilename,
102100
height = "450px",
103101
onDocumentChange,
104102
}: ISimpleDocumentViewerProps) => {
@@ -108,12 +106,14 @@ export const SimpleDocumentViewer = ({
108106
id: "LOG_VIEW",
109107
name: "Log view",
110108
languages: [Language.yaml, Language.json],
109+
downloadFilename: `log-${taskId}`,
111110
},
112111
{
113112
id: "MERGED_VIEW",
114113
name: "Merged log view",
115114
description: "with inlined commands output",
116115
languages: [Language.yaml, Language.json],
116+
downloadFilename: `log-merged-${taskId}`,
117117
},
118118
...attachments.map(({ id, name = "unknown" }) => ({
119119
id,
@@ -123,16 +123,20 @@ export const SimpleDocumentViewer = ({
123123
name.endsWith(".json") && Language.json,
124124
Language.plaintext,
125125
].filter(Boolean),
126+
downloadFilename: name.match(/\.(yaml|yml|json)$/)
127+
? name.substring(0, name.lastIndexOf("."))
128+
: name,
126129
})),
127130
],
128-
[attachments]
131+
[attachments, taskId]
129132
);
130133

131-
const [selectedId, setSelectedId] = React.useState<DocumentId>(
132-
configuredDocuments.find(({ id }) => id === documentId)?.id ?? "LOG_VIEW"
134+
const [selectedDocument, setSelectedDocument] = React.useState<Document>(
135+
configuredDocuments.find(({ id }) => id === documentId) ??
136+
configuredDocuments[0]
133137
);
134138
const supportedLanguages = configuredDocuments.find(
135-
({ id }) => id === selectedId
139+
({ id }) => id === selectedDocument.id
136140
)?.languages ?? [Language.yaml, Language.json];
137141

138142
const [currentLanguage, setCurrentLanguage] = React.useState(
@@ -144,7 +148,7 @@ export const SimpleDocumentViewer = ({
144148
const { code, refetch } = useDocuments({
145149
taskId,
146150
currentLanguage,
147-
selectedId,
151+
selectedId: selectedDocument.id,
148152
});
149153

150154
// move focus on first code change AFTER a new document was selected
@@ -165,12 +169,12 @@ export const SimpleDocumentViewer = ({
165169

166170
const onSelect = (docId: string | number) => {
167171
const doc = configuredDocuments.find(({ id }) => id === docId);
168-
if (!doc || doc.id === selectedId) {
172+
if (!doc || doc.id === selectedDocument.id) {
169173
return;
170174
}
171175

172176
setCurrentLanguage(doc.languages[0] ?? Language.plaintext);
173-
setSelectedId(doc.id);
177+
setSelectedDocument(doc);
174178
focusMovedOnSelectedDocumentChange.current = false;
175179
onDocumentChange?.(doc.id);
176180
};
@@ -184,7 +188,7 @@ export const SimpleDocumentViewer = ({
184188
isLineNumbersVisible
185189
isReadOnly
186190
height={height === "full" ? "100%" : height}
187-
downloadFileName={downloadFilename}
191+
downloadFileName={selectedDocument.downloadFilename}
188192
language={currentLanguage}
189193
code={code}
190194
onEditorDidMount={(editor) => {
@@ -210,7 +214,7 @@ export const SimpleDocumentViewer = ({
210214
key="attachmentToggle"
211215
documents={configuredDocuments.map((it) => ({
212216
...it,
213-
isSelected: it.id === selectedId,
217+
isSelected: it.id === selectedDocument.id,
214218
}))}
215219
onSelect={onSelect}
216220
/>,

0 commit comments

Comments
 (0)