Changeset View
Changeset View
Standalone View
Standalone View
js_modules/dagit/src/workspace/WorkspaceContext.tsx
import {gql, useQuery} from '@apollo/client'; | import {ApolloQueryResult, gql, useQuery} from '@apollo/client'; | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import {useRouteMatch} from 'react-router-dom'; | import {useRouteMatch} from 'react-router-dom'; | ||||
import {PythonErrorInfo} from 'src/PythonErrorInfo'; | import {PythonErrorInfo} from 'src/PythonErrorInfo'; | ||||
import {REPOSITORY_INFO_FRAGMENT} from 'src/RepositoryInformation'; | import {REPOSITORY_INFO_FRAGMENT} from 'src/RepositoryInformation'; | ||||
import {RepositorySelector} from 'src/types/globalTypes'; | import {RepositorySelector} from 'src/types/globalTypes'; | ||||
import {repoAddressAsString} from 'src/workspace/repoAddressAsString'; | import {repoAddressAsString} from 'src/workspace/repoAddressAsString'; | ||||
import {repoAddressFromPath} from 'src/workspace/repoAddressFromPath'; | import {repoAddressFromPath} from 'src/workspace/repoAddressFromPath'; | ||||
import {RepoAddress} from 'src/workspace/types'; | import {RepoAddress} from 'src/workspace/types'; | ||||
import {RepositoryLocationsQuery} from 'src/workspace/types/RepositoryLocationsQuery'; | |||||
import { | import { | ||||
RootRepositoriesQuery, | RootRepositoriesQuery, | ||||
RootRepositoriesQuery_repositoryLocationsOrError_PythonError, | RootRepositoriesQuery_repositoryLocationsOrError_PythonError, | ||||
RootRepositoriesQuery_repositoryLocationsOrError_RepositoryLocationConnection_nodes, | RootRepositoriesQuery_repositoryLocationsOrError_RepositoryLocationConnection_nodes, | ||||
RootRepositoriesQuery_repositoryLocationsOrError_RepositoryLocationConnection_nodes_RepositoryLocation, | RootRepositoriesQuery_repositoryLocationsOrError_RepositoryLocationConnection_nodes_RepositoryLocation, | ||||
RootRepositoriesQuery_repositoryLocationsOrError_RepositoryLocationConnection_nodes_RepositoryLocation_repositories, | RootRepositoriesQuery_repositoryLocationsOrError_RepositoryLocationConnection_nodes_RepositoryLocation_repositories, | ||||
} from 'src/workspace/types/RootRepositoriesQuery'; | } from 'src/workspace/types/RootRepositoriesQuery'; | ||||
Show All 15 Lines | type WorkspaceState = { | ||||
locations: RepositoryLocationNode[]; | locations: RepositoryLocationNode[]; | ||||
allRepos: DagsterRepoOption[]; | allRepos: DagsterRepoOption[]; | ||||
activeRepo: null | { | activeRepo: null | { | ||||
repo: DagsterRepoOption; | repo: DagsterRepoOption; | ||||
address: RepoAddress; | address: RepoAddress; | ||||
path: string; | path: string; | ||||
}; | }; | ||||
repoPath: string | null; | repoPath: string | null; | ||||
refetch: () => Promise<ApolloQueryResult<RootRepositoriesQuery>>; | |||||
}; | }; | ||||
export const WorkspaceContext = React.createContext<WorkspaceState>( | export const WorkspaceContext = React.createContext<WorkspaceState>( | ||||
new Error('WorkspaceContext should never be uninitialized') as any, | new Error('WorkspaceContext should never be uninitialized') as any, | ||||
); | ); | ||||
const ROOT_REPOSITORIES_QUERY = gql` | const ROOT_REPOSITORIES_QUERY = gql` | ||||
query RootRepositoriesQuery { | query RootRepositoriesQuery { | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | ... on RepositoryLocationConnection { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
...PythonErrorFragment | ...PythonErrorFragment | ||||
} | } | ||||
${PythonErrorInfo.fragments.PythonErrorFragment} | ${PythonErrorInfo.fragments.PythonErrorFragment} | ||||
`; | `; | ||||
const REPOSITORY_LOCATIONS_QUERY = gql` | |||||
query RepositoryLocationsQuery { | |||||
repositoryLocationsOrError { | |||||
...RepositoryLocationsFragment | |||||
} | |||||
} | |||||
${REPOSITORY_LOCATIONS_FRAGMENT} | |||||
`; | |||||
const getRepositoryOptionHash = (a: DagsterRepoOption) => | const getRepositoryOptionHash = (a: DagsterRepoOption) => | ||||
`${a.repository.name}:${a.repositoryLocation.name}`; | `${a.repository.name}:${a.repositoryLocation.name}`; | ||||
export const isRepositoryOptionEqual = (a: DagsterRepoOption, b: DagsterRepoOption) => | export const isRepositoryOptionEqual = (a: DagsterRepoOption, b: DagsterRepoOption) => | ||||
getRepositoryOptionHash(a) === getRepositoryOptionHash(b); | getRepositoryOptionHash(a) === getRepositoryOptionHash(b); | ||||
/** | /** | ||||
* useLocalStorageState vends `[repo, setRepo]` and internally mirrors the current | * useLocalStorageState vends `[repo, setRepo]` and internally mirrors the current | ||||
Show All 23 Lines | ) { | ||||
setRepoKey(getRepositoryOptionHash(last || options[0])); | setRepoKey(getRepositoryOptionHash(last || options[0])); | ||||
} | } | ||||
}, [repoKey, options]); | }, [repoKey, options]); | ||||
const repoForKey = options.find((o) => getRepositoryOptionHash(o) === repoKey) || null; | const repoForKey = options.find((o) => getRepositoryOptionHash(o) === repoKey) || null; | ||||
return [repoForKey, setRepo] as [typeof repoForKey, typeof setRepo]; | return [repoForKey, setRepo] as [typeof repoForKey, typeof setRepo]; | ||||
}; | }; | ||||
export const useNetworkedRepositoryLocations = () => { | |||||
const {data, loading, refetch} = useQuery<RepositoryLocationsQuery>(REPOSITORY_LOCATIONS_QUERY, { | |||||
fetchPolicy: 'no-cache', | |||||
}); | |||||
const locations = React.useMemo(() => { | |||||
return data?.repositoryLocationsOrError.__typename === 'RepositoryLocationConnection' | |||||
? data?.repositoryLocationsOrError.nodes | |||||
: []; | |||||
}, [data]); | |||||
return { | |||||
loading, | |||||
locations, | |||||
refetch, | |||||
}; | |||||
}; | |||||
/** | /** | ||||
* A hook that supplies the current workspace state of Dagit, including the current | * A hook that supplies the current workspace state of Dagit, including the current | ||||
* "active" repo based on the URL or localStorage, all fetched repositories available | * "active" repo based on the URL or localStorage, all fetched repositories available | ||||
* in the workspace, and loading/error state for the relevant query. | * in the workspace, and loading/error state for the relevant query. | ||||
*/ | */ | ||||
const useWorkspaceState = () => { | const useWorkspaceState = () => { | ||||
const match = useRouteMatch<{repoPath: string}>(['/workspace/:repoPath']); | const match = useRouteMatch<{repoPath: string}>(['/workspace/:repoPath']); | ||||
const repoPath: string | null = match?.params?.repoPath || null; | const repoPath: string | null = match?.params?.repoPath || null; | ||||
const {data, loading} = useQuery<RootRepositoriesQuery>(ROOT_REPOSITORIES_QUERY, { | const {data, loading, refetch} = useQuery<RootRepositoriesQuery>(ROOT_REPOSITORIES_QUERY, { | ||||
fetchPolicy: 'cache-and-network', | fetchPolicy: 'cache-and-network', | ||||
}); | }); | ||||
const locations = React.useMemo(() => { | const locations = React.useMemo(() => { | ||||
return data?.repositoryLocationsOrError.__typename === 'RepositoryLocationConnection' | return data?.repositoryLocationsOrError.__typename === 'RepositoryLocationConnection' | ||||
? data?.repositoryLocationsOrError.nodes | ? data?.repositoryLocationsOrError.nodes | ||||
: []; | : []; | ||||
}, [data]); | }, [data]); | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | const useWorkspaceState = () => { | ||||
// Update local storage with the latest active repo. | // Update local storage with the latest active repo. | ||||
React.useEffect(() => { | React.useEffect(() => { | ||||
if (activeRepo?.repo && activeRepo.repo !== localStorageRepo) { | if (activeRepo?.repo && activeRepo.repo !== localStorageRepo) { | ||||
setLocalStorageRepo(activeRepo.repo); | setLocalStorageRepo(activeRepo.repo); | ||||
} | } | ||||
}, [activeRepo, localStorageRepo, setLocalStorageRepo]); | }, [activeRepo, localStorageRepo, setLocalStorageRepo]); | ||||
return { | return { | ||||
refetch, | |||||
loading, | loading, | ||||
error, | error, | ||||
locations, | locations, | ||||
allRepos: options, | allRepos: options, | ||||
activeRepo, | activeRepo, | ||||
repoPath, | repoPath, | ||||
}; | }; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |