mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 06:25:45 +01:00
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:
@@ -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,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user