-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added a view-for-all page * added copied to clipboard toast
- Loading branch information
1 parent
82450fe
commit 8105210
Showing
7 changed files
with
223 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// api to check if hash is valid, accepts username and hash, return true or false | ||
|
||
import { | ||
bcryptGen, | ||
extractFilenameWithoutExtension | ||
} from '@/utils/stringHelpers'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { fetchSavedCards } from '@/api-helpers/persistance'; | ||
import { logException } from '@/utils/logger'; | ||
|
||
const checkHash = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const { username, hash } = req.query; | ||
if (!username || !hash) { | ||
return res.status(400).json({ | ||
message: 'Username or hash not found.' | ||
}); | ||
} | ||
|
||
const userNameHash = bcryptGen(username as string); | ||
const isValid = userNameHash === hash; | ||
|
||
if (isValid) { | ||
try { | ||
const imageData = await fetchSavedCards(username as string); | ||
const imageDataWithURL = imageData.map((image) => { | ||
const filename = extractFilenameWithoutExtension(image.fileName); | ||
const hash = bcryptGen((username as string) + filename); | ||
return { | ||
...image, | ||
url: `/shared/${username}/${filename}/${hash}` | ||
}; | ||
}); | ||
|
||
res.status(200).json({ isValid, data: imageDataWithURL }); | ||
} catch (e) { | ||
logException('Error fetching from share-all data from s3', { | ||
originalException: e | ||
}); | ||
res.status(400).json({ isValid, data: null }); | ||
} | ||
} else res.status(400).json({ isValid, data: null }); | ||
}; | ||
|
||
export default checkHash; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { handleRequest } from '@/utils/axios'; | ||
import { LoaderWithFacts } from '@/components/LoaderWithFacts'; | ||
import SwiperCarousel from '@/components/SwiperCarousel'; | ||
import { useImageDownloader } from '@/hooks/useImageDownloader'; | ||
import Confetti from 'react-confetti'; | ||
import toast from 'react-hot-toast'; | ||
import { UpdatedImageFile } from '@/types/images'; | ||
import { useRouter } from 'next/router'; | ||
|
||
export default function StatsUnwrapped() { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const downloadImage = useImageDownloader(); | ||
|
||
const router = useRouter(); | ||
const userName = router.query.username as string; | ||
const hash = (router.query.hash as string[])?.join('/'); | ||
|
||
const [isUrlValid, setIsUrlValid] = useState(false); | ||
const [images, setImages] = useState<UpdatedImageFile[] | null>(null); | ||
|
||
useEffect(() => { | ||
if (!userName || !hash || isUrlValid) return; | ||
setIsLoading(true); | ||
handleRequest<{ | ||
isValid: boolean; | ||
data: UpdatedImageFile[]; | ||
}>('/api/getAllImages', { | ||
method: 'GET', | ||
params: { | ||
hash: hash, | ||
username: userName | ||
} | ||
}) | ||
.then((res) => { | ||
setIsUrlValid(res.isValid); | ||
if (res.isValid) { | ||
const imageData: UpdatedImageFile[] = res.data.map((link) => ({ | ||
url: `${window.location.origin}${link.url}`, | ||
fileName: link.fileName, | ||
data: link.data | ||
})); | ||
setImages(imageData); | ||
} | ||
}) | ||
.catch((_) => { | ||
toast.error('Invalid URL', { | ||
position: 'top-right' | ||
}); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
}, [userName, hash, isUrlValid]); | ||
|
||
if (isLoading) { | ||
return ( | ||
<div className="h-screen flex flex-col items-center justify-between p-10"> | ||
<LoaderWithFacts /> | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div className="items-center justify-center p-4 min-h-screen w-full flex flex-col gap-10 text-center"> | ||
<div> | ||
<h2 className="text-2xl"> | ||
Unwrap{' '} | ||
<span className="font-bold text-[#bc9aef]">{userName}'s</span>{' '} | ||
GitHub journey of 2023! 🎉 | ||
</h2> | ||
</div> | ||
{images?.length && <Confetti numberOfPieces={400} recycle={false} />} | ||
{images?.length && ( | ||
<div className="flex flex-col items-center gap-4 w-full "> | ||
<SwiperCarousel | ||
useLinksToRenderImages | ||
hideShareButtons | ||
userName={userName} | ||
images={images} | ||
singleImageSharingCallback={downloadImage} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters