Skip to content

Commit 1934e34

Browse files
refactor: create custom sandboxByPath fragment to reduce query payload
1 parent d77c675 commit 1934e34

File tree

7 files changed

+114
-20
lines changed

7 files changed

+114
-20
lines changed

packages/app/src/app/graphql/types.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4678,6 +4678,46 @@ export type RecentlyDeletedTeamSandboxesQuery = {
46784678
} | null;
46794679
};
46804680

4681+
export type SandboxByPathFragment = {
4682+
__typename?: 'Sandbox';
4683+
id: string;
4684+
alias: string | null;
4685+
title: string | null;
4686+
insertedAt: string;
4687+
updatedAt: string;
4688+
screenshotUrl: string | null;
4689+
isV2: boolean;
4690+
isFrozen: boolean;
4691+
privacy: number;
4692+
restricted: boolean;
4693+
draft: boolean;
4694+
viewCount: number;
4695+
teamId: any | null;
4696+
source: { __typename?: 'Source'; template: string | null };
4697+
customTemplate: {
4698+
__typename?: 'Template';
4699+
id: any | null;
4700+
iconUrl: string | null;
4701+
} | null;
4702+
forkedTemplate: {
4703+
__typename?: 'Template';
4704+
id: any | null;
4705+
color: string | null;
4706+
iconUrl: string | null;
4707+
} | null;
4708+
collection: {
4709+
__typename?: 'Collection';
4710+
path: string;
4711+
id: any | null;
4712+
} | null;
4713+
author: { __typename?: 'User'; username: string } | null;
4714+
permissions: {
4715+
__typename?: 'SandboxProtectionSettings';
4716+
preventSandboxLeaving: boolean;
4717+
preventSandboxExport: boolean;
4718+
} | null;
4719+
};
4720+
46814721
export type SandboxesByPathQueryVariables = Exact<{
46824722
path: Scalars['String'];
46834723
teamId: InputMaybe<Scalars['ID']>;
@@ -4703,20 +4743,15 @@ export type SandboxesByPathQuery = {
47034743
id: string;
47044744
alias: string | null;
47054745
title: string | null;
4706-
description: string | null;
4707-
lastAccessedAt: any;
47084746
insertedAt: string;
47094747
updatedAt: string;
4710-
removedAt: string | null;
4711-
privacy: number;
4712-
isFrozen: boolean;
47134748
screenshotUrl: string | null;
4714-
viewCount: number;
4715-
likeCount: number;
47164749
isV2: boolean;
4717-
draft: boolean;
4750+
isFrozen: boolean;
4751+
privacy: number;
47184752
restricted: boolean;
4719-
authorId: any | null;
4753+
draft: boolean;
4754+
viewCount: number;
47204755
teamId: any | null;
47214756
source: { __typename?: 'Source'; template: string | null };
47224757
customTemplate: {

packages/app/src/app/overmind/effects/gql/dashboard/queries.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,53 @@ export const deletedTeamSandboxes: Query<
9898
${RECENTLY_DELETED_TEAM_SANDBOXES_FRAGMENT}
9999
`;
100100

101+
const SANDBOX_BY_PATH_FRAGMENT = gql`
102+
fragment sandboxByPath on Sandbox {
103+
id
104+
alias
105+
title
106+
insertedAt
107+
updatedAt
108+
screenshotUrl
109+
isV2
110+
isFrozen
111+
privacy
112+
restricted
113+
draft
114+
viewCount
115+
116+
source {
117+
template
118+
}
119+
120+
customTemplate {
121+
id
122+
iconUrl
123+
}
124+
125+
forkedTemplate {
126+
id
127+
color
128+
iconUrl
129+
}
130+
131+
collection {
132+
path
133+
id
134+
}
135+
136+
author {
137+
username
138+
}
139+
teamId
140+
141+
permissions {
142+
preventSandboxLeaving
143+
preventSandboxExport
144+
}
145+
}
146+
`;
147+
101148
export const sandboxesByPath: Query<
102149
SandboxesByPathQuery,
103150
SandboxesByPathQueryVariables
@@ -113,12 +160,12 @@ export const sandboxesByPath: Query<
113160
id
114161
path
115162
sandboxes {
116-
...sandboxFragmentDashboard
163+
...sandboxByPath
117164
}
118165
}
119166
}
120167
}
121-
${sandboxFragmentDashboard}
168+
${SANDBOX_BY_PATH_FRAGMENT}
122169
${sidebarCollectionDashboard}
123170
`;
124171

packages/app/src/app/overmind/namespaces/dashboard/internalActions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Context } from 'app/overmind';
2-
import { SandboxFragmentDashboardFragment } from 'app/graphql/types';
2+
import {
3+
SandboxFragmentDashboardFragment,
4+
SandboxByPathFragment,
5+
} from 'app/graphql/types';
36

47
/**
58
* Change sandbox frozen in state and returns the sandboxes that have changed in their old state
@@ -15,14 +18,14 @@ export const changeSandboxesInState = (
1518
* The mutation that happens on the sandbox, make sure to return a *new* sandbox here, to make sure
1619
* that we can still rollback easily in the future.
1720
*/
18-
sandboxMutation: <T extends SandboxFragmentDashboardFragment>(
21+
sandboxMutation: <T extends SandboxFragmentDashboardFragment | SandboxByPathFragment>(
1922
sandbox: T
2023
) => T;
2124
}
2225
) => {
2326
const changedSandboxes: Set<ReturnType<typeof sandboxMutation>> = new Set();
2427

25-
const doMutateSandbox = <T extends SandboxFragmentDashboardFragment>(
28+
const doMutateSandbox = <T extends SandboxFragmentDashboardFragment | SandboxByPathFragment>(
2629
sandbox: T
2730
): T => {
2831
changedSandboxes.add(sandbox);

packages/app/src/app/overmind/namespaces/dashboard/state.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
SandboxFragmentDashboardFragment as Sandbox,
3+
SandboxByPathFragment,
34
RepoFragmentDashboardFragment as Repo,
45
TemplateFragmentDashboardFragment as Template,
56
TeamFragmentDashboardFragment,
@@ -25,7 +26,7 @@ export type DashboardSandboxStructure = {
2526
TEMPLATE_HOME: Template[] | null;
2627
SHARED: Sandbox[] | null;
2728
ALL: {
28-
[path: string]: Sandbox[];
29+
[path: string]: (Sandbox | SandboxByPathFragment)[];
2930
} | null;
3031
REPOS: {
3132
[path: string]: {
@@ -48,7 +49,7 @@ export type State = {
4849
viewMode: 'grid' | 'list';
4950
orderBy: OrderBy;
5051
getFilteredSandboxes: (
51-
sandboxes: Array<Sandbox | RecentlyDeletedTeamSandboxesFragment | Repo | Template['sandbox']>
52+
sandboxes: Array<Sandbox | SandboxByPathFragment | RecentlyDeletedTeamSandboxesFragment | Repo | Template['sandbox']>
5253
) => Sandbox[];
5354
deletedSandboxesByTime: {
5455
week: RecentlyDeletedTeamSandboxesFragment[];

packages/app/src/app/overmind/namespaces/profile/state.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Profile, Sandbox, UserSandbox } from '@codesandbox/common/lib/types';
22
import {
33
Collection,
44
SandboxFragmentDashboardFragment as CollectionSandbox,
5+
SandboxByPathFragment,
56
} from 'app/graphql/types';
67
import { Context } from 'app/overmind';
78
import { derived } from 'overmind';
@@ -11,7 +12,7 @@ export type ProfileCollection = Pick<
1112
Collection,
1213
'id' | 'path' | 'sandboxCount'
1314
> & {
14-
sandboxes: CollectionSandbox[];
15+
sandboxes: (CollectionSandbox | SandboxByPathFragment)[];
1516
};
1617

1718
type State = {

packages/app/src/app/pages/Profile/SandboxPicker/SandboxCard.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import {
1313
import designLanguage from '@codesandbox/components/lib/design-language/theme';
1414
import css from '@styled-system/css';
1515
import { Sandbox } from '@codesandbox/common/lib/types';
16-
import { SandboxFragmentDashboardFragment } from 'app/graphql/types';
16+
import {
17+
SandboxFragmentDashboardFragment,
18+
SandboxByPathFragment,
19+
} from 'app/graphql/types';
1720
import { SandboxType } from '../constants';
1821

1922
const PrivacyIcons = {
@@ -29,7 +32,7 @@ const privacyToName = {
2932
};
3033

3134
export const SandboxCard: React.FC<{
32-
sandbox: Sandbox | SandboxFragmentDashboardFragment;
35+
sandbox: Sandbox | SandboxFragmentDashboardFragment | SandboxByPathFragment;
3336
onClick?: (event: React.MouseEvent<HTMLDivElement>) => void;
3437
}> = ({ sandbox, onClick }) => {
3538
const { contextMenu } = useAppState().profile;

packages/app/src/app/pages/Profile/SandboxPicker/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010
Column,
1111
} from '@codesandbox/components';
1212
import css from '@styled-system/css';
13+
import {
14+
SandboxFragmentDashboardFragment,
15+
SandboxByPathFragment,
16+
} from 'app/graphql/types';
1317
import { SandboxCard, SkeletonCard } from './SandboxCard';
1418
import { FolderCard } from './FolderCard';
1519
import { ProfileCollectionType } from '../constants';
@@ -53,7 +57,7 @@ export const SandboxPicker: React.FC<{ closeModal?: () => void }> = ({
5357
.filter(collection => collection.parent === selectedPath)
5458
.filter(collection => collection.path !== selectedPath);
5559

56-
const sandboxesInPath =
60+
const sandboxesInPath: (SandboxFragmentDashboardFragment | SandboxByPathFragment)[] =
5761
collections.find(collection => collection.path === selectedPath)
5862
?.sandboxes || [];
5963

0 commit comments

Comments
 (0)