Skip to content

Commit 4d8ac38

Browse files
committed
feat: Update cache when editing files
https://harperdb.atlassian.net/browse/STUDIO-483
1 parent 2f9f8c6 commit 4d8ac38

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/features/instance/applications/context/EditorViewProvider.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
SetComponentFileRequest,
1010
useSetComponentFile,
1111
} from '@/features/instance/operations/mutations/setComponentFile';
12-
import { getComponentFileQueryOptions } from '@/features/instance/operations/queries/getComponentFile';
12+
import {
13+
getComponentFileQueryKey,
14+
getComponentFileQueryOptions,
15+
} from '@/features/instance/operations/queries/getComponentFile';
1316
import {
1417
APIDirectoryEntry,
1518
APIFileEntry,
@@ -114,6 +117,11 @@ export function EditorViewProvider({ children }: PropsWithChildren) {
114117
const pathToLoad = openedEntry && (isDirectory(openedEntry) ? openedEntry.overviewEntry?.path : openedEntry.path) || '';
115118
const projectToLoad = openedEntry && (isDirectory(openedEntry) ? openedEntry.overviewEntry?.project : openedEntry.project) || '';
116119
const loadedOverviewEntry = openedEntry && (isDirectory(openedEntry) ? !!openedEntry.overviewEntry?.path : false) || false;
120+
const fileQueryKey = getComponentFileQueryKey({
121+
file: pathToLoad?.split('/').slice(1).join('/'),
122+
project: projectToLoad,
123+
...instanceParams,
124+
});
117125
const { data: getComponentFileQueryData } = useQuery(
118126
getComponentFileQueryOptions(
119127
{
@@ -148,16 +156,17 @@ export function EditorViewProvider({ children }: PropsWithChildren) {
148156
saveComponentFile(data, {
149157
onSuccess: () => {
150158
if (openedEntry?.path === filePath && data.payload !== undefined) {
151-
setOpenedEntryContents(data.payload || undefined);
152159
setUpdatedEntryContents(undefined);
160+
setOpenedEntryContents(data.payload || undefined);
161+
queryClient.setQueryData(fileQueryKey, { ...getComponentFileQueryData, message: data.payload });
153162
}
154163
},
155164
onError: (error) => {
156165
console.error('Error saving file:', error);
157166
},
158167
});
159168
},
160-
[saveComponentFile, setUpdatedEntryContents, openedEntry],
169+
[saveComponentFile, setUpdatedEntryContents, setOpenedEntryContents, openedEntry],
161170
);
162171

163172
const restrictPackageModification = useMemo(() => {

src/features/instance/operations/queries/getComponentFile.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,20 @@ export async function getComponentFile({
3333
};
3434
}
3535

36-
export function getComponentFileQueryOptions({ entityId, instanceClient, file, project }: GetComponentFileRequest) {
36+
export function getComponentFileQueryOptions(params: GetComponentFileRequest) {
3737
return queryOptions({
38-
queryKey: [
39-
entityId,
40-
queryKeys.operations.get_component_file,
41-
file,
42-
project,
43-
] as const,
44-
queryFn: () => getComponentFile({ entityId, instanceClient, file, project }),
45-
enabled: !!file && !!project,
38+
queryKey: getComponentFileQueryKey(params),
39+
queryFn: () => getComponentFile(params),
40+
enabled: !!params.file && !!params.project,
4641
retry: false,
4742
});
4843
}
44+
45+
export function getComponentFileQueryKey(params: GetComponentFileRequest) {
46+
return [
47+
params.entityId,
48+
queryKeys.operations.get_component_file,
49+
params.file,
50+
params.project,
51+
] as const;
52+
}

0 commit comments

Comments
 (0)