-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6841aa7
commit 52ee76a
Showing
35 changed files
with
1,968 additions
and
90 deletions.
There are no files selected for viewing
Empty file.
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
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,84 @@ | ||
/** | ||
* This is a GraphQL approach to querying project information. | ||
*/ | ||
|
||
import {gql, useApolloClient} from '@apollo/client'; | ||
import {useEffect, useState} from 'react'; | ||
|
||
// Note: id is the "external" ID, which changes when a project is renamed. | ||
// internalId does not change. | ||
const PROJECT_QUERY = gql` | ||
query Project($entityName: String!, $projectName: String!) { | ||
project(name: $projectName, entityName: $entityName) { | ||
id | ||
internalId | ||
} | ||
} | ||
`; | ||
|
||
export type ProjectInfo = { | ||
externalIdEncoded: string; | ||
internalIdEncoded: string; | ||
}; | ||
type ProjectInfoResponseLoading = { | ||
loading: true; | ||
projectInfo: {}; | ||
}; | ||
export type MaybeProjectInfo = ProjectInfo | null; | ||
type ProjectInfoResponseSuccess = { | ||
loading: false; | ||
projectInfo: MaybeProjectInfo; | ||
}; | ||
type ProjectInfoResponse = | ||
| ProjectInfoResponseLoading | ||
| ProjectInfoResponseSuccess; | ||
|
||
export const useProjectInfo = ( | ||
entityName: string, | ||
projectName: string | ||
): ProjectInfoResponse => { | ||
const [response, setResponse] = useState<ProjectInfoResponse>({ | ||
loading: true, | ||
projectInfo: {}, | ||
}); | ||
|
||
const apolloClient = useApolloClient(); | ||
|
||
useEffect(() => { | ||
let mounted = true; | ||
apolloClient | ||
.query({ | ||
query: PROJECT_QUERY as any, | ||
variables: { | ||
entityName, | ||
projectName, | ||
}, | ||
}) | ||
.then(result => { | ||
if (!mounted) { | ||
return; | ||
} | ||
const projectInfo = result.data.project; | ||
if (!projectInfo) { | ||
// Invalid project | ||
setResponse({ | ||
loading: false, | ||
projectInfo: null, | ||
}); | ||
return; | ||
} | ||
setResponse({ | ||
loading: false, | ||
projectInfo: { | ||
externalIdEncoded: projectInfo.id, | ||
internalIdEncoded: projectInfo.internalId, | ||
}, | ||
}); | ||
}); | ||
return () => { | ||
mounted = false; | ||
}; | ||
}, [apolloClient, entityName, projectName]); | ||
|
||
return response; | ||
}; |
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
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
Oops, something went wrong.