Skip to content

Commit

Permalink
big update!
Browse files Browse the repository at this point in the history
  • Loading branch information
E-boi committed Jan 24, 2023
1 parent 5136bc4 commit f62bafb
Show file tree
Hide file tree
Showing 26 changed files with 1,125 additions and 583 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ rules:
camelcase: off

overrides:
- files: "**/*.ts"
- files: "**/*.{ts,tsx}"
extends: eslint-config-dmitmel/presets/typescript-addon
rules:
node/no-missing-import: off
Expand All @@ -47,7 +47,7 @@ overrides:
no-void: off
consistent-return: off

- files: "**/plugins/**/*.ts"
- files: "**/plugins/**/*.{ts,tsx}"
extends: eslint-config-dmitmel/rules/typescript/with-type-checking
rules:
"@typescript-eslint/explicit-function-return-type": off
Expand All @@ -57,6 +57,6 @@ overrides:
rules:
no-var: off

- files: "plugins/**/*.ts"
- files: "plugins/**/*.{ts,tsx}"
parserOptions:
project: tsconfig.json
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"devDependencies": {
"@electron/asar": "^3.2.1",
"@octokit/openapi-types": "^14.0.0",
"@octokit/types": "^8.1.1",
"@types/node": "^18.11.2",
"@types/react": "^18.0.26",
"@typescript-eslint/eslint-plugin": "^5.40.1",
Expand All @@ -35,7 +36,7 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-react": "^7.31.10",
"prettier": "^2.8.1",
"replugged": "4.0.0-beta0.21",
"replugged": "4.0.0-beta0.22",
"tsx": "^3.10.3",
"typescript": "^4.8.4"
},
Expand Down
52 changes: 29 additions & 23 deletions plugins/githubindiscord/Modal/Code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@ import { TreeView } from "@primer/react/drafts";
import { FileDirectoryFillIcon, FileIcon } from "@primer/styled-octicons";
import { useCallback, useContext, useEffect, useState } from "react";
import { common, webpack } from "replugged";
import { TabProps } from ".";
import { SelectMenu } from "../components";
import { Context } from "../context";
import { TreeWithContent, getCommits, getFile, pluginSettings } from "../utils";
import CommitsView from "./Commits/CommitsView";
import CommitsView from "./Commits/CommitView";

const { parser } = common;
const blober = webpack.getByProps("blob");

export default (props: TabProps) => {
return pluginSettings.get("view", "standard") === "treeview" ? (
<Tree />
) : (
<StandardView {...props} />
);
export default () => {
return pluginSettings.get("view", "standard") === "treeview" ? <Tree /> : <StandardView />;
};

function StandardView({ branch, url, switchBranches }: TabProps) {
const { data } = useContext(Context)!;
const { tree, branches } = data!;
function StandardView() {
const { data, switchBranch } = useContext(Context)!;
const { tree, branches, currentBranch: branch, url } = data!;
const [folder, setFolder] = useState<{
current: { latestCommit?: components["schemas"]["commit"]; tree: TreeWithContent[] };
prevs: Array<{ latestCommit?: components["schemas"]["commit"]; tree: TreeWithContent[] }>;
Expand Down Expand Up @@ -55,7 +50,7 @@ function StandardView({ branch, url, switchBranches }: TabProps) {
let ending = path.pop();
const latestCommit = file ? file.latestCommit : folder.current.latestCommit;

if (commit) return <CommitsView commit={commit} url={url} />;
if (commit) return <CommitsView commit={commit} onClose={() => setCommit(null)} />;
return (
<>
<Box className="repository-options">
Expand All @@ -64,9 +59,7 @@ function StandardView({ branch, url, switchBranches }: TabProps) {
className="Gbranches"
value={branch.name}
options={branches.map((branch) => ({ value: branch.name, label: branch.name }))}
onChange={(value: string) => {
switchBranches(value);
}}
onChange={switchBranch}
/>
)}
{folder.prevs.length ? (
Expand Down Expand Up @@ -94,7 +87,22 @@ function StandardView({ branch, url, switchBranches }: TabProps) {
</Breadcrumbs>
) : null}
</Box>
<Box borderColor="border.default" borderStyle="solid" borderWidth={1} borderRadius={2}>
<Box
borderColor="border.default"
borderStyle="solid"
borderWidth={1}
borderRadius={2}
onMouseUp={(event) => {
if (event.button !== 2 || event.detail !== 2 || !folder.prevs.length) return;
setFolder((prev) => {
const current = prev.prevs.pop();
return {
current: current ?? { tree, latestCommit: branch.commit },
prevs: prev.prevs,
};
});
setFile(null);
}}>
<Box
p={3}
bg="canvas.subtle"
Expand Down Expand Up @@ -160,7 +168,7 @@ function StandardView({ branch, url, switchBranches }: TabProps) {
sx={{ userSelect: "text", code: { bg: "inherit" } }}>
{parser.defaultRules.codeBlock.react(
{ content: window.atob(file.content!).trimEnd(), lang: file.fileType },
// @ts-ignore okay
// @ts-expect-error okay
null,
{},
)}
Expand All @@ -187,20 +195,18 @@ function StandardView({ branch, url, switchBranches }: TabProps) {
onClick={() => {
if (c.type === "tree") {
setFolder((prev) => ({
current: c as any,
current: c as typeof prev["current"],
prevs: [...prev.prevs, folder.current],
}));
} else {
getBlob(c);
}
} else void getBlob(c);

if (!c.latestCommit)
getCommits(url, { path: c.path, sha: branch.name }).then((o) => {
void getCommits(url, { path: c.path, sha: branch.name }).then((o) => {
if (o[0]) {
c.latestCommit = o[0];
if (c.type === "tree")
setFolder((prev) => ({
current: c as any,
current: c as typeof prev["current"],
prevs: [...prev.prevs],
}));
else
Expand Down
14 changes: 13 additions & 1 deletion plugins/githubindiscord/Modal/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Avatar, Box, CaretProps, PointerBox, RelativeTime, Text, Timeline } from "@primer/react";
import { BetterSystemStyleObject } from "@primer/react/lib/sx";
import { parseMarkdown } from "../parser";

export const TimelineComment = ({
comment,
caret,
sx,
}: {
// ill type later
// eslint-disable-next-line @typescript-eslint/no-explicit-any
comment: any;
caret?: CaretProps["location"];
sx?: BetterSystemStyleObject;
Expand Down Expand Up @@ -48,7 +51,16 @@ export const TimelineComment = ({
borderTop={0}
borderBottomLeftRadius={2}
borderBottomRightRadius={2}
dangerouslySetInnerHTML={{ __html: comment.body }}></Box>
sx={{
userSelect: "text",
code: { bg: "canvas.subtle" },
lineHeight: "2rem",
img: {
maxWidth: "100%",
},
}}>
{parseMarkdown(comment.body as string)}
</Box>
</Box>
</Timeline.Body>
</Timeline.Item>
Expand Down
5 changes: 5 additions & 0 deletions plugins/githubindiscord/Modal/Commits.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import CommitHistory from "./Commits/CommitHistory";

export default () => {
return <CommitHistory />;
};
54 changes: 54 additions & 0 deletions plugins/githubindiscord/Modal/Commits/Commit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { operations } from "@octokit/openapi-types";
import { common } from "replugged";
import { Box, Link, Text } from "@primer/react";
import { SxProp } from "@primer/react/lib-esm/sx";
import { useState } from "react";
import { ChevronDownIcon, ChevronRightIcon } from "@primer/styled-octicons";
const { parser } = common;

export default ({
commit,
sx,
}: {
commit: operations["pulls/list-files"]["responses"]["200"]["content"]["application/json"][0];
sx?: SxProp["sx"];
}) => {
const [expanded, setExpanded] = useState(true);

return (
<Box
borderStyle="solid"
borderColor="border.default"
borderWidth={1}
borderRadius={2}
{...(sx || {})}>
<Box
px={3}
py={2}
bg="canvas.subtle"
borderTopLeftRadius={2}
borderTopRightRadius={2}
display="flex"
alignItems="center">
<Link muted sx={{ display: "flex" }} onClick={() => setExpanded(!expanded)}>
{expanded ? <ChevronDownIcon /> : <ChevronRightIcon />}
</Link>
<Text ml={1}>{commit.filename}</Text>
</Box>
{expanded && (
<Box
borderColor="border.muted"
borderTopWidth={1}
borderStyle="solid"
sx={{ userSelect: "text", code: { bg: "inherit" } }}>
{parser.defaultRules.codeBlock.react(
{ content: commit.patch?.trimEnd(), lang: "patch" },
// @ts-expect-error better types need butttttttttttttttttt i wont pr :)
null,
{},
)}
</Box>
)}
</Box>
);
};
96 changes: 96 additions & 0 deletions plugins/githubindiscord/Modal/Commits/CommitHistory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {
Avatar,
Box,
Link,
Pagination,
RelativeTime,
Text,
Timeline,
Truncate,
} from "@primer/react";
import { CommitIcon } from "@primer/styled-octicons";
import { useContext, useEffect, useState } from "react";
import { Context } from "../../context";
import { useCommits } from "../../paginate";
import { Issue, TreeWithContent } from "../../utils";
import Spinner from "../Spinner";
import CommitsView from "./CommitView";

export default ({ pr }: { pr?: NonNullable<Issue["pull"]> }) => {
const { url } = useContext(Context)!.data!;
const commits = pr ? useCommits(url, { pr: pr.number }) : useContext(Context)?.data?.commits;
const [commit, setCommit] = useState<NonNullable<TreeWithContent["latestCommit"]> | null>(null);

useEffect(() => {
void commits?.fetch();
}, []);

if (!commits?.data?.page || !commits?.data?.info) return <Spinner>Fetching commits...</Spinner>;

if (commit) return <CommitsView commit={commit} onClose={() => setCommit(null)} />;

const page = commits.data.page[commits.data.info.currentPage - 1];

return (
<Timeline clipSidebar>
{page.map((p) => (
<Timeline.Item condensed>
<Timeline.Badge>
<CommitIcon />
</Timeline.Badge>
<Timeline.Body>
<Text>
Commits <RelativeTime datetime={p[0].commit.author?.date} format="datetime" />
</Text>
<Box
borderStyle="solid"
borderWidth={1}
borderColor="border.default"
borderRadius={2}
mt={3}>
{p.map((c, i) => (
<Box
borderStyle="solid"
borderTopWidth={i ? 1 : 0}
borderColor="border.muted"
px={3}
py={2}>
<Truncate maxWidth={"100%"} title={`${c.commit.message}`}>
<Text as="p" m={0} mb={1}>
<Link
muted
sx={{ fontWeight: "bold", color: "fg.default" }}
onClick={() => setCommit(c)}>
{c.commit.message.split("\n\n")[0]}
</Link>
</Text>
</Truncate>
<Box>
<Avatar src={c.author?.avatar_url || ""} alt={`${c.author?.login}'s profile`} />
<Text>
<Text fontWeight="bold" ml={1} color="fg.default">
{c.author?.login}
</Text>{" "}
committed <RelativeTime datetime={c.commit.author?.date} />
</Text>
</Box>
</Box>
))}
</Box>
</Timeline.Body>
</Timeline.Item>
))}
{commits.data.info.lastPage && (
<Pagination
currentPage={commits.data.info.currentPage}
pageCount={commits.data.info.lastPage}
showPages={false}
onPageChange={(_, page) => {
if (page > commits.data.info!.currentPage) void commits.nextPage();
else void commits.previousPage();
}}
/>
)}
</Timeline>
);
};
Loading

0 comments on commit f62bafb

Please sign in to comment.