import type { ReactIsland } from "@ethicdevs/react-monolith";
import React, { useCallback } from "react";
import styled from "styled-components";
import { RepositoryFile, RepositoryHead } from "../types";
export interface RepositoryTreeViewProps {
currPath: string;
orgSlug: string;
repoHead: RepositoryHead;
repoFiles: RepositoryFile[];
repoSlug: string;
}
const RepositoryTreeView: ReactIsland<RepositoryTreeViewProps> = ({
currPath,
orgSlug,
repoFiles,
repoHead,
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]
);
return (
<StyledRepositoryTreeViewContainer>
<div>
<strong>{repoHead.author.name}</strong>
{" ∙ "}
<span>{repoHead.commitMessage}</span>
{" - "}
<span>
{repoHead.treeId.substring(0, 8)}
{repoHead.parentId
? ` ∙ parent ${repoHead.parentId.substring(0, 8)}`
: ""}
</span>
{" ∙ "}
<span>{new Date(repoHead.author.timestamp * 1000).toUTCString()}</span>
</div>
<div>
<ul>
{repoFiles.map((file) => {
const fileLink = buildRepoFileLink(file);
return (
<li key={file.id}>
<a href={fileLink.href}>{fileLink.text}</a>
</li>
);
})}
</ul>
</div>
</StyledRepositoryTreeViewContainer>
);
};
const StyledRepositoryTreeViewContainer = styled.div`
width: 100%;
`;
RepositoryTreeView.displayName = "RepositoryTreeView";
export default RepositoryTreeView;