Skip to content
Snippets Groups Projects
Select Git revision
  • cf2025
  • main default protected
  • cf2024
  • cf2023-euro
  • cf2023-offline
5 results

global-info.js

Blame
  • global-info.js 1.79 KiB
    import isArray from "lodash/isArray";
    import { createAsyncAction, errorResult, successResult } from "pullstate";
    
    import { fetchApi } from "api";
    import { markdownConverter } from "markdown";
    import { GlobalInfoStore } from "stores";
    
    export const loadConfig = createAsyncAction(
      async () => {
        try {
          const resp = await fetchApi("/config");
          const payload = await resp.json();
    
          if (!isArray(payload)) {
            return errorResult([], "Unexpected response format");
          }
    
          return successResult(payload);
        } catch (err) {
          return errorResult([], err.toString());
        }
      },
      {
        postActionHook: ({ result }) => {
          if (!result.error) {
            GlobalInfoStore.update((state) => {
              result.payload.forEach((rawConfigItem) => {
                if (rawConfigItem.id === "stream_url") {
                  state.streamUrl = rawConfigItem.value;
                }
                if (rawConfigItem.id === "websocket_url") {
                  state.websocketUrl = rawConfigItem.value;
                }
                if (rawConfigItem.id === "record_url") {
                  state.protocolUrl = rawConfigItem.value;
                }
              });
            });
          }
        },
      },
    );
    
    export const loadProtocol = createAsyncAction(
      async () => {
        const { protocolUrl } = GlobalInfoStore.getRawState();
    
        try {
          const resp = await fetch(protocolUrl);
    
          if (resp.status !== 200) {
            return errorResult([], `Unexpected status code ${resp.status}`);
          }
    
          return successResult(await resp.text());
        } catch (err) {
          return errorResult([], err.toString());
        }
      },
      {
        postActionHook: ({ result }) => {
          if (!result.error) {
            GlobalInfoStore.update((state) => {
              state.protocol = markdownConverter.makeHtml(result.payload);
            });
          }
        },
      },
    );