Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohitha-pudu committed Jun 2, 2024
2 parents 09b4d6e + 20ff558 commit d41eb30
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 53 deletions.
16 changes: 16 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"axios": "^1.7.2",
"clsx": "^2.1.1",
"framer-motion": "^11.2.10",
"dompurify": "^3.1.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^5.2.1",
Expand All @@ -24,6 +25,7 @@
"zxcvbn": "^4.4.2"
},
"devDependencies": {
"@types/dompurify": "^3.0.5",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@types/zxcvbn": "^4.4.4",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function App() {
<React.Suspense fallback={<Loader/>}>
<Navbar />

<div className="min-h-[80vh]">
<div className="min-h-[80vh] mt-12 pt-12" >
<Routes>
<Route path="/app" element={<Home />} />
<Route path="/app/posts/:id" element={<Post />} />
Expand Down
52 changes: 17 additions & 35 deletions frontend/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,11 @@ const Navbar = () => {
: "block py-2 px-3 rounded md:border-0 md:p-0 text-white md:hover:text-blue-500 hover:bg-gray-700 hover:text-white md:hover:bg-transparent";
};

return (
<nav className="border-gray-200">
return (
<nav className="border-gray-200 bg-gray-800 fixed top-0 left-0 w-full z-10 py-1 bg-gradient-to-r from-pink-900 to-gray-800">
<div className="max-w-screen-xl flex flex-wrap items-center justify-between mx-auto p-4">
<Link
to="/app"
className="flex items-center space-x-3 rtl:space-x-reverse"
>
{/* <img
src=""
className="h-8"
alt="Logo"
/> */}
<span className="self-center text-2xl font-semibold whitespace-nowrap text-white">
<Link to="/app" className="flex items-center space-x-3 rtl:space-x-reverse">
<span className="self-center text-3xl leading-6 font-semibold whitespace-nowrap text-white font-teko">
Style Share
</span>
</Link>
Expand Down Expand Up @@ -67,58 +59,48 @@ const Navbar = () => {
<div
className={`${
isMenuOpen ? "block" : "hidden"
} w-full md:block md:w-auto transition-all duration-1000 ease-in-out`}
} w-full md:block md:w-auto transition-all duration-300 ease-in-out`}
id="navbar-default"
>
<ul className="font-medium flex flex-col p-4 md:p-0 mt-4 border rounded-lg md:flex-row md:space-x-8 rtl:space-x-reverse md:mt-0 md:border-0 bg-[#000435] border-gray-700">
<li>
<Link
to="/app"
className={getNavLinkClass("/app")}
aria-current="page"
>
<ul className="font-medium flex flex-col p-4 md:p-0 mt-4 border rounded-lg md:flex-row md:space-x-8 rtl:space-x-reverse md:mt-0 md:border-0">
<li className="mt-2 md:mb-0">
<Link to="/app" className={getNavLinkClass("/app")} aria-current="page">
Home
</Link>
</li>
<li>
<li className="mt-2">
<Link to="/app/posts" className={getNavLinkClass("/app/posts")}>
Posts
</Link>
</li>
{!isLoggedIn ? (
<>
<li>
<div className="flex flex-col md:flex-row md:space-x-4">
<li className="mb-2 md:mb-0">
<Link
to="/app/signin"
className={getNavLinkClass("/app/signin")}
className="block py-2 px-3 rounded-full text-white bg-gradient-to-l from-blue-400 to-pink-500 hover:from-pink-500 hover:to-blue-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-pink-500"
>
Sign in
</Link>
</li>
<li>
<Link
to="/app/signup"
className={getNavLinkClass("/app/signup")}
className="block py-2 px-3 rounded-full text-white bg-gradient-to-l from-blue-400 to-pink-500 hover:from-pink-500 hover:to-blue-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-pink-500"
>
Sign up
</Link>
</li>
</>
</div>
) : (
<>
<li>
<Link
to="/app/new-post"
className={getNavLinkClass("/app/new-post")}
>
<Link to="/app/new-post" className={getNavLinkClass("/app/new-post")}>
New Post
</Link>
</li>
<li>
<Link
to="/app/profile"
className={getNavLinkClass("/app/profile")}
>
<Link to="/app/profile" className={getNavLinkClass("/app/profile")}>
Profile
</Link>
</li>
Expand All @@ -139,4 +121,4 @@ const Navbar = () => {
);
};

export default Navbar;
export default Navbar;
1 change: 1 addition & 0 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import url('https://fonts.googleapis.com/css2?family=Sevillana&family=Seymour+One&family=Teko:[email protected]&display=swap');
@tailwind base;
@tailwind components;
@tailwind utilities;
Expand Down
95 changes: 85 additions & 10 deletions frontend/src/pages/Post.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useState } from 'react';
import { useEffect, useState, useRef } from 'react';
import { useParams } from 'react-router-dom';
import axios, { AxiosError } from 'axios';
import { IPost } from '../types';
import DOMPurify from 'dompurify';

const Post = () => {
const { id } = useParams<{ id: string }>();
Expand All @@ -19,6 +20,14 @@ const Post = () => {
});
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isPreview, setIsPreview] = useState(false);
const ref = useRef<HTMLIFrameElement>(null);
const [height, setHeight] = useState('0px');

const onLoad = () => {
setHeight(ref.current?.contentWindow?.document.body.scrollHeight + 'px');
console.log(ref.current?.contentWindow?.document.body.scrollHeight);
};

useEffect(() => {
const fetchPost = async () => {
Expand All @@ -39,11 +48,19 @@ const Post = () => {
fetchPost();
}, [id]);

useEffect(() => {
onLoad();
}, [isPreview, post.codeSnippet]);

const handleCopy = () => {
navigator.clipboard.writeText(post.codeSnippet);
alert('Code snippet copied to clipboard');
};

const togglePreview = () => {
setIsPreview(!isPreview);
};

if (loading) {
return <div className="text-white">Loading...</div>;
}
Expand All @@ -52,22 +69,80 @@ const Post = () => {
return <div className="text-red-500 text-lg w-full text-center mt-5">{error}</div>;
}

DOMPurify.addHook('uponSanitizeElement', (node, data) => {
if (data.tagName === 'img' || data.tagName === 'div') {
const src = node.getAttribute('src');
const style = node.getAttribute('style');
if (src && src.startsWith('http')) {
node.setAttribute('src', src);
}
if (style && style.includes('url(')) {
node.setAttribute('style', style);
}
}
});

const sanitizedSnippet = DOMPurify.sanitize(post?.codeSnippet || '', {
ADD_ATTR: ['style', 'background'],
});

return (
<div className="p-6 text-white max-w-screen-xl mx-auto">
{post && (
<>
<h2 className="text-2xl font-semibold mb-4">{post.title}</h2>
<p className="mb-4">{post.description}</p>
<div className="relative mb-4">
<pre className="p-4 bg-gray-800 border border-gray-700 rounded overflow-auto">
<code>{post.codeSnippet}</code>
</pre>
<button
onClick={handleCopy}
className="absolute top-2 right-2 px-2 py-1 bg-blue-600 hover:bg-blue-700 text-white text-sm rounded"
>
Copy
</button>
{isPreview ? (
<div className="p-4 bg-gray-800 z-0 h-full overflow-hidden rounded border border-gray-700">
<iframe
ref={ref}
onLoad={onLoad}
className="w-full h-full border-0"
srcDoc={
`<html class='flex w-full h-full'>
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('a[href="#"]').forEach(function(anchor) {
anchor.addEventListener('click', function(e) {
e.preventDefault();
window.top.location.reload();
});
});
});
</script>
</head>
<body class='w-full h-full flex items-center justify-center minw-full min-h-full'>
<div class='w-full h-full p-6'>${sanitizedSnippet}</div>
</body>
</html>`}
title="Preview"
sandbox="allow-scripts allow-same-origin"
style={{ minHeight: height, maxWidth: '100%' }}
/>
</div>
) : (
<pre className="p-4 bg-gray-800 border border-gray-700 rounded overflow-auto max-h-96 line-numbers language-html">
<code>{post.codeSnippet}</code>
</pre>
)}
<div className="absolute top-2 right-3 flex space-x-2">
{isPreview ? null :
<button
onClick={handleCopy}
className="px-2 py-1 bg-blue-600 hover:bg-blue-700 text-white text-sm rounded"
>
Copy
</button>}
<button
onClick={togglePreview}
className="px-2 py-1 bg-blue-600 hover:bg-blue-700 text-white text-sm rounded"
>
{isPreview ? 'Show Code' : 'Preview'}
</button>
</div>
</div>
<div className="mb-4">
<h3 className="text-xl font-semibold mb-2">Tags</h3>
Expand Down
14 changes: 7 additions & 7 deletions frontend/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/** @type {import('tailwindcss').Config} */
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
extend: {
fontFamily: {
teko: ["Teko", "sans-serif"],
},
},
},
plugins: [],
}

};

0 comments on commit d41eb30

Please sign in to comment.