Skip to content

Commit 9fdbf01

Browse files
committed
migrate to nextjs
1 parent 0442239 commit 9fdbf01

22 files changed

+1856
-11550
lines changed

.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mongourl=<your-mongodb-url>
1+
MONGODB_URI=<your-mongodb-url>

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ yarn-error.log*
2323

2424
# db files
2525
*.db
26+
27+
.next

api/index.js

-110
This file was deleted.

lib/mongoClient.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { MongoClient } from "mongodb";
2+
3+
const uri = process.env.MONGODB_URI;
4+
const options = {};
5+
6+
let client;
7+
8+
/**
9+
* @type {Promise<MongoClient>}
10+
*/
11+
let clientPromise;
12+
13+
if (!process.env.MONGODB_URI) {
14+
throw new Error("Please add your Mongo URI to .env.local");
15+
}
16+
17+
if (process.env.NODE_ENV === "development") {
18+
// In development mode, use a global variable so that the value
19+
// is preserved across module reloads caused by HMR (Hot Module Replacement).
20+
if (!global._mongoClientPromise) {
21+
client = new MongoClient(uri, options);
22+
global._mongoClientPromise = client.connect();
23+
}
24+
clientPromise = global._mongoClientPromise;
25+
} else {
26+
// In production mode, it's best to not use a global variable.
27+
client = new MongoClient(uri, options);
28+
clientPromise = client.connect();
29+
}
30+
31+
// Export a module-scoped MongoClient promise. By doing this in a
32+
// separate module, the client can be shared across functions.
33+
export default clientPromise;

models/paste.js

-8
This file was deleted.

package.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@
1515
"dotenv": "^8.2.0",
1616
"express-simple-stats": "https://github.com/il3ven/express-simple-stats/",
1717
"highlight.js": "^10.5.0",
18-
"mongodb": "^3.6.3",
19-
"react": "^17.0.1",
18+
"mongodb": "^4.3.1",
19+
"next": "^12.0.8",
20+
"react": "^17.0.2",
2021
"react-codemirror2": "^7.2.1",
2122
"react-collapse": "^5.1.0",
22-
"react-dom": "^17.0.1",
23-
"react-router-dom": "^5.2.0",
24-
"react-scripts": "4.0.0",
23+
"react-dom": "^17.0.2",
2524
"styled-components": "^5.2.1",
2625
"web-vitals": "^0.2.4"
2726
},
2827
"scripts": {
29-
"start": "node server.js",
30-
"dev": "react-scripts start",
31-
"build": "react-scripts build",
32-
"test": "react-scripts test",
33-
"eject": "react-scripts eject"
28+
"dev": "next dev",
29+
"build": "next build",
30+
"start": "next start"
3431
},
3532
"proxy": "http://localhost:5000",
3633
"eslintConfig": {
@@ -50,5 +47,8 @@
5047
"last 1 firefox version",
5148
"last 1 safari version"
5249
]
50+
},
51+
"devDependencies": {
52+
"babel-plugin-styled-components": "^2.0.2"
5353
}
5454
}

pages/[langExt]/[id].js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React, { useState } from "react";
2+
import clientPromise from "../../lib/mongoClient";
3+
import base64url from 'base64url'
4+
import { ObjectId } from "mongodb";
5+
6+
import Main from "../../src/main";
7+
import codeMirrorLanguages from "../../src/static/langauges.json";
8+
import { getTheme, getLangFromExt } from "../../src/utils/utils";
9+
import { postDump, getDump } from "../../src/api";
10+
11+
// prettier-ignore
12+
const START_INPUT = `${'*'.repeat(44)}\n\n# Press Ctrl/CMD + V or Paste something here\n\n${'*'.repeat(44)}${'\n'.repeat(11)}`;
13+
14+
function WithInputCode(props) {
15+
return <Main {...props} />;
16+
}
17+
18+
export const getServerSideProps = async (ctx) => {
19+
console.log('getServerSideProps', new Date())
20+
// get input from query
21+
const { id } = ctx.query;
22+
const { res } = ctx;
23+
24+
try {
25+
const client = await clientPromise;
26+
const db = client.db();
27+
28+
29+
const idBase64 = base64url.toBase64(id);
30+
const readid = Buffer.from(idBase64, "base64").toString("hex");
31+
32+
const pastesCollection = db.collection("pastes");
33+
const paste = await pastesCollection.findOne({
34+
_id: ObjectId(readid),
35+
});
36+
37+
if (!paste.content) {
38+
throw new Error("Paste not found");
39+
}
40+
41+
res.setHeader(
42+
"Cache-Control",
43+
"max-age=31536000, s-max-age=31536000, public"
44+
);
45+
46+
return {
47+
props: {
48+
input: paste.content,
49+
},
50+
};
51+
} catch (err) {
52+
console.error("Error in retrieving document", err);
53+
return {
54+
props: {
55+
input: "Some error occured (•_•)",
56+
},
57+
};
58+
}
59+
};
60+
61+
export default WithInputCode;

pages/[langExt]/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Main from "../../src/main";
2+
3+
export default Main;

pages/_app.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React, { useState } from "react";
2+
3+
import { ThemeProvider } from "styled-components";
4+
import { GlobalStyles } from "../src/components/globalStyles";
5+
import { themes } from "../src/components/themes";
6+
import { useDarkMode } from "../src/hooks/useDarkMode";
7+
import { config } from "@fortawesome/fontawesome-svg-core";
8+
import "@fortawesome/fontawesome-svg-core/styles.css";
9+
import { Collapse } from "react-collapse";
10+
import Welcome from "../src/components/welcome";
11+
import { useWelcomePref } from "../src/hooks/useWelcomePref";
12+
13+
// config.autoAddCss = false;
14+
15+
const App = ({ Component, pageProps, initialWelcomePref }) => {
16+
const [themeKey, themeSetter] = useDarkMode();
17+
const [welcomePref, setWelcomePref] = useWelcomePref(initialWelcomePref);
18+
const [featureOpen, setFeatureOpen] = useState(false);
19+
20+
const handleTips = () => {
21+
setWelcomePref(true);
22+
setFeatureOpen(true);
23+
};
24+
25+
return (
26+
<ThemeProvider theme={themes[themeKey]}>
27+
<GlobalStyles />
28+
<Collapse isOpened={welcomePref}>
29+
<Welcome
30+
onClose={() => {
31+
setWelcomePref(false);
32+
}}
33+
featureOpen={featureOpen}
34+
setFeatureOpen={setFeatureOpen}
35+
/>
36+
</Collapse>
37+
38+
<Component
39+
themeKey={themeKey}
40+
themeSetter={themeSetter}
41+
handleTips={handleTips}
42+
{...pageProps}
43+
/>
44+
</ThemeProvider>
45+
);
46+
};
47+
48+
App.getInitialProps = async ({ ctx }) => {
49+
const { req, res } = ctx;
50+
51+
let props = { initialWelcomePref: true };
52+
53+
if (req?.cookies.welcomePref) {
54+
props.initialWelcomePref = req?.cookies.welcomePref === "true";
55+
} else {
56+
res?.setHeader("Set-Cookie", "welcomePref=true; path=/");
57+
}
58+
59+
return props;
60+
};
61+
62+
export default App;

pages/_document.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Document from 'next/document'
2+
import { ServerStyleSheet } from 'styled-components'
3+
4+
export default class MyDocument extends Document {
5+
static async getInitialProps(ctx) {
6+
const sheet = new ServerStyleSheet()
7+
const originalRenderPage = ctx.renderPage
8+
9+
try {
10+
ctx.renderPage = () =>
11+
originalRenderPage({
12+
enhanceApp: (App) => (props) =>
13+
sheet.collectStyles(<App {...props} />),
14+
})
15+
16+
const initialProps = await Document.getInitialProps(ctx)
17+
return {
18+
...initialProps,
19+
styles: (
20+
<>
21+
{initialProps.styles}
22+
{sheet.getStyleElement()}
23+
</>
24+
),
25+
}
26+
} finally {
27+
sheet.seal()
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)