forked from devhubapp/devhub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.ts
More file actions
152 lines (125 loc) · 4.54 KB
/
shared.ts
File metadata and controls
152 lines (125 loc) · 4.54 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import qs from 'qs'
import { findDOMNode } from 'react-dom'
import { constants } from '@devhub/core'
import {
AppLayoutProviderState,
getLayoutConsumerState,
} from '../../components/context/LayoutContext'
import { Browser } from '../../libs/browser'
import { Linking } from '../../libs/linking'
import { Platform } from '../../libs/platform'
export function findNode(ref: any) {
try {
let node = ref && (ref.current || ref)
if (node && (node as any).getNode && (node as any).getNode())
node = (node as any).getNode()
if (node && (node as any)._touchableNode)
node = (node as any)._touchableNode
if (node && (node as any)._node) node = (node as any)._node
if (node && Platform.OS === 'web') node = findDOMNode(node)
return node
} catch (error) {
console.error('Failed to find node', error, { ref })
return null
}
}
export function tryFocus(ref: any) {
const node = findNode(ref)
if (node && node.focus) {
if (!(node.tabIndex >= 0)) node.tabIndex = -1
node.focus()
return true
}
return false
}
export function getGitHubAppInstallUri(
options: {
redirectUri?: string | undefined
suggestedTargetId?: number | string | undefined
repositoryIds?: Array<number | string> | undefined
} = {},
) {
const query: Record<string, any> = {}
const redirectUri =
options.redirectUri ||
(Platform.OS === 'ios' || Platform.OS === 'android' || Platform.isElectron
? 'devhub://'
: Platform.OS === 'web'
? window.location.origin
: '')
if (redirectUri) query.redirect_uri = redirectUri
if (options.repositoryIds) query.repository_ids = options.repositoryIds
if (options.suggestedTargetId)
query.suggested_target_id = options.suggestedTargetId
const querystring = qs.stringify(query, {
arrayFormat: 'brackets',
encode: false,
})
const baseUri = `${constants.API_BASE_URL}/github/app/install`
return `${baseUri}${querystring ? `?${querystring}` : ''}`
}
export async function openAppStore({
showReviewModal,
}: { showReviewModal?: boolean } = {}) {
try {
if (Platform.realOS === 'android') {
let storeUrl = `market://details?id=${constants.GOOGLEPLAY_ID}`
if (Platform.OS === 'android' && (await Linking.canOpenURL(storeUrl))) {
if (__DEV__) console.log(`Requested to open Play Store: ${storeUrl}`) // tslint:disable-line no-console
await Linking.openURL(storeUrl)
return true
}
storeUrl = `https://play.google.com/store/apps/details?id=${
constants.GOOGLEPLAY_ID
}`
if (__DEV__) console.log(`Requested to open Play Store: ${storeUrl}`) // tslint:disable-line no-console
await Browser.openURL(storeUrl)
return true
}
if (Platform.realOS === 'ios') {
let storeUrl = `itms-apps://itunes.apple.com/app/id${
constants.APPSTORE_ID
}`
if (Platform.OS === 'ios' && (await Linking.canOpenURL(storeUrl))) {
if (__DEV__) console.log(`Requested to open App Store: ${storeUrl}`) // tslint:disable-line no-console
await Linking.openURL(storeUrl)
return true
}
storeUrl = showReviewModal
? `https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=${
constants.APPSTORE_ID
}&pageNumber=0&sortOrdering=2&mt=8`
: `https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=${
constants.APPSTORE_ID
}&pageNumber=0&sortOrdering=2&mt=8`
if (__DEV__) console.log(`Requested to open App Store: ${storeUrl}`) // tslint:disable-line no-console
await Browser.openURL(storeUrl)
return true
}
throw new Error(`Invalid platform: ${Platform.realOS}`)
} catch (error) {
if (__DEV__) console.error(`Failed to open App Store: ${error}`) // tslint:disable-line no-console
return false
}
}
export function genericParseText<T extends string>(
text: string,
pattern: RegExp,
fn: (match: T) => React.ReactNode,
) {
if (!(text && typeof text === 'string')) return [text].filter(Boolean)
const matches = text.match(new RegExp(pattern, 'g')) as T[]
if (!(matches && matches.length)) return [text].filter(Boolean)
return text.split(pattern).reduce(
(result, item, index) => {
if (!matches[index]) return result.concat([item].filter(Boolean))
return result.concat([item, fn(matches[index])].filter(Boolean))
},
[] as React.ReactNode[],
)
}
export function isBigEnoughForMultiColumnView(
sizename?: AppLayoutProviderState['sizename'],
) {
return (sizename || getLayoutConsumerState().sizename) >= '2-medium'
}