openreplay/frontend/app/mstore/settingsStore.ts
Delirium 5e21d88e8c
feat(tracker): Msg buffering and conditional recording (#1775)
* feat(tracker) start message buffering support

* feat(tracker): buffered recordings

* feat(tracker): buffered recordings timedelay adjust

* fix(tracker): condition manager

* fix(tracker): conditions handlers

* fix(tracker): conditions

* fix(tracker): pre-fetch feature flags and conditions, fix naming and dnt check repeating

* fix(tracker): fix conditions fetch

* feat(tracker): test coverage for conditionsManager

* feat(tracker): some api connections

* feat(tracker): fix projid in session info

* feat(tracker): added fetch req status condition, partially added offline recording, type fixes

* fix(tracker): fix tests

* fix(tracker): fix network req c

* fix(tracker): fix conditions test

* feat(ui): conditional recording ui

* fix(tracker): fix prestart callbacks

* feat(ui): conditions ui and api stuff

* feat(ui): fix ?

* fix(tracker): map raw db response in tracker

* fix(tracker): fix condition processing, add cond name to trigger event, change unit tests

* fix(tracker): simplify mapping, rename functions

* fix(tracker): change toggler design, change network request condition

* fix(tracker): some formatting

* fix(tracker): reformat logging

* fix(ui): rm console log
2024-01-09 13:18:26 +01:00

152 lines
4.4 KiB
TypeScript

import { makeAutoObservable, observable } from 'mobx';
import SessionSettings from './types/sessionSettings';
import { sessionService } from 'App/services';
import { toast } from 'react-toastify';
import Webhook, { IWebhook } from 'Types/webhook';
import { webhookService } from 'App/services';
import { GettingStarted } from './types/gettingStarted';
import { MENU_COLLAPSED } from 'App/constants/storageKeys';
interface CaptureConditions {
rate: number;
captureAll: boolean;
conditions: { name: string; captureRate: number; filters: any[] }[];
}
export default class SettingsStore {
loadingCaptureRate: boolean = false;
sessionSettings: SessionSettings = new SessionSettings();
captureRateFetched: boolean = false;
limits: any = null;
webhooks: Webhook[] = [];
webhookInst = new Webhook();
hooksLoading = false;
gettingStarted: GettingStarted = new GettingStarted();
menuCollapsed: boolean = localStorage.getItem(MENU_COLLAPSED) === 'true';
constructor() {
makeAutoObservable(this, {
sessionSettings: observable,
});
}
updateMenuCollapsed = (collapsed: boolean) => {
this.menuCollapsed = collapsed;
localStorage.setItem(MENU_COLLAPSED, collapsed.toString());
};
saveCaptureRate = (projectId: number, data: any) => {
return sessionService
.saveCaptureRate(projectId, data)
.then((data) => data.json())
.then(({ data }) => {
this.sessionSettings.merge({
captureRate: data.rate,
captureAll: data.captureAll,
});
toast.success('Settings updated successfully');
})
.catch((err) => {
toast.error('Error saving capture rate');
});
};
fetchCaptureRate = (projectId: number): Promise<any> => {
this.loadingCaptureRate = true;
return sessionService
.fetchCaptureRate(projectId)
.then((data) => {
this.sessionSettings.merge({
captureRate: data.rate,
captureAll: data.captureAll,
});
this.captureRateFetched = true;
})
.finally(() => {
this.loadingCaptureRate = false;
});
};
fetchCaptureConditions = (projectId: number): Promise<any> => {
this.loadingCaptureRate = true;
return sessionService
.fetchCaptureConditions(projectId)
.then((data) => {
this.sessionSettings.merge({
captureRate: data.rate,
captureAll: data.captureAll,
captureConditions: data.conditions,
});
})
.finally(() => {
this.loadingCaptureRate = false;
});
};
updateCaptureConditions = (projectId: number, data: CaptureConditions) => {
this.loadingCaptureRate = true;
return sessionService
.saveCaptureConditions(projectId, data)
.then((data) => data.json())
.then(({ data }) => {
this.sessionSettings.merge({
captureRate: data.rate,
captureAll: data.captureAll,
captureConditions: data.conditions,
});
toast.success('Settings updated successfully');
})
.catch((err) => {
toast.error('Error saving capture rate');
})
.finally(() => {
this.loadingCaptureRate = false;
});
};
fetchWebhooks = () => {
this.hooksLoading = true;
return webhookService.fetchList().then((data) => {
this.webhooks = data.map((hook) => new Webhook(hook));
this.hooksLoading = false;
});
};
initWebhook = (inst?: Partial<IWebhook> | Webhook) => {
this.webhookInst = inst instanceof Webhook ? inst : new Webhook(inst);
};
saveWebhook = (inst: Webhook) => {
this.hooksLoading = true;
return webhookService
.saveWebhook(inst)
.then((data) => {
this.webhookInst = new Webhook(data);
if (inst.webhookId === undefined) this.setWebhooks([...this.webhooks, this.webhookInst]);
else
this.setWebhooks([
...this.webhooks.filter((hook) => hook.webhookId !== data.webhookId),
this.webhookInst,
]);
})
.finally(() => {
this.hooksLoading = false;
});
};
setWebhooks = (webhooks: Webhook[]) => {
this.webhooks = webhooks;
};
removeWebhook = (hookId: string) => {
this.hooksLoading = true;
return webhookService.removeWebhook(hookId).then(() => {
this.webhooks = this.webhooks.filter((hook) => hook.webhookId !== hookId);
this.hooksLoading = false;
});
};
editWebhook = (diff: Partial<IWebhook>) => {
Object.assign(this.webhookInst, diff);
};
}