Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
feat: navigate to pro features on publish dialog (#5036)
Bogdan Chadkine730aa3c6b18
diff --git a/apps/builder/app/builder/features/topbar/publish.tsx b/apps/builder/app/builder/features/topbar/publish.tsxindex bae2252c47..224315c882 100644--- a/apps/builder/app/builder/features/topbar/publish.tsx+++ b/apps/builder/app/builder/features/topbar/publish.tsx@@ -1,3 +1,4 @@+import stripIndent from "strip-indent";import { computed } from "nanostores";import {useEffect,@@ -35,10 +36,15 @@ import {PopoverTitle,PopoverClose,PopoverTitleActions,+ css,+ textVariants,} from "@webstudio-is/design-system";-import stripIndent from "strip-indent";-import { $publishDialog } from "../../shared/nano-states";import { validateProjectDomain, type Project } from "@webstudio-is/project";+import {+ $awareness,+ findAwarenessByInstanceId,+ type Awareness,+} from "~/shared/awareness";import {$authTokenPermissions,$dataSources,@@ -48,6 +54,7 @@ import {$publishedOrigin,$userPlanFeatures,} from "~/shared/nano-states";+import { $publishDialog } from "../../shared/nano-states";import { Domains, PENDING_TIMEOUT, getPublishStatusAndText } from "./domains";import { CollapsibleDomainSection } from "./collapsible-domain-section";import {@@ -212,28 +219,41 @@ const ChangeProjectDomain = ({const $usedProFeatures = computed([$pages, $dataSources, $instances],(pages, dataSources, instances) => {- const features = new Set<string>();+ const features = new Map<string, undefined | Awareness>();+ if (pages === undefined) {+ return features;+ }// specified emails for default webhook formif ((pages?.meta?.contactEmail ?? "").trim()) {- features.add("Custom contant email");+ features.set("Custom contact email", undefined);}// pages with dynamic paths- for (const page of pages ? [pages.homePage, ...pages.pages] : []) {+ for (const page of [pages.homePage, ...pages.pages]) {if (isPathnamePattern(page.path)) {- features.add("Dynamic path");+ features.set("Dynamic path", {+ pageId: page.id,+ instanceSelector: [page.rootInstanceId],+ });}}// has resource variablesfor (const dataSource of dataSources.values()) {if (dataSource.type === "resource") {- features.add("Resource variable");+ const instanceId = dataSource.scopeInstanceId ?? "";+ features.set(+ "Resource variable",+ findAwarenessByInstanceId(pages, instances, instanceId)+ );}}// instances with animationsfor (const instance of instances.values()) {const [namespace] = parseComponentName(instance.component);if (namespace === "@webstudio-is/sdk-components-animation") {- features.add("Animation component");+ features.set(+ "Animation component",+ findAwarenessByInstanceId(pages, instances, instance.id)+ );}}return features;@@ -646,6 +666,12 @@ const refreshProject = async () => {toast.error(result.error);};+const buttonLinkClass = css({+ all: "unset",+ cursor: "pointer",+ ...textVariants.link,+}).toString();+const Content = (props: {projectId: Project["id"];onExportClick: () => void;@@ -696,9 +722,23 @@ const Content = (props: {Upgrade to publish with following features:</Text><Text as="ul">- {Array.from(usedProFeatures).map((message, index) => (- <li key={index}>{message}</li>- ))}+ {Array.from(usedProFeatures).map(+ ([message, awareness], index) => (+ <li key={index}>+ {awareness ? (+ <button+ className={buttonLinkClass}+ type="button"+ onClick={() => $awareness.set(awareness)}+ >+ {message}+ </button>+ ) : (+ message+ )}+ </li>+ )+ )}</Text><Flex align="center" gap={1}><UpgradeIcon />diff --git a/apps/builder/app/shared/awareness.test.tsx b/apps/builder/app/shared/awareness.test.tsxnew file mode 100644index 0000000000..1c51b85ec6--- /dev/null+++ b/apps/builder/app/shared/awareness.test.tsx@@ -0,0 +1,47 @@+import { expect, test } from "vitest";+import { findAwarenessByInstanceId } from "./awareness";+import { createDefaultPages } from "@webstudio-is/project-build";+import { $, renderData } from "@webstudio-is/template";++test("find awareness by instance", () => {+ const pages = createDefaultPages({+ homePageId: "homePageId",+ rootInstanceId: "bodyId",+ });+ const { instances } = renderData(+ <$.Body ws:id="bodyId">+ <$.Box ws:id="boxId">+ <$.Text ws:id="textId"></$.Text>+ </$.Box>+ </$.Body>+ );+ expect(findAwarenessByInstanceId(pages, instances, "textId")).toEqual({+ pageId: "homePageId",+ instanceSelector: ["textId", "boxId", "bodyId"],+ });+});++test("find awareness by instance inside of slot", () => {+ const pages = createDefaultPages({+ homePageId: "homePageId",+ rootInstanceId: "bodyId",+ });+ const { instances } = renderData(+ <$.Body ws:id="bodyId">+ <$.Slot ws:id="slotOneId">+ <$.Fragment ws:id="fragmentId">+ <$.Box ws:id="boxId"></$.Box>+ </$.Fragment>+ </$.Slot>+ <$.Slot ws:id="slotTwoId">+ <$.Fragment ws:id="fragmentId">+ <$.Box ws:id="boxId"></$.Box>+ </$.Fragment>+ </$.Slot>+ </$.Body>+ );+ expect(findAwarenessByInstanceId(pages, instances, "boxId")).toEqual({+ pageId: "homePageId",+ instanceSelector: ["boxId", "fragmentId", "slotTwoId", "bodyId"],+ });+});diff --git a/apps/builder/app/shared/awareness.ts b/apps/builder/app/shared/awareness.tsindex 4dd991c45d..7e65b21031 100644--- a/apps/builder/app/shared/awareness.ts+++ b/apps/builder/app/shared/awareness.ts@@ -6,11 +6,13 @@ import {ROOT_INSTANCE_ID,type Page,rootComponent,+ Pages,} from "@webstudio-is/sdk";import { $pages } from "./nano-states/pages";import { $instances, $selectedInstanceSelector } from "./nano-states/instances";+import type { InstanceSelector } from "./tree-utils";-type Awareness = {+export type Awareness = {pageId: Page["id"];instanceSelector?: Instance["id"][];};@@ -183,3 +185,50 @@ export const selectInstance = (});}};++const findPageId = (pages: Pages, instanceSelector: InstanceSelector) => {+ const rootInstanceId = instanceSelector.at(-1);+ for (const page of [pages.homePage, ...pages.pages]) {+ if (page.rootInstanceId === rootInstanceId) {+ return page.id;+ }+ }+ return pages.homePage.id;+};++const parentInstanceByIdCache = new WeakMap<+ Instances,+ Map<Instance["id"], Instance["id"]>+>();++/**+ * traverse the tree up until body to build awareness+ * when instance id is inside of slot last matching parent is used to further+ */+export const findAwarenessByInstanceId = (+ pages: Pages,+ instances: Instances,+ startingInstanceId: Instance["id"]+): Awareness => {+ // recompute parent instances only when instances are changed+ let parentInstanceById = parentInstanceByIdCache.get(instances);+ if (parentInstanceById === undefined) {+ parentInstanceById = new Map<Instance["id"], Instance["id"]>();+ for (const instance of instances.values()) {+ for (const child of instance.children) {+ if (child.type === "id") {+ parentInstanceById.set(child.value, instance.id);+ }+ }+ }+ parentInstanceByIdCache.set(instances, parentInstanceById);+ }+ const instanceSelector = [];+ let currentInstanceId: undefined | Instance["id"] = startingInstanceId;+ while (currentInstanceId) {+ instanceSelector.push(currentInstanceId);+ currentInstanceId = parentInstanceById.get(currentInstanceId);+ }+ const pageId = findPageId(pages, instanceSelector);+ return { pageId, instanceSelector };+};