mirror of
https://github.com/Nezumi-2711/next-gdrive-index.git
synced 2026-09-23 04:10:03 +00:00
Remove .env
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { toast } from "react-toastify";
|
||||
|
||||
export default function useCopyText() {
|
||||
return (text: string) => {
|
||||
if (!navigator.clipboard) {
|
||||
toast.error("Your browser does not support copying to clipboard.");
|
||||
return;
|
||||
}
|
||||
if (!text) {
|
||||
toast.error("No text to copy.");
|
||||
return;
|
||||
}
|
||||
|
||||
navigator.clipboard
|
||||
.writeText(text)
|
||||
.then(() => {
|
||||
toast.success("Copied to clipboard.");
|
||||
})
|
||||
.catch(() => {
|
||||
toast.error("Could not copy to clipboard.");
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
type SetValue<T> = (value: T | ((val: T) => T)) => void;
|
||||
|
||||
export default function useLocalStorage<T>(key: string, initialValue: T) {
|
||||
// First check if there is a value in localStorage
|
||||
function readLocalStorage() {
|
||||
if (typeof window === "undefined") return initialValue;
|
||||
|
||||
try {
|
||||
const item = window.localStorage.getItem(key);
|
||||
return item ? JSON.parse(item) : initialValue;
|
||||
} catch (error) {
|
||||
console.warn(`Error reading localStorage key “${key}”:`, error);
|
||||
return initialValue;
|
||||
}
|
||||
}
|
||||
|
||||
const [storedValue, setStoredValue] = useState<T>(readLocalStorage);
|
||||
|
||||
// Create dispatch function to set or remove localStorage
|
||||
const setValue: SetValue<T> = (value) => {
|
||||
try {
|
||||
const valueToStore =
|
||||
value instanceof Function ? value(storedValue) : value;
|
||||
setStoredValue(valueToStore);
|
||||
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
||||
} catch (error) {
|
||||
console.error("Error setting localStorage:", error);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleStorageChange = (event: StorageEvent) => {
|
||||
if (event.key === key) {
|
||||
try {
|
||||
if (event.newValue === null) {
|
||||
setStoredValue(initialValue);
|
||||
return;
|
||||
}
|
||||
const newValue = JSON.parse(event.newValue);
|
||||
setStoredValue(newValue);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("storage", handleStorageChange);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("storage", handleStorageChange);
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [key]);
|
||||
|
||||
return [storedValue, setValue] as const;
|
||||
}
|
||||
Reference in New Issue
Block a user