refactor: metadata store cleanup, SSE unification, mock metadata loading & login redirect fix (#18651)
## Summary - **SSE unification**: Replaced 11 individual SSE effect components with a single generic `MetadataStoreSSEEffect` - **Metadata store cleanup**: Merged `metadataCollectionHashesState` into `metadataStoreState` (currentCollectionHash / draftCollectionHash per entity), moved `objectMetadataItemsSelector` to `object-metadata` domain, converted `navigationMenuItemsState` to a derived selector - **Naming clarity**: Renamed `isAppMetadataReadyState` → `isMinimalMetadataReadyState`, `MetadataGater` → `MinimalMetadataGater`, `useIsLogged` → `useHasAccessTokenPair`, `patchMetadataStoreFromSSEEvent` now takes named object params - **Mock metadata loading**: Added `generate-navigation-menu-items.ts` script, rewrote `useLoadMockedMinimalMetadata` to load full objects/fields/indexes/views/navItems from generated mock data, enabling proper sign-in background rendering (table columns, view picker, navigation) - **Login/logout transitions**: `MinimalMetadataLoadEffect` manages mocked↔real metadata transitions based on auth state, `MainContextStoreProvider` computes context on auth pages for view picker support - **Login redirect fix**: `handleLoadWorkspaceAfterAuthentication` now re-enables `isAppEffectRedirectEnabled` after `loadCurrentUser()` completes, fixing the blocked post-login navigation - **Dead code removal**: Deleted `useRefreshPageLayouts`, `useApplyPageLayouts`, `useStaleMetadataEntities`, `metadataCollectionHashesState`, and all individual SSE effects ## Test plan - [x] Login from welcome page redirects to companies page - [x] Logout transitions cleanly to mocked metadata on welcome page - [x] Sign-in background shows table columns, view picker, and navigation items - [x] SSE events still update metadata store entries correctly - [x] Navigation menu items persist across page refreshes - [ ] CI: lint, typecheck, tests pass
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
import { Field, ObjectType } from '@nestjs/graphql';
|
||||
|
||||
import {
|
||||
ALL_METADATA_NAME,
|
||||
type AllMetadataName,
|
||||
} from 'twenty-shared/metadata';
|
||||
|
||||
@ObjectType('CollectionHash')
|
||||
export class CollectionHashDTO {
|
||||
@Field(() => ALL_METADATA_NAME)
|
||||
collectionName: AllMetadataName;
|
||||
|
||||
@Field(() => String)
|
||||
hash: string;
|
||||
}
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
import { Field, ObjectType } from '@nestjs/graphql';
|
||||
import { GraphQLJSON } from 'graphql-type-json';
|
||||
|
||||
import { CollectionHashDTO } from 'src/engine/metadata-modules/minimal-metadata/dtos/collection-hash.dto';
|
||||
import { MinimalObjectMetadataDTO } from 'src/engine/metadata-modules/minimal-metadata/dtos/minimal-object-metadata.dto';
|
||||
import { MinimalViewDTO } from 'src/engine/metadata-modules/minimal-metadata/dtos/minimal-view.dto';
|
||||
|
||||
@@ -12,6 +12,6 @@ export class MinimalMetadataDTO {
|
||||
@Field(() => [MinimalViewDTO])
|
||||
views: MinimalViewDTO[];
|
||||
|
||||
@Field(() => GraphQLJSON)
|
||||
collectionHashes: Record<string, string>;
|
||||
@Field(() => [CollectionHashDTO])
|
||||
collectionHashes: CollectionHashDTO[];
|
||||
}
|
||||
|
||||
+10
-8
@@ -9,13 +9,13 @@ import { isDefined, uncapitalize } from 'twenty-shared/utils';
|
||||
|
||||
import { ALL_FLAT_ENTITY_MAPS_PROPERTIES } from 'src/engine/metadata-modules/flat-entity/constant/all-flat-entity-maps-properties.constant';
|
||||
import { WorkspaceManyOrAllFlatEntityMapsCacheService } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.service';
|
||||
import { type CollectionHashDTO } from 'src/engine/metadata-modules/minimal-metadata/dtos/collection-hash.dto';
|
||||
import { MinimalMetadataDTO } from 'src/engine/metadata-modules/minimal-metadata/dtos/minimal-metadata.dto';
|
||||
import { MinimalObjectMetadataDTO } from 'src/engine/metadata-modules/minimal-metadata/dtos/minimal-object-metadata.dto';
|
||||
import { MinimalViewDTO } from 'src/engine/metadata-modules/minimal-metadata/dtos/minimal-view.dto';
|
||||
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
|
||||
import { type WorkspaceCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';
|
||||
|
||||
// Inverse of getMetadataFlatEntityMapsKey: "flatObjectMetadataMaps" -> "objectMetadata"
|
||||
const flatMapsKeyToMetadataName = (
|
||||
flatMapsKey: string,
|
||||
): AllMetadataName | undefined => {
|
||||
@@ -51,15 +51,17 @@ export class MinimalMetadataService {
|
||||
),
|
||||
]);
|
||||
|
||||
const collectionHashes: Record<string, string> = {};
|
||||
const collectionHashes: CollectionHashDTO[] = Object.entries(cacheHashes)
|
||||
.map(([cacheKey, hash]) => {
|
||||
const metadataName = flatMapsKeyToMetadataName(cacheKey);
|
||||
|
||||
for (const [cacheKey, hash] of Object.entries(cacheHashes)) {
|
||||
const metadataName = flatMapsKeyToMetadataName(cacheKey);
|
||||
if (!isDefined(metadataName) || !isDefined(hash)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (isDefined(metadataName) && isDefined(hash)) {
|
||||
collectionHashes[metadataName] = hash;
|
||||
}
|
||||
}
|
||||
return { collectionName: metadataName, hash };
|
||||
})
|
||||
.filter(isDefined);
|
||||
|
||||
const objectMetadataItems: MinimalObjectMetadataDTO[] = Object.values(
|
||||
flatObjectMetadataMaps.byUniversalIdentifier,
|
||||
|
||||
+11
-10
@@ -294,16 +294,17 @@ export class FieldsWidgetUpsertService {
|
||||
return [];
|
||||
}
|
||||
|
||||
const { viewFieldGroupUniversalIdentifier } =
|
||||
resolveEntityRelationUniversalIdentifiers({
|
||||
metadataName: 'viewField',
|
||||
foreignKeyValues: {
|
||||
viewFieldGroupId: newViewFieldGroupId,
|
||||
},
|
||||
flatEntityMaps: {
|
||||
flatViewFieldGroupMaps: optimisticFlatViewFieldGroupMaps,
|
||||
},
|
||||
});
|
||||
const {
|
||||
viewFieldGroupUniversalIdentifier: _viewFieldGroupUniversalIdentifier,
|
||||
} = resolveEntityRelationUniversalIdentifiers({
|
||||
metadataName: 'viewField',
|
||||
foreignKeyValues: {
|
||||
viewFieldGroupId: newViewFieldGroupId,
|
||||
},
|
||||
flatEntityMaps: {
|
||||
flatViewFieldGroupMaps: optimisticFlatViewFieldGroupMaps,
|
||||
},
|
||||
});
|
||||
|
||||
const shouldOverride = isCallerOverridingEntity({
|
||||
callerApplicationUniversalIdentifier: applicationUniversalIdentifier,
|
||||
|
||||
+9
-70
@@ -13,7 +13,6 @@ import {
|
||||
import {
|
||||
combineFilters,
|
||||
isDefined,
|
||||
isMetadataGqlOperationSignature,
|
||||
isNonEmptyArray,
|
||||
isRecordGqlOperationSignature,
|
||||
} from 'twenty-shared/utils';
|
||||
@@ -51,10 +50,7 @@ import { buildRowLevelPermissionRecordFilter } from 'src/engine/twenty-orm/utils
|
||||
import { isRecordMatchingRLSRowLevelPermissionPredicate } from 'src/engine/twenty-orm/utils/is-record-matching-rls-row-level-permission-predicate.util';
|
||||
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
|
||||
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/types/workspace-event-batch.type';
|
||||
import { isMetadataRecordMatchingFilter } from 'src/engine/workspace-event-emitter/utils/is-metadata-record-matching-filter.util';
|
||||
import { parseEventNameOrThrow } from 'src/engine/workspace-event-emitter/utils/parse-event-name';
|
||||
import { type MetadataEvent } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/types/metadata-event';
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceEventEmitterService {
|
||||
constructor(
|
||||
@@ -183,36 +179,22 @@ export class WorkspaceEventEmitterService {
|
||||
|
||||
private async processMetadataStreamEvents(
|
||||
streamChannelId: string,
|
||||
streamData: EventStreamData,
|
||||
_streamData: EventStreamData,
|
||||
metadataEventBatch: MetadataEventBatch,
|
||||
): Promise<void> {
|
||||
const metadataEventsWithQueryIds: {
|
||||
queryIds: string[];
|
||||
metadataEvent: MetadataEvent & { updatedCollectionHash?: string };
|
||||
}[] = [];
|
||||
if (!isNonEmptyArray(metadataEventBatch.events)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const metadataEvent of metadataEventBatch.events) {
|
||||
const matchedQueryIds = this.getMatchingMetadataQueryIds(
|
||||
streamData.queries,
|
||||
metadataEvent,
|
||||
);
|
||||
|
||||
if (!isNonEmptyArray(matchedQueryIds)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
metadataEventsWithQueryIds.push({
|
||||
queryIds: matchedQueryIds,
|
||||
const metadataEventsWithQueryIds = metadataEventBatch.events.map(
|
||||
(metadataEvent) => ({
|
||||
queryIds: [] as string[],
|
||||
metadataEvent: {
|
||||
...metadataEvent,
|
||||
updatedCollectionHash: metadataEventBatch.updatedCollectionHash,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!isNonEmptyArray(metadataEventsWithQueryIds)) {
|
||||
return;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
const payload: EventStreamPayload = {
|
||||
objectRecordEventsWithQueryIds: [],
|
||||
@@ -226,49 +208,6 @@ export class WorkspaceEventEmitterService {
|
||||
});
|
||||
}
|
||||
|
||||
private getMatchingMetadataQueryIds(
|
||||
queries: Record<string, RecordOrMetadataGqlOperationSignature>,
|
||||
metadataEvent: MetadataEvent,
|
||||
): string[] {
|
||||
const properties = metadataEvent.properties as {
|
||||
after?: Record<string, unknown>;
|
||||
before?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
const record = properties?.after ?? properties?.before;
|
||||
|
||||
return Object.entries(queries)
|
||||
.filter(([, operationSignature]) => {
|
||||
if (!isMetadataGqlOperationSignature(operationSignature)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (operationSignature.metadataName !== metadataEvent.metadataName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const queryFilter = (
|
||||
operationSignature.variables as {
|
||||
filter?: Record<string, unknown>;
|
||||
}
|
||||
)?.filter;
|
||||
|
||||
if (!isDefined(queryFilter) || Object.keys(queryFilter).length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!isDefined(record)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isMetadataRecordMatchingFilter({
|
||||
record,
|
||||
filter: queryFilter,
|
||||
});
|
||||
})
|
||||
.map(([queryId]) => queryId);
|
||||
}
|
||||
|
||||
private async processObjectRecordStreamEvents(
|
||||
streamChannelId: string,
|
||||
streamData: EventStreamData,
|
||||
|
||||
Reference in New Issue
Block a user