-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview-entry-resolver.js
More file actions
36 lines (27 loc) · 906 Bytes
/
preview-entry-resolver.js
File metadata and controls
36 lines (27 loc) · 906 Bytes
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
const previewEntryNamePattern = /(?:^|\/)(?:app|main)\.[jt]sx?$/i
const normalizeTabIdentity = tab => {
if (!tab || typeof tab !== 'object') {
return ''
}
if (typeof tab.path === 'string' && tab.path.trim().length > 0) {
return tab.path.trim()
}
if (typeof tab.name === 'string' && tab.name.trim().length > 0) {
return tab.name.trim()
}
return ''
}
export const isPreviewEntryTab = tab =>
previewEntryNamePattern.test(normalizeTabIdentity(tab))
export const resolvePreviewEntryTab = tabs => {
if (!Array.isArray(tabs) || tabs.length === 0) {
return null
}
return tabs.find(isPreviewEntryTab) ?? null
}
export const canRenderPreview = ({ tabs, fallbackSource = '' } = {}) => {
if (Array.isArray(tabs) && tabs.length > 0) {
return Boolean(resolvePreviewEntryTab(tabs))
}
return typeof fallbackSource === 'string' && fallbackSource.trim().length > 0
}