Skip to content

Instantly share code, notes, and snippets.

View gaurangrshah's full-sized avatar
💭
🚀

gshah2020 gaurangrshah

💭
🚀
View GitHub Profile
function useThrottle(value, interval = 500) {
const [throttledValue, setThrottledValue] = useState(value);
const lastExecuted = useRef(Date.now());
useEffect(() => {
if (Date.now() >= lastExecuted.current + interval) {
lastExecuted.current = Date.now();
setThrottledValue(value);
}
else {
const timerId = setTimeout(() => {
function useLocalStorageState(
key,
defaultValue = '',
// the = {} fixes the error we would get from destructuring when no argument was passed
// Check https://jacobparis.com/blog/destructure-arguments for a detailed explanation
{serialize = JSON.stringify, deserialize = JSON.parse} = {},
) {
const [state, setState] = React.useState(() => {
const valueInLocalStorage = window.localStorage.getItem(key)
if (valueInLocalStorage) {
const delay = (delay, value) => {
let timeout;
let _reject = null;
const promise = new Promise((resolve, reject) => {
_reject= reject;
timeout = setTimeout(resolve, delay, value);
});
return {
promise,
cancel() {
@gaurangrshah
gaurangrshah / dataMiner.js
Created June 15, 2021 17:58 — forked from Harshmakadia/dataMiner.js
twitter-analytics-data-scraper
// 1. Go to https://analytics.twitter.com/
// 2. Keep scrolling till the end until all the stats data is loaded
// 3. Right click on page click on last option "Inspect" a window should open select console from that
// 4. copy this entire function
function dataMiner(){
let masterArray = [];
@gaurangrshah
gaurangrshah / performance.js
Created June 1, 2021 18:20
use to test the performance of the execution of any function or method.
const begin = performance.now();
// include the operation you want to measure:
for let(i = 0; i < 100; i++) {
console.log(i);
}
const end = performance.now();
const timeElapsed = end - begin;
function checkFonts() {
document.fonts.onloadingdone = function (fontFaceSetEvent) {
console.log(
"🚀 ~ file: default.js ~ line 27 ~ checkFonts ~ fontFaceSetEvent",
fontFaceSetEvent
);
alert(
"onloadingdone we have " +
fontFaceSetEvent.fontfaces.length +
" font faces loaded"
import { useTheme, useColorMode } from "@chakra-ui/react";
export function useColor() {
const theme = useTheme();
const { colorMode } = useColorMode();
const { mode } = theme.colors;
return {
mode: colorMode,
color: (colorName) => mode[colorMode][colorName],
};
/**
* A simple method of demonstrating how you can use the Next.js
* internal API for 'server-less' functions. We export an async function.
*
* We import the nodemailer package to handle SMTP transactional mail.
*
* In a production environment, you could create a .env.production
* to hold your secure details, e.g.,
* HOST, AUTH_USER, AUTH_PASS
*
// pages/api/my-api-call.js
const fetch = require("node-fetch");
export default async (req, res) => {
try {
const apiCall = await fetch(`http://api-path?api-key=${process.env.SECRET_KEY}`);
if (apiCall.response.status === 200) {
res.status(200).json({});
res.end();
return res;
@gaurangrshah
gaurangrshah / .page..js
Last active December 16, 2021 17:13
Synced via Snip
j// [page].js
import { Box, Flex, Image } from "@chakra-ui/react";
import { Section } from "@/components/section";
import { getPage, getRecords } from "@/libs/airtable";
const Sandbox = ({ page }) => {
console.log("🚀 ~ file: sandbox.js ~ line 6 ~ Sandbox ~ page", page);
return (
<>