App: Add app support in settings token callback (#51750)

This commit is contained in:
Marek 2023-05-11 19:57:51 -04:00 committed by GitHub
parent 6e6f91c42e
commit d3fc0f85cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 5 deletions

View File

@ -27,12 +27,21 @@ import { UserSettingsAreaRouteContext } from '../UserSettingsArea'
import { createAccessToken } from './create'
/**
* Utility function to open the callback URL in Sourcegraph App. Used where
* window.open or target="_blank" cannot be used.
*/
function tauriShellOpen(uri: string): void {
;(window as any).__TAURI__?.shell?.open(uri)
}
interface Props extends Pick<UserSettingsAreaRouteContext, 'authenticatedUser' | 'user'>, TelemetryProps {
/**
* Called when a new access token is created and should be temporarily displayed to the user.
*/
onDidCreateAccessToken: (value: CreateAccessTokenResult['createAccessToken']) => void
isSourcegraphDotCom: boolean
isSourcegraphApp: boolean
}
interface TokenRequester {
/** The name of the source */
@ -106,6 +115,7 @@ export const UserSettingsCreateAccessTokenCallbackPage: React.FC<Props> = ({
onDidCreateAccessToken,
user,
isSourcegraphDotCom,
isSourcegraphApp,
}) => {
const navigate = useNavigate()
const location = useLocation()
@ -150,9 +160,17 @@ export const UserSettingsCreateAccessTokenCallbackPage: React.FC<Props> = ({
nextRequester.redirectURL = redirectURL.toString()
}
}
if (isSourcegraphApp) {
// Append type=app to the url to indicate to the requester that the callback is fulfilled by App
const redirectURL = new URL(nextRequester.redirectURL)
redirectURL.searchParams.set('type', 'app')
nextRequester.redirectURL = redirectURL.toString()
}
setRequester(nextRequester)
setNote(REQUESTERS[requestFrom].name)
}, [isSourcegraphDotCom, location.search, navigate, requestFrom, requester])
}, [isSourcegraphDotCom, isSourcegraphApp, location.search, navigate, requestFrom, requester])
/**
* We use this to handle token creation request from redirections.
* Don't create token if this page wasn't linked to from a valid
@ -169,6 +187,15 @@ export const UserSettingsCreateAccessTokenCallbackPage: React.FC<Props> = ({
onDidCreateAccessToken(result)
setNewToken(result.token)
const uri = replaceToken(requester?.redirectURL, result.token)
// If we're in App, override the callbackType
// because we need to use tauriShellOpen to open the
// callback in a browser.
if (isSourcegraphApp) {
tauriShellOpen(uri)
return
}
switch (requester.callbackType) {
case 'new-tab':
window.open(uri, '_blank')
@ -181,7 +208,7 @@ export const UserSettingsCreateAccessTokenCallbackPage: React.FC<Props> = ({
startWith('loading'),
catchError(error => [asError(error)])
),
[requester, user.id, note, onDidCreateAccessToken]
[requester, user.id, note, onDidCreateAccessToken, isSourcegraphApp]
)
)
/**

View File

@ -14,6 +14,7 @@ import { UserSettingsTokensPage } from './UserSettingsTokensPage'
interface Props extends Pick<UserSettingsAreaRouteContext, 'user' | 'authenticatedUser'>, TelemetryProps {
isSourcegraphDotCom: boolean
isSourcegraphApp: boolean
}
export const UserSettingsTokensArea: React.FunctionComponent<React.PropsWithChildren<Props>> = props => {

View File

@ -3,7 +3,8 @@
"build": {
"beforeBuildCommand": "./src-tauri/compose-assets.sh",
"devPath": "http://localhost:3080/sign-in?secret=foobar&returnTo=/",
"distDir": "./assets"
"distDir": "./assets",
"withGlobalTauri": true
},
"package": {
"productName": "Sourcegraph App",
@ -12,7 +13,6 @@
"tauri": {
"allowlist": {
"shell": {
"open": true,
"sidecar": true,
"scope": [
{
@ -20,7 +20,7 @@
"sidecar": true
}
],
"open": "^https?:|com.sourcegraph.app/.*.log$"
"open": "^(vscode:|https?:)|com.sourcegraph.app/.*.log$"
},
"window": {
"startDragging": true