-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseSplitgraphRepoUrls.tsx
38 lines (33 loc) · 1.24 KB
/
useSplitgraphRepoUrls.tsx
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
36
37
38
import useImmutableSWR from 'swr/immutable';
import { unifiedFetcher, SplitgraphURLBatch } from './data'
import { DiffResponse } from './data/seafowl';
import { selectIdNameDomain } from './util';
/** Append namespace/repository to this URL */
const SPLITGRAPH_URL = 'https://www.splitgraph.com'
interface ReturnedRepositories {
socrataExternalRepositories: Array<Repository>;
}
interface Repository {
namespace: string;
repository: string;
}
/** Returns a Splitgraph namespace + repository name for a given SocrataDatasetKey */
const useSplitgraphRepoUrls = (diffData: DiffResponse[] | undefined) => {
const datasets = selectIdNameDomain(diffData);
const { data, error } = useImmutableSWR<ReturnedRepositories>([SplitgraphURLBatch, datasets], unifiedFetcher)
let response;
if (data) {
const { socrataExternalRepositories } = data;
response = diffData?.map((diffResponse, index) =>
(
{
...diffResponse,
splitgraphURL: `${SPLITGRAPH_URL}/${socrataExternalRepositories[index]['namespace']}/${socrataExternalRepositories[index]['repository']}`
}
))
} else if (error) {
console.warn('Unable to fetch repo URLs')
}
return { dataWithSplitgraphURLs: response, error };
}
export default useSplitgraphRepoUrls;