Skip to content

Commit

Permalink
Yeet API route usages because apparently it uses a lot of resources
Browse files Browse the repository at this point in the history
  • Loading branch information
DJDavid98 committed Dec 20, 2021
1 parent 6e861ed commit 8a1c2ce
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 45 deletions.
29 changes: 14 additions & 15 deletions src/components/NoFuckingThanks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import moment from 'moment';
import { useTranslation } from 'next-i18next';
import React, { MouseEventHandler, useCallback, useEffect, useMemo, useState, VFC } from 'react';
import { Alert, Badge, Button } from 'reactstrap';
import type { NftStatus } from 'src/pages/api/nft';
import { useLocale } from 'src/util/common';
import { getNftData, NftStatus, rickRollLinkPool } from 'src/util/nft';
import { setInterval } from 'timers';

export const fuckNftsStorageKey = 'fuck_nfts';
Expand All @@ -20,7 +20,6 @@ export const NoFuckingThanks: VFC<{ handleClose?: VoidFunction }> = ({ handleClo

useEffect(() => {
let mounted = true;
let abortController: AbortController;
let checkBackInterval: null | ReturnType<typeof setInterval> = null;

const clearTs = () => {
Expand All @@ -30,9 +29,7 @@ export const NoFuckingThanks: VFC<{ handleClose?: VoidFunction }> = ({ handleClo
const updateStatus = () => {
if (!mounted) return;

if (!abortController) abortController = new AbortController();
fetch('/api/nft', { signal: abortController.signal })
.then((r) => r.json())
getNftData()
.then((data: NftStatus) => {
if (!mounted) return;
setStatus(data);
Expand Down Expand Up @@ -86,6 +83,17 @@ export const NoFuckingThanks: VFC<{ handleClose?: VoidFunction }> = ({ handleClo
alert('Right clicking has been disabled on this element to prevent theft via "Save as…"');
}, []);

const handleLogin: MouseEventHandler = useCallback((e) => {
e.preventDefault();

const redirectUrl = rickRollLinkPool[Math.floor(rickRollLinkPool.length * Math.random())];

const newWindow = window.open(redirectUrl, '_blank', 'noopener,noreferrer');
if (newWindow) {
newWindow.opener = null;
}
}, []);

return (
<Alert color="secondary" className={`mb-0 text-center ${styles.nftCard}`} onContextMenu={handleRightClick} isOpen toggle={handleClose}>
<p>
Expand Down Expand Up @@ -122,16 +130,7 @@ export const NoFuckingThanks: VFC<{ handleClose?: VoidFunction }> = ({ handleClo
</Badge>
)}
</div>
<Button
tag="a"
color="light"
size="sm"
disabled={!status}
className="m-1"
href="/api/login"
target="_blank"
rel="noreferrer noopener"
>
<Button color="light" size="sm" disabled={!status} className="m-1" onClick={handleLogin}>
<FontAwesomeIcon icon={['fab', 'ethereum']} className="me-2" />
Sign in with Ethereum
</Button>
Expand Down
26 changes: 0 additions & 26 deletions src/pages/api/login.ts

This file was deleted.

24 changes: 20 additions & 4 deletions src/pages/api/nft.ts → src/util/nft.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { mulberry32 } from 'src/util/random';

export interface NftStatus {
Expand All @@ -24,16 +23,33 @@ const getPrice = (offset?: number) => {

const oneDayInMilliseconds = 1e3 * 60 * 60 * 24;

export default async (_: NextApiRequest, response: NextApiResponse<NftStatus>) => {
export const getNftData = async (): Promise<NftStatus> => {
await new Promise((resolve) => setTimeout(resolve, 750 + Math.random() * 1000));

const offset = oneDayInMilliseconds;
const currentPrice = getPrice();
const previousPrice = getPrice(-offset);

response.status(200).json({
return {
currentPrice,
previousPrice,
nextUpdate: getUpdateTime(offset).toISOString(),
});
};
};

export const rickRollLinkPool = [
// The original, truly a masterpiece
'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
// Russian version
'https://www.youtube.com/watch?v=LNME_sRbJJU&t=4s',
// "dorime. ameno. Rick Astley FULL VERSION"
'https://www.youtube.com/watch?v=h2a6YvNdliI',
// "AVICII & RICK ASTLEY - Never Gonna Wake You Up (NilsOfficial Mashup)"
'https://www.youtube.com/watch?v=oT3mCybbhf0',
// "Rick Astley - Never Gonna Coffin You Up"
'https://www.youtube.com/watch?v=FpFztrJbksg&t=8s',
// "Never Gonna Give You Up MA E' BLUE - Rick Astley and Eiffel65 [Mashup Lol]"
'https://www.youtube.com/watch?v=3MKBDIbydZo',
// "Never Gonna Give Together Forever Up [Rick Astley mash-up]"
'https://www.youtube.com/watch?v=gQfGgHfQgv0',
];

0 comments on commit 8a1c2ce

Please sign in to comment.