generated from theodorusclarence/ts-nextjs-tailwind-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2405980
commit 2fe2211
Showing
13 changed files
with
236 additions
and
27 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
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 |
---|---|---|
@@ -1,7 +1,81 @@ | ||
import React from 'react'; | ||
'use client'; | ||
|
||
const Code = () => { | ||
return <div className='h-20 w-full pb-12 md:w-2/3'>Code display</div>; | ||
import { useTheme } from 'next-themes'; | ||
import React, { useEffect, useState } from 'react'; | ||
import SyntaxHighlighter from 'react-syntax-highlighter'; | ||
import { | ||
atomOneLight, | ||
darcula, | ||
} from 'react-syntax-highlighter/dist/cjs/styles/hljs'; | ||
import Typed, { TypedOptions } from 'typed.js'; | ||
|
||
import { CodeSnippet } from '@/types/CodeSnippet'; | ||
|
||
export interface CodeSnippetsProps { | ||
codeSnippets: CodeSnippet[]; | ||
} | ||
|
||
const Code = ({ codeSnippets }: CodeSnippetsProps) => { | ||
const [loading, setLoading] = useState(true); | ||
|
||
const { theme } = useTheme(); | ||
|
||
const ideStyle = theme === 'dark' ? darcula : atomOneLight; | ||
|
||
useEffect(() => { | ||
setTimeout(() => { | ||
setLoading(false); | ||
}, 2000); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!loading) { | ||
const typedStringsElement = document.getElementById('typed-strings'); | ||
const typedElement = document.getElementById('typed'); | ||
|
||
if (typedStringsElement && typedElement) { | ||
const typedOptions: TypedOptions = { | ||
stringsElement: '#typed-strings', | ||
typeSpeed: 40, | ||
backSpeed: 10, | ||
backDelay: 5000, | ||
loop: true, | ||
loopCount: 0, | ||
smartBackspace: true, | ||
showCursor: true, | ||
cursorChar: '_', | ||
}; | ||
|
||
const typed = new Typed(typedElement, typedOptions); | ||
|
||
// Clean up the Typed.js instance when the component unmounts | ||
return () => { | ||
typed.destroy(); | ||
}; | ||
} | ||
} | ||
}, [loading]); | ||
|
||
return ( | ||
<div className='me-4 h-72 w-full rounded border-double bg-slate-200 px-4 pb-12 pt-6 dark:bg-slate-800 md:w-2/3'> | ||
<div id='typed-strings'> | ||
{loading ? <span className='cursor-blink'>_</span> : null} | ||
{!loading && | ||
codeSnippets.map((snippet) => ( | ||
<SyntaxHighlighter | ||
className='text-xs md:text-base' | ||
key={snippet._id} | ||
language={snippet.language} | ||
style={ideStyle} | ||
wrapLongLines={true} | ||
> | ||
{snippet.code} | ||
</SyntaxHighlighter> | ||
))} | ||
</div> | ||
<span id='typed'></span> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Code; |
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,19 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { codeSnippetsQuery } from '@/queries/codeSnippets'; | ||
|
||
import { sanityClient } from '../../../sanity/lib/client'; | ||
|
||
import { CodeSnippet } from '@/types/CodeSnippet'; | ||
|
||
const codeSnippetsApi = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const codeSnippets: CodeSnippet[] = await sanityClient.fetch( | ||
codeSnippetsQuery | ||
); | ||
|
||
res.status(200).json({ | ||
codeSnippets, | ||
}); | ||
}; | ||
|
||
export default codeSnippetsApi; |
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,9 @@ | ||
import { groq } from 'next-sanity'; | ||
|
||
export const codeSnippetsQuery = groq` | ||
*[_type == "codeSnippet"] { | ||
_id, | ||
title, | ||
"code": code.code, | ||
"language": code.language, | ||
}`; |
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,19 @@ | ||
import { defineField, defineType } from 'sanity'; | ||
|
||
export default defineType({ | ||
name: 'codeSnippet', | ||
title: 'Code Snippet', | ||
type: 'document', | ||
fields: [ | ||
defineField({ | ||
name: 'title', | ||
title: 'Title', | ||
type: 'string', | ||
}), | ||
defineField({ | ||
name: 'code', | ||
title: 'Code Snippet', | ||
type: 'code', | ||
}), | ||
], | ||
}); |
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,6 @@ | ||
export interface CodeSnippet { | ||
_id: string; | ||
title: string; | ||
code: string; | ||
language: string; | ||
} |
Oops, something went wrong.