import type { ReactIsland } from "@ethicdevs/react-monolith";
import React, { useCallback } from "react";
import styled from "styled-components";
import type { RepositoryFile, RepositoryLog } from "../types";
import { Grid } from "../components";
export interface RepositoryTreeViewProps {
currPath: string;
lastCommit: RepositoryLog;
orgSlug: string;
repoFiles: RepositoryFile[];
repoSlug: string;
}
const RepositoryTreeView: ReactIsland<RepositoryTreeViewProps> = ({
currPath,
lastCommit,
orgSlug,
repoFiles,
repoSlug,
}) => {
const buildRepoFileLink = useCallback(
(file: RepositoryFile) => {
const fileName = `${file.name}${file.type === "tree" ? "/" : ""}`;
return {
text: fileName,
href:
currPath === "/"
? `/${orgSlug}/${repoSlug}/main/tree/${fileName}`
: `/${orgSlug}/${repoSlug}/main/tree/${
currPath.endsWith("/") ? currPath : `${currPath}/`
}${fileName}`,
};
},
[orgSlug, repoSlug, currPath]
);
const currPathParts = currPath.split("/");
let prevPath: string | null = currPathParts
.slice(0, currPathParts.length - 2)
.join("/");
prevPath = prevPath.trim() === "" ? null : prevPath;
prevPath = prevPath == null ? "/" : prevPath;
const prevPathLink =
prevPath === "/"
? `/${orgSlug}/${repoSlug}`
: `/${orgSlug}/${repoSlug}/main/tree/${
prevPath.endsWith("/") ? prevPath : `${prevPath}/`
}`;
const shouldShowPrevPath = currPath !== "/";
return (
<StyledRepositoryTreeViewContainer>
<Grid.Col fluid>
<Grid.Row gap={8} alignItems={"center"}>
<strong>{lastCommit.author.name}</strong>
{" ∙ "}
<span style={{ flex: 1 }}>
<a
href={`/${orgSlug}/${repoSlug}/compare/${lastCommit.abbreviated_parent}..${lastCommit.abbreviated_commit}`}
>
{lastCommit.subject}
</a>
</span>
{" ∙ "}
<a
href={`/${orgSlug}/${repoSlug}/compare/${lastCommit.abbreviated_parent}..${lastCommit.abbreviated_commit}`}
>
<span>
{lastCommit.abbreviated_commit}
{lastCommit.abbreviated_parent.trim() != ""
? ` (parent ${lastCommit.abbreviated_parent})`
: ""}
</span>
</a>
{" ∙ "}
<span>{new Date(lastCommit.author.date).toLocaleDateString()}</span>
</Grid.Row>
<Grid.Row
gap={8}
alignItems={"center"}
justifyContent={"flex-end"}
style={{ marginTop: 8, width: "100%" }}
>
<a href={`/${orgSlug}/${repoSlug}/commits`}>Commits History</a>
</Grid.Row>
<div>
<ul>
{shouldShowPrevPath && (
<li key={"go-previous"}>
<a href={prevPathLink}>..</a>
</li>
)}
{repoFiles.map((file) => {
const fileLink = buildRepoFileLink(file);
return (
<li key={[file.id, file.name].join(":")}>
<a href={fileLink.href}>{fileLink.text}</a>
</li>
);
})}
</ul>
</div>
</Grid.Col>
</StyledRepositoryTreeViewContainer>
);
};
const StyledRepositoryTreeViewContainer = styled.div`
width: 100%;
`;
RepositoryTreeView.displayName = "RepositoryTreeView";
export default RepositoryTreeView;