Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(review): activate the site toolbar with the parameter the SDK reads (#21742)

Inneie5f3feb62a40
diff --git a/locales/en-US/project.json b/locales/en-US/project.json
index ed301b61ab8..46a6c147a6a 100644
--- a/locales/en-US/project.json
+++ b/locales/en-US/project.json
@@ -214,8 +214,11 @@
"review.role.admin": "Administrator",
"review.role.member": "Can edit",
"review.role.viewer": "Read-only",
+ "review.sites.activation": "Activation link",
+ "review.sites.activationHint": "The script alone does not show the toolbar: a page activates it only when its URL carries the {{param}} parameter. Open this link, or append the parameter to any page on the site.",
"review.sites.add": "Add site",
"review.sites.addOrigin": "Add origin",
+ "review.sites.copyActivation": "Copy activation link",
"review.sites.copySnippet": "Copy script",
"review.sites.create": "Create site",
"review.sites.delete": "Delete",
diff --git a/locales/zh-CN/project.json b/locales/zh-CN/project.json
index 9eb30ce6d9b..baa81a95e26 100644
--- a/locales/zh-CN/project.json
+++ b/locales/zh-CN/project.json
@@ -214,8 +214,11 @@
"review.role.admin": "管理员",
"review.role.member": "可编辑",
"review.role.viewer": "只读",
+ "review.sites.activation": "激活链接",
+ "review.sites.activationHint": "只有页面 URL 带 {{param}} 参数时才会出现工具栏,光装脚本不会显示。可直接打开下面的链接,或把这个参数加到站点的任意页面。",
"review.sites.add": "添加站点",
"review.sites.addOrigin": "添加来源",
+ "review.sites.copyActivation": "复制激活链接",
"review.sites.copySnippet": "复制脚本",
"review.sites.create": "创建站点",
"review.sites.delete": "删除",
diff --git a/packages/const/src/acceptanceReview.ts b/packages/const/src/acceptanceReview.ts
index 0e49dcb73ff..eb90fbdb413 100644
--- a/packages/const/src/acceptanceReview.ts
+++ b/packages/const/src/acceptanceReview.ts
@@ -1,5 +1,12 @@
export const ACCEPTANCE_REVIEW_CLIENT_ID = 'lobehub-review';
+/**
+ * URL parameter that activates the review toolbar (`@lobehub/review-sdk`). A
+ * page mounts the toolbar only when its URL carries this parameter; the SDK
+ * persists the value in sessionStorage and strips the parameter afterwards.
+ */
+export const ACCEPTANCE_REVIEW_ACTIVATION_PARAM = 'lh-acceptance';
+
export const ACCEPTANCE_REVIEW_SCOPE = 'acceptance:review';
export const ACCEPTANCE_REVIEW_RESOURCE_PREFIX = 'urn:lobehub:acceptance:';
diff --git a/packages/locales/src/default/project.ts b/packages/locales/src/default/project.ts
index a3f82f938c9..e31d4828d72 100644
--- a/packages/locales/src/default/project.ts
+++ b/packages/locales/src/default/project.ts
@@ -273,8 +273,12 @@ export default {
'review.origin.path': 'Use the origin only, without a path, query or hash.',
'review.origin.sharedHostWildcard':
'This is a shared host where anyone can deploy, so * isn’t allowed. Add the exact address instead.',
+ 'review.sites.activation': 'Activation link',
+ 'review.sites.activationHint':
+ 'The script alone does not show the toolbar: a page activates it only when its URL carries the {{param}} parameter. Open this link, or append the parameter to any page on the site.',
'review.sites.add': 'Add site',
'review.sites.addOrigin': 'Add origin',
+ 'review.sites.copyActivation': 'Copy activation link',
'review.sites.copySnippet': 'Copy script',
'review.sites.create': 'Create site',
'review.sites.delete': 'Delete',
diff --git a/src/features/Acceptance/Viewer/Header/previewReview.ts b/src/features/Acceptance/Viewer/Header/previewReview.ts
index 75779349479..08d4cc3da45 100644
--- a/src/features/Acceptance/Viewer/Header/previewReview.ts
+++ b/src/features/Acceptance/Viewer/Header/previewReview.ts
@@ -1,4 +1,4 @@
-const ACTIVATION_PARAM = 'lh-acceptance';
+import { ACCEPTANCE_REVIEW_ACTIVATION_PARAM } from '@lobechat/const';
interface RoundLike {
run: { metadata?: unknown };
@@ -10,7 +10,7 @@ export const previewReviewUrl = (acceptanceId: string, rounds: RoundLike[]) => {
try {
const url = new URL(metadata.previewUrl);
if (url.protocol !== 'https:' && url.protocol !== 'http:') return null;
- url.searchParams.set(ACTIVATION_PARAM, acceptanceId);
+ url.searchParams.set(ACCEPTANCE_REVIEW_ACTIVATION_PARAM, acceptanceId);
return url.toString();
} catch {
return null;
diff --git a/src/features/Projects/Review/SiteCard.tsx b/src/features/Projects/Review/SiteCard.tsx
index e457d8074aa..b0a14a5894e 100644
--- a/src/features/Projects/Review/SiteCard.tsx
+++ b/src/features/Projects/Review/SiteCard.tsx
@@ -1,4 +1,4 @@
-import { isDesktop, OFFICIAL_URL } from '@lobechat/const';
+import { ACCEPTANCE_REVIEW_ACTIVATION_PARAM, isDesktop, OFFICIAL_URL } from '@lobechat/const';
import {
ActionIcon,
Block,
@@ -47,7 +47,7 @@ const reviewServer = () => (isDesktop ? OFFICIAL_URL : window.location.origin);
export function SiteCard({ onChanged, site }: { onChanged: () => void; site: ProjectSiteItem }) {
const { t } = useTranslation('project');
const [deleting, setDeleting] = useState(false);
- const link = reviewLink(site.origins);
+ const link = reviewLink(site.origins, site.acceptanceId);
const snippet = siteSnippet(site.id, reviewServer());
const remove = () =>
@@ -127,6 +127,20 @@ export function SiteCard({ onChanged, site }: { onChanged: () => void; site: Pro
<code {...stylex.props(styles.snippet)}>{snippet}</code>
<CopyButton content={snippet} size="small" title={t('review.sites.copySnippet')} />
</Block>
+ {link ? (
+ <Flexbox gap={6}>
+ <Text fontSize={12} weight={500}>
+ {t('review.sites.activation')}
+ </Text>
+ <Text fontSize={12} type="secondary">
+ {t('review.sites.activationHint', { param: ACCEPTANCE_REVIEW_ACTIVATION_PARAM })}
+ </Text>
+ <Block horizontal align="flex-start" gap={10} padding={12} variant="filled">
+ <code {...stylex.props(styles.snippet)}>{link}</code>
+ <CopyButton content={link} size="small" title={t('review.sites.copyActivation')} />
+ </Block>
+ </Flexbox>
+ ) : null}
{site.acceptanceId ? (
<Flexbox horizontal justify="flex-end">
<WorkspaceLink to={`/acceptance/${site.acceptanceId}`}>
diff --git a/src/features/Projects/Review/siteSnippet.test.ts b/src/features/Projects/Review/siteSnippet.test.ts
index c703d7acfda..1d4a0464523 100644
--- a/src/features/Projects/Review/siteSnippet.test.ts
+++ b/src/features/Projects/Review/siteSnippet.test.ts
@@ -23,14 +23,30 @@ describe('siteSnippet', () => {
});
describe('reviewLink', () => {
- it('opens the first exact origin with the wake parameter', () => {
- expect(reviewLink(['https://*.fieldnotes.app', 'https://fieldnotes.app'])).toBe(
- 'https://fieldnotes.app/?lh-review',
+ it('opens the first exact origin with the activation parameter', () => {
+ expect(reviewLink(['https://*.fieldnotes.app', 'https://fieldnotes.app'], 'acc_1')).toBe(
+ 'https://fieldnotes.app/?lh-acceptance=acc_1',
);
});
+ it('escapes the acceptance id instead of appending it raw', () => {
+ expect(reviewLink(['https://fieldnotes.app'], 'acc&next=1')).toBe(
+ 'https://fieldnotes.app/?lh-acceptance=acc%26next%3D1',
+ );
+ });
+
+ it('leaves the site unreachable for review without an acceptance', () => {
+ expect(reviewLink(['https://fieldnotes.app'])).toBeNull();
+ expect(reviewLink(['https://fieldnotes.app'], null)).toBeNull();
+ });
+
it('has nothing to open when every origin is a wildcard', () => {
- expect(reviewLink(['https://*.fieldnotes.app'])).toBeNull();
+ expect(reviewLink(['https://*.fieldnotes.app'], 'acc_1')).toBeNull();
+ });
+
+ it('refuses an origin that is not an http URL instead of throwing', () => {
+ expect(reviewLink(['fieldnotes.app'], 'acc_1')).toBeNull();
+ expect(reviewLink(['javascript:alert(1)'], 'acc_1')).toBeNull();
});
});
diff --git a/src/features/Projects/Review/siteSnippet.ts b/src/features/Projects/Review/siteSnippet.ts
index 474f98d1aa0..114f93f6c23 100644
--- a/src/features/Projects/Review/siteSnippet.ts
+++ b/src/features/Projects/Review/siteSnippet.ts
@@ -1,8 +1,8 @@
+import { ACCEPTANCE_REVIEW_ACTIVATION_PARAM } from '@lobechat/const';
import { normalizeSiteOrigin, type SiteOriginError } from '@lobechat/utils/siteOrigin';
const REVIEW_SDK_SRC = 'https://web-assets.lobehub.com/review-sdk/assets/v1/lobehub-review.js';
const REVIEW_DEFAULT_SERVER = 'https://app.lobehub.com';
-const SITE_WAKE_PARAM = 'lh-review';
const HTML_ATTRIBUTE_ESCAPES: Record<string, string> = {
'"': '&quot;',
@@ -20,9 +20,25 @@ export const siteSnippet = (siteId: string, server: string) =>
server === REVIEW_DEFAULT_SERVER ? '' : ` data-server="${escapeAttribute(server)}"`
}></script>`;
-export const reviewLink = (origins: string[]) => {
+/**
+ * Opens a site with the toolbar activated. A site only stores origins (paths,
+ * queries and hashes are rejected), so this always targets the origin root;
+ * the parameter is what tells the SDK which acceptance to review.
+ */
+export const reviewLink = (origins: string[], acceptanceId?: null | string) => {
const origin = origins.find((item) => !item.includes('://*.'));
- return origin ? `${origin}/?${SITE_WAKE_PARAM}` : null;
+ if (!origin || !acceptanceId) return null;
+
+ // Origins are normalized on write, so this only keeps a drifted row out of a
+ // rendered href instead of crashing the card.
+ try {
+ const url = new URL(origin);
+ if (url.protocol !== 'https:' && url.protocol !== 'http:') return null;
+ url.searchParams.set(ACCEPTANCE_REVIEW_ACTIVATION_PARAM, acceptanceId);
+ return url.toString();
+ } catch {
+ return null;
+ }
};
export const checkOrigins = (origins: string[]): (SiteOriginError | undefined)[] =>