Skip to content

Instantly share code, notes, and snippets.

View johnpolacek's full-sized avatar

John Polacek johnpolacek

View GitHub Profile
@johnpolacek
johnpolacek / PrintObject.tsx
Created February 22, 2024 22:11
Utility React Tailwind Component for printing an object to the screen for building and debugging
import React from "react"
const PrintObject: React.FC<{ object: Record<string, unknown> }> = ({ object }) => {
return (
<pre className="w-full max-w-5xl overflow-scroll text-sm border bg-black text-white rounded p-2 ">
<code>{JSON.stringify(object, null, 2)}</code>
</pre>
)
}
@johnpolacek
johnpolacek / AnimateIn.tsx
Last active October 30, 2024 01:25
Utility React UI Component for animating elements in with Tailwind and CSS Animation and plays nicely with shadcn
import React, { useEffect, useState } from "react";
import cn from "mxcn";
// or if using shadcn:
// import { cn } from "@/lib/utils"; // https://github.com/shadcn-ui/ui/blob/main/apps/www/lib/utils.ts
const AnimateIn = ({
children,
delay = 0,
duration = 500,
className = "",
@johnpolacek
johnpolacek / createPublicS3Bucket.js
Created September 15, 2023 13:27
Create a Public S3 Bucket via AWS SDK for Node
// Create the bucket
const bucketName = await getBucketName()
const createBucketCommand = new CreateBucketCommand({
Bucket: bucketName,
ObjectOwnership: "ObjectWriter",
})
await s3.send(createBucketCommand)
// Delete the block public access
const deletePublicAccessBlockCommand = new DeletePublicAccessBlockCommand({
@johnpolacek
johnpolacek / scratchpad.js
Created September 8, 2023 19:50
scratchpad
function getNumMatchesAtCoordinate(matrix, x, y) {
const match = matrix[x][y]; // 1st is row, 2nd is col
let numMatches = 0; // at least 1 match
const maxLength = Math.min(matrix.length - x, matrix[0].length - y);
for (let i = 0; i < maxLength; i++) {
// increment row
let isMatch = true;
for (let j = 0; j <= i; j++) {
// increment col
// checks outermost row and column in the growing square
import { NextApiRequest, NextApiResponse } from "next";
export async function handleApiRequest(
req: NextApiRequest,
res: NextApiResponse,
expectedReqMethod: string,
handleRequest: (body: any) => Promise<any>
): Promise<void> {
if (req.method === expectedReqMethod) {
try {
import AWS from "aws-sdk"
AWS.config.update({
region: process.env.AWS_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
})
const s3 = new AWS.S3()
const BUCKET = "your-bucket-name"
@johnpolacek
johnpolacek / FadeIn.tsx
Created March 7, 2023 12:11
Tailwind Component for Animate Fade-In and Up
import React, { useEffect, useState } from "react"
const FadeIn = ({
children,
}: {
children: JSX.Element | JSX.Element[] | string
}) => {
const [animate, setAnimate] = useState("opacity-0 translate-y-10")
useEffect(() => {
@johnpolacek
johnpolacek / nextjs-api-stream-hooks.ts
Last active March 1, 2023 12:29
React Hooks for handline data from Next.js API routes that return readable streams from the OpenAI API
// use this hook to fetch data from a stream
// get the cumulative response as it is read
export const useStreamingDataFromPrompt = async (
prompt: string,
onData: (data: string) => void,
onDone?: () => void
) => {
const response = await fetch("/api/generate", {
method: "POST",
headers: {
@johnpolacek
johnpolacek / Fetch Example
Created February 19, 2022 21:43
Fetch Example
fetch('https://example.com').then(response => {
if (!response.ok) {
throw Error(response.statusText);
}
return response.json();
})
.then(data => {
res.status(200).json({ data })
})
@johnpolacek
johnpolacek / CodeBlock.js
Created November 11, 2021 21:32
React Component for rendering blocks of code with Prism
import Highlight, { defaultProps } from "prism-react-renderer"
import github from "prism-react-renderer/themes/github"
const CodeBlock = ({ children, className }) => {
const language = className ? className.replace(/language-/, "") : "javascript"
return (
<Highlight
{...defaultProps}
code={children}
language={language}