Skip to content

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.

2.9 KB
Raw Blame
import {
  Button,
  Flex,
  Popover,
  PopoverContent,
  PopoverTrigger,
  StorySection,
} from "@webstudio-is/design-system";
import { useEffect, useState } from "react";
import { createId } from "@webstudio-is/sdk";
import {
  type LinkOptions,
  ShareProject as ShareProjectComponent,
} from "./share-project";

export default {
  title: "Share project",
  component: ShareProjectComponent,
};

const initialLinks: Array<LinkOptions> = [
  {
    token: createId("nano"),
    name: "View Only",
    relation: "viewers",
    canClone: false,
    canCopy: false,
    canPublish: false,
    canUseApi: false,
  },
  {
    token: createId("nano"),
    name: "View and Edit",
    relation: "editors",
    canClone: false,
    canCopy: false,
    canPublish: false,
    canUseApi: false,
  },
  {
    token: createId("nano"),
    name: "Build",
    relation: "builders",
    canClone: false,
    canCopy: false,
    canPublish: false,
    canUseApi: false,
  },
];

const INITIAL_LINKS: LinkOptions[] = [];

const useShareProject = (
  links: Array<LinkOptions> = INITIAL_LINKS,
  async = false
) => {
  const [currentLinks, setLinks] = useState(async ? [] : links);

  const onChange = (updatedLink: LinkOptions) => {
    setLinks(
      currentLinks.map((link) =>
        link.token === updatedLink.token ? updatedLink : link
      )
    );
  };
  const onDelete = (deletedLink: LinkOptions) => {
    setLinks(currentLinks.filter((link) => link.token !== deletedLink.token));
  };
  const onCreate = () => {
    setLinks([
      ...currentLinks,
      {
        token: createId("nano"),
        name: "Custom Link",
        relation: "viewers",
        canClone: false,
        canCopy: false,
        canPublish: false,
        canUseApi: false,
      },
    ]);
  };

  useEffect(() => {
    if (async) {
      setTimeout(() => {
        setLinks(links);
      }, 1000);
      return;
    }

    setLinks(links);
  }, [async, links]);

  return { links: currentLinks, onChange, onDelete, onCreate };
};

const builderUrl = ({ authToken, mode }: { authToken: string; mode: string }) =>
  `https://blabla.com/${authToken}/${mode}`;

const ShareProjectPopover = ({
  label,
  linkOptions,
  async,
}: {
  label: string;
  linkOptions?: Array<LinkOptions>;
  async?: boolean;
}) => {
  const props = useShareProject(linkOptions, async);
  return (
    <Popover modal open>
      <PopoverTrigger asChild>
        <Button color="primary">{label}</Button>
      </PopoverTrigger>
      <PopoverContent>
        <ShareProjectComponent
          {...props}
          allowAdditionalPermissions={false}
          isFreePlan={true}
          isPending={false}
          builderUrl={builderUrl}
        />
      </PopoverContent>
    </Popover>
  );
};

export const ShareProject = () => (
  <StorySection title="Share project">
    <Flex gap="9" css={{ padding: 100 }}>
      <ShareProjectPopover label="Empty" />
      <ShareProjectPopover label="With Links" linkOptions={initialLinks} />
    </Flex>
  </StorySection>
);