Skip to content

Commit c53fcb8

Browse files
committed
extract shared code
1 parent abae19c commit c53fcb8

File tree

3 files changed

+58
-98
lines changed

3 files changed

+58
-98
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/credential-selector/credential-selector.tsx

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { useCredentialSets } from '@/hooks/queries/credential-sets'
2424
import { useOAuthCredentials } from '@/hooks/queries/oauth-credentials'
2525
import { useOrganizations } from '@/hooks/queries/organization'
2626
import { useSubscriptionData } from '@/hooks/queries/subscription'
27+
import { useCredentialRefreshTriggers } from '@/hooks/use-credential-refresh-triggers'
2728
import { getMissingRequiredScopes } from '@/hooks/use-oauth-scope-status'
2829
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
2930

@@ -122,6 +123,8 @@ export function CredentialSelector({
122123
return
123124
}
124125

126+
setInaccessibleCredentialName(null)
127+
125128
let cancelled = false
126129
;(async () => {
127130
try {
@@ -412,52 +415,3 @@ export function CredentialSelector({
412415
</div>
413416
)
414417
}
415-
416-
function useCredentialRefreshTriggers(
417-
refetchCredentials: () => Promise<unknown>,
418-
providerId: string,
419-
workspaceId: string
420-
) {
421-
useEffect(() => {
422-
const refresh = () => {
423-
void refetchCredentials()
424-
}
425-
426-
const handleVisibilityChange = () => {
427-
if (document.visibilityState === 'visible') {
428-
refresh()
429-
}
430-
}
431-
432-
const handlePageShow = (event: Event) => {
433-
if ('persisted' in event && (event as PageTransitionEvent).persisted) {
434-
refresh()
435-
}
436-
}
437-
438-
const handleCredentialsUpdated = (
439-
event: CustomEvent<{ providerId?: string; workspaceId?: string }>
440-
) => {
441-
if (event.detail?.providerId && event.detail.providerId !== providerId) {
442-
return
443-
}
444-
if (event.detail?.workspaceId && workspaceId && event.detail.workspaceId !== workspaceId) {
445-
return
446-
}
447-
refresh()
448-
}
449-
450-
document.addEventListener('visibilitychange', handleVisibilityChange)
451-
window.addEventListener('pageshow', handlePageShow)
452-
window.addEventListener('oauth-credentials-updated', handleCredentialsUpdated as EventListener)
453-
454-
return () => {
455-
document.removeEventListener('visibilitychange', handleVisibilityChange)
456-
window.removeEventListener('pageshow', handlePageShow)
457-
window.removeEventListener(
458-
'oauth-credentials-updated',
459-
handleCredentialsUpdated as EventListener
460-
)
461-
}
462-
}, [providerId, workspaceId, refetchCredentials])
463-
}

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/components/tools/credential-selector.tsx

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@/lib/oauth'
1515
import { OAuthRequiredModal } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/credential-selector/components/oauth-required-modal'
1616
import { useOAuthCredentials } from '@/hooks/queries/oauth-credentials'
17+
import { useCredentialRefreshTriggers } from '@/hooks/use-credential-refresh-triggers'
1718
import { getMissingRequiredScopes } from '@/hooks/use-oauth-scope-status'
1819
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
1920

@@ -100,6 +101,8 @@ export function ToolCredentialSelector({
100101
return
101102
}
102103

104+
setInaccessibleCredentialName(null)
105+
103106
let cancelled = false
104107
;(async () => {
105108
try {
@@ -271,52 +274,3 @@ export function ToolCredentialSelector({
271274
</div>
272275
)
273276
}
274-
275-
function useCredentialRefreshTriggers(
276-
refetchCredentials: () => Promise<unknown>,
277-
providerId: string,
278-
workspaceId: string
279-
) {
280-
useEffect(() => {
281-
const refresh = () => {
282-
void refetchCredentials()
283-
}
284-
285-
const handleVisibilityChange = () => {
286-
if (document.visibilityState === 'visible') {
287-
refresh()
288-
}
289-
}
290-
291-
const handlePageShow = (event: Event) => {
292-
if ('persisted' in event && (event as PageTransitionEvent).persisted) {
293-
refresh()
294-
}
295-
}
296-
297-
const handleCredentialsUpdated = (
298-
event: CustomEvent<{ providerId?: string; workspaceId?: string }>
299-
) => {
300-
if (event.detail?.providerId && event.detail.providerId !== providerId) {
301-
return
302-
}
303-
if (event.detail?.workspaceId && workspaceId && event.detail.workspaceId !== workspaceId) {
304-
return
305-
}
306-
refresh()
307-
}
308-
309-
document.addEventListener('visibilitychange', handleVisibilityChange)
310-
window.addEventListener('pageshow', handlePageShow)
311-
window.addEventListener('oauth-credentials-updated', handleCredentialsUpdated as EventListener)
312-
313-
return () => {
314-
document.removeEventListener('visibilitychange', handleVisibilityChange)
315-
window.removeEventListener('pageshow', handlePageShow)
316-
window.removeEventListener(
317-
'oauth-credentials-updated',
318-
handleCredentialsUpdated as EventListener
319-
)
320-
}
321-
}, [providerId, workspaceId, refetchCredentials])
322-
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use client'
2+
3+
import { useEffect } from 'react'
4+
5+
export function useCredentialRefreshTriggers(
6+
refetchCredentials: () => Promise<unknown>,
7+
providerId: string,
8+
workspaceId: string
9+
) {
10+
useEffect(() => {
11+
const refresh = () => {
12+
void refetchCredentials()
13+
}
14+
15+
const handleVisibilityChange = () => {
16+
if (document.visibilityState === 'visible') {
17+
refresh()
18+
}
19+
}
20+
21+
const handlePageShow = (event: Event) => {
22+
if ('persisted' in event && (event as PageTransitionEvent).persisted) {
23+
refresh()
24+
}
25+
}
26+
27+
const handleCredentialsUpdated = (
28+
event: CustomEvent<{ providerId?: string; workspaceId?: string }>
29+
) => {
30+
if (event.detail?.providerId && event.detail.providerId !== providerId) {
31+
return
32+
}
33+
if (event.detail?.workspaceId && workspaceId && event.detail.workspaceId !== workspaceId) {
34+
return
35+
}
36+
refresh()
37+
}
38+
39+
document.addEventListener('visibilitychange', handleVisibilityChange)
40+
window.addEventListener('pageshow', handlePageShow)
41+
window.addEventListener('oauth-credentials-updated', handleCredentialsUpdated as EventListener)
42+
43+
return () => {
44+
document.removeEventListener('visibilitychange', handleVisibilityChange)
45+
window.removeEventListener('pageshow', handlePageShow)
46+
window.removeEventListener(
47+
'oauth-credentials-updated',
48+
handleCredentialsUpdated as EventListener
49+
)
50+
}
51+
}, [providerId, workspaceId, refetchCredentials])
52+
}

0 commit comments

Comments
 (0)