Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(react): migrate Watermark from Vue to React (#20315)

boojack9691da83b662
diff --git a/frontend/scripts/check-react-layering.mjs b/frontend/scripts/check-react-layering.mjs
index 6f84e3d49f..5d352adeb1 100644
--- a/frontend/scripts/check-react-layering.mjs
+++ b/frontend/scripts/check-react-layering.mjs
@@ -28,6 +28,7 @@ const APPROVED_PREFIXES = [
const APPROVED_FILES = new Set([
"src/react/components/auth/SessionExpiredSurface.tsx",
+ "src/react/components/Watermark.tsx",
]);
const CLASS_ATTR_PATTERN = /\bclass(Name)?\s*=\s*/g;
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 94954d72ab..41c5a36f9f 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -4,7 +4,7 @@
:date-locale="dateLang"
:theme-overrides="themeOverrides"
>
- <Watermark />
+ <ReactPageMount page="Watermark" container-class="contents" />
<NNotificationProvider
:max="MAX_NOTIFICATION_DISPLAY_COUNT"
@@ -39,7 +39,7 @@ import {
watchEffect,
} from "vue";
import { useRoute, useRouter } from "vue-router";
-import Watermark from "@/components/misc/Watermark.vue";
+import ReactPageMount from "@/react/ReactPageMount.vue";
import { dateLang, generalLang, themeOverrides } from "../naive-ui.config";
import AuthContext from "./AuthContext.vue";
import OverlayStackManager from "./components/misc/OverlayStackManager.vue";
diff --git a/frontend/src/components/misc/Watermark.vue b/frontend/src/components/misc/Watermark.vue
deleted file mode 100644
index 3172c1d309..0000000000
--- a/frontend/src/components/misc/Watermark.vue
+++ /dev/null
@@ -1,82 +0,0 @@
-<template>
- <NWatermark
- style="z-index: 10000000"
- :content="version"
- cross
- fullscreen
- :font-size="16"
- :font-color="`rgba(256, 128, 128, .01)`"
- :line-height="16"
- :width="128"
- :height="128"
- :x-offset="24"
- :y-offset="80"
- :rotate="15"
- />
- <NWatermark
- v-for="(line, i) in lines"
- :key="i"
- style="z-index: 10000001"
- :content="line"
- :cross="true"
- :fullscreen="true"
- :font-size="SIZE"
- :font-color="`rgba(128, 128, 128, .1)`"
- :line-height="SIZE"
- :width="GAP"
- :height="GAP"
- :x-offset="0"
- :y-offset="calcYOffset(i)"
- :rotate="-15"
- :debug="false"
- />
-</template>
-
-<script lang="ts" setup>
-import { NWatermark } from "naive-ui";
-import { computed } from "vue";
-import {
- extractUserEmail,
- featureToRef,
- useActuatorV1Store,
- useCurrentUserV1,
- useSettingV1Store,
-} from "@/store";
-import { UNKNOWN_USER_NAME } from "@/types";
-import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";
-
-const GAP = 320;
-const SIZE = 16;
-const PADDING = 6;
-
-const currentUserV1 = useCurrentUserV1();
-const actuatorStore = useActuatorV1Store();
-const settingV1Store = useSettingV1Store();
-const version = computed(
- () => actuatorStore.version + "-" + actuatorStore.gitCommitBE.substring(0, 7)
-);
-const hasWatermarkFeature = featureToRef(PlanFeature.FEATURE_WATERMARK);
-
-const lines = computed(() => {
- const user = currentUserV1.value;
- const uid = extractUserEmail(user.name);
- if (user.name === UNKNOWN_USER_NAME) return [];
- if (!hasWatermarkFeature.value) return [];
- if (!settingV1Store.workspaceProfile.watermark) return [];
-
- const lines: string[] = [];
- lines.push(`${user.title} (${uid})`);
- if (user.email) {
- lines.push(user.email);
- }
-
- return lines;
-});
-
-const calcYOffset = (i: number) => {
- const total = lines.value.length;
- const base = GAP - PADDING;
- const offset = (total - i) * (SIZE * 1.25);
- return base - offset;
-};
-</script>
diff --git a/frontend/src/react/components/Watermark.tsx b/frontend/src/react/components/Watermark.tsx
new file mode 100644
index 0000000000..f0a6bc6c2e
--- /dev/null
+++ b/frontend/src/react/components/Watermark.tsx
@@ -0,0 +1,143 @@
+import type { CSSProperties } from "react";
+import { useMemo } from "react";
+import {
+ useCurrentUser,
+ usePlanFeature,
+ useServerInfo,
+ useWorkspaceProfile,
+} from "@/react/hooks/useAppState";
+import { extractUserEmail } from "@/store/modules/v1/common";
+import { UNKNOWN_USER_NAME } from "@/types";
+import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";
+
+const USER_LAYER_CELL_W = 320;
+const USER_LAYER_CELL_H = 200;
+const USER_LAYER_FONT_SIZE = 16;
+const USER_LAYER_LINE_STEP = 20;
+const USER_LAYER_PADDING = 6;
+const VERSION_LAYER_CELL = 128;
+const VERSION_LAYER_FONT_SIZE = 16;
+
+const baseLayerStyle: CSSProperties = {
+ position: "fixed",
+ inset: 0,
+ pointerEvents: "none",
+ backgroundRepeat: "repeat",
+};
+
+function makeWatermarkDataURL(opts: {
+ content: string;
+ fontSize: number;
+ fontColor: string;
+ rotateDeg: number;
+ cellW: number;
+ cellH: number;
+}): string {
+ const { content, fontSize, fontColor, rotateDeg, cellW, cellH } = opts;
+ if (typeof document === "undefined" || !content) return "";
+ const dpr = window.devicePixelRatio || 1;
+ const canvas = document.createElement("canvas");
+ canvas.width = cellW * dpr;
+ canvas.height = cellH * dpr;
+ const ctx = canvas.getContext("2d");
+ if (!ctx) return "";
+ ctx.scale(dpr, dpr);
+ ctx.font = `${fontSize}px sans-serif`;
+ ctx.fillStyle = fontColor;
+ ctx.textAlign = "center";
+ ctx.textBaseline = "middle";
+ ctx.translate(cellW / 2, cellH / 2);
+ ctx.rotate((rotateDeg * Math.PI) / 180);
+ ctx.fillText(content, 0, 0);
+ return canvas.toDataURL();
+}
+
+export function Watermark() {
+ const currentUser = useCurrentUser();
+ const hasWatermarkFeature = usePlanFeature(PlanFeature.FEATURE_WATERMARK);
+ const serverInfo = useServerInfo();
+ const workspaceProfile = useWorkspaceProfile();
+
+ const version = useMemo(() => {
+ const v = serverInfo?.version ?? "";
+ const commit = (serverInfo?.gitCommit ?? "").substring(0, 7);
+ return `${v}-${commit}`;
+ }, [serverInfo?.version, serverInfo?.gitCommit]);
+
+ const userLines = useMemo(() => {
+ if (
+ !currentUser ||
+ currentUser.name === UNKNOWN_USER_NAME ||
+ !hasWatermarkFeature ||
+ !workspaceProfile?.watermark
+ ) {
+ return [];
+ }
+ const uid = extractUserEmail(currentUser.name);
+ const lines = [`${currentUser.title} (${uid})`];
+ if (currentUser.email) lines.push(currentUser.email);
+ return lines;
+ }, [currentUser, hasWatermarkFeature, workspaceProfile?.watermark]);
+
+ const versionDataURL = useMemo(
+ () =>
+ makeWatermarkDataURL({
+ content: version,
+ fontSize: VERSION_LAYER_FONT_SIZE,
+ fontColor: "rgba(255, 128, 128, 0.01)",
+ rotateDeg: 15,
+ cellW: VERSION_LAYER_CELL,
+ cellH: VERSION_LAYER_CELL,
+ }),
+ [version]
+ );
+
+ const userDataURLs = useMemo(
+ () =>
+ userLines.map((line) =>
+ makeWatermarkDataURL({
+ content: line,
+ fontSize: USER_LAYER_FONT_SIZE,
+ fontColor: "rgba(128, 128, 128, 0.1)",
+ rotateDeg: -15,
+ cellW: USER_LAYER_CELL_W,
+ cellH: USER_LAYER_CELL_H,
+ })
+ ),
+ [userLines]
+ );
+
+ return (
+ <>
+ {versionDataURL && (
+ <div
+ aria-hidden="true"
+ style={{
+ ...baseLayerStyle,
+ zIndex: 10000000,
+ backgroundImage: `url(${versionDataURL})`,
+ backgroundSize: `${VERSION_LAYER_CELL}px ${VERSION_LAYER_CELL}px`,
+ backgroundPosition: "24px 80px",
+ }}
+ />
+ )}
+ {userDataURLs.map((url, i) => {
+ const base = USER_LAYER_CELL_H - USER_LAYER_PADDING;
+ const yOffset = base - (userDataURLs.length - i) * USER_LAYER_LINE_STEP;
+ return (
+ <div
+ key={i}
+ aria-hidden="true"
+ style={{
+ ...baseLayerStyle,
+ zIndex: 10000001,
+ backgroundImage: `url(${url})`,
+ backgroundSize: `${USER_LAYER_CELL_W}px ${USER_LAYER_CELL_H}px`,
+ backgroundPosition: `0px ${yOffset}px`,
+ }}
+ />
+ );
+ })}
+ </>
+ );
+}
diff --git a/frontend/src/shell-bridge.test.ts b/frontend/src/shell-bridge.test.ts
index eeb9ccc30f..f52fec9ad6 100644
--- a/frontend/src/shell-bridge.test.ts
+++ b/frontend/src/shell-bridge.test.ts
@@ -58,11 +58,12 @@ vi.mock("../naive-ui.config", () => ({
themeOverrides: {},
}));
-vi.mock("@/components/misc/Watermark.vue", async () => {
+vi.mock("@/react/ReactPageMount.vue", async () => {
const { defineComponent, h } = await import("vue");
return {
default: defineComponent({
- name: "MockWatermark",
+ name: "MockReactPageMount",
+ props: ["page", "pageProps", "containerClass"],
setup() {
return () => h("div");
},