Skip to content

Commit d116dbf

Browse files
authored
Only show share modal from sharebutton (#5475)
1 parent e9dca0a commit d116dbf

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

webview-ui/src/components/chat/ShareButton.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,32 @@ export const ShareButton = ({ item, disabled = false }: ShareButtonProps) => {
3535
const { t } = useTranslation()
3636
const { sharingEnabled, cloudIsAuthenticated, cloudUserInfo } = useExtensionState()
3737
const wasUnauthenticatedRef = useRef(false)
38+
const initiatedAuthFromThisButtonRef = useRef(false)
3839

3940
// Track authentication state changes to auto-open popover after login
4041
useEffect(() => {
4142
if (!cloudIsAuthenticated || !sharingEnabled) {
4243
wasUnauthenticatedRef.current = true
4344
} else if (wasUnauthenticatedRef.current && cloudIsAuthenticated && sharingEnabled) {
44-
// User just authenticated, send telemetry, close modal, and open the popover
45-
telemetryClient.capture(TelemetryEventName.ACCOUNT_CONNECT_SUCCESS)
46-
setConnectModalOpen(false)
47-
setShareDropdownOpen(true)
45+
// Only open dropdown if auth was initiated from this button
46+
if (initiatedAuthFromThisButtonRef.current) {
47+
// User just authenticated from this share button, send telemetry, close modal, and open the popover
48+
telemetryClient.capture(TelemetryEventName.ACCOUNT_CONNECT_SUCCESS)
49+
setConnectModalOpen(false)
50+
setShareDropdownOpen(true)
51+
initiatedAuthFromThisButtonRef.current = false // Reset the flag
52+
}
4853
wasUnauthenticatedRef.current = false
4954
}
5055
}, [cloudIsAuthenticated, sharingEnabled])
5156

57+
// Cleanup effect to reset flag on unmount
58+
useEffect(() => {
59+
return () => {
60+
initiatedAuthFromThisButtonRef.current = false
61+
}
62+
}, [])
63+
5264
// Listen for share success messages from the extension
5365
useEffect(() => {
5466
const handleMessage = (event: MessageEvent) => {
@@ -92,6 +104,8 @@ export const ShareButton = ({ item, disabled = false }: ShareButtonProps) => {
92104
// Send telemetry for connect to cloud action
93105
telemetryClient.capture(TelemetryEventName.SHARE_CONNECT_TO_CLOUD_CLICKED)
94106

107+
// Mark that authentication was initiated from this button
108+
initiatedAuthFromThisButtonRef.current = true
95109
vscode.postMessage({ type: "rooCloudSignIn" })
96110
setShareDropdownOpen(false)
97111
setConnectModalOpen(false)

webview-ui/src/components/chat/__tests__/TaskActions.spec.tsx

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ describe("TaskActions", () => {
265265
expect(screen.queryByText("Connect to Cloud")).not.toBeInTheDocument()
266266
})
267267

268-
it("automatically opens popover when user becomes authenticated", () => {
268+
it("does not automatically open popover when user becomes authenticated from elsewhere", () => {
269269
// Start with unauthenticated state
270270
mockUseExtensionState.mockReturnValue({
271271
sharingEnabled: false,
@@ -277,7 +277,7 @@ describe("TaskActions", () => {
277277
// Verify popover is not open initially
278278
expect(screen.queryByText("Share with Organization")).not.toBeInTheDocument()
279279

280-
// Simulate user becoming authenticated
280+
// Simulate user becoming authenticated (e.g., from AccountView)
281281
mockUseExtensionState.mockReturnValue({
282282
sharingEnabled: true,
283283
cloudIsAuthenticated: true,
@@ -288,7 +288,47 @@ describe("TaskActions", () => {
288288

289289
rerender(<TaskActions item={mockItem} buttonsDisabled={false} />)
290290

291-
// Verify popover automatically opens and shows sharing options
291+
// Verify popover does NOT automatically open when auth happens from elsewhere
292+
expect(screen.queryByText("Share with Organization")).not.toBeInTheDocument()
293+
expect(screen.queryByText("Share Publicly")).not.toBeInTheDocument()
294+
})
295+
296+
it("automatically opens popover when user authenticates from share button", () => {
297+
// Start with unauthenticated state
298+
mockUseExtensionState.mockReturnValue({
299+
sharingEnabled: false,
300+
cloudIsAuthenticated: false,
301+
} as any)
302+
303+
const { rerender } = render(<TaskActions item={mockItem} buttonsDisabled={false} />)
304+
305+
// Click share button to open connect modal
306+
const buttons = screen.getAllByRole("button")
307+
const shareButton = buttons.find((btn) => btn.querySelector(".codicon-link"))
308+
expect(shareButton).toBeDefined()
309+
fireEvent.click(shareButton!)
310+
311+
// Click connect button to initiate authentication
312+
const connectButton = screen.getByText("Connect")
313+
fireEvent.click(connectButton)
314+
315+
// Verify rooCloudSignIn message was sent
316+
expect(mockPostMessage).toHaveBeenCalledWith({
317+
type: "rooCloudSignIn",
318+
})
319+
320+
// Simulate user becoming authenticated after clicking connect from share button
321+
mockUseExtensionState.mockReturnValue({
322+
sharingEnabled: true,
323+
cloudIsAuthenticated: true,
324+
cloudUserInfo: {
325+
organizationName: "Test Organization",
326+
},
327+
} as any)
328+
329+
rerender(<TaskActions item={mockItem} buttonsDisabled={false} />)
330+
331+
// Verify popover automatically opens when auth was initiated from share button
292332
expect(screen.getByText("Share with Organization")).toBeInTheDocument()
293333
expect(screen.getByText("Share Publicly")).toBeInTheDocument()
294334
})

0 commit comments

Comments
 (0)