Repository navigation
improvement(workflow): scope the editor by props instead of the URL #8996
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
apps/sim/app/workspace/[workspaceId]/components/resource/resource-navigation.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| 'use client' | ||
|
|
||
| import { createContext, useCallback, useContext, useMemo } from 'react' | ||
| import { useRouter } from 'next/navigation' | ||
|
|
||
| interface ResourceNavigation { | ||
| /** Handles workspace destinations within a containing resource pane. */ | ||
| navigate: (href: string, replace?: boolean) => void | ||
| } | ||
|
|
||
| /** Provided by a containing resource pane; absent on the routed pages. */ | ||
| const ResourceNavigationContext = createContext<ResourceNavigation | null>(null) | ||
|
TheodoreSpeaks marked this conversation as resolved.
|
||
|
|
||
| /** Navigates the route, or the containing resource pane when one provides navigation. */ | ||
| export function useResourceRouter() { | ||
| const router = useRouter() | ||
| const navigation = useContext(ResourceNavigationContext) | ||
| const push = useCallback( | ||
| (href: string) => { | ||
| if (navigation) navigation.navigate(href) | ||
| else router.push(href) | ||
| }, | ||
| [navigation, router] | ||
| ) | ||
| const replace = useCallback( | ||
| (href: string) => { | ||
| if (navigation) navigation.navigate(href, true) | ||
| else router.replace(href) | ||
| }, | ||
| [navigation, router] | ||
| ) | ||
| return useMemo(() => ({ ...router, push, replace }), [router, push, replace]) | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
apps/sim/app/workspace/[workspaceId]/providers/workflow-scope-provider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| 'use client' | ||
|
|
||
| import { createContext, type ReactNode, useContext, useMemo } from 'react' | ||
|
|
||
| interface WorkflowScope { | ||
| workspaceId: string | ||
| /** Null in read-only previews, which render editor sub-blocks outside an open workflow. */ | ||
| workflowId: string | null | ||
| } | ||
|
|
||
| const WorkflowScopeContext = createContext<WorkflowScope | null>(null) | ||
|
|
||
| interface WorkflowScopeProviderProps extends WorkflowScope { | ||
| children: ReactNode | ||
| } | ||
|
|
||
| /** | ||
| * Supplies the workspace and workflow ids to the editor tree, so the editor | ||
| * works wherever it is mounted instead of reading them from the route. | ||
| */ | ||
| export function WorkflowScopeProvider({ | ||
| workspaceId, | ||
| workflowId, | ||
| children, | ||
| }: WorkflowScopeProviderProps) { | ||
| if (!workspaceId) { | ||
| throw new Error('WorkflowScopeProvider requires a workspaceId') | ||
| } | ||
| const value = useMemo(() => ({ workspaceId, workflowId }), [workspaceId, workflowId]) | ||
| return <WorkflowScopeContext.Provider value={value}>{children}</WorkflowScopeContext.Provider> | ||
| } | ||
|
|
||
| /** Returns the enclosing scope, or null outside a WorkflowScopeProvider. */ | ||
| export function useOptionalWorkflowScope(): WorkflowScope | null { | ||
| return useContext(WorkflowScopeContext) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the editor's workspace and workflow ids. `workflowId` is null in previews. | ||
| * Throws outside a WorkflowScopeProvider. | ||
| */ | ||
| export function useWorkflowScope(): WorkflowScope { | ||
|
TheodoreSpeaks marked this conversation as resolved.
|
||
| const scope = useOptionalWorkflowScope() | ||
| if (!scope) { | ||
| throw new Error('useWorkflowScope must be used within a WorkflowScopeProvider') | ||
| } | ||
| return scope | ||
| } | ||
|
|
||
| /** For editor-only components: throws in a preview scope, which has no workflow. */ | ||
| export function useScopedWorkflowId(): string { | ||
| const { workflowId } = useWorkflowScope() | ||
| if (!workflowId) { | ||
| throw new Error('useScopedWorkflowId must be used within an editor workflow scope') | ||
| } | ||
| return workflowId | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.