Skip to content

Commit 261a6ae

Browse files
author
Greg Trihus
committed
TT-6953 team name update
This PR refactors the OrgHead component to use the modern useOrbitData hook pattern instead of directly querying memory with findRecord. This change aligns the component with the codebases preferred data fetching approach, which provides automatic reactivity to data changes through Orbit.js liveQuery mechanism. Key Changes - Replaced direct memory queries with the reactive useOrbitData hook for fetching organizations - Updated the organization lookup from findRecord to JavaScripts Array.find() method - Enhanced test mocks to support both findRecord and findRecords query patterns
1 parent 40878bf commit 261a6ae

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/renderer/src/components/App/OrgHead.cy.tsx

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,52 @@ import { TeamContext } from '../../context/TeamContext';
1616
import { TokenContext } from '../../context/TokenProvider';
1717
import { OrganizationD } from '@model/organization';
1818

19-
// Create a mock liveQuery object with subscribe and query methods
20-
const createMockLiveQuery = () => ({
21-
subscribe: () => () => {}, // Returns unsubscribe function
22-
query: () => [],
23-
});
24-
2519
// Mock memory with query function that can return organization data
2620
// The findRecord function uses: memory.cache.query((q) => q.findRecord({ type, id }))
2721
// The findRecords function uses: memory.cache.query((q) => q.findRecords(type))
2822
const createMockMemory = (orgData?: OrganizationD): Memory => {
23+
// Store organizations in an array for findRecords queries
24+
const organizations = orgData ? [orgData] : [];
25+
26+
// Create a mock query builder with both findRecord and findRecords
27+
const createMockQueryBuilder = () => ({
28+
findRecord: ({ type, id }: { type: string; id: string }) => {
29+
// Return the organization data if it matches
30+
if (type === 'organization' && id === orgData?.id && orgData) {
31+
return orgData;
32+
}
33+
// Return undefined for other record types (user, role, etc.)
34+
return undefined;
35+
},
36+
findRecords: (type: string) => {
37+
// Return organizations array when querying for 'organization' type
38+
// This is used by useOrbitData('organization')
39+
if (type === 'organization') {
40+
return organizations;
41+
}
42+
// Return empty array for other types (roles, organizationmembership, etc.)
43+
// This is used by useRole for roles, organizationmembership, etc.
44+
return [];
45+
},
46+
});
47+
2948
return {
3049
cache: {
3150
query: (queryFn: (q: any) => any) => {
32-
// Create a mock query builder with both findRecord and findRecords
33-
const mockQueryBuilder = {
34-
findRecord: ({ type, id }: { type: string; id: string }) => {
35-
// Return the organization data if it matches
36-
if (type === 'organization' && id === orgData?.id && orgData) {
37-
return orgData;
38-
}
39-
// Return undefined for other record types (user, role, etc.)
40-
return undefined;
41-
},
42-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
43-
findRecords: (_type: string) => {
44-
// Return empty array for all findRecords queries
45-
// This is used by useRole for roles, organizationmembership, etc.
46-
return [];
51+
// Execute the query function with our mock builder
52+
return queryFn(createMockQueryBuilder());
53+
},
54+
liveQuery: (queryFn: (q: any) => any) => {
55+
// liveQuery captures the query function and returns an object with subscribe and query methods
56+
// When query() is called, it executes the captured query function
57+
return {
58+
subscribe: () => () => {}, // Returns unsubscribe function
59+
query: () => {
60+
// Execute the query function that was passed to liveQuery
61+
return queryFn(createMockQueryBuilder());
4762
},
4863
};
49-
// Execute the query function with our mock builder
50-
return queryFn(mockQueryBuilder);
5164
},
52-
liveQuery: createMockLiveQuery,
5365
},
5466
update: () => {},
5567
} as unknown as Memory;

src/renderer/src/components/App/OrgHead.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from '@mui/material';
1111
import SettingsIcon from '@mui/icons-material/Settings';
1212
import UsersIcon from '@mui/icons-material/People';
13-
import { findRecord } from '../../crud/tryFindRecord';
1413
import { API_CONFIG } from '../../../api-variable';
1514
import { OrganizationD } from '@model/organization';
1615
import TeamDialog, { ITeamDialog } from '../Team/TeamDialog';
@@ -24,10 +23,11 @@ import BigDialog from '../../hoc/BigDialog';
2423
import { BigDialogBp } from '../../hoc/BigDialogBp';
2524
import GroupTabs from '../GroupTabs';
2625
import { useRole } from '../../crud/useRole';
26+
import { useOrbitData } from '../../hoc/useOrbitData';
2727

2828
export const OrgHead = () => {
2929
const [user] = useGlobal('user');
30-
const [memory] = useGlobal('memory');
30+
const organizations = useOrbitData<OrganizationD[]>('organization');
3131
const [editOpen, setEditOpen] = useState(false);
3232
const [deleteItem, setDeleteItem] = useState<RecordIdentity>();
3333
const [openMember, setOpenMember] = useState(false);
@@ -47,9 +47,9 @@ export const OrgHead = () => {
4747
);
4848
const orgRec = useMemo(() => {
4949
if (!orgId) return undefined;
50-
return findRecord(memory, 'organization', orgId) as OrganizationD;
50+
return organizations.find((o) => o.id === orgId);
5151
// eslint-disable-next-line react-hooks/exhaustive-deps
52-
}, [orgId]);
52+
}, [orgId, organizations]);
5353

5454
const handleSettings = () => {
5555
setEditOpen(true);

0 commit comments

Comments
 (0)