Skip to content

Commit 724f43c

Browse files
committed
move to utils
1 parent 0468099 commit 724f43c

File tree

5 files changed

+62
-69
lines changed

5 files changed

+62
-69
lines changed

frontend/src/features/crawl-workflows/workflow-action-menu/workflow-action-menu.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { BtrixElement } from "@/classes/BtrixElement";
1010
import { ClipboardController } from "@/controllers/clipboard";
1111
import { WorkflowTab } from "@/routes";
1212
import type { Crawl, ListWorkflow, Workflow } from "@/types/crawler";
13+
import { downloadLink } from "@/utils/crawl-workflows/downloadLink";
1314
import { isNotFailed, isSuccessfullyFinished } from "@/utils/crawler";
1415
import { isArchivingDisabled } from "@/utils/orgs";
1516

@@ -209,23 +210,14 @@ export class WorkflowActionMenu extends BtrixElement {
209210
private renderLatestCrawlMenu(latestCrawl: Crawl) {
210211
const authToken = this.authState?.headers.Authorization.split(" ")[1];
211212
const logTotals = this.logTotals;
212-
213-
let path = `/api/orgs/${this.orgId}/all-crawls/${latestCrawl.id}/download?auth_bearer=${authToken}`;
214-
let name = `${latestCrawl.id}.wacz`;
215-
216-
if (latestCrawl.resources?.length === 1) {
217-
const file = latestCrawl.resources[0];
218-
219-
path = file.path;
220-
name = file.name;
221-
}
213+
const download = downloadLink(latestCrawl, this.authState);
222214

223215
return html`
224216
<sl-menu slot="submenu">
225217
<btrix-menu-item-link
226-
href=${path}
218+
href=${download.path}
227219
?disabled=${!latestCrawl.fileSize}
228-
download=${name}
220+
download=${download.name}
229221
>
230222
<sl-icon name="cloud-download" slot="prefix"></sl-icon>
231223
${msg("Download Item")}

frontend/src/pages/org/archived-item-detail/archived-item-detail.ts

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import type {
2020
Workflow,
2121
} from "@/types/crawler";
2222
import type { QARun } from "@/types/qa";
23-
import type { NonEmptyArray } from "@/types/utils";
2423
import { isApiError } from "@/utils/api";
24+
import { downloadLink } from "@/utils/crawl-workflows/downloadLink";
25+
import { hasFiles } from "@/utils/crawl-workflows/hasFiles";
2526
import {
2627
isActive,
2728
isNotFailed,
@@ -126,29 +127,6 @@ export class ArchivedItemDetail extends BtrixElement {
126127
config: msg("Crawl Settings"),
127128
};
128129

129-
private get authQuery() {
130-
return `auth_bearer=${this.authState?.headers.Authorization.split(" ")[1]}`;
131-
}
132-
133-
private get itemDownload() {
134-
let path = "";
135-
let name = "";
136-
137-
if (this.hasFiles(this.item)) {
138-
if (this.item.resources.length > 1) {
139-
path = `/api/orgs/${this.orgId}/all-crawls/${this.itemId}/download?${this.authQuery}`;
140-
name = `${this.itemId}.wacz`;
141-
} else {
142-
const file = this.item.resources[0];
143-
144-
path = file.path;
145-
name = file.name;
146-
}
147-
}
148-
149-
return { path, name };
150-
}
151-
152130
private get listUrl(): string {
153131
let path = "items";
154132
if (this.workflowId) {
@@ -169,15 +147,6 @@ export class ArchivedItemDetail extends BtrixElement {
169147

170148
private timerId?: number;
171149

172-
private hasFiles(item?: ArchivedItem): item is ArchivedItem & {
173-
resources: NonEmptyArray<NonNullable<ArchivedItem["resources"]>[number]>;
174-
} {
175-
if (!item) return false;
176-
if (!item.resources) return false;
177-
178-
return Boolean(item.resources[0]);
179-
}
180-
181150
private get formattedFinishedDate() {
182151
if (!this.item) return;
183152

@@ -351,7 +320,7 @@ export class ArchivedItemDetail extends BtrixElement {
351320
case "files":
352321
sectionContent = this.renderPanel(
353322
html` ${this.renderTitle(this.tabLabels.files)}
354-
${this.renderDownloadFiles()}`,
323+
${this.renderDownloadFilesButton()}`,
355324
this.renderFiles(),
356325
);
357326
break;
@@ -360,7 +329,7 @@ export class ArchivedItemDetail extends BtrixElement {
360329
html` ${this.renderTitle(this.tabLabels.logs)}
361330
<sl-tooltip content=${msg("Download Entire Log File")}>
362331
<sl-button
363-
href=${`/api/orgs/${this.orgId}/crawls/${this.itemId}/logs?${this.authQuery}`}
332+
href=${`/api/orgs/${this.orgId}/crawls/${this.itemId}/logs?auth_bearer=${this.authState?.headers.Authorization.split(" ")[1]}`}
364333
download=${`browsertrix-${this.itemId}-logs.log`}
365334
size="small"
366335
variant="primary"
@@ -637,7 +606,7 @@ export class ArchivedItemDetail extends BtrixElement {
637606
private renderMenu() {
638607
if (!this.item) return;
639608

640-
const { path, name } = this.itemDownload;
609+
const download = downloadLink(this.item, this.authState);
641610

642611
return html`
643612
<sl-dropdown placement="bottom-end" distance="4" hoist>
@@ -671,7 +640,10 @@ export class ArchivedItemDetail extends BtrixElement {
671640
</btrix-menu-item-link>
672641
`,
673642
)}
674-
<btrix-menu-item-link href=${path} download=${name}>
643+
<btrix-menu-item-link
644+
href=${download.path}
645+
download=${download.name}
646+
>
675647
<sl-icon name="cloud-download" slot="prefix"></sl-icon>
676648
${msg("Download Item")}
677649
${this.item?.fileSize
@@ -775,7 +747,7 @@ export class ArchivedItemDetail extends BtrixElement {
775747

776748
const config = JSON.stringify({ headers });
777749

778-
const canReplay = this.hasFiles(this.item);
750+
const canReplay = hasFiles(this.item);
779751

780752
return html`
781753
<!-- https://github.com/webrecorder/browsertrix-crawler/blob/9f541ab011e8e4bccf8de5bd7dc59b632c694bab/screencast/index.html -->
@@ -989,7 +961,7 @@ export class ArchivedItemDetail extends BtrixElement {
989961

990962
private renderFiles() {
991963
return html`
992-
${this.hasFiles(this.item)
964+
${hasFiles(this.item)
993965
? html`
994966
<ul class="rounded-lg border text-sm">
995967
${this.item.resources.map(
@@ -1038,17 +1010,22 @@ export class ArchivedItemDetail extends BtrixElement {
10381010
`;
10391011
}
10401012

1041-
private renderDownloadFiles() {
1042-
if (!this.hasFiles(this.item)) return;
1013+
private renderDownloadFilesButton() {
1014+
if (!hasFiles(this.item)) return;
10431015

10441016
const singleFile = this.item.resources.length === 1;
1045-
const { path, name } = this.itemDownload;
1017+
const download = downloadLink(this.item, this.authState);
10461018

10471019
return html`<sl-tooltip
10481020
content=${msg("Download Files as Multi-WACZ")}
10491021
?disabled=${singleFile}
10501022
>
1051-
<sl-button href=${path} download=${name} size="small" variant="primary">
1023+
<sl-button
1024+
href=${download.path}
1025+
download=${download.name}
1026+
size="small"
1027+
variant="primary"
1028+
>
10521029
<sl-icon slot="prefix" name="cloud-download"></sl-icon>
10531030
${singleFile ? msg("Download File") : msg("Download Files")}
10541031
</sl-button>

frontend/src/pages/org/workflow-detail.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import type { APIPaginatedList, APIPaginationQuery } from "@/types/api";
3737
import { type CrawlState } from "@/types/crawlState";
3838
import { type StorageSeedFile } from "@/types/workflow";
3939
import { isApiError } from "@/utils/api";
40+
import { downloadLink } from "@/utils/crawl-workflows/downloadLink";
4041
import { settingsForDuplicate } from "@/utils/crawl-workflows/settingsForDuplicate";
4142
import {
4243
DEFAULT_MAX_SCALE,
@@ -718,16 +719,7 @@ export class WorkflowDetail extends BtrixElement {
718719
const authToken = this.authState?.headers.Authorization.split(" ")[1];
719720
const disableDownload = this.isRunning;
720721
const disableReplay = !latestCrawl.fileSize;
721-
722-
let path = `/api/orgs/${this.orgId}/all-crawls/${latestCrawlId}/download?auth_bearer=${authToken}`;
723-
let name = `${latestCrawlId}.wacz`;
724-
725-
if (latestCrawl.resources?.length === 1) {
726-
const file = latestCrawl.resources[0];
727-
728-
path = file.path;
729-
name = file.name;
730-
}
722+
const download = downloadLink(latestCrawl, this.authState);
731723

732724
return html`
733725
<btrix-copy-button
@@ -746,8 +738,8 @@ export class WorkflowDetail extends BtrixElement {
746738
<sl-button-group>
747739
<sl-button
748740
size="small"
749-
href=${path}
750-
download=${name}
741+
href=${download.path}
742+
download=${download.name}
751743
?disabled=${disableDownload || disableReplay}
752744
>
753745
<sl-icon name="cloud-download" slot="prefix"></sl-icon>
@@ -766,9 +758,9 @@ export class WorkflowDetail extends BtrixElement {
766758
</sl-button>
767759
<sl-menu>
768760
<btrix-menu-item-link
769-
href=${path}
761+
href=${download.path}
770762
?disabled=${disableDownload || disableReplay}
771-
download=${name}
763+
download=${download.name}
772764
>
773765
<sl-icon name="cloud-download" slot="prefix"></sl-icon>
774766
${msg("Item")}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Auth } from "@/types/auth";
2+
import type { ArchivedItem } from "@/types/crawler";
3+
import { hasFiles } from "@/utils/crawl-workflows/hasFiles";
4+
5+
export function downloadLink(
6+
item?: ArchivedItem,
7+
authState?: Auth | null,
8+
): { path: string; name: string } {
9+
if (!hasFiles(item)) return { path: "", name: "" };
10+
11+
if (item.resources.length > 1) {
12+
return {
13+
path: `/api/orgs/${item.oid}/all-crawls/${item.id}/download?auth_bearer=${authState?.headers.Authorization.split(" ")[1]}`,
14+
name: `${item.id}.wacz`,
15+
};
16+
}
17+
18+
const { path, name } = item.resources[0];
19+
20+
return { path, name };
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { ArchivedItem } from "@/types/crawler";
2+
import type { NonEmptyArray } from "@/types/utils";
3+
4+
export function hasFiles(item?: ArchivedItem): item is ArchivedItem & {
5+
resources: NonEmptyArray<NonNullable<ArchivedItem["resources"]>[number]>;
6+
} {
7+
if (!item) return false;
8+
if (!item.resources) return false;
9+
10+
return Boolean(item.resources[0]);
11+
}

0 commit comments

Comments
 (0)