Skip to content

Commit

Permalink
Adding in a quick fix to make sure a store is active before hydration
Browse files Browse the repository at this point in the history
 completes.
  • Loading branch information
travjenkins committed Oct 13, 2023
1 parent 6e7745f commit 88bd3cd
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
useExistingEntity_hydrated,
useExistingEntity_hydrateState,
useExistingEntity_setActive,
useExistingEntity_setHydrated,
useExistingEntity_setHydrationErrorsExist,
} from 'components/shared/Entity/ExistingEntityCards/Store/hooks';
Expand All @@ -17,7 +18,7 @@ function ExistingEntityHydrator({ children }: BaseComponentProps) {

const hydrated = useExistingEntity_hydrated();
const setHydrated = useExistingEntity_setHydrated();

const setActive = useExistingEntity_setActive();
const setHydrationErrorsExist = useExistingEntity_setHydrationErrorsExist();

const hydrateState = useExistingEntity_hydrateState();
Expand All @@ -28,6 +29,7 @@ function ExistingEntityHydrator({ children }: BaseComponentProps) {
connectorId &&
(entityType === 'capture' || entityType === 'materialization')
) {
setActive(true);
hydrateState(entityType, connectorId).then(
() => {
setHydrated(true);
Expand All @@ -39,12 +41,13 @@ function ExistingEntityHydrator({ children }: BaseComponentProps) {
);
}
}, [
hydrateState,
setHydrated,
setHydrationErrorsExist,
connectorId,
entityType,
hydrateState,
hydrated,
setActive,
setHydrated,
setHydrationErrorsExist,
]);

// eslint-disable-next-line react/jsx-no-useless-fragment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export const useExistingEntity_setHydrated = () => {
>(getStoreName(entityType), (state) => state.setHydrated);
};

export const useExistingEntity_setActive = () => {
const entityType = useEntityType();

return useZustandStore<
ExistingEntityState,
ExistingEntityState['setActive']
>(getStoreName(entityType), (state) => state.setActive);
};

export const useExistingEntity_setHydrationErrorsExist = () => {
const entityType = useEntityType();

Expand Down
4 changes: 3 additions & 1 deletion src/stores/DetailsForm/Hydrator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { logRocketConsole } from 'services/logrocket';
import {
useDetailsForm_hydrated,
useDetailsForm_hydrateState,
useDetailsForm_setActive,
useDetailsForm_setHydrated,
useDetailsForm_setHydrationErrorsExist,
} from 'stores/DetailsForm/hooks';
Expand All @@ -16,7 +17,7 @@ export const DetailsFormHydrator = ({ children }: BaseComponentProps) => {

const hydrated = useDetailsForm_hydrated();
const setHydrated = useDetailsForm_setHydrated();

const setActive = useDetailsForm_setActive();
const setHydrationErrorsExist = useDetailsForm_setHydrationErrorsExist();

const hydrateState = useDetailsForm_hydrateState();
Expand All @@ -26,6 +27,7 @@ export const DetailsFormHydrator = ({ children }: BaseComponentProps) => {
!hydrated &&
(entityType === 'capture' || entityType === 'materialization')
) {
setActive(true);
hydrateState(entityType, workflow).then(
() => {
setHydrated(true);
Expand Down
9 changes: 9 additions & 0 deletions src/stores/DetailsForm/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ export const useDetailsForm_setHydrated = () => {
);
};

export const useDetailsForm_setActive = () => {
const entityType = useEntityType();

return useZustandStore<DetailsFormState, DetailsFormState['setActive']>(
getStoreName(entityType),
(state) => state.setActive
);
};

export const useDetailsForm_hydrationErrorsExist = () => {
const entityType = useEntityType();

Expand Down
3 changes: 3 additions & 0 deletions src/stores/EndpointConfig/Hydrator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { logRocketConsole } from 'services/logrocket';
import {
useEndpointConfig_hydrated,
useEndpointConfig_hydrateState,
useEndpointConfig_setActive,
useEndpointConfig_setHydrated,
useEndpointConfig_setHydrationErrorsExist,
} from 'stores/EndpointConfig/hooks';
Expand All @@ -18,12 +19,14 @@ export const EndpointConfigHydrator = ({ children }: BaseComponentProps) => {
const setHydrated = useEndpointConfig_setHydrated();
const setHydrationErrorsExist = useEndpointConfig_setHydrationErrorsExist();
const hydrateState = useEndpointConfig_hydrateState();
const setActive = useEndpointConfig_setActive();

useEffectOnce(() => {
if (
!hydrated &&
(entityType === 'capture' || entityType === 'materialization')
) {
setActive(true);
hydrateState(entityType, workflow).then(
() => {
setHydrated(true);
Expand Down
24 changes: 9 additions & 15 deletions src/stores/EndpointConfig/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,42 +198,36 @@ const getInitialState = (
const liveSpecId = searchParams.get(GlobalSearchParams.LIVE_SPEC_ID);
const draftId = searchParams.get(GlobalSearchParams.DRAFT_ID);

get().setHydrated(false);

if (
workflow === 'capture_create' ||
workflow === 'materialization_create'
) {
const { setServerUpdateRequired } = get();

setServerUpdateRequired(true);
get().setServerUpdateRequired(true);
}

if (connectorId) {
if (get().active && connectorId) {
const { data, error } = await getSchema_Endpoint(connectorId);

if (error) {
const { setHydrationErrorsExist } = get();

setHydrationErrorsExist(true);
get().setHydrationErrorsExist(true);
}

if (data && data.length > 0) {
const { setEndpointSchema } = get();

setEndpointSchema(
if (get().active && data && data.length > 0) {
get().setEndpointSchema(
data[0].endpoint_spec_schema as unknown as Schema
);
}
}

if (liveSpecId) {
if (get().active && liveSpecId) {
const { data, error } = draftId
? await getDraftSpecsByDraftId(draftId, entityType)
: await getLiveSpecsByLiveSpecId(liveSpecId, entityType);

if (error) {
const { setHydrationErrorsExist } = get();

setHydrationErrorsExist(true);
get().setHydrationErrorsExist(true);
}

if (data && data.length > 0) {
Expand Down
9 changes: 9 additions & 0 deletions src/stores/EndpointConfig/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ export const useEndpointConfig_hydrateState = () => {
>(getStoreName(entityType), (state) => state.hydrateState);
};

export const useEndpointConfig_setActive = () => {
const entityType = useEntityType();

return useZustandStore<
EndpointConfigState,
EndpointConfigState['setActive']
>(getStoreName(entityType), (state) => state.setActive);
};

export const useEndpointConfig_serverUpdateRequired = () => {
const entityType = useEntityType();

Expand Down
8 changes: 8 additions & 0 deletions src/stores/Entities/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export const useEntitiesStore_setHydrated = () => {
(state) => state.setHydrated
);
};
export const useEntitiesStore_setActive = () => {
return useZustandStore<EntitiesState, EntitiesState['setActive']>(
GlobalStoreNames.ENTITIES,
(state) => state.setActive
);
};
export const useEntitiesStore_hydrationErrors = () => {
return useZustandStore<EntitiesState, EntitiesState['hydrationErrors']>(
GlobalStoreNames.ENTITIES,
Expand Down Expand Up @@ -107,10 +113,12 @@ export const useSidePanelDocsStore_resetState = () => {
// We hardcode the key here as we only call once
export const useHydrateState = () => {
const hydrateState = useEntitiesStore_hydrateState();
const setActive = useEntitiesStore_setActive();

const response = useSWR(
'entities_hydrator',
() => {
setActive(true);
return hydrateState();
},
singleCallSettings
Expand Down
4 changes: 3 additions & 1 deletion src/stores/ResourceConfig/Hydrator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BaseComponentProps } from 'types';
import {
useResourceConfig_hydrated,
useResourceConfig_hydrateState,
useResourceConfig_setActive,
useResourceConfig_setHydrated,
useResourceConfig_setHydrationErrorsExist,
} from './hooks';
Expand All @@ -23,12 +24,13 @@ export const ResourceConfigHydrator = ({ children }: BaseComponentProps) => {

const hydrated = useResourceConfig_hydrated();
const setHydrated = useResourceConfig_setHydrated();

const setActive = useResourceConfig_setActive();
const setHydrationErrorsExist = useResourceConfig_setHydrationErrorsExist();

const hydrateState = useResourceConfig_hydrateState();

const hydrateTheState = (rehydrating: boolean) => {
setActive(true);
hydrateState(editWorkflow, entityType, rehydrating).then(
() => {
setHydrated(true);
Expand Down
8 changes: 8 additions & 0 deletions src/stores/ResourceConfig/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
sortBy,
} from 'lodash';
import { createJSONFormDefaults } from 'services/ajv';
import {
getInitialHydrationData,
getStoreWithHydrationSettings,
} from 'stores/extensions/Hydration';
import { ResourceConfigStoreNames } from 'stores/names';
import { Schema } from 'types';
import { devtoolsOptions } from 'utils/store-utils';
Expand All @@ -26,6 +30,8 @@ import { create, StoreApi } from 'zustand';
import { devtools, NamedSet } from 'zustand/middleware';
import { ResourceConfigDictionary, ResourceConfigState } from './types';

const STORE_KEY = 'Resource Config';

const populateCollections = (
state: ResourceConfigState,
collections: string[]
Expand Down Expand Up @@ -139,13 +145,15 @@ const getInitialMiscStoreData = (): Pick<
const getInitialStateData = () => ({
...getInitialCollectionStateData(),
...getInitialMiscStoreData(),
...getInitialHydrationData(),
});

const getInitialState = (
set: NamedSet<ResourceConfigState>,
get: StoreApi<ResourceConfigState>['getState']
): ResourceConfigState => ({
...getInitialStateData(),
...getStoreWithHydrationSettings(STORE_KEY, set),

preFillEmptyCollections: (value, rehydrating) => {
set(
Expand Down
7 changes: 7 additions & 0 deletions src/stores/ResourceConfig/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ export const useResourceConfig_setHydrated = () => {
>(ResourceConfigStoreNames.GENERAL, (state) => state.setHydrated);
};

export const useResourceConfig_setActive = () => {
return useZustandStore<
ResourceConfigState,
ResourceConfigState['setActive']
>(ResourceConfigStoreNames.GENERAL, (state) => state.setActive);
};

export const useResourceConfig_hydrationErrorsExist = () => {
return useZustandStore<
ResourceConfigState,
Expand Down
10 changes: 2 additions & 8 deletions src/stores/ResourceConfig/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DraftSpecQuery } from 'hooks/useDraftSpecs';
import { LiveSpecsExt_MaterializeCapture } from 'hooks/useLiveSpecsExt';
import { CallSupabaseResponse } from 'services/supabase';
import { StoreWithHydration } from 'stores/extensions/Hydration';
import { Entity, EntityWorkflow, JsonFormsData, Schema } from 'types';

export interface ResourceConfig extends JsonFormsData {
Expand All @@ -14,7 +15,7 @@ export interface ResourceConfigDictionary {

// TODO (naming): Determine whether the resourceConfig state property should be made plural.
// It is a dictionary of individual resource configs, so I am leaning "yes."
export interface ResourceConfigState {
export interface ResourceConfigState extends StoreWithHydration {
// Collection Selector
collections: string[] | null;
preFillEmptyCollections: (
Expand Down Expand Up @@ -63,13 +64,6 @@ export interface ResourceConfigState {
resourceSchema: Schema;
setResourceSchema: (val: ResourceConfigState['resourceSchema']) => void;

// Hydration
hydrated: boolean;
setHydrated: (value: boolean) => void;

hydrationErrorsExist: boolean;
setHydrationErrorsExist: (value: boolean) => void;

hydrateState: (
editWorkflow: boolean,
entityType: Entity,
Expand Down
23 changes: 20 additions & 3 deletions src/stores/extensions/Hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ export interface StoreWithHydration {

hydrationErrorsExist: boolean;
setHydrationErrorsExist: (value: boolean) => void;

// TODO (store hydration) we need to make store hydration better
// Used to keep track if the store should be getting hydrated
active: boolean;
setActive: (val: boolean) => void;
}

export const getInitialHydrationData = (): Pick<
StoreWithHydration,
'hydrated' | 'hydrationErrorsExist'
'hydrated' | 'hydrationErrorsExist' | 'active'
> => ({
active: false,
hydrated: false,
hydrationErrorsExist: false,
});
Expand All @@ -34,7 +40,7 @@ export const getStoreWithHydrationSettings = (
setHydrated: (value) => {
set(
produce((state: StoreWithHydration) => {
state.hydrated = value;
state.hydrated = value && state.active ? value : false;
}),
false,
`${key} State Hydrated`
Expand All @@ -44,12 +50,23 @@ export const getStoreWithHydrationSettings = (
setHydrationErrorsExist: (value) => {
set(
produce((state: StoreWithHydration) => {
state.hydrationErrorsExist = value;
state.hydrationErrorsExist =
value && state.active ? value : false;
}),
false,
`${key} Hydration Errors Detected`
);
},

setActive: (val) => {
set(
produce((state: StoreWithHydration) => {
state.active = val;
}),
false,
`${key} Active Set`
);
},
};
};

Expand Down

0 comments on commit 88bd3cd

Please sign in to comment.