-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathuseSelectAssetSource.ts
More file actions
35 lines (30 loc) · 1.46 KB
/
Copy pathuseSelectAssetSource.ts
File metadata and controls
35 lines (30 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { useCallback, useMemo } from 'react';
import { useMutation, useQuery } from '@apollo/client';
import { ExecutionResult } from 'graphql';
import { useSetRecoilState } from 'recoil';
import { SELECTED_ASSET_SOURCE_ID, SET_SELECTED_ASSET_SOURCE_ID } from '../queries';
import { AssetSource } from '../interfaces';
import { useAssetSourcesQuery } from './index';
import { currentPageState } from '../state';
const useSelectAssetSource = (): [AssetSource, (assetSource: AssetSource) => Promise<ExecutionResult<any>>] => {
const { data } = useQuery(SELECTED_ASSET_SOURCE_ID);
const selectedAssetSourceId = useMemo(() => data?.selectedAssetSourceId ?? null, [data]);
const { assetSources } = useAssetSourcesQuery();
const setCurrentPage = useSetRecoilState(currentPageState);
const selectedAssetSource = useMemo(
() => assetSources.find((assetSource) => assetSource.id === selectedAssetSourceId),
[assetSources, selectedAssetSourceId]
);
const [mutateSelectedAssetSourceId] = useMutation(SET_SELECTED_ASSET_SOURCE_ID);
const setSelectedAssetSource = useCallback(
(assetSource: AssetSource) => {
setCurrentPage(1);
return mutateSelectedAssetSourceId({
variables: { selectedAssetSourceId: assetSource.id },
});
},
[mutateSelectedAssetSourceId, setCurrentPage]
);
return [selectedAssetSource, setSelectedAssetSource];
};
export default useSelectAssetSource;