diff --git a/scm-ui/src/repos/sources/components/FileSize.js b/scm-ui-components/packages/ui-components/src/FileSize.js similarity index 78% rename from scm-ui/src/repos/sources/components/FileSize.js rename to scm-ui-components/packages/ui-components/src/FileSize.js index c7d966d901..2796e0fd0c 100644 --- a/scm-ui/src/repos/sources/components/FileSize.js +++ b/scm-ui-components/packages/ui-components/src/FileSize.js @@ -12,9 +12,9 @@ class FileSize extends React.Component { } const units = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"]; - const i = Math.floor(Math.log(bytes) / Math.log(1024)); + const i = Math.floor(Math.log(bytes) / Math.log(1000)); - const size = i === 0 ? bytes : (bytes / 1024 ** i).toFixed(2); + const size = i === 0 ? bytes : (bytes / 1000 ** i).toFixed(2); return `${size} ${units[i]}`; } diff --git a/scm-ui/src/repos/sources/components/FileSize.test.js b/scm-ui-components/packages/ui-components/src/FileSize.test.js similarity index 100% rename from scm-ui/src/repos/sources/components/FileSize.test.js rename to scm-ui-components/packages/ui-components/src/FileSize.test.js diff --git a/scm-ui-components/packages/ui-components/src/apiclient.js b/scm-ui-components/packages/ui-components/src/apiclient.js index 80c2c4dbfa..2ad0cd1f9f 100644 --- a/scm-ui-components/packages/ui-components/src/apiclient.js +++ b/scm-ui-components/packages/ui-components/src/apiclient.js @@ -3,17 +3,16 @@ import { contextPath } from "./urls"; import { createBackendError, ForbiddenError, isBackendError, UnauthorizedError } from "./errors"; import type { BackendErrorContent } from "./errors"; -const fetchOptions: RequestOptions = { - credentials: "same-origin", - headers: { +const applyFetchOptions: (RequestOptions) => RequestOptions = o => { + o.credentials = "same-origin"; + o.headers = { Cache: "no-cache", // identify the request as ajax request "X-Requested-With": "XMLHttpRequest" - } + }; + return o; }; - - function handleFailure(response: Response) { if (!response.ok) { if (isBackendError(response)) { @@ -47,7 +46,7 @@ export function createUrl(url: string) { class ApiClient { get(url: string): Promise { - return fetch(createUrl(url), fetchOptions).then(handleFailure); + return fetch(createUrl(url), applyFetchOptions).then(handleFailure); } post(url: string, payload: any, contentType: string = "application/json") { @@ -73,7 +72,7 @@ class ApiClient { let options: RequestOptions = { method: "HEAD" }; - options = Object.assign(options, fetchOptions); + options = applyFetchOptions(options); return fetch(createUrl(url), options).then(handleFailure); } @@ -81,7 +80,7 @@ class ApiClient { let options: RequestOptions = { method: "DELETE" }; - options = Object.assign(options, fetchOptions); + options = applyFetchOptions(options); return fetch(createUrl(url), options).then(handleFailure); } @@ -99,7 +98,7 @@ class ApiClient { } httpRequestWithBinaryBody(options: RequestOptions, url: string, contentType?: string) { - options = Object.assign(options, fetchOptions); + options = applyFetchOptions(options); if (contentType) { // $FlowFixMe options.headers["Content-Type"] = contentType; diff --git a/scm-ui-components/packages/ui-components/src/index.js b/scm-ui-components/packages/ui-components/src/index.js index 91cfe7ef72..35acd26369 100644 --- a/scm-ui-components/packages/ui-components/src/index.js +++ b/scm-ui-components/packages/ui-components/src/index.js @@ -19,6 +19,7 @@ export { default as Paginator } from "./Paginator.js"; export { default as LinkPaginator } from "./LinkPaginator.js"; export { default as StatePaginator } from "./StatePaginator.js"; +export { default as FileSize } from "./FileSize.js"; export { default as ProtectedRoute } from "./ProtectedRoute.js"; export { default as Help } from "./Help"; export { default as HelpIcon } from "./HelpIcon"; diff --git a/scm-ui/src/repos/sources/components/FileTreeLeaf.js b/scm-ui/src/repos/sources/components/FileTreeLeaf.js index d3c81b960c..2515891de0 100644 --- a/scm-ui/src/repos/sources/components/FileTreeLeaf.js +++ b/scm-ui/src/repos/sources/components/FileTreeLeaf.js @@ -1,8 +1,7 @@ //@flow import * as React from "react"; import injectSheet from "react-jss"; -import { DateFromNow } from "@scm-manager/ui-components"; -import FileSize from "./FileSize"; +import { DateFromNow, FileSize } from "@scm-manager/ui-components"; import FileIcon from "./FileIcon"; import { Link } from "react-router-dom"; import type { File } from "@scm-manager/ui-types"; diff --git a/scm-ui/src/repos/sources/containers/Content.js b/scm-ui/src/repos/sources/containers/Content.js index ee8e3673bf..927e1869c0 100644 --- a/scm-ui/src/repos/sources/containers/Content.js +++ b/scm-ui/src/repos/sources/containers/Content.js @@ -2,8 +2,7 @@ import React from "react"; import { translate } from "react-i18next"; import type { File, Repository } from "@scm-manager/ui-types"; -import { DateFromNow, ButtonGroup } from "@scm-manager/ui-components"; -import FileSize from "../components/FileSize"; +import { DateFromNow, ButtonGroup, FileSize } from "@scm-manager/ui-components"; import injectSheet from "react-jss"; import classNames from "classnames"; import FileButtonGroup from "../components/content/FileButtonGroup"; diff --git a/scm-ui/src/repos/sources/containers/Sources.js b/scm-ui/src/repos/sources/containers/Sources.js index 95cfe9e332..b12a716d3d 100644 --- a/scm-ui/src/repos/sources/containers/Sources.js +++ b/scm-ui/src/repos/sources/containers/Sources.js @@ -61,49 +61,42 @@ class Sources extends React.Component { repository, revision, path, - branches, - baseUrl, fetchSources } = this.props; fetchBranches(repository); fetchSources(repository, revision, path); - if (branches) { - const defaultBranches = branches.filter(b => b.defaultBranch); - - if (defaultBranches.length > 0) - this.setState({ selectedBranch: defaultBranches[0] }); - this.props.history.push(`${baseUrl}/${defaultBranches[0].name}/`); - } + this.redirectToDefaultBranch(); } componentDidUpdate(prevProps) { - const { - fetchSources, - repository, - revision, - path, - branches, - baseUrl - } = this.props; + const { fetchSources, repository, revision, path } = this.props; if (prevProps.revision !== revision || prevProps.path !== path) { fetchSources(repository, revision, path); } - const currentUrl = this.props.location.pathname; + this.redirectToDefaultBranch(); + } - if ( - branches && - (currentUrl.endsWith("sources") || currentUrl.endsWith("sources/")) - ) { + redirectToDefaultBranch = () => { + const { branches, baseUrl } = this.props; + if (this.shouldRedirect()) { const defaultBranches = branches.filter(b => b.defaultBranch); - if (defaultBranches.length > 0) + if (defaultBranches.length > 0) { this.setState({ selectedBranch: defaultBranches[0] }); - this.props.history.push(`${baseUrl}/${defaultBranches[0].name}/`); + this.props.history.push( + `${baseUrl}/${encodeURIComponent(defaultBranches[0].name)}/` + ); + } } - } + }; + + shouldRedirect = () => { + const { branches, revision } = this.props; + return branches && !revision; + }; branchSelected = (branch?: Branch) => { const { baseUrl, history, path } = this.props;