Skip to content

Commit e49ebc1

Browse files
authored
feat: add Initialize Client CLI onboarding page (#5084)
* feat: add init token page to cli wizard
1 parent 75be787 commit e49ebc1

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Libraries
2+
import React, {FC, useEffect, useMemo} from 'react'
3+
import {useDispatch, useSelector} from 'react-redux'
4+
5+
// Actions
6+
import {
7+
createAuthorization,
8+
getAllResources,
9+
} from 'src/authorizations/actions/thunks'
10+
11+
// Selectors
12+
import {getOrg} from 'src/organizations/selectors'
13+
import {getMe} from 'src/me/selectors'
14+
import {getAllTokensResources} from 'src/resources/selectors'
15+
16+
// Helper Components
17+
import CodeSnippet from 'src/shared/components/CodeSnippet'
18+
19+
// Utils
20+
import {allAccessPermissions} from 'src/authorizations/utils/permissions'
21+
import {event} from 'src/cloud/utils/reporting'
22+
23+
// Types
24+
import {AppState, Authorization} from 'src/types'
25+
26+
type OwnProps = {
27+
wizardEventName: string
28+
setTokenValue: (tokenValue: string) => void
29+
tokenValue: string
30+
}
31+
32+
// Style
33+
34+
const inlineStyle = {
35+
marginTop: '0px',
36+
marginBottom: '8px',
37+
}
38+
39+
const collator = new Intl.Collator(navigator.language || 'en-US')
40+
41+
export const InitializeClient: FC<OwnProps> = ({
42+
wizardEventName,
43+
setTokenValue,
44+
tokenValue,
45+
}) => {
46+
const org = useSelector(getOrg)
47+
const me = useSelector(getMe)
48+
const allPermissionTypes = useSelector(getAllTokensResources)
49+
const dispatch = useDispatch()
50+
const url =
51+
me.quartzMe?.clusterHost || 'https://us-west-2-1.aws.cloud2.influxdata.com/'
52+
const orgID = org.id
53+
const currentAuth = useSelector((state: AppState) => {
54+
return state.resources.tokens.currentAuth.item
55+
})
56+
const token = currentAuth.token
57+
58+
const codeSnippet = `influx config create --config-name onboarding
59+
--host-url "${url}"
60+
--org "${orgID}"
61+
--token "${token}"
62+
--active`
63+
64+
const bucketSnippet = `influx bucket create --name sample-bucket -c onboarding`
65+
66+
const sortedPermissionTypes = useMemo(
67+
() => allPermissionTypes.sort((a, b) => collator.compare(a, b)),
68+
[allPermissionTypes]
69+
)
70+
71+
useEffect(() => {
72+
const fetchResources = async () => {
73+
await dispatch(getAllResources())
74+
}
75+
fetchResources()
76+
}, [])
77+
78+
useEffect(() => {
79+
if (sortedPermissionTypes.length && tokenValue === null) {
80+
const authorization: Authorization = {
81+
orgID: org.id,
82+
description: `onboarding-${wizardEventName}-token-${Date.now()}`,
83+
permissions: allAccessPermissions(sortedPermissionTypes, org.id, me.id),
84+
}
85+
86+
dispatch(createAuthorization(authorization))
87+
event(`firstMile.${wizardEventName}.tokens.tokenCreated`)
88+
}
89+
}, [sortedPermissionTypes.length])
90+
91+
// when token generated, save it to the parent component
92+
useEffect(() => {
93+
if (currentAuth.token) {
94+
setTokenValue(currentAuth.token)
95+
}
96+
}, [currentAuth.token])
97+
98+
// Events log handling
99+
const logCopyCodeSnippet = () => {
100+
event(`firstMile.${wizardEventName}.buckets.code.copied`)
101+
}
102+
103+
return (
104+
<>
105+
<h1>Initialize Client</h1>
106+
<h2 style={inlineStyle}>Configure an InfluxDB profile</h2>
107+
<p style={inlineStyle}>
108+
Next we'll need to configure the client and its initial connection to
109+
InfluxDB.
110+
</p>
111+
<p>
112+
The snippet below creates a configuration profile named onboarding. You
113+
may choose a different name if you'd like. You'll likely create another
114+
profile using a different token for working with your own data.
115+
</p>
116+
<CodeSnippet
117+
text={codeSnippet}
118+
onCopy={logCopyCodeSnippet}
119+
language="properties"
120+
/>
121+
<p style={{fontSize: '14px', marginTop: '8px', marginBottom: '48px'}}>
122+
Note: we've created an all-access token here for simplicity, but we
123+
recommend using a token with more specific permissions in the long-term.
124+
You can edit your tokens anytime from the app.
125+
</p>
126+
<h2 style={{marginTop: '48px', marginBottom: '8px'}}>Create a bucket</h2>
127+
<p style={inlineStyle}>
128+
Now we'll create a bucket. A <b>bucket</b> is used to store time-series
129+
data. We'll link the bucket to the profile you created.
130+
</p>
131+
<CodeSnippet
132+
text={bucketSnippet}
133+
onCopy={logCopyCodeSnippet}
134+
language="properties"
135+
/>
136+
</>
137+
)
138+
}

0 commit comments

Comments
 (0)