Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react, plugin-history-sync, link): unsafe api for register activity dynamically #559

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

tonyfromundefined
Copy link
Member

@tonyfromundefined tonyfromundefined commented Jan 20, 2025

Description

Exposes an unsafe api (react context) for dynamically registering activities. You can use it as follows

import { UNSAFE_ActivityComponentMapContext, UNSAFE_CoreActionsContext } from "@stackflow/react/future";

function NewActivity() {
  // ...
}

function MyComponent() {
  const { push } = useFlow()
  const { dispatchEvent } = useContext(UNSAFE_CoreActionsContext);
  const { setActivityComponentMap } = useContext(UNSAFE_ActivityComponentMapContext);

  const onSomethingHappened = () => {
    const activityName = crypto.randomUUID();

    dispatchEvent("ActivityRegistered", {
      activityName,
    });
    setActivityComponentMap({
      activityName,
      Component: NewActivity,
    });

    push(activityName as string, {});
  }

  // ...
}

or you can create your own custom push()

function useDynamicFlow() {
  const { push } = useFlow()
  const { dispatchEvent } = useContext(UNSAFE_CoreActionsContext);
  const { setActivityComponentMap } = useContext(UNSAFE_ActivityComponentMapContext);

  return {
    push(Component: ActivityComponentType<any>) {
      const activityName = crypto.randomUUID();

      dispatchEvent("ActivityRegistered", {
        activityName,
      });
      setActivityComponentMap({
        activityName,
        Component,
      });

      push(activityName as string, {});
    },
  };
}

function NewActivity() {
  // ...
}

function MyComponent() {
  const { push } = useDynamicFlow();

  const onClick = () => {
    push(NewActivity);
  }

  // ...
}

Copy link

changeset-bot bot commented Jan 20, 2025

🦋 Changeset detected

Latest commit: 419779a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@stackflow/plugin-history-sync Minor
@stackflow/react Minor
@stackflow/link Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jan 20, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
stackflow-docs ✅ Ready (Inspect) Visit Preview Jan 20, 2025 0:08am

Copy link

pkg-pr-new bot commented Jan 20, 2025

Open in Stackblitz@stackflow/demo

yarn add https://pkg.pr.new/@stackflow/[email protected]
yarn add https://pkg.pr.new/@stackflow/[email protected]
yarn add https://pkg.pr.new/@stackflow/[email protected]

commit: 419779a

@tonyfromundefined tonyfromundefined changed the title WIP: dynamically create activity feat(react, plugin-history-sync, link): unsafe api for register activity dynamically Jan 20, 2025
@tonyfromundefined tonyfromundefined marked this pull request as ready for review January 20, 2025 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant