refactor index.tsx and [id].tsx

This commit is contained in:
Divyam 2023-10-03 13:26:14 +05:30
parent 7d9e2a35e9
commit a99be0c1d8
2 changed files with 5 additions and 8 deletions

View file

@ -8,23 +8,20 @@ import header_styles from '../styles/Header.module.css'
import 'highlight.js/styles/atom-one-dark.css'; import 'highlight.js/styles/atom-one-dark.css';
import NoteAdd from '@material-ui/icons/NoteAdd' import NoteAdd from '@material-ui/icons/NoteAdd'
import { GetServerSideProps } from 'next' import { GetServerSidePropsContext } from 'next'
const Viewer = ({ code }: {code: string}) => { const Viewer = ({ code }: {code: string}) => {
const codeRef = createRef<HTMLTextAreaElement>(); const codeRef = createRef<HTMLTextAreaElement>();
const router = useRouter() const router = useRouter()
const lines = code.split('\n'); const lines = code.split('\n');
const html = hljs.highlightAuto(code); const html = hljs.highlightAuto(code);
useEffect(() => { useEffect(() => {
if (codeRef.current) if (codeRef.current)
codeRef.current.innerHTML = html.value; codeRef.current.innerHTML = html.value;
}, [html]) }, [html, codeRef])
return ( return (
<div className={styles.container}> <div className={styles.container}>
@ -59,7 +56,7 @@ const Viewer = ({ code }: {code: string}) => {
) )
} }
export const getServerSideProps = (async (context) => { export const getServerSideProps = async (context: GetServerSidePropsContext) => {
const id = context.params?.id const id = context.params?.id
// const setCode = (data) => {}; // const setCode = (data) => {};
@ -78,6 +75,6 @@ export const getServerSideProps = (async (context) => {
code: data.code, code: data.code,
} }
} }
}) satisfies GetServerSideProps<{ code: string }> }
export default Viewer export default Viewer

View file

@ -24,7 +24,7 @@ const Home: NextPage = () => {
}).then(res => res.json()) }).then(res => res.json())
.then(({ id }) => router.push(`/${id}`)) .then(({ id }) => router.push(`/${id}`))
.catch(() => router.push('/')) .catch(() => router.push('/'))
}, []) }, [router])
useEffect(() => { useEffect(() => {
const listener = (event : KeyboardEvent) => { const listener = (event : KeyboardEvent) => {