Skip to content

Instantly share code, notes, and snippets.

View anthowave's full-sized avatar
🏠
Working from home

anthowave

🏠
Working from home
View GitHub Profile
@anthowave
anthowave / next-graph-nfts.js
Created May 21, 2021 00:07 — forked from dabit3/next-graph-nfts.js
StackBlitz, GraphQL, The Graph, Zora, & Next.js
import React from 'react'
import { createClient } from 'urql';
const client = createClient({
url: 'https://api.thegraph.com/subgraphs/name/dabit3/zoranftsubgraph'
})
const query = `
query {
<LoadingButton
ariaErrorAlert={"There was an error creating your account"}
ariaLoadingAlert={
authState === AuthState.CreatingUser
? "Registering account, please wait..."
: authState === AuthState.FulfillingPurchase
? "Generating license, please wait..."
: "Loading..."
}
ariaSuccessAlert="Account created! Redirecting."
@anthowave
anthowave / snowflake.sql
Last active January 18, 2021 09:49 — forked from dustinrouillard/snowflake.sql
DB Best Practices & PostgreSQL Snowflake ID Generator Function
CREATE SEQUENCE IF NOT EXISTS public.global_id_sequence;
CREATE OR REPLACE FUNCTION id_generator(OUT result BIGINT) AS $$
DECLARE
epoch BIGINT := 1610850820000;
seq_id BIGINT;
now_millis BIGINT;
shard_id INT := 1;
BEGIN
SELECT nextval('public.global_id_sequence') % 1024 INTO seq_id;
@anthowave
anthowave / valtio-promise.js
Last active March 27, 2022 23:15
Valtio(proxy-based state management library) demo that natively handles promise with React suspense
import React, { Suspense } from "react";
import { proxy, useProxy } from "valtio";
import { a, useSpring } from "@react-spring/web";
const fetchData = async (id) => {
const response = await fetch(
`https://hacker-news.firebaseio.com/v0/item/${id}.json`
);
return await response.json();
};
@anthowave
anthowave / README.md
Created December 28, 2020 21:55 — forked from tannerlinsley/README.md
Replacing Create React App with the Next.js CLI

Replacing Create React App with the Next.js CLI

How dare you make a jab at Create React App!?

Firstly, Create React App is good. But it's a very rigid CLI, primarily designed for projects that require very little to no configuration. This makes it great for beginners and simple projects but unfortunately, this means that it's pretty non-extensible. Despite the involvement from big names and a ton of great devs, it has left me wanting a much better developer experience with a lot more polish when it comes to hot reloading, babel configuration, webpack configuration, etc. It's definitely simple and good, but not amazing.

Now, compare that experience to Next.js which for starters has a much larger team behind it provided by a world-class company (Vercel) who are all financially dedicated to making it the best DX you could imagine to build any React application. Next.js is the 💣-diggity. It has amazing docs, great support, can grow with your requirements into SSR or static site generation, etc.

So why

@anthowave
anthowave / searchDOM.js
Last active December 24, 2020 10:53
6-line javascript code for lazy DFS over the DOM
function* search(node) {
if (!node) return;
yield node;
yield* search(node.firstChild);
yield* search(node.nextSibling);
}
// example usage
for (let node of search(document)) {
if (node.localName === 'title') {
@anthowave
anthowave / useDarkMode.js
Created December 23, 2020 10:57
A custom effect that updates the dark/light mode accordingly when the system setting gets changed
const matchDark = '(prefers-color-scheme: dark)'
function useDarkMode() {
const [isDark, setIsDark] = React.useState(
() => window.matchMedia && window.matchMedia(matchDark).matches
);
React.useEffect(() => {
const matcher = window.matchMedia(matchDark);
const onChange = ({ matches }) => setIsDark(matches);
@anthowave
anthowave / custom-type-check.ts
Created December 2, 2020 12:50
TS - determine if var is type of custom type
const stringLitArray = <L extends string>(arr: L[]) => arr;
const linkTypes = stringLitArray(['a', 'link']);
export type LinkType = (typeof linkTypes)[number];
const isLinkType = (x: any): x is LinkType => linkTypes.includes(x);
let link = 'a';
if (isLinkType('a')) {
console.log("The current link is of type 'LinkType'");
}
@anthowave
anthowave / createCrudHooks.js
Created November 29, 2020 23:45 — forked from tannerlinsley/createCrudHooks.js
A naive, but efficient starter to generate crud hooks for React Query
export default function createCrudHooks({
baseKey,
indexFn,
singleFn,
createFn,
updateFn,
deleteFn,
}) {
const useIndex = (config) => useQuery([baseKey], indexFn, config)
const useSingle = (id, config) =>
@anthowave
anthowave / CountdownTimer.js
Created November 3, 2020 22:24
CountdownTimer react component
import React from 'react'
export default function CountdownTimer({ startSeconds }) {
const [seconds, setSeconds] = React.useState(startSeconds);
const timeoutCallback = React.useCallback(() => {
if(seconds >= 1)
setSeconds(seconds - 1);
}, [seconds]);