Repository navigation
feat(web): save files from chat and the file panel to your machine #33827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xtlt
wants to merge
4
commits into
pingdotgg:main
Choose a base branch
from
0xtlt:feat/save-file-as
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−4
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d49d8b3
feat(web): save files from chat and the file panel to your machine
0xtlt 28c9293
fix(web): keep the save-file hook compilable and cover Save in chat p…
0xtlt 2a7e225
Merge remote-tracking branch 'github/main' into pr-33827
maria-rcks 2437902
fix(web): hide Save file for directory links
maria-rcks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets"; | ||
| import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; | ||
| import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; | ||
|
|
||
| import { assetEnvironment } from "../../state/assets"; | ||
| import { readPreparedConnection } from "../../state/session"; | ||
| import { useAtomQueryRunner } from "../../state/use-atom-query-runner"; | ||
| import { downloadMedia } from "../media/mediaContent"; | ||
| import { stackedThreadToast, toastManager } from "../ui/toast"; | ||
|
|
||
| interface SaveFileInput { | ||
| readonly environmentId: EnvironmentId; | ||
| readonly threadId: ThreadId; | ||
| /** Absolute host path, or a lookup that finds it. */ | ||
| readonly path: string | (() => Promise<string>); | ||
| } | ||
|
|
||
| async function downloadHostFile( | ||
| createAssetUrl: ReturnType<typeof useCreateAssetUrl>, | ||
| input: SaveFileInput, | ||
| ) { | ||
| const path = typeof input.path === "string" ? input.path : await input.path(); | ||
| const connection = readPreparedConnection(input.environmentId); | ||
| if (!connection) throw new Error("Reconnect to this environment and try again."); | ||
| const result = await createAssetUrl({ | ||
| environmentId: input.environmentId, | ||
| input: { | ||
| resource: { _tag: "media-file", threadId: input.threadId, path, disposition: "attachment" }, | ||
| }, | ||
| }); | ||
| if (result._tag === "Failure") throw squashAtomCommandFailure(result); | ||
| const url = resolveAssetUrl(connection.httpBaseUrl, result.value.relativeUrl); | ||
| if (!url) throw new Error("The environment returned an invalid file URL."); | ||
| await downloadMedia(url, path.split(/[\\/]/).at(-1) || path); | ||
| } | ||
|
|
||
| function useCreateAssetUrl() { | ||
| return useAtomQueryRunner(assetEnvironment.createUrl, { reportFailure: false, refresh: true }); | ||
| } | ||
|
|
||
| /** | ||
| * Copies a host file to the viewing device, wherever the environment runs. | ||
| * Desktop asks where to save it; browsers follow their download settings. | ||
| */ | ||
| export function useSaveFileAs() { | ||
| const createAssetUrl = useCreateAssetUrl(); | ||
| return (input: SaveFileInput) => { | ||
| const toast = toastManager.add({ type: "loading", title: "Preparing file…" }); | ||
| return downloadHostFile(createAssetUrl, input).then( | ||
| () => toastManager.update(toast, { type: "success", title: "Download started" }), | ||
| (error: unknown) => | ||
| toastManager.update( | ||
| toast, | ||
| stackedThreadToast({ | ||
| type: "error", | ||
| title: "Could not save file", | ||
| description: error instanceof Error ? error.message : "An error occurred.", | ||
| }), | ||
| ), | ||
| ); | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.