Skip to content

Instantly share code, notes, and snippets.

View johnpolacek's full-sized avatar

John Polacek johnpolacek

View GitHub Profile
@johnpolacek
johnpolacek / .gitconfig
Last active April 30, 2024 06:00
My current .gitconfig aliases
[alias]
co = checkout
cob = checkout -b
coo = !git fetch && git checkout
br = branch
brd = branch -d
brD = branch -D
merged = branch --merged
st = status
aa = add -A .
@johnpolacek
johnpolacek / AnimateIn.tsx
Last active April 20, 2024 21:00
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 / 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 / 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 / gist:3827270
Last active January 20, 2023 15:46
Prevent FOUC
<!-- Prevent FOUC (flash of unstyled content) - http://johnpolacek.com/2012/10/03/help-prevent-fouc/ -->
<style type="text/css">
.no-fouc {display: none;}
</style>
<script type="text/javascript">
document.documentElement.className = 'no-fouc';
// add to document ready: $('.no-fouc').removeClass('no-fouc');
</script>