(input: {
readonly threadRef: ScopedThreadRef;
readonly filePath: string;
readonly httpBaseUrl: string;
readonly createAssetUrl: (input: {
readonly environmentId: EnvironmentId;
readonly input: { readonly resource: AssetResource };
}) => Promise<AtomCommandResult<AssetCreateUrlResult, AssetError>>;
readonly openPreview: OpenPreviewMutation<PreviewError>;
})
| 53 | } |
| 54 | |
| 55 | export async function openFileInPreview<AssetError, PreviewError>(input: { |
| 56 | readonly threadRef: ScopedThreadRef; |
| 57 | readonly filePath: string; |
| 58 | readonly httpBaseUrl: string; |
| 59 | readonly createAssetUrl: (input: { |
| 60 | readonly environmentId: EnvironmentId; |
| 61 | readonly input: { readonly resource: AssetResource }; |
| 62 | }) => Promise<AtomCommandResult<AssetCreateUrlResult, AssetError>>; |
| 63 | readonly openPreview: OpenPreviewMutation<PreviewError>; |
| 64 | }): Promise<AtomCommandResult<void, AssetError | PreviewError | BrowserPreviewUnavailableError>> { |
| 65 | if (!isPreviewSupportedInRuntime()) { |
| 66 | return AsyncResult.failure( |
| 67 | Cause.fail( |
| 68 | new BrowserPreviewUnavailableError({ |
| 69 | message: "The integrated browser is unavailable in this runtime.", |
| 70 | }), |
| 71 | ), |
| 72 | ); |
| 73 | } |
| 74 | const assetResult = await input.createAssetUrl({ |
| 75 | environmentId: input.threadRef.environmentId, |
| 76 | input: { |
| 77 | resource: { |
| 78 | _tag: "workspace-file", |
| 79 | threadId: input.threadRef.threadId, |
| 80 | path: input.filePath, |
| 81 | }, |
| 82 | }, |
| 83 | }); |
| 84 | if (assetResult._tag === "Failure") { |
| 85 | return AsyncResult.failure(assetResult.cause); |
| 86 | } |
| 87 | const assetUrl = resolveAssetUrl(input.httpBaseUrl, assetResult.value.relativeUrl); |
| 88 | if (assetUrl === null) { |
| 89 | return AsyncResult.failure( |
| 90 | Cause.die(new Error("The environment returned an invalid asset URL.")), |
| 91 | ); |
| 92 | } |
| 93 | return openUrlInPreview({ |
| 94 | threadRef: input.threadRef, |
| 95 | url: assetUrl, |
| 96 | openPreview: input.openPreview, |
| 97 | }); |
| 98 | } |
no test coverage detected