Skip to content

Commit

Permalink
feat: Add interstitial to review shinylive app before saving from URI
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Aug 23, 2024
1 parent 506e1b4 commit 8eafc52
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
68 changes: 64 additions & 4 deletions src/extension-onUri.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import * as vscode from "vscode";
import { URLSearchParams } from "url";
import { shinyliveSaveAppFromUrl } from "./shinylive";
import {
shinyliveSaveAppFromUrl,
shinyliveUrlDecode,
shinyliveUrlEncode,
} from "./shinylive";

export function handlePositShinyUri(uri: vscode.Uri): void {
export async function handlePositShinyUri(uri: vscode.Uri): Promise<void> {
if (!["/shinylive", "/shinylive/"].includes(uri.path)) {
console.warn(`[shiny] Unexpected URI: ${uri.toString()}`);
return;
Expand All @@ -17,6 +21,62 @@ export function handlePositShinyUri(uri: vscode.Uri): void {
return;
}

const decodedUrl = decodeURIComponent(encodedUrl);
shinyliveSaveAppFromUrl(decodedUrl);
const url = decodeURIComponent(encodedUrl);
const bundle = shinyliveUrlDecode(url);

if (!bundle) {
vscode.window.showErrorMessage(
"Shinylive: Failed to parse the Shinylive link. " +
"Please check the link and try again."
);
return;
}

let filesText = bundle.files
.slice(0, 3)
.map((f) => f.name)
.join(", ");
if (bundle.files.length > 3) {
filesText += `, and ${bundle.files.length - 3} more files`;
}

const reviewAction = await vscode.window.showWarningMessage(
`You are about to save a Shinylive app with ${filesText} to your workspace. Would you like to...`,
{ modal: true },
{ title: "Cancel", action: "cancel" },
{ title: "Review the app on shinylive.io", action: "review" },
{
title: `Save app ${bundle.files.length === 1 ? "file" : "files"} locally`,
action: "save",
}
);

let { action } = reviewAction || { action: "cancel" };

if (action === "cancel") {
return;
}

if (action === "review") {
bundle.mode = "editor";
const editorUrl = shinyliveUrlEncode(bundle);
vscode.env.openExternal(vscode.Uri.parse(editorUrl));

const openAfterReview = await vscode.window.showInformationMessage(
"After reviewing the Shinylive app, would you like to save it?",
{ modal: true },
"No",
"Yes"
);

if (!openAfterReview || openAfterReview === "No") {
return;
}
action = "save";
}

if (action === "save") {
await shinyliveSaveAppFromUrl(url);
return;
}
}
4 changes: 2 additions & 2 deletions src/shinylive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ async function askUserForOutputLocation(
* with the language, files, and mode to encode.
* @returns {string} The encoded Shinylive URL.
*/
function shinyliveUrlEncode({ language, files, mode }: ShinyliveBundle) {
export function shinyliveUrlEncode({ language, files, mode }: ShinyliveBundle) {
const filesJson = JSON.stringify(files);
const filesLZ = lzstring.compressToEncodedURIComponent(filesJson);

Expand All @@ -531,7 +531,7 @@ function shinyliveUrlEncode({ language, files, mode }: ShinyliveBundle) {
* @returns {ShinyliveBundle | undefined} The decoded Shinylive bundle, or
* `undefined` if the URL could not be decoded.
*/
function shinyliveUrlDecode(url: string): ShinyliveBundle | undefined {
export function shinyliveUrlDecode(url: string): ShinyliveBundle | undefined {
const { hash, pathname } = new URL(url);
const { searchParams } = new URL(
"https://shinylive.io/?" + hash.substring(1)
Expand Down

0 comments on commit 8eafc52

Please sign in to comment.