Run prettier

This commit is contained in:
Jared Palmer 2023-06-02 15:33:48 -04:00
parent aa83a871dd
commit 417f69e0f1
34 changed files with 530 additions and 523 deletions

View file

@ -1,19 +1,19 @@
import type { RefObject } from "react";
import { useRef } from "react";
import type { RefObject } from 'react'
import { useRef } from 'react'
export function useCmdEnterSubmit(): {
formRef: RefObject<HTMLFormElement>;
onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void;
formRef: RefObject<HTMLFormElement>
onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void
} {
const formRef = useRef<HTMLFormElement>(null);
const formRef = useRef<HTMLFormElement>(null)
const handleKeyDown = (
event: React.KeyboardEvent<HTMLTextAreaElement>
): void => {
if (event.key === "Enter") {
formRef.current?.requestSubmit();
if (event.key === 'Enter') {
formRef.current?.requestSubmit()
}
};
}
return { formRef, onKeyDown: handleKeyDown };
return { formRef, onKeyDown: handleKeyDown }
}

View file

@ -1,14 +1,14 @@
'use client';
import { useState } from 'react';
'use client'
import { useState } from 'react'
export interface useCopyToClipboardProps {
timeout?: number;
timeout?: number
}
export function useCopyToClipboard({
timeout = 2000,
timeout = 2000
}: useCopyToClipboardProps) {
const [isCopied, setIsCopied] = useState<Boolean>(false);
const [isCopied, setIsCopied] = useState<Boolean>(false)
const copyToClipboard = (value: string) => {
if (
@ -16,17 +16,17 @@ export function useCopyToClipboard({
!navigator.clipboard ||
!navigator.clipboard.writeText
) {
return;
return
}
navigator.clipboard.writeText(value).then(() => {
setIsCopied(true);
setIsCopied(true)
setTimeout(() => {
setIsCopied(false);
}, timeout);
});
};
setIsCopied(false)
}, timeout)
})
}
return { isCopied, copyToClipboard };
return { isCopied, copyToClipboard }
}

View file

@ -1,43 +1,43 @@
import { useEffect, useRef } from "react";
import { useEffect, useRef } from 'react'
const scrollToEnd = () => {
window.scrollTo({
top: document.body.scrollHeight,
left: 0,
});
};
left: 0
})
}
export function useFollowScroll(shouldFollow: boolean) {
const shouldFollowRef = useRef(shouldFollow);
const scrollRef = useRef(false);
const triggeredBySelfRef = useRef(false);
const shouldFollowRef = useRef(shouldFollow)
const scrollRef = useRef(false)
const triggeredBySelfRef = useRef(false)
useEffect(() => {
const handleScroll = () => {
if (triggeredBySelfRef.current) {
triggeredBySelfRef.current = false;
return;
triggeredBySelfRef.current = false
return
}
scrollRef.current = true;
};
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
scrollRef.current = true
}
window.addEventListener('scroll', handleScroll)
return () => window.removeEventListener('scroll', handleScroll)
}, [])
useEffect(() => {
if (!scrollRef.current && shouldFollowRef.current) {
setTimeout(() => {
triggeredBySelfRef.current = true;
scrollToEnd();
});
triggeredBySelfRef.current = true
scrollToEnd()
})
}
});
})
useEffect(() => {
shouldFollowRef.current = shouldFollow;
shouldFollowRef.current = shouldFollow
if (!shouldFollow) {
// Reset scrollRef
scrollRef.current = false;
scrollRef.current = false
}
}, [shouldFollow]);
}, [shouldFollow])
}

View file

@ -1,26 +1,26 @@
import { useEffect, useState } from "react";
import { useEffect, useState } from 'react'
const useLocalStorage = <T>(
key: string,
initialValue: T
): [T, (value: T) => void] => {
const [storedValue, setStoredValue] = useState(initialValue);
const [storedValue, setStoredValue] = useState(initialValue)
useEffect(() => {
// Retrieve from localStorage
const item = window.localStorage.getItem(key);
const item = window.localStorage.getItem(key)
if (item) {
setStoredValue(JSON.parse(item));
setStoredValue(JSON.parse(item))
}
}, [key]);
}, [key])
const setValue = (value: T) => {
// Save state
setStoredValue(value);
setStoredValue(value)
// Save to localStorage
window.localStorage.setItem(key, JSON.stringify(value));
};
return [storedValue, setValue];
};
window.localStorage.setItem(key, JSON.stringify(value))
}
return [storedValue, setValue]
}
export default useLocalStorage;
export default useLocalStorage

View file

@ -1,16 +1,16 @@
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useState } from 'react'
export default function useScroll(threshold: number) {
const [scrolled, setScrolled] = useState(false);
const [scrolled, setScrolled] = useState(false)
const onScroll = useCallback(() => {
setScrolled(window.pageYOffset > threshold);
}, [threshold]);
setScrolled(window.pageYOffset > threshold)
}, [threshold])
useEffect(() => {
window.addEventListener("scroll", onScroll);
return () => window.removeEventListener("scroll", onScroll);
}, [onScroll]);
window.addEventListener('scroll', onScroll)
return () => window.removeEventListener('scroll', onScroll)
}, [onScroll])
return scrolled;
return scrolled
}

View file

@ -1,14 +1,14 @@
"use client";
import { useEffect, useState } from "react";
'use client'
import { useEffect, useState } from 'react'
export default function useWindowSize() {
const [windowSize, setWindowSize] = useState<{
width: number | undefined;
height: number | undefined;
width: number | undefined
height: number | undefined
}>({
width: undefined,
height: undefined,
});
height: undefined
})
useEffect(() => {
// Handler to call on window resize
@ -16,24 +16,23 @@ export default function useWindowSize() {
// Set window width/height to state
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
height: window.innerHeight
})
}
// Add event listener
window.addEventListener("resize", handleResize);
window.addEventListener('resize', handleResize)
// Call handler right away so state gets updated with initial window size
handleResize();
handleResize()
// Remove event listener on cleanup
return () => window.removeEventListener("resize", handleResize);
}, []); // Empty array ensures that effect is only run on mount
return () => window.removeEventListener('resize', handleResize)
}, []) // Empty array ensures that effect is only run on mount
return {
windowSize,
isMobile: typeof windowSize?.width === "number" && windowSize?.width < 768,
isDesktop:
typeof windowSize?.width === "number" && windowSize?.width >= 768,
};
isMobile: typeof windowSize?.width === 'number' && windowSize?.width < 768,
isDesktop: typeof windowSize?.width === 'number' && windowSize?.width >= 768
}
}