/* * MIT License * * Copyright (c) 2020-present Cloudogu GmbH and Contributors * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import React, { ReactNode, useEffect, useState } from "react"; import { storiesOf } from "@storybook/react"; import Diff from "./Diff"; // @ts-ignore import parser from "gitdiff-parser"; import simpleDiff from "../__resources__/Diff.simple"; import hunksDiff from "../__resources__/Diff.hunks"; import binaryDiff from "../__resources__/Diff.binary"; import markdownDiff from "../__resources__/Diff.markdown"; import { DiffEventContext, FileControlFactory } from "./DiffTypes"; import Toast from "../toast/Toast"; import { getPath } from "./diffs"; import DiffButton from "./DiffButton"; import styled from "styled-components"; import { MemoryRouter } from "react-router-dom"; import { two } from "../__resources__/changesets"; import { Changeset, FileDiff } from "@scm-manager/ui-types"; import JumpToFileButton from "./JumpToFileButton"; import Button from "../buttons/Button"; const diffFiles = parser.parse(simpleDiff); const Container = styled.div` padding: 2rem 6rem; `; type ExternalDiffState = { [key: string]: boolean; }; const RoutingDecorator = (story: () => ReactNode) => {story()}; const fileControlFactory: (changeset: Changeset) => FileControlFactory = (changeset) => (file) => { const baseUrl = "/repo/hitchhiker/heartOfGold/code/changeset"; const sourceLink = { url: `${baseUrl}/${changeset.id}/${file.newPath}/`, label: "Jump to source", }; const targetLink = changeset._embedded?.parents?.length === 1 && { url: `${baseUrl}/${changeset._embedded.parents[0].id}/${file.oldPath}`, label: "Jump to target", }; const links = []; switch (file.type) { case "add": links.push(sourceLink); break; case "delete": if (targetLink) { links.push(targetLink); } break; default: if (targetLink) { links.push(sourceLink, targetLink); } else { links.push(sourceLink); } } return links.map(({ url, label }) => ); }; storiesOf("Diff", module) .addDecorator(RoutingDecorator) .addDecorator((storyFn) => {storyFn()}) .add("Default", () => ) .add("Side-By-Side", () => ) .add("Collapsed", () => ) .add("File Controls", () => ( ( alert("Arrrgggghhhh ...")} /> )} /> )) .add("File Annotation", () => ( [

Custom File annotation for {file.newPath}

]} /> )) .add("Line Annotation", () => ( { return { N2:

Line Annotation

, }; }} /> )) .add("OnClick", () => { const OnClickDemo = () => { const [changeId, setChangeId] = useState(); useEffect(() => { const interval = setInterval(() => setChangeId(undefined), 2000); return () => clearInterval(interval); }); // @ts-ignore const onClick = (context: DiffEventContext) => setChangeId(context.changeId); return ( <> {changeId && } ); }; return ; }) .add("Hunks", () => { const hunkDiffFiles = parser.parse(hunksDiff); return ; }) .add("Binaries", () => { const binaryDiffFiles = parser.parse(binaryDiff); return ; }) .add("SyntaxHighlighting", () => { const filesWithLanguage = diffFiles.map((file: FileDiff) => { const ext = getPath(file).split(".")[1]; if (ext === "tsx") { file.language = "typescript"; } else { file.language = ext; } return file; }); return ; }) .add("SyntaxHighlighting (Markdown)", () => { // @ts-ignore return ; }) .add("CollapsingWithFunction", () => ( oldPath.endsWith(".java")} /> )) .add("Expandable", () => { const filesWithLanguage = diffFiles.map((file: FileDiff) => { file._links = { lines: { href: "http://example.com/" } }; return file; }); return ; }) .add("WithLinkToFile", () => ) .add("Changing Content", () => { const ChangingContentDiff = () => { const [markdown, setMarkdown] = useState(false); return (
{/* @ts-ignore */}
); }; return ; }) .add("External state management", () => { const [externalState, setExternalState] = useState({}); const isCollapsed = (file: FileDiff) => { return externalState[file.newPath] || false; }; const onCollapseStateChange = (file: FileDiff) => { setExternalState((current) => ({ ...current, [file.newPath]: !current[file.newPath] })); }; return ; });