Skip to content

Commit 12afe5a

Browse files
author
Snehil
committed
Scripts added for creating sitemap and params
1 parent 68e6995 commit 12afe5a

12 files changed

Lines changed: 281 additions & 105 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
# production
1616
/build
1717

18+
#seo
19+
/seo
20+
public/sitemap.xml
21+
1822
# misc
1923
.DS_Store
2024
*.pem

components/account/overview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { sortBy } from "lodash";
1212
import getAnyRepositoryAll from "../../helpers/getAnyRepositoryAll";
1313

1414
function AccountOverview(props) {
15-
const [allRepos, setAllRepos] = useState([]);
15+
const [allRepos, setAllRepos] = useState(props.allRepos || []);
1616
const [contributions, setContributions] = useState([{}]);
1717
const [totalContributions, setTotalContributions] = useState(0);
1818

components/account/repositories.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { useState, useEffect } from "react";
22
import dayjs from "dayjs";
33
import Link from "next/link";
4-
import getRepository from "../../helpers/getRepository";
54
import sortBy from "lodash/sortBy";
65
import getAnyRepositoryAll from "../../helpers/getAnyRepositoryAll";
76

87
function AccountRepositories(props) {
9-
const [allRepos, setAllRepos] = useState([]);
8+
const [allRepos, setAllRepos] = useState(props.allRepos || []);
109

1110
const getAllRepos = async () => {
1211
if (props.userId) {

helpers/getRepository.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import api from "./getApi";
22

3-
export default async function getUser(repoId) {
3+
export default async function getRepository(repoId) {
4+
if (!repoId) return null;
45
try {
56
const res = await api.queryRepository(repoId);
67
if (res.ok) {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
7+
"prebuild": "node scripts/dump-paths.mjs",
78
"build": "next build && next export",
9+
"postbuild": "cp ./seo/sitemap.xml ./public/",
810
"start": "next start",
911
"lint": "next lint",
1012
"analyze": "cross-env ANALYZE=true next build",
@@ -33,6 +35,7 @@
3335
"express-fileupload": "^1.4.0",
3436
"express-rate-limit": "^6.6.0",
3537
"file-saver": "^2.0.5",
38+
"globby": "^11.0.1",
3639
"graphql": "^15.8.0",
3740
"graphql-ws": "^5.6.4",
3841
"lodash": "^4.17.21",

pages/[userId]/[repositoryId]/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ export async function getStaticProps({ params }) {
7979
}
8080

8181
export async function getStaticPaths() {
82-
const repos = await getRepositoryAll();
82+
const fs = (await import("fs")).default;
8383
let paths = [];
84-
repos?.map((r) => {
85-
paths.push({ params: { userId: r.owner.id, repositoryId: r.name } });
86-
});
84+
try {
85+
paths = JSON.parse(fs.readFileSync("./seo/paths-repositories.json"));
86+
} catch (e) {
87+
console.error(e);
88+
}
8789
return {
8890
paths,
8991
fallback: "blocking",

pages/[userId]/[repositoryId]/issues/[issueIid].js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,62 @@ import IssuePullDescription from "../../../../components/repository/issuePullDes
3131
import { useErrorStatus } from "../../../../hooks/errorHandler";
3232
import pluralize from "../../../../helpers/pluralize";
3333
import Link from "next/link";
34+
import getAnyRepository from "../../../../helpers/getAnyRepository";
35+
import getAllRepositoryBranch from "../../../../helpers/getAllRepositoryBranch";
36+
import getAllRepositoryTag from "../../../../helpers/getAllRepositoryTag";
3437

35-
export async function getStaticProps() {
38+
export async function getStaticProps({ params }) {
39+
const [r, b, t] = await Promise.all([
40+
getAnyRepository(params.userId, params.repositoryId),
41+
getAllRepositoryBranch(params.userId, params.repositoryId),
42+
getAllRepositoryTag(params.userId, params.repositoryId),
43+
]);
44+
45+
if (r) {
46+
r.branches = b;
47+
r.tags = t;
48+
const i = await getRepositoryIssue(
49+
params.userId,
50+
params.repositoryId,
51+
params.issueIid
52+
);
53+
if (i) {
54+
const pr = i.comments.map((c) => getComment(c));
55+
const comments = await Promise.all(pr);
56+
return { props: { repository: r, issue: i, comments }, revalidate: 1 };
57+
}
58+
}
3659
return { props: {} };
3760
}
3861

3962
export async function getStaticPaths() {
63+
const fs = (await import("fs")).default;
64+
let paths = [];
65+
try {
66+
paths = JSON.parse(fs.readFileSync("./seo/paths-issues.json"));
67+
} catch (e) {
68+
console.error(e);
69+
}
4070
return {
41-
paths: [],
71+
paths,
4272
fallback: "blocking",
4373
};
4474
}
4575

4676
function RepositoryIssueView(props) {
4777
const router = useRouter();
4878
const { setErrorStatusCode } = useErrorStatus();
49-
const { repository } = useRepository();
79+
const { repository } = useRepository(props.repository);
5080
const [issue, setIssue] = useState({
5181
iid: router.query.issueIid,
5282
creator: "",
5383
description: "",
5484
comments: [],
5585
assignees: [],
5686
labels: [],
87+
...props.issue,
5788
});
58-
const [allComments, setAllComments] = useState([]);
89+
const [allComments, setAllComments] = useState(props.comments || []);
5990
const [allLabels, setAllLabels] = useState([]);
6091

6192
useEffect(() => {

pages/[userId]/[repositoryId]/pulls/[pullRequestIid]/index.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ import getRepositoryPull from "../../../../../helpers/getRepositoryPull";
3232
import getPullRepoInfo from "../../../../../helpers/getPullRepoInfo";
3333
import getPullAll from "../../../../../helpers/getPullAll";
3434
import getRepositoryAll from "../../../../../helpers/getRepositoryAll";
35-
import getUserRepository from "../../../../../helpers/getUserRepository";
35+
import getAnyRepository from "../../../../../helpers/getAnyRepository";
36+
import getAllRepositoryBranch from "../../../../../helpers/getAllRepositoryBranch";
37+
import getAllRepositoryTag from "../../../../../helpers/getAllRepositoryTag";
3638

3739
export async function getStaticProps({ params }) {
38-
const r = await getUserRepository(params.userId, params.repositoryId);
40+
const [r, b, t] = await Promise.all([
41+
getAnyRepository(params.userId, params.repositoryId),
42+
getAllRepositoryBranch(params.userId, params.repositoryId),
43+
getAllRepositoryTag(params.userId, params.repositoryId),
44+
]);
45+
3946
if (r) {
47+
r.branches = b;
48+
r.tags = t;
4049
const p = await getRepositoryPull(
4150
params.userId,
4251
params.repositoryId,
@@ -53,24 +62,13 @@ export async function getStaticProps({ params }) {
5362
}
5463

5564
export async function getStaticPaths() {
56-
const [pulls, repos] = await Promise.all([getPullAll(), getRepositoryAll()]);
57-
let repoIdMap = {},
58-
paths = [];
59-
repos?.map((r) => {
60-
repoIdMap[r.id] = r;
61-
});
62-
pulls?.map((p) => {
63-
const baseRepo = repoIdMap[p.base.repositoryId];
64-
if (baseRepo) {
65-
paths.push({
66-
params: {
67-
userId: baseRepo.creator,
68-
repositoryId: baseRepo.name,
69-
pullRequestIid: p.iid,
70-
},
71-
});
72-
}
73-
});
65+
const fs = (await import("fs")).default;
66+
let paths = [];
67+
try {
68+
paths = JSON.parse(fs.readFileSync("./seo/paths-pulls.json"));
69+
} catch (e) {
70+
console.error(e);
71+
}
7472
return {
7573
paths,
7674
fallback: "blocking",

pages/[userId]/index.js

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import PublicTabs from "../../components/dashboard/publicTabs";
1010
import UserHeader from "../../components/user/header";
1111
import AccountOverview from "../../components/account/overview";
1212
import { useErrorStatus } from "../../hooks/errorHandler";
13-
import getUserAll from "../../helpers/getUserAll";
14-
import getDaoAll from "../../helpers/getDaoAll";
1513
import getWhois from "../../helpers/getWhois";
1614
import AccountRepositories from "../../components/account/repositories";
1715
import AccountTransactions from "../../components/account/transactions";
@@ -21,55 +19,38 @@ import DaoProposalList from "../../components/account/daoProposalList";
2119
import DaoProposalCreate from "../../components/account/daoProposalCreate";
2220
import DaoProposalDetails from "../../components/account/daoProposalDetails";
2321
import validAddress from "../../helpers/validAddress";
24-
25-
const getAllRepos = async (repoIds) => {
26-
if (repoIds) {
27-
const pr = repoIds.map((r) => getRepository(r.id));
28-
return await Promise.all(pr);
29-
}
30-
return [];
31-
};
32-
33-
const getOrgAvatarLink = (org) => {
34-
if (org?.name) {
35-
let letter = org.name.slice(0, 1);
36-
const link =
37-
process.env.NEXT_PUBLIC_GITOPIA_ADDRESS === org.address
38-
? "/logo-g.svg"
39-
: "https://avatar.oxro.io/avatar.svg?length=1&height=100&width=100&fontSize=52&caps=1&name=" +
40-
letter;
41-
return link;
42-
}
43-
return "";
44-
};
22+
import getAnyRepositoryAll from "../../helpers/getAnyRepositoryAll";
23+
import sortBy from "lodash/sortBy";
4524

4625
export async function getStaticProps({ params }) {
47-
const [u, o] = await Promise.all([
48-
getUser(params.userId),
49-
getDao(params.userId),
50-
]);
51-
const orgAvatarLink = getOrgAvatarLink(o);
52-
let allRepos = [];
53-
if (u && u.id) {
54-
allRepos = await getAllRepos(u.repositories);
55-
} else if (o && o.id) {
56-
allRepos = await getAllRepos(o.repositories);
26+
const data = await getWhois(params.userId);
27+
let u,
28+
d,
29+
allRepos = [];
30+
if (data?.ownerType === "USER") {
31+
u = await getUser(params.userId);
32+
const pr = await getAnyRepositoryAll(params.userId);
33+
allRepos = sortBy(pr, (r) => -Number(r.updatedAt));
34+
} else if (data?.ownerType === "DAO") {
35+
d = await getDao(params.userId);
36+
const pr = await getAnyRepositoryAll(params.userId);
37+
allRepos = sortBy(pr, (r) => -Number(r.updatedAt));
5738
}
39+
5840
return {
59-
props: { user: u || {}, org: o || {}, orgAvatarLink, allRepos },
41+
props: { user: u || {}, dao: d || {}, allRepos },
6042
revalidate: 1,
6143
};
6244
}
6345

6446
export async function getStaticPaths() {
65-
const [users, orgs] = await Promise.all([getUserAll(), getDaoAll()]);
47+
const fs = (await import("fs")).default;
6648
let paths = [];
67-
users?.map((u) => {
68-
paths.push({ params: { userId: u.creator } });
69-
});
70-
orgs?.map((o) => {
71-
paths.push({ params: { userId: o.address } });
72-
});
49+
try {
50+
paths = JSON.parse(fs.readFileSync("./seo/paths-owners.json"));
51+
} catch (e) {
52+
console.error(e);
53+
}
7354
return {
7455
paths,
7556
fallback: "blocking",
@@ -87,37 +68,8 @@ function AccountView(props) {
8768
const [dao, setDao] = useState({
8869
name: "",
8970
repositories: [],
90-
...props.org,
71+
...props.dao,
9172
});
92-
// const [allRepos, setAllRepos] = useState(props.allRepos);
93-
// const [avatarLink, setAvatarLink] = useState(props.orgAvatarLink);
94-
95-
useEffect(async () => {
96-
const [u, o] = await Promise.all([
97-
getUser(router.query.userId),
98-
getDao(router.query.userId),
99-
]);
100-
if (u) {
101-
setUser(u);
102-
setDao({ name: "", repositories: [] });
103-
} else if (o) {
104-
setDao(o);
105-
setUser({ creator: "", repositories: [] });
106-
} else {
107-
setErrorStatusCode(404);
108-
setUser({ creator: "", repositories: [] });
109-
setDao({ name: "", repositories: [] });
110-
}
111-
}, [router.query]);
112-
113-
// useEffect(async () => {
114-
// setAvatarLink(getOrgAvatarLink(org));
115-
// if (user && user.id) {
116-
// setAllRepos(await getAllRepos(user.repositories));
117-
// } else if (org && org.id) {
118-
// setAllRepos(await getAllRepos(org.repositories));
119-
// }
120-
// }, [user.id, org.id]);
12173

12274
const hrefBase = "/" + router.query.userId;
12375

@@ -167,7 +119,7 @@ function AccountView(props) {
167119

168120
useEffect(() => {
169121
getId();
170-
}, [router.query]);
122+
}, [router.query.userId]);
171123

172124
return (
173125
<div
@@ -204,6 +156,7 @@ function AccountView(props) {
204156
user={user}
205157
dao={dao}
206158
userId={router.query.userId}
159+
allRepos={props.allRepos}
207160
/>
208161
) : (
209162
""
@@ -213,6 +166,7 @@ function AccountView(props) {
213166
user={user}
214167
dao={dao}
215168
userId={router.query.userId}
169+
allRepos={props.allRepos}
216170
/>
217171
) : (
218172
""

public/favicon.ico

16.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)