Skip to content

Commit 94daa28

Browse files
authored
ref(seer): Refactor Code Review table to better optimistically update (#112979)
Actually update the query cache, and remove that weird workaround stuff
1 parent 335ee16 commit 94daa28

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

static/gsApp/views/seerAutomation/components/repoTable/seerRepoTable.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,20 @@ export function SeerRepoTable() {
107107
isFetchingNextPage,
108108
} = result;
109109

110-
const [mutationData, setMutations] = useState<Record<string, RepositoryWithSettings>>(
111-
{}
112-
);
113-
114110
const {mutate: mutateRepositorySettings} = useBulkUpdateRepositorySettings({
115111
onSuccess: mutations => {
116-
setMutations(prev => {
117-
const updated = {...prev};
118-
mutations.forEach(mutation => {
119-
updated[mutation.id] = mutation;
120-
});
121-
return updated;
112+
const mutationMap = new Map(mutations.map(m => [m.id, m]));
113+
queryClient.setQueryData(queryOptions.queryKey, prev => {
114+
if (!prev) {
115+
return prev;
116+
}
117+
return {
118+
...prev,
119+
pages: prev.pages.map(page => ({
120+
...page,
121+
json: page.json.map(repo => mutationMap.get(repo.id) ?? repo),
122+
})),
123+
};
122124
});
123125
},
124126
onSettled: mutations => {
@@ -192,10 +194,10 @@ export function SeerRepoTable() {
192194
<TablePanel>
193195
<SeerRepoTableHeader
194196
gridColumns={GRID_COLUMNS}
197+
isFetchingNextPage={isFetchingNextPage}
198+
isPending={isPending}
195199
mutateRepositorySettings={mutateRepositorySettings}
196200
onSortClick={setSort}
197-
isPending={isPending}
198-
isFetchingNextPage={isFetchingNextPage}
199201
sort={sort}
200202
/>
201203
{isPending ? (
@@ -221,7 +223,6 @@ export function SeerRepoTable() {
221223
hasNextPage={hasNextPage}
222224
isFetchingNextPage={isFetchingNextPage}
223225
mutateRepositorySettings={mutateRepositorySettings}
224-
mutationData={mutationData}
225226
repositories={repositories}
226227
scrollBodyRef={scrollBodyRef}
227228
/>
@@ -236,14 +237,12 @@ function VirtualizedRepoTable({
236237
hasNextPage,
237238
isFetchingNextPage,
238239
mutateRepositorySettings,
239-
mutationData,
240240
repositories,
241241
scrollBodyRef,
242242
}: {
243243
hasNextPage: boolean;
244244
isFetchingNextPage: boolean;
245245
mutateRepositorySettings: ReturnType<typeof useBulkUpdateRepositorySettings>['mutate'];
246-
mutationData: Record<string, RepositoryWithSettings>;
247246
repositories: RepositoryWithSettings[];
248247
scrollBodyRef: React.RefObject<HTMLDivElement | null>;
249248
}) {
@@ -296,7 +295,6 @@ function VirtualizedRepoTable({
296295
gridColumns={GRID_COLUMNS}
297296
style={{transform: `translateY(${virtualItem.start}px)`}}
298297
mutateRepositorySettings={mutateRepositorySettings}
299-
mutationData={mutationData}
300298
repository={repository}
301299
/>
302300
);

static/gsApp/views/seerAutomation/components/repoTable/seerRepoTableRow.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ import {
1313
} from 'sentry/actionCreators/indicator';
1414
import {getRepoStatusLabel} from 'sentry/components/repositories/getRepoStatusLabel';
1515
import {useBulkUpdateRepositorySettings} from 'sentry/components/repositories/useBulkUpdateRepositorySettings';
16-
import {
17-
getRepositoryWithSettingsQueryKey,
18-
useRepositoryWithSettings,
19-
} from 'sentry/components/repositories/useRepositoryWithSettings';
16+
import {getRepositoryWithSettingsQueryKey} from 'sentry/components/repositories/useRepositoryWithSettings';
2017
import {SimpleTable} from 'sentry/components/tables/simpleTable';
2118
import {IconOpen} from 'sentry/icons/iconOpen';
2219
import {t} from 'sentry/locale';
@@ -35,36 +32,21 @@ import {useCanWriteSettings} from 'getsentry/views/seerAutomation/components/use
3532
interface Props {
3633
gridColumns: string;
3734
mutateRepositorySettings: ReturnType<typeof useBulkUpdateRepositorySettings>['mutate'];
38-
mutationData: Record<string, RepositoryWithSettings>;
3935
repository: RepositoryWithSettings;
4036
style?: React.CSSProperties;
4137
}
4238

4339
export function SeerRepoTableRow({
4440
gridColumns,
4541
mutateRepositorySettings,
46-
mutationData,
47-
repository: initialRepository,
42+
repository,
4843
style,
4944
}: Props) {
5045
const queryClient = useQueryClient();
5146
const organization = useOrganization();
5247
const canWrite = useCanWriteSettings();
5348
const {isSelected, toggleSelected} = useListItemCheckboxContext();
5449

55-
// We're defaulting to read from the list data, which is passed in as `initialRepository`
56-
// But if an update has been made to one record, we're going to enable reading
57-
// from `useRepositoryWithSettings` which will have the latest data, letting us
58-
// do optimistic updates, without re-rendering the entire table.
59-
// `initialRepository` will become stale at that point, but we'll have fresh data
60-
// in the cache to override it.
61-
const {data, isError, isPending} = useRepositoryWithSettings({
62-
repositoryId: initialRepository.id,
63-
initialData: [initialRepository, undefined, undefined],
64-
enabled: mutationData[initialRepository.id] !== undefined,
65-
});
66-
const repository = isError || isPending ? initialRepository : data;
67-
6850
return (
6951
<Grid
7052
columns={gridColumns}

0 commit comments

Comments
 (0)