Skip to content

Opening HelpView pages in external browser uses the public docsite equivalent #1289

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

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions frontend/app/view/helpview/helpview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import { atom, useAtomValue } from "jotai";
import { useCallback } from "react";
import "./helpview.less";

const docsiteWebUrl = "https://docs.waveterm.dev/";
const baseUrlRegex = /http[s]?:\/\/([^:\/])+(:\d+)?/;

class HelpViewModel extends WebViewModel {
constructor(blockId: string, nodeModel: BlockNodeModel) {
super(blockId, nodeModel);
this.getSettingsMenuItems = undefined;
this.viewText = atom((get) => {
// force a dependency on meta.url so we re-render the buttons when the url changes
let url = get(this.blockAtom)?.meta?.url || get(this.homepageUrl);
get(this.blockAtom)?.meta?.url || get(this.homepageUrl);
return [
{
elemtype: "iconbutton",
Expand All @@ -41,15 +44,26 @@ class HelpViewModel extends WebViewModel {
this.viewType = "help";
this.viewIcon = atom("circle-question");
this.viewName = atom("Help");

/*
Add callback to take the current embedded docsite url and return the equivalent page in the public docsite.
The port used by the embedded docsite changes every time the app runs and the current page may be cached from a previous run so we can't trust that it matches the current embedded url.
We have a regex at the top of this file that can extract the base part of the url (i.e. http://127.0.0.1:53288). We'll use this regex to strip the base part of the url from both the current
page and the embedded docsite url. Because we host the embedded docsite at a subdirectory, we also need to strip that (hence the second replace). Then, we can build the public url from whatever's left.
*/
this.modifyExternalUrl = (url: string) => {
const strippedDocsiteUrl = getApi().getDocsiteUrl().replace(baseUrlRegex, "");
const strippedCurUrl = url.replace(baseUrlRegex, "").replace(strippedDocsiteUrl, "");
const newUrl = docsiteWebUrl + strippedCurUrl;
return newUrl;
};
}
}

function makeHelpViewModel(blockId: string, nodeModel: BlockNodeModel) {
return new HelpViewModel(blockId, nodeModel);
}

const baseUrlRegex = /http[s]?:\/\/([^:\/])+(:\d+)?/;

function HelpView({ model }: { model: HelpViewModel }) {
const homepageUrl = useAtomValue(model.homepageUrl);

Expand Down
4 changes: 3 additions & 1 deletion frontend/app/view/webview/webview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class WebViewModel implements ViewModel {
endIconButtons?: Atom<IconButtonDecl[]>;
mediaPlaying: PrimitiveAtom<boolean>;
mediaMuted: PrimitiveAtom<boolean>;
modifyExternalUrl?: (url: string) => string;

constructor(blockId: string, nodeModel: BlockNodeModel) {
this.nodeModel = nodeModel;
Expand Down Expand Up @@ -142,7 +143,8 @@ export class WebViewModel implements ViewModel {
title: "Open in External Browser",
click: () => {
if (url != null && url != "") {
return getApi().openExternal(url);
const externalUrl = this.modifyExternalUrl?.(url) ?? url;
return getApi().openExternal(externalUrl);
}
},
},
Expand Down
Loading