Skip to content

Instantly share code, notes, and snippets.

@SimpleCookie
SimpleCookie / useFetch.tsx
Last active July 13, 2023 13:39
useFetch.sx
import { useEffect, useState } from "react"
/**
* Example usage:
const App = () => {
const { data, error, loading } = useFetch<any>("https://jsonplaceholder.typicode.com/posts")
if (loading) return <div>Loading...</div>
if (error) return <div>Error</div>
if (data) <div>{JSON.stringify(data)}</div>
}
@SimpleCookie
SimpleCookie / useFetch.jsx
Last active June 22, 2023 20:40
Fetch in react
export const useFetch = (url) => {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const fetchData = async () => {
setLoading(true);
setError(null);
try {
@SimpleCookie
SimpleCookie / git author stats.bat
Created August 28, 2020 09:32
Retrieves stats for authors in a git repo
git log --shortstat --pretty="%cE" | sed 's/\(.*\)@.*/\1/' | grep -v "^$" | awk 'BEGIN { line=""; } !/^ / { if (line=="" || !match(line, $0)) {line = $0 "," line }} /^ / { print line " # " $0; line=""}' | sort | sed -E 's/# //;s/ files? changed,//;s/([0-9]+) ([0-9]+ deletion)/\1 0 insertions\(+\), \2/;s/\(\+\)$/\(\+\), 0 deletions\(-\)/;s/insertions?\(\+\), //;s/ deletions?\(-\)//' | awk 'BEGIN {name=""; files=0; insertions=0; deletions=0;} {if ($1 != name && name != "") { print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net"; files=0; insertions=0; deletions=0; name=$1; } name=$1; files+=$2; insertions+=$3; deletions+=$4} END {print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net";}'
# Credits to Dan and his team
# https://stackoverflow.com/questions/1265040/how-to-count-total-lines-changed-by-a-specific-author-in-a-git-repository
@SimpleCookie
SimpleCookie / useRelativeClick.tsx
Created April 21, 2020 08:57
useRelativeClick React Hook
import { useEffect } from "react";
export function useRelativeClick(ref, callback: (inside: boolean) => any) {
function handleClickOutside(event) {
if (ref.current) {
const insideClick = ref.current.contains(event.target)
callback(insideClick)
}
}
@SimpleCookie
SimpleCookie / cloudSettings
Last active June 9, 2020 07:29
Visual Studio Code Settings Sync Gist
{"lastUpload":"2020-06-09T07:29:00.207Z","extensionVersion":"v3.4.3"}