web: Add Test connection button and API call to code host#45972
Conversation
Bundle size report 📦
Look at the Statoscope report for a full comparison between the commits f502511 and 821918c or learn more. Open explanation
|
Step 1 of many. Need to tie down the response to the button to show changes.
35e2008 to
5c677c2
Compare
Needs cleanup, but first lunch 🍔
| updatedAt: '2021-03-15T19:39:11Z', | ||
| createdAt: '2021-03-15T19:39:11Z', | ||
| webhookURL: null, | ||
| hasConnectionCheck: true, |
There was a problem hiding this comment.
This change brought to you by yarn build-ts errors.
| id: 'service1', | ||
| kind: ExternalServiceKind.GITHUB, | ||
| displayName: 'GitHub.com', | ||
| displayName: 'GitHub #1', |
There was a problem hiding this comment.
Small fix: Fixed a warning for both the displayName being same in the storybook.
| <span className={classNames('text-primary')}> | ||
| <LoadingSpinner /> Checking connection... | ||
| </span> | ||
| )} |
There was a problem hiding this comment.
Do this rather(same for other places):
const checkConnectionNode = checkConnectionData?.node
const isCodeHostReachable = node?.__typename === 'ExternalService' &&
node?.checkConnection?.__typename === 'ExternalServiceAvailable' &&
node?.checkConnection?.lastCheckedAt;
{isCodeHostReachable && (
<span className="text-success">
<Icon aria-hidden={true} svgPath={mdiCheckCircle} /> Code host is reachable.
</span>
)}
| { called: checkConnectionCalled, loading: checkConnectionLoading, data: checkConnectionData }, | ||
| ] = useExternalServiceCheckConnectionByIdLazyQuery(node.id) | ||
|
|
||
| const [checkConnectionError, setCheckConnectionError] = useState<boolean | Error>(false) |
There was a problem hiding this comment.
I guess the type of state can be simplified to just Error and initially set to undefined.
(Or you might need Error | undefined, I'm not 100% sure 😁)
There was a problem hiding this comment.
Hmm. Probably. But we're doing something similar already for isDeleting a little above this code. Maybe we keep them the same to have a recognizable pattern in this part of the code? What do you think?
There was a problem hiding this comment.
useLazyQuery should return error on its own and you don't need to save error state separately.
You can do const [doCheckConnection, { loading, data, error }] = useExternalServiceCheckConnectionByIdLazyQuery(node.id)
deleteExternalService is legacy and not using the react hooks; that is why the error is handled separately there.
Read: https://www.apollographql.com/docs/react/api/react/hooks/#error
| )} | ||
| {node.nextSyncAt === null && <>No next sync scheduled.</>} | ||
| <br /> | ||
| {isErrorLike(checkConnectionError) && ( |
There was a problem hiding this comment.
I think this can be simplified to !checkConnectionError. Because currently it is either false or Error which is isErrorLike
There was a problem hiding this comment.
probably, loading check should be added here as well
There was a problem hiding this comment.
I think this can be simplified to !checkConnectionError. Because currently it is either false or Error which is isErrorLike
I think the isErrorLike is a stronger check here in case there's ever a bug and checkConnectionError is true, then we will end up displaying an error message with true in it. I checked in other places and it looks like isErrorLike is used in such cases. I'd vote to keep it as is.
+1 on moving the loading check to be the first. Fixed that.
There was a problem hiding this comment.
don't believe this can happen, we need to trust our code :)
but still it's your call
There was a problem hiding this comment.
don't believe this can happen, we need to trust our code :)
Yeah I trust the code now. But I'm thinking that the current check is stronger for any potential future bugs if anyone / me end up setting true as a value by mistake only because its technically allowed by the type here. Just defensive programming I guess. 😛
Since it's not too complicated in its current form and also is used quite widely in our code, keeping it is not the worst idea. 😀
Improve code organisation and variable naming.
sashaostrikov
left a comment
There was a problem hiding this comment.
LGTM!
But let's wait for @thenamankumar as well.
|
|
||
| const [checkConnectionError, setCheckConnectionError] = useState<boolean | Error>(false) | ||
|
|
||
| const onTestConnection = useCallback<React.MouseEventHandler>(async () => { |
There was a problem hiding this comment.
You can directly use doCheckConnection. No need to wrap with useCallback.
No need for extra state management for error handling. 🎉
|
|
||
| const [checkConnectionError, setCheckConnectionError] = useState<boolean | Error>(false) | ||
|
|
||
| const onTestConnection = useCallback<React.MouseEventHandler>(async () => { |
There was a problem hiding this comment.
You can directly use doCheckConnection. No need to wrap with useCallback.
| </div> | ||
| <div className="flex-shrink-0 ml-3"> | ||
| <Tooltip | ||
| content={node.hasConnectionCheck ? 'Test code host connection' : 'Connection check unavailable'} |
There was a problem hiding this comment.
I'd change Test code host connection to Test whether code host is reachable from Sourcegraph
There was a problem hiding this comment.
Created a PR: https://github.com/sourcegraph/sourcegraph/pull/46029
There was a problem hiding this comment.
Tweaked the suggested message slightly to make it a little shorter.
| className="test-connection-external-service-button" | ||
| variant="secondary" | ||
| onClick={() => doCheckConnection()} | ||
| disabled={!node.hasConnectionCheck || loading} |
There was a problem hiding this comment.
(man, GitHub lost my comment, so here it is again)
I think we should just hide the button if !node.hasConnectionCheck. A disabled button suggests there's a way to enable it. In this case though, we don't support the connection check and a Sourcegraph update is required to get it enabled once we add support, meaning: nothing the user can do.
There was a problem hiding this comment.
(man, GitHub lost my comment, so here it is again)
Have been seeing some reliability issues with GitHub myself this week. 🙁
There was a problem hiding this comment.
I get how a user might thinkg that the disabled button suggests that there's a way to enabled it. But with the suggested change, it looks inconsistent to me when one entry doesn't have that button. Besides we do indicate that the connection check is unavailable on the disabled button. Think that should be okay?
If it looks all right to you, I can create the PR.
There was a problem hiding this comment.
I think that's alright. (Ideally and further down the road we create a "details" page for code hosts where we can put this)

UI for #44683.
Test plan
Check connection unavailable
Check connection available
Check connection successful
Check connection failed
Check connection errored (application error)
Test button in action 🎥 for success, failure and error modes
App preview:
Check out the client app preview documentation to learn more.