openreplay/frontend/app/mstore/integrationsStore.ts
Delirium a71381da40
getting rid of redux for good (#2556)
* start moving ui to redux tlk

* remove unused reducer

* changes for gdpr and site types

* ui: migrating duck/roles to mobx

* ui: drop unreferenced types

* ui: drop unreferenced types

* ui: move player slice reducer to mobx family

* ui: move assignments to issueReportingStore.ts

* remove issues store

* some fixes after issues store

* remove errors reducer, drop old components

* finish removing errors reducer

* start moving integrations state to mobx

* change(ui): funnel duck cleanup

* change(ui): custom fields

* change(ui): customMetrics cleanup

* change(ui): customMetrics cleanup

* change(ui): duck/filters minor cleanup

* change(ui): duck/filters cleanup

* change(ui): duck/customMetrics cleanup and upgrades

* fix integrations service, fix babel config to >.25 + not ie

* refactoring integrations reducers etc WIP

* finish removing integrations state

* some fixes for integrated check

* start of projects refactoring

* move api and "few" files to new project store

* new batch for site -> projects

* fix setid context

* move all critical components, drop site duck

* remove all duck/site refs, remove old components

* fixup for SessionTags.tsx, remove duck/sources (?)

* move session store

* init sessionstore outside of context

* fix userfilter

* replace simple actions for session store

* sessions sotre

* Rtm temp (#2597)

* change(ui): duck/search wip

* change(ui): duck/search wip

* change(ui): duck/search wip

* change(ui): duck/searchLive wip

* change(ui): duck/searchLive wip

* change(ui): duck/searchLive wip

* change(ui): duck/searchLive wip

* change(ui): search states

* change(ui): search states

* change(ui): search states

* change(ui): fix savedSearch store

* change(ui): fix savedSearch store

* some fixes for session connector

* change(ui): fix savedSearch store

* change(ui): fix searchLive

* change(ui): fix searchLive

* fixes for session replay

* change(ui): bookmark fetch

* last components for sessions

* add fetchautoplaylist

* finish session reducer, remove deleted reducers

* change(ui): fix the search fetch

* change(ui): fix the search fetch

* fix integrations call ctx

* ensure ctx for sessionstore

* fix(ui): checking for latest sessions path

* start removing user reducer

* removing user reducer pt2...

* finish user store

* remove rand log

* fix crashes

* tinkering workflow file for tracker test

* making sure prefetched sessions work properly

* fix conflict

* fix router redirects during loading

---------

Co-authored-by: Shekar Siri <sshekarsiri@gmail.com>
2024-10-03 11:38:36 +02:00

296 lines
7.2 KiB
TypeScript

import { makeAutoObservable } from 'mobx';
import { integrationsService } from 'App/services';
import ElasticsearchForm from "../components/Client/Integrations/ElasticsearchForm";
import { MessengerConfig } from './types/integrations/messengers';
import {
Bugsnag,
Cloudwatch,
DatadogInt,
ElasticSearchInt,
GithubInt,
Integration,
IssueTracker,
JiraInt,
NewRelicInt,
RollbarInt,
SentryInt,
StackDriverInt,
SumoLogic,
} from './types/integrations/services';
class GenericIntegrationsStore {
list: any[] = [];
isLoading: boolean = false;
siteId: string = '';
constructor() {
makeAutoObservable(this);
}
setSiteId(siteId: string) {
this.siteId = siteId;
}
setList(list: any[]) {
this.list = list;
}
setLoading(loading: boolean) {
this.isLoading = loading;
}
fetchIntegrations = async (siteId?: string) => {
this.setLoading(true);
try {
const { data } = await integrationsService.fetchList(undefined, siteId);
this.setList(data);
} catch (e) {
console.log(e);
} finally {
this.setLoading(false);
}
};
}
class NamedIntegrationStore<T extends Integration> {
instance: T | null = null;
list: T[] = [];
fetched: boolean = false;
issuesFetched: boolean = false;
loading = false;
constructor(
private readonly name: string,
private readonly namedTypeCreator: (config: Record<string, any>) => T
) {
this.instance = namedTypeCreator({});
makeAutoObservable(this);
}
setLoading(loading: boolean): void {
this.loading = loading;
}
setInstance(instance: T): void {
this.instance = instance;
}
setList = (list: T[]): void => {
this.list = list;
}
setFetched = (fetched: boolean): void => {
this.fetched = fetched;
}
setIssuesFetched = (issuesFetched: boolean): void => {
this.issuesFetched = issuesFetched;
}
fetchIntegrations = async (): Promise<void> => {
this.setLoading(true);
try {
const { data } = await integrationsService.fetchList(this.name);
if (Array.isArray(data)) {
this.setList(
data.map((config: Record<string, any>) => this.namedTypeCreator(config))
);
} else {
this.setList([this.namedTypeCreator(data)]);
}
} catch (e) {
console.log(e);
} finally {
this.setFetched(true);
this.setLoading(false);
}
};
fetchIntegration = async (siteId: string): Promise<void> => {
this.setLoading(true);
try {
const { data } = await integrationsService.fetchIntegration(
this.name,
siteId
);
this.setInstance(this.namedTypeCreator(data));
} catch (e) {
console.log(e);
} finally {
this.setLoading(false);
}
};
saveIntegration = async (name: string, siteId?: string): Promise<void> => {
if (!this.instance) return;
await integrationsService.saveIntegration(
this.name ?? name,
this.instance.toData(),
siteId
);
return;
}
edit = (data: T): void => {
if (!this.instance) {
this.instance = this.namedTypeCreator({});
}
this.instance.edit(data);
}
deleteIntegration = async (siteId?: string) => {
if (!this.instance) return;
return integrationsService.removeIntegration(this.name, siteId);
}
init = (config: Record<string, any>): void => {
this.instance = this.namedTypeCreator(config);
}
}
class MessengerIntegrationStore {
list: MessengerConfig[] = [];
instance: MessengerConfig | null = null;
loaded: boolean = false;
loading: boolean = false;
errors: any[] = [];
constructor(private readonly mName: 'slack' | 'msteams') {
makeAutoObservable(this);
}
setList(list: MessengerConfig[]): void {
this.list = list;
}
setLoading(loading: boolean): void {
this.loading = loading;
}
setInstance(instance: MessengerConfig): void {
this.instance = instance;
}
setLoaded(loaded: boolean): void {
this.loaded = loaded;
}
setErrors = (errors: any[]) => {
this.errors = errors;
};
saveIntegration = async (): Promise<void> => {
// redux todo: errors
if (!this.instance) return;
this.setLoading(true);
try {
await integrationsService.saveIntegration(
this.mName,
this.instance.toData(),
undefined
);
this.setList([...this.list, this.instance]);
} catch (e) {
console.log(e);
this.setErrors(["Couldn't process the request: check your data."]);
} finally {
this.setLoading(false);
}
};
fetchIntegrations = async (): Promise<void> => {
const { data } = await integrationsService.fetchMessengerChannels(
this.mName
);
this.setList(
data.map((config: Record<string, any>) => new MessengerConfig(config))
);
this.setLoaded(true);
};
sendMessage = ({
integrationId,
entity,
entityId,
data,
}: {
integrationId: string;
entity: string;
entityId: string;
data: any;
}) => {
return integrationsService.sendMsg(
integrationId,
entity,
entityId,
this.mName,
data
);
};
init = (config: Record<string, any>): void => {
this.instance = new MessengerConfig(config);
};
removeInt = async (intId: string) => {
await integrationsService.removeMessengerInt(this.mName, intId);
this.setList(this.list.filter((int) => int.webhookId !== intId));
};
edit = (data: Record<string, any>): void => {
if (!this.instance) {
this.instance = new MessengerConfig({});
}
this.instance.edit(data);
};
update = async () => {
// redux todo: errors
if (!this.instance) return;
this.setLoading(true);
await integrationsService.updateMessengerInt(
this.mName,
this.instance.toData()
);
this.setList(
this.list.map((int) =>
int.webhookId === this.instance?.webhookId ? this.instance : int
)
);
this.setLoading(false);
};
}
export type namedStore = 'sentry'
| 'datadog'
| 'stackdriver'
| 'rollbar'
| 'newrelic'
| 'bugsnag'
| 'cloudwatch'
| 'elasticsearch'
| 'sumologic'
| 'jira'
| 'github'
| 'issues'
export class IntegrationsStore {
sentry = new NamedIntegrationStore('sentry', (d) => new SentryInt(d));
datadog = new NamedIntegrationStore('datadog', (d) => new DatadogInt(d));
stackdriver = new NamedIntegrationStore('stackdriver', (d) => new StackDriverInt(d));
rollbar = new NamedIntegrationStore('rollbar', (d) => new RollbarInt(d));
newrelic = new NamedIntegrationStore('newrelic', (d) => new NewRelicInt(d));
bugsnag = new NamedIntegrationStore('bugsnag', (d) => new Bugsnag(d));
cloudwatch = new NamedIntegrationStore('cloudwatch', (d) => new Cloudwatch(d));
elasticsearch = new NamedIntegrationStore('elasticsearch', (d) => new ElasticSearchInt(d));
sumologic = new NamedIntegrationStore('sumologic', (d) => new SumoLogic(d));
jira = new NamedIntegrationStore('jira', (d) => new JiraInt(d));
github = new NamedIntegrationStore('github', (d) => new GithubInt(d));
issues = new NamedIntegrationStore('issues', (d) => new IssueTracker(d));
integrations = new GenericIntegrationsStore();
slack = new MessengerIntegrationStore('slack');
msteams = new MessengerIntegrationStore('msteams');
constructor() {
makeAutoObservable(this);
}
}