-
Notifications
You must be signed in to change notification settings - Fork 195
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
fix(compass-connections, sidebar): move all connection related logic and state to compass-connections COMPASS-8067 #6059
Merged
gribnoysup
merged 11 commits into
main
from
compass-8067-consolidate-connection-store-logic
Jul 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
41e9d44
fix(compass-connections, sidebar): move all connection related logic …
gribnoysup 0601031
fix(connections): add missing dependency
gribnoysup 2649ce1
chore(connections): fix active connections count
gribnoysup 8e02c11
chore(sidebar): fix test failures
gribnoysup 27c41a8
chore(connections): more tests
gribnoysup ade3ada
fix(connections): adjust regex to handle multi digit numbers and to n…
gribnoysup 9293cdb
fix(connections): fix electron tests
gribnoysup b2b4ac6
chore(connections): add more tests
gribnoysup f013de7
chore(connections): different fix for a test
gribnoysup b04b478
chore(connections): fix typos
gribnoysup f625304
fix(connections): correctly use existing connection info for single c…
gribnoysup 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
173 changes: 173 additions & 0 deletions
173
packages/compass-connections/src/components/connection-status-toasts.tsx
This file contains 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,173 @@ | ||
import React, { useCallback } from 'react'; | ||
import { css, Link, spacing, useToast } from '@mongodb-js/compass-components'; | ||
import type { ConnectionInfo } from '@mongodb-js/connection-info'; | ||
import { getConnectionTitle } from '@mongodb-js/connection-info'; | ||
import { usePreference } from 'compass-preferences-model/provider'; | ||
import ConnectionString from 'mongodb-connection-string-url'; | ||
import { isCancelError } from '@mongodb-js/compass-utils'; | ||
|
||
export function isOIDCAuth(connectionString: string): boolean { | ||
const authMechanismString = ( | ||
new ConnectionString(connectionString).searchParams.get('authMechanism') || | ||
'' | ||
).toUpperCase(); | ||
|
||
return authMechanismString === 'MONGODB-OIDC'; | ||
} | ||
|
||
export function getConnectionErrorMessage(err?: any) { | ||
return isCancelError(err) ? null : err?.message ?? null; | ||
} | ||
|
||
export function getConnectingStatusText(connectionInfo: ConnectionInfo) { | ||
const connectionTitle = getConnectionTitle(connectionInfo); | ||
const isOIDC = isOIDCAuth(connectionInfo.connectionOptions.connectionString); | ||
return { | ||
title: `Connecting to ${connectionTitle}`, | ||
description: isOIDC ? 'Go to the browser to complete authentication' : '', | ||
}; | ||
} | ||
|
||
type ConnectionErrorToastBodyProps = { | ||
info?: ConnectionInfo | null; | ||
onReview: () => void; | ||
}; | ||
|
||
const connectionErrorToastBodyStyles = css({ | ||
display: 'flex', | ||
alignItems: 'start', | ||
gap: spacing[2], | ||
}); | ||
|
||
const connectionErrorToastActionMessageStyles = css({ | ||
marginTop: spacing[1], | ||
flexGrow: 0, | ||
}); | ||
|
||
function ConnectionErrorToastBody({ | ||
info, | ||
onReview, | ||
}: ConnectionErrorToastBodyProps): React.ReactElement { | ||
return ( | ||
<span className={connectionErrorToastBodyStyles}> | ||
<span data-testid="connection-error-text"> | ||
There was a problem connecting{' '} | ||
{info ? `to ${getConnectionTitle(info)}` : ''} | ||
</span> | ||
{info && ( | ||
<Link | ||
className={connectionErrorToastActionMessageStyles} | ||
hideExternalIcon={true} | ||
onClick={onReview} | ||
data-testid="connection-error-review" | ||
> | ||
REVIEW | ||
</Link> | ||
)} | ||
</span> | ||
); | ||
} | ||
|
||
const noop = () => undefined; | ||
|
||
export function useConnectionStatusToasts() { | ||
const enableNewMultipleConnectionSystem = usePreference( | ||
'enableNewMultipleConnectionSystem' | ||
); | ||
const { openToast, closeToast } = useToast('connection-status'); | ||
|
||
const openConnectionStartedToast = useCallback( | ||
(connectionInfo: ConnectionInfo, onCancelClick: () => void) => { | ||
const { title, description } = getConnectingStatusText(connectionInfo); | ||
openToast(connectionInfo.id, { | ||
title, | ||
description, | ||
dismissible: true, | ||
variant: 'progress', | ||
actionElement: ( | ||
<Link | ||
hideExternalIcon={true} | ||
onClick={() => { | ||
closeToast(connectionInfo.id); | ||
onCancelClick(); | ||
}} | ||
> | ||
CANCEL | ||
</Link> | ||
), | ||
}); | ||
}, | ||
[closeToast, openToast] | ||
); | ||
|
||
const openConnectionSucceededToast = useCallback( | ||
(connectionInfo: ConnectionInfo) => { | ||
openToast(connectionInfo.id, { | ||
title: `Connected to ${getConnectionTitle(connectionInfo)}`, | ||
variant: 'success', | ||
timeout: 3_000, | ||
}); | ||
}, | ||
[openToast] | ||
); | ||
|
||
const openConnectionFailedToast = useCallback( | ||
( | ||
// Connection info might be missing if we failed connecting before we | ||
// could even resolve connection info. Currently the only case where this | ||
// can happen is autoconnect flow | ||
connectionInfo: ConnectionInfo | null | undefined, | ||
error: Error, | ||
onReviewClick: () => void | ||
) => { | ||
const failedToastId = connectionInfo?.id ?? 'failed'; | ||
|
||
openToast(failedToastId, { | ||
title: error.message, | ||
description: ( | ||
<ConnectionErrorToastBody | ||
info={connectionInfo} | ||
onReview={() => { | ||
closeToast(failedToastId); | ||
onReviewClick(); | ||
}} | ||
/> | ||
), | ||
variant: 'warning', | ||
}); | ||
}, | ||
[closeToast, openToast] | ||
); | ||
|
||
const openMaximumConnectionsReachedToast = useCallback( | ||
(maxConcurrentConnections: number) => { | ||
const message = `Only ${maxConcurrentConnections} connection${ | ||
maxConcurrentConnections > 1 ? 's' : '' | ||
} can be connected to at the same time. First disconnect from another connection.`; | ||
|
||
openToast('max-connections-reached', { | ||
title: 'Maximum concurrent connections limit reached', | ||
description: message, | ||
variant: 'warning', | ||
timeout: 5_000, | ||
}); | ||
}, | ||
[openToast] | ||
); | ||
|
||
return enableNewMultipleConnectionSystem | ||
? { | ||
openConnectionStartedToast, | ||
openConnectionSucceededToast, | ||
openConnectionFailedToast, | ||
openMaximumConnectionsReachedToast, | ||
closeConnectionStatusToast: closeToast, | ||
} | ||
: { | ||
openConnectionStartedToast: noop, | ||
openConnectionSucceededToast: noop, | ||
openConnectionFailedToast: noop, | ||
openMaximumConnectionsReachedToast: noop, | ||
closeConnectionStatusToast: noop, | ||
}; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This text is now applied both to multiple and single connections flow now that we're sharing more UI code between the two