-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
logic.ts
121 lines (116 loc) 路 4.18 KB
/
logic.ts
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
import { kea } from 'kea'
import api from 'lib/api'
import { PreflightStatus } from '~/types'
import { preflightLogicType } from './logicType'
import posthog from 'posthog-js'
import { getAppContext } from 'lib/utils/getAppContext'
type PreflightMode = 'experimentation' | 'live'
export const preflightLogic = kea<preflightLogicType<PreflightMode>>({
path: ['scenes', 'PreflightCheck', 'preflightLogic'],
loaders: {
preflight: [
null as PreflightStatus | null,
{
loadPreflight: async () => await api.get('_preflight/'),
},
],
},
actions: {
registerInstrumentationProps: true,
setPreflightMode: (mode: PreflightMode | null, noReload?: boolean) => ({ mode, noReload }),
},
reducers: {
preflightMode: [
null as PreflightMode | null,
{
setPreflightMode: (_, { mode }) => mode,
},
],
},
selectors: {
socialAuthAvailable: [
(s) => [s.preflight],
(preflight): boolean =>
Boolean(preflight && Object.values(preflight.available_social_auth_providers).filter((i) => i).length),
],
clickhouseEnabled: [(s) => [s.preflight], (preflight): boolean => !!preflight?.is_clickhouse_enabled],
realm: [
(s) => [s.preflight],
(preflight): 'cloud' | 'hosted' | 'hosted-clickhouse' | null => {
if (!preflight) {
return null
}
return preflight.realm
},
],
siteUrlMisconfigured: [
(s) => [s.preflight],
(preflight): boolean => {
return Boolean(preflight && (!preflight.site_url || preflight.site_url != window.location.origin))
},
],
configOptions: [
(s) => [s.preflight],
(preflight): Record<string, string>[] => {
// Returns the preflight config options to display in the /instance/status page
const RELEVANT_CONFIGS = [
{
key: 'site_url',
label: 'Site URL',
},
{ key: 'email_service_available', label: 'Email service available' },
]
if (!preflight) {
return []
}
// @ts-ignore
return RELEVANT_CONFIGS.map((config) => ({ metric: config.label, value: preflight[config.key] }))
},
],
},
listeners: ({ values, actions }) => ({
loadPreflightSuccess: () => {
actions.registerInstrumentationProps()
},
registerInstrumentationProps: async (_, breakpoint) => {
await breakpoint(100)
if (posthog && values.preflight) {
posthog.register({
posthog_version: values.preflight.posthog_version,
realm: values.realm,
is_clickhouse_enabled: values.preflight.is_clickhouse_enabled,
ee_available: values.preflight.ee_available,
email_service_available: values.preflight.email_service_available,
})
if (values.preflight.site_url) {
posthog.group('instance', values.preflight.site_url)
}
}
},
setPreflightMode: async ({ mode, noReload }) => {
if (mode && !noReload) {
actions.loadPreflight()
}
},
}),
events: ({ actions, values }) => ({
afterMount: () => {
const preflight = getAppContext()?.preflight
if (preflight) {
actions.loadPreflightSuccess(preflight)
} else if (!values.preflight) {
actions.loadPreflight()
}
},
}),
actionToUrl: ({ values }) => ({
setPreflightMode: () => ['/preflight', { mode: values.preflightMode }],
}),
urlToAction: ({ actions }) => ({
'/preflight': (_, { mode }) => {
if (mode) {
actions.setPreflightMode(mode as PreflightMode, true)
}
},
}),
})