Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(share): label a standalone share as a preview
Elian Doran17f4619f08f9
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 2bb525dd1d..c64bc117db 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2506,7 +2506,8 @@"shared_info": {"shared_publicly": "This note is shared publicly on {{- link}}.","shared_locally": "This note is shared locally on {{- link}}.",- "help_link": "For help visit <a href=\"https://triliumnext.github.io/Docs/Wiki/sharing.html\">wiki</a>."+ "help_link": "For help visit <a href=\"https://triliumnext.github.io/Docs/Wiki/sharing.html\">wiki</a>.",+ "shared_preview": "This note is shared on {{- link}}, but only this browser can open that page, and only while Trilium is open in it. Publishing it for others needs a Trilium server."},"read-only-info": {"read-only-note": "Currently viewing a read-only note.",@@ -3755,7 +3756,9 @@"save_status_error": "Save failed","save_status_saving_tooltip": "Changes are being saved.","save_status_unsaved_tooltip": "There are unsaved changes. They will be saved automatically in a moment.",- "save_status_error_tooltip": "An error occurred while saving the note. If possible, try copying the note content elsewhere and reloading the application."+ "save_status_error_tooltip": "An error occurred while saving the note. If possible, try copying the note content elsewhere and reloading the application.",+ "shared_preview": "Share preview",+ "shared_preview_description": "Only this browser can open the shared page, and only while Trilium is open in it. Publishing it for others needs a Trilium server."},"status_bar": {"language_title": "Change content language",diff --git a/apps/client/src/widgets/layout/NoteBadges.tsx b/apps/client/src/widgets/layout/NoteBadges.tsxindex 78e21eccac..ada8c0d3e1 100644--- a/apps/client/src/widgets/layout/NoteBadges.tsx+++ b/apps/client/src/widgets/layout/NoteBadges.tsx@@ -11,7 +11,7 @@ import { Badge, BadgeWithDropdown } from "../react/Badge";import { FormDropdownDivider, FormListItem } from "../react/FormList";import { useGetContextDataFrom, useIsNoteReadOnly, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteProperty } from "../react/hooks";import { useShareState } from "../ribbon/BasicPropertiesTab";-import { useShareInfo } from "../shared_info";+import { type ShareScope, useShareInfo } from "../shared_info";import { ActiveContentBadges } from "./ActiveContentBadges";import { SnippetBadge } from "./SnippetBadge";@@ -82,12 +82,13 @@ export function OfficePreviewBadge() {function ShareBadge() {const { note } = useNoteContext();const [ , switchShareState ] = useShareState(note);- const { isSharedExternally, linkHref } = useShareInfo(note);+ const { scope, linkHref } = useShareInfo(note);return (linkHref &&<BadgeWithDropdown- icon={isSharedExternally ? "bx bx-world" : "bx bx-share-alt"}- text={isSharedExternally ? t("breadcrumb_badges.shared_publicly") : t("breadcrumb_badges.shared_locally")}+ icon={SHARE_BADGES[scope].icon}+ text={t(SHARE_BADGES[scope].text)}+ tooltip={scope === "preview" ? t("breadcrumb_badges.shared_preview_description") : undefined}className="share-badge"><FormListItem@@ -107,6 +108,12 @@ function ShareBadge() {);}+const SHARE_BADGES: Record<ShareScope, { icon: string; text: string }> = {+ public: { icon: "bx bx-world", text: "breadcrumb_badges.shared_publicly" },+ local: { icon: "bx bx-share-alt", text: "breadcrumb_badges.shared_locally" },+ preview: { icon: "bx bx-show", text: "breadcrumb_badges.shared_preview" }+};+function ClippedNoteBadge() {const { note } = useNoteContext();const isHelpNote = !!note?.noteId.startsWith("_help");diff --git a/apps/client/src/widgets/shared_info.spec.tsx b/apps/client/src/widgets/shared_info.spec.tsxindex 89d354cfdc..7eff6af18e 100644--- a/apps/client/src/widgets/shared_info.spec.tsx+++ b/apps/client/src/widgets/shared_info.spec.tsx@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { buildShareLinkHtml } from "./shared_info";+import { buildShareLinkHtml, getShareScope } from "./shared_info";describe("buildShareLinkHtml", () => {it("keeps the whole link in the anchor's href and text, whatever characters it carries", () => {@@ -13,6 +13,18 @@ describe("buildShareLinkHtml", () => {});});+describe("getShareScope", () => {+ it("tells a public share from a local one and from a standalone preview", () => {+ const syncServerHost = "https://sync.example.com";++ expect(getShareScope({ isElectron: false, isStandalone: false, syncServerHost: "" })).toBe("public");+ expect(getShareScope({ isElectron: true, isStandalone: false, syncServerHost: "" })).toBe("local");+ expect(getShareScope({ isElectron: true, isStandalone: false, syncServerHost })).toBe("public");+ expect(getShareScope({ isElectron: false, isStandalone: true, syncServerHost: "" })).toBe("preview");+ expect(getShareScope({ isElectron: false, isStandalone: true, syncServerHost })).toBe("public");+ });+});+function parseAnchor(html: string) {const container = document.createElement("div");container.innerHTML = html;diff --git a/apps/client/src/widgets/shared_info.tsx b/apps/client/src/widgets/shared_info.tsxindex ef05b578eb..209925cc92 100644--- a/apps/client/src/widgets/shared_info.tsx+++ b/apps/client/src/widgets/shared_info.tsx@@ -6,7 +6,7 @@ import FNote from "../entities/fnote";import attributes from "../services/attributes";import { t } from "../services/i18n";import { buildShareLink } from "../services/share_link";-import { escapeHtml, isElectron } from "../services/utils";+import { escapeHtml, isElectron, isStandalone } from "../services/utils";import HelpButton from "./react/HelpButton";import { useNoteContext, useTriliumEvent, useTriliumOption } from "./react/hooks";import InfoBar from "./react/InfoBar";@@ -14,15 +14,11 @@ import RawHtml from "./react/RawHtml";export default function SharedInfo() {const { note } = useNoteContext();- const { isSharedExternally, link } = useShareInfo(note);+ const { scope, link } = useShareInfo(note);return (<InfoBar className="shared-info-widget" type="subtle" style={{display: (!link) ? "none" : undefined}}>- {link && (- <RawHtml html={isSharedExternally- ? t("shared_info.shared_publicly", { link })- : t("shared_info.shared_locally", { link })} />- )}+ {link && <RawHtml html={t(SCOPE_MESSAGES[scope], { link })} />}<HelpButton helpPage="R9pX4DGra2Vt" style={{ width: "24px", height: "24px" }} /></InfoBar>);@@ -59,10 +55,40 @@ export function useShareInfo(note: FNote | null | undefined) {return {link,linkHref,- isSharedExternally: !isElectron() || !!syncServerHost // on server we can't reliably detect if the note is shared locally or available publicly.+ scope: getShareScope({ isElectron: isElectron(), isStandalone: !!isStandalone, syncServerHost })};}+/**+ * Where a shared note can be opened from. `local` is the desktop app without a sync server, which+ * serves the page on the loopback address. `preview` is the standalone build without a sync server,+ * which renders the page in its own service worker, so only this browser can open it. A server+ * cannot tell whether it is reachable from outside, so it always reports `public`.+ */+export function getShareScope({ isElectron, isStandalone, syncServerHost }: {+ isElectron: boolean;+ isStandalone: boolean;+ syncServerHost: string | null | undefined;+}): ShareScope {+ if (syncServerHost) {+ return "public";+ }++ if (isStandalone) {+ return "preview";+ }++ return isElectron ? "local" : "public";+}++export type ShareScope = "public" | "local" | "preview";++const SCOPE_MESSAGES: Record<ShareScope, string> = {+ public: "shared_info.shared_publicly",+ local: "shared_info.shared_locally",+ preview: "shared_info.shared_preview"+};+export function buildShareLinkHtml(link: string) {const escapedLink = escapeHtml(link);