Skip to content

Commit

Permalink
[GEN-1629]: boolean store (isPolling) (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenElferink authored Nov 10, 2024
1 parent f596073 commit b3b5007
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
7 changes: 4 additions & 3 deletions frontend/webapp/components/overview/add-entity/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Image from 'next/image';
import theme from '@/styles/theme';
import { useModalStore } from '@/store';
import { useOnClickOutside } from '@/hooks';
import React, { useState, useRef } from 'react';
import styled, { css } from 'styled-components';
import { useActualSources, useOnClickOutside } from '@/hooks';
import { useBooleanStore } from '@/store/useBooleanStore';
import { DropdownOption, OVERVIEW_ENTITY_TYPES } from '@/types';
import { Button, FadeLoader, Text } from '@/reuseable-components';

Expand Down Expand Up @@ -71,12 +72,12 @@ interface AddEntityButtonDropdownProps {
}

const AddEntity: React.FC<AddEntityButtonDropdownProps> = ({ options = DEFAULT_OPTIONS, placeholder = 'ADD...' }) => {
const { isPolling } = useBooleanStore();
const { setCurrentModal } = useModalStore();

const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);

const { isPolling } = useActualSources();

useOnClickOutside(dropdownRef, () => setIsDropdownOpen(false));

const handleToggle = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export const useInstrumentationRuleCRUD = (params?: Params) => {
return {
loading: cState.loading || uState.loading || dState.loading,
createInstrumentationRule: (instrumentationRule: InstrumentationRuleInput) => createInstrumentationRule({ variables: { instrumentationRule } }),
updateInstrumentationRule: (ruleId: string, instrumentationRule: InstrumentationRuleInput) =>
updateInstrumentationRule({ variables: { ruleId, instrumentationRule } }),
updateInstrumentationRule: (ruleId: string, instrumentationRule: InstrumentationRuleInput) => updateInstrumentationRule({ variables: { ruleId, instrumentationRule } }),
deleteInstrumentationRule: (ruleId: string) => deleteInstrumentationRule({ variables: { ruleId } }),
};
};
32 changes: 10 additions & 22 deletions frontend/webapp/hooks/sources/useActualSources.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { useState, useCallback } from 'react';
import { useCallback } from 'react';
import { usePersistSource } from '../sources';
import { useNamespace } from '../compute-platform';
import { useUpdateSource } from './useUpdateSource';
import { useComputePlatform } from '../compute-platform';
import {
PatchSourceRequestInput,
PersistSourcesArray,
WorkloadId,
} from '@/types';
import { useBooleanStore } from '@/store/useBooleanStore';
import { PatchSourceRequestInput, PersistSourcesArray, WorkloadId } from '@/types';

export function useActualSources() {
const { data, refetch } = useComputePlatform();
const { isPolling, togglePolling } = useBooleanStore();
const { persistSource, error: sourceError } = usePersistSource();
const { updateSource, error: updateError } = useUpdateSource();

const { persistNamespace } = useNamespace(undefined);
const [isPolling, setIsPolling] = useState(false);

const startPolling = useCallback(async () => {
setIsPolling(true);
togglePolling(true);
const maxRetries = 5;
const retryInterval = 1000; // Poll every second
let retries = 0;
Expand All @@ -29,13 +26,10 @@ export function useActualSources() {
retries++;
}

setIsPolling(false);
}, [refetch]);
togglePolling(false);
}, [refetch, togglePolling]);

const createSourcesForNamespace = async (
namespaceName: string,
sources: PersistSourcesArray[]
) => {
const createSourcesForNamespace = async (namespaceName: string, sources: PersistSourcesArray[]) => {
await persistSource(namespaceName, sources);

startPolling();
Expand All @@ -44,10 +38,7 @@ export function useActualSources() {
}
};

const deleteSourcesForNamespace = async (
namespaceName: string,
sources: PersistSourcesArray[]
) => {
const deleteSourcesForNamespace = async (namespaceName: string, sources: PersistSourcesArray[]) => {
await persistSource(namespaceName, sources);

startPolling();
Expand All @@ -56,10 +47,7 @@ export function useActualSources() {
}
};

const updateActualSource = async (
sourceId: WorkloadId,
patchRequest: PatchSourceRequestInput
) => {
const updateActualSource = async (sourceId: WorkloadId, patchRequest: PatchSourceRequestInput) => {
try {
await updateSource(sourceId, patchRequest);
refetch();
Expand Down
11 changes: 11 additions & 0 deletions frontend/webapp/store/useBooleanStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { create } from 'zustand';

interface StoreState {
isPolling: boolean;
togglePolling: (bool?: boolean) => void;
}

export const useBooleanStore = create<StoreState>((set) => ({
isPolling: false,
togglePolling: (bool) => set(({ isPolling }) => ({ isPolling: bool ?? !isPolling })),
}));

0 comments on commit b3b5007

Please sign in to comment.