From 604ab8710b1b72c54d7d3a392faabcaf22951004 Mon Sep 17 00:00:00 2001 From: lassecapel Date: Sun, 17 Nov 2024 20:11:51 +0100 Subject: [PATCH 1/3] feat: add custom unique filename when doanload as zip --- app/lib/stores/workbench.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/lib/stores/workbench.ts b/app/lib/stores/workbench.ts index 4db14e7b..9f0401d7 100644 --- a/app/lib/stores/workbench.ts +++ b/app/lib/stores/workbench.ts @@ -15,6 +15,7 @@ import { Octokit, type RestEndpointMethodTypes } from "@octokit/rest"; import * as nodePath from 'node:path'; import type { WebContainerProcess } from '@webcontainer/api'; import { extractRelativePath } from '~/utils/diff'; +import { description } from '../persistence'; export interface ArtifactState { id: string; @@ -171,6 +172,7 @@ export class WorkbenchStore { this.#editorStore.setSelectedFile(filePath); } + async saveFile(filePath: string) { const documents = this.#editorStore.documents.get(); const document = documents[filePath]; @@ -325,6 +327,15 @@ export class WorkbenchStore { async downloadZip() { const zip = new JSZip(); const files = this.files.get(); + // Get the project name (assuming it's stored in this.projectName) + const projectName = (description.value ?? 'project').toLocaleLowerCase().split(' ').join('_'); + + // Generate a simple 6-character hash based on the current timestamp + const timestampHash = Date.now().toString(36).slice(-6); + const uniqueProjectName = `${projectName}_${timestampHash}`; + + // Prompt the user for a file name, prefilled with the project name + const fileName = prompt('Enter the file name', `${uniqueProjectName}.zip`); for (const [filePath, dirent] of Object.entries(files)) { if (dirent?.type === 'file' && !dirent.isBinary) { @@ -348,8 +359,14 @@ export class WorkbenchStore { } } + + + + if (fileName) { + // Generate the zip file and save it const content = await zip.generateAsync({ type: 'blob' }); - saveAs(content, 'project.zip'); + saveAs(content, fileName); + } } async syncFiles(targetHandle: FileSystemDirectoryHandle) { From 399affd1e9e0cd113c45cabcdebb8db11a5434b3 Mon Sep 17 00:00:00 2001 From: lassecapel Date: Sun, 17 Nov 2024 20:24:33 +0100 Subject: [PATCH 2/3] update comment to reflect the the codeline --- app/lib/stores/workbench.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/stores/workbench.ts b/app/lib/stores/workbench.ts index 9f0401d7..dbdd6892 100644 --- a/app/lib/stores/workbench.ts +++ b/app/lib/stores/workbench.ts @@ -327,7 +327,7 @@ export class WorkbenchStore { async downloadZip() { const zip = new JSZip(); const files = this.files.get(); - // Get the project name (assuming it's stored in this.projectName) + // Get the project name from the description input, or use a default name const projectName = (description.value ?? 'project').toLocaleLowerCase().split(' ').join('_'); // Generate a simple 6-character hash based on the current timestamp From 8978ed0ff34c4d164ab67ad25f03951c8d5eaf5f Mon Sep 17 00:00:00 2001 From: Lasse Capel Date: Wed, 20 Nov 2024 09:54:31 +0100 Subject: [PATCH 3/3] use a descriptive anique filename when downloading the files to zip --- app/lib/stores/workbench.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/app/lib/stores/workbench.ts b/app/lib/stores/workbench.ts index dbdd6892..7eed952a 100644 --- a/app/lib/stores/workbench.ts +++ b/app/lib/stores/workbench.ts @@ -334,9 +334,6 @@ export class WorkbenchStore { const timestampHash = Date.now().toString(36).slice(-6); const uniqueProjectName = `${projectName}_${timestampHash}`; - // Prompt the user for a file name, prefilled with the project name - const fileName = prompt('Enter the file name', `${uniqueProjectName}.zip`); - for (const [filePath, dirent] of Object.entries(files)) { if (dirent?.type === 'file' && !dirent.isBinary) { const relativePath = extractRelativePath(filePath); @@ -358,15 +355,10 @@ export class WorkbenchStore { } } } - - - - - if (fileName) { // Generate the zip file and save it const content = await zip.generateAsync({ type: 'blob' }); - saveAs(content, fileName); - } + saveAs(content, `${uniqueProjectName}.zip`); + } async syncFiles(targetHandle: FileSystemDirectoryHandle) {