forked from Guepard-Corp/qwery-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasource-navigation.ts
More file actions
81 lines (75 loc) · 2.19 KB
/
datasource-navigation.ts
File metadata and controls
81 lines (75 loc) · 2.19 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Resolves a datasource by id, slug, or name and opens its view in a new tab.
* Used by getSchema UI (datasource names, error cards, minimal table names) and any other link that should open the datasource page.
*/
export interface DatasourceItemLike {
id: string;
name?: string;
slug?: string;
}
function slugify(s: string): string {
return (
String(s)
.replace(/\s+/g, '_')
.replace(/[^a-zA-Z0-9_-]/g, '') || s
);
}
export function resolveDatasource(
items: DatasourceItemLike[],
idOrSlug: string,
name: string,
): DatasourceItemLike | undefined {
return (
items.find((d) => d.id === idOrSlug) ??
items.find((d) => d.slug === idOrSlug) ??
items.find(
(d) =>
slugify(d.name ?? '') === idOrSlug ||
slugify(d.slug ?? '') === idOrSlug,
) ??
items.find((d) => d.name === name)
);
}
/**
* Resolves the datasource and opens its view URL in a new tab.
* @param getPath - e.g. createDatasourceViewPath from project.navigation.config
*/
export function openDatasourceInNewTab(
items: DatasourceItemLike[],
idOrSlug: string,
name: string,
getPath: (slug: string) => string,
): void {
const ds = resolveDatasource(items, idOrSlug, name);
if (ds?.slug) {
const path = getPath(ds.slug);
openUrlInNewTab(path);
}
}
/**
* Opens a relative or absolute URL in a new tab.
*/
export function openUrlInNewTab(pathOrUrl: string): void {
const url = pathOrUrl.startsWith('http')
? pathOrUrl
: `${typeof window !== 'undefined' ? window.location.origin : ''}${pathOrUrl}`;
window.open(url, '_blank', 'noopener,noreferrer');
}
/**
* Resolves the datasource and opens the table view URL in a new tab.
* @param getTablePath - e.g. createDatasourceTableViewPath(slug, schema, tableName)
*/
export function openTableInNewTab(
items: DatasourceItemLike[],
datasourceIdOrSlug: string,
datasourceName: string,
schema: string,
tableName: string,
getTablePath: (slug: string, schema: string, tableName: string) => string,
): void {
const ds = resolveDatasource(items, datasourceIdOrSlug, datasourceName);
if (ds?.slug) {
const path = getTablePath(ds.slug, schema || 'main', tableName);
openUrlInNewTab(path);
}
}