Fixed missing update if content of diff changes (#1714)

* Fixed missing update if content of diff changes

* Add property to disable automatic refetch in diffs

Co-authored-by: René Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
Sebastian Sdorra
2021-06-30 16:36:26 +02:00
committed by GitHub
parent 59c15feb87
commit e5ebb78146
7 changed files with 4515 additions and 81 deletions

View File

@@ -30,9 +30,14 @@ import { Diff, Link } from "@scm-manager/ui-types";
type UseDiffOptions = {
limit?: number;
refetchOnWindowFocus?: boolean;
};
export const useDiff = (link: string, options: UseDiffOptions = {}) => {
const defaultOptions: UseDiffOptions = {
refetchOnWindowFocus: true,
};
export const useDiff = (link: string, options: UseDiffOptions = defaultOptions) => {
let initialLink = link;
if (options.limit) {
const separator = initialLink.includes("?") ? "&" : "?";
@@ -41,7 +46,7 @@ export const useDiff = (link: string, options: UseDiffOptions = {}) => {
const { isLoading, error, data, isFetchingNextPage, fetchNextPage } = useInfiniteQuery<Diff, Error, Diff>(
["link", link],
({ pageParam }) => {
return apiClient.get(pageParam || initialLink).then(response => {
return apiClient.get(pageParam || initialLink).then((response) => {
const contentType = response.headers.get("Content-Type");
if (contentType && contentType.toLowerCase() === "application/vnd.scmm-diffparsed+json;v=2") {
return response.json();
@@ -49,18 +54,19 @@ export const useDiff = (link: string, options: UseDiffOptions = {}) => {
return response
.text()
.then(parser.parse)
.then(parsedGit => {
.then((parsedGit) => {
return {
files: parsedGit,
partial: false,
_links: {}
_links: {},
};
});
}
});
},
{
getNextPageParam: lastPage => (lastPage._links.next as Link)?.href
getNextPageParam: (lastPage) => (lastPage._links.next as Link)?.href,
refetchOnWindowFocus: options.refetchOnWindowFocus,
}
);
@@ -71,7 +77,7 @@ export const useDiff = (link: string, options: UseDiffOptions = {}) => {
fetchNextPage: () => {
fetchNextPage();
},
data: merge(data?.pages)
data: merge(data?.pages),
};
};
@@ -79,9 +85,9 @@ const merge = (diffs?: Diff[]): Diff | undefined => {
if (!diffs || diffs.length === 0) {
return;
}
const joinedFiles = diffs.flatMap(diff => diff.files);
const joinedFiles = diffs.flatMap((diff) => diff.files);
return {
...diffs[diffs.length - 1],
files: joinedFiles
files: joinedFiles,
};
};