From 5ad8f6c2ce86081a1e0ff0436c02cb5ead98bfed Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Tue, 19 Dec 2023 11:26:37 +0530 Subject: [PATCH] chore: move window.open to platform io to handle desktop app --- .../src/components/collections/graphql/ImportExport.vue | 2 +- .../src/components/environments/ImportExport.vue | 2 +- packages/hoppscotch-common/src/platform/io.ts | 7 +++++++ packages/hoppscotch-common/src/platform/std/io.ts | 6 ++++++ .../src/services/spotlight/searchers/general.searcher.ts | 3 ++- packages/hoppscotch-selfhost-desktop/src/platform/io.ts | 4 ++++ 6 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue b/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue index db8123d94..45e887c26 100644 --- a/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue +++ b/packages/hoppscotch-common/src/components/collections/graphql/ImportExport.vue @@ -199,7 +199,7 @@ const GqlCollectionsGistExporter: ImporterOrExporter = { exporter: "gist", }) - window.open(res.right, "_blank") + platform.io.openExternalLink(res.right) } }, } diff --git a/packages/hoppscotch-common/src/components/environments/ImportExport.vue b/packages/hoppscotch-common/src/components/environments/ImportExport.vue index 3bbe7d32a..88e1e3a38 100644 --- a/packages/hoppscotch-common/src/components/environments/ImportExport.vue +++ b/packages/hoppscotch-common/src/components/environments/ImportExport.vue @@ -297,7 +297,7 @@ const HoppEnvironmentsGistExporter: ImporterOrExporter = { platform: "rest", }) - window.open(res.right, "_blank") + platform.io.openExternalLink(res.right) } }, } diff --git a/packages/hoppscotch-common/src/platform/io.ts b/packages/hoppscotch-common/src/platform/io.ts index 97d32a7a5..ca8791ab7 100644 --- a/packages/hoppscotch-common/src/platform/io.ts +++ b/packages/hoppscotch-common/src/platform/io.ts @@ -81,4 +81,11 @@ export type IOPlatformDef = { saveFileWithDialog: ( opts: SaveFileWithDialogOptions ) => Promise + + /** + * Opens a link in the user's browser. + * The expected behaviour is for the browser to open a new tab/window (for example in desktop app) with the given URL. + * @param url The URL to open + */ + openExternalLink: (url: string) => Promise } diff --git a/packages/hoppscotch-common/src/platform/std/io.ts b/packages/hoppscotch-common/src/platform/std/io.ts index 568a7ecbf..b8fd50ffc 100644 --- a/packages/hoppscotch-common/src/platform/std/io.ts +++ b/packages/hoppscotch-common/src/platform/std/io.ts @@ -34,4 +34,10 @@ export const browserIODef: IOPlatformDef = { // Browsers provide no way for us to know the save went successfully. return Promise.resolve({ type: "unknown" }) }, + openExternalLink(url) { + window.open(url, "_blank") + + // Browsers provide no way for us to know the open went successfully. + return Promise.resolve() + }, } diff --git a/packages/hoppscotch-common/src/services/spotlight/searchers/general.searcher.ts b/packages/hoppscotch-common/src/services/spotlight/searchers/general.searcher.ts index cfa3fbf37..563d9a2f9 100644 --- a/packages/hoppscotch-common/src/services/spotlight/searchers/general.searcher.ts +++ b/packages/hoppscotch-common/src/services/spotlight/searchers/general.searcher.ts @@ -14,6 +14,7 @@ import IconGitHub from "~icons/lucide/github" import IconBook from "~icons/lucide/book" import IconLifeBuoy from "~icons/lucide/life-buoy" import IconZap from "~icons/lucide/zap" +import { platform } from "~/platform" type Doc = { text: string | string[] @@ -113,7 +114,7 @@ export class GeneralSpotlightSearcherService extends StaticSpotlightSearcherServ } private openURL(url: string) { - window.open(url, "_blank") + platform.io.openExternalLink(url) } public onDocSelected(id: string): void { diff --git a/packages/hoppscotch-selfhost-desktop/src/platform/io.ts b/packages/hoppscotch-selfhost-desktop/src/platform/io.ts index 0d4b9d2b8..1488e1924 100644 --- a/packages/hoppscotch-selfhost-desktop/src/platform/io.ts +++ b/packages/hoppscotch-selfhost-desktop/src/platform/io.ts @@ -1,5 +1,6 @@ import { IOPlatformDef } from "@hoppscotch/common/platform/io" import { save } from "@tauri-apps/api/dialog" +import { open } from "@tauri-apps/api/shell" import { writeBinaryFile, writeTextFile } from "@tauri-apps/api/fs" export const ioDef: IOPlatformDef = { @@ -21,4 +22,7 @@ export const ioDef: IOPlatformDef = { return { type: "saved", path } }, + openExternalLink(url) { + return open(url) + }, }