()
| 58 | * - `kb-connectors`: Redirect to the KB page. The KB page picks up the context. |
| 59 | */ |
| 60 | export function useOAuthReturnRouter() { |
| 61 | const router = useRouter() |
| 62 | const params = useParams() |
| 63 | const workspaceId = params.workspaceId as string |
| 64 | const handledRef = useRef(false) |
| 65 | |
| 66 | useEffect(() => { |
| 67 | if (handledRef.current) return |
| 68 | const ctx = readOAuthReturnContext() |
| 69 | if (!ctx) return |
| 70 | if (Date.now() - ctx.requestedAt > CONTEXT_MAX_AGE_MS) { |
| 71 | consumeOAuthReturnContext() |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | handledRef.current = true |
| 76 | |
| 77 | if (ctx.origin === 'integrations') { |
| 78 | consumeOAuthReturnContext() |
| 79 | void (async () => { |
| 80 | const message = await resolveOAuthMessage(ctx) |
| 81 | toast.success(message) |
| 82 | dispatchCredentialUpdate(ctx) |
| 83 | })() |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | if (ctx.origin === 'workflow') { |
| 88 | try { |
| 89 | sessionStorage.removeItem(SETTINGS_RETURN_URL_KEY) |
| 90 | } catch {} |
| 91 | router.replace(`/workspace/${workspaceId}/w/${ctx.workflowId}`) |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | if (ctx.origin === 'kb-connectors') { |
| 96 | try { |
| 97 | sessionStorage.removeItem(SETTINGS_RETURN_URL_KEY) |
| 98 | } catch {} |
| 99 | const kbUrl = `/workspace/${workspaceId}/knowledge/${ctx.knowledgeBaseId}` |
| 100 | const connectorParam = ctx.connectorType |
| 101 | ? `?${ADD_CONNECTOR_SEARCH_PARAM}=${encodeURIComponent(ctx.connectorType)}` |
| 102 | : '' |
| 103 | router.replace(`${kbUrl}${connectorParam}`) |
| 104 | return |
| 105 | } |
| 106 | }, [router, workspaceId]) |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Post-OAuth handler for workflow pages. |
no test coverage detected