This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: enable semicolons in prettier config
- Loading branch information
1 parent
8801e5a
commit 4c1c23a
Showing
19 changed files
with
129 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ export default { | |
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
import { useState } from "react" | ||
import { useState } from "react"; | ||
|
||
import { notify } from "../util" | ||
import { notify } from "../util"; | ||
|
||
export default function useFavoriteQuotes() { | ||
const getFavoriteQuoteIds = () => { | ||
if (typeof window === "undefined") return [] | ||
if (typeof window === "undefined") return []; | ||
|
||
const favoriteQuoteIds = window.localStorage.getItem("favoriteQuoteIds") | ||
return favoriteQuoteIds ? (JSON.parse(favoriteQuoteIds) as number[]) : [] | ||
} | ||
const favoriteQuoteIds = window.localStorage.getItem("favoriteQuoteIds"); | ||
return favoriteQuoteIds ? (JSON.parse(favoriteQuoteIds) as number[]) : []; | ||
}; | ||
|
||
const [favoriteQuoteIds, setFavoriteQuoteIds] = useState(getFavoriteQuoteIds) | ||
const [favoriteQuoteIds, setFavoriteQuoteIds] = useState(getFavoriteQuoteIds); | ||
|
||
const addFavoriteQuote = (quoteId: number) => { | ||
if (favoriteQuoteIds.includes(quoteId)) { | ||
const filteredQuoteIds = favoriteQuoteIds.filter(id => id !== quoteId) | ||
const filteredQuoteIds = favoriteQuoteIds.filter(id => id !== quoteId); | ||
|
||
setFavoriteQuoteIds(filteredQuoteIds) | ||
window.localStorage.setItem("favoriteQuoteIds", JSON.stringify(filteredQuoteIds)) | ||
notify("💔", "Quote removed from favorites!") | ||
setFavoriteQuoteIds(filteredQuoteIds); | ||
window.localStorage.setItem("favoriteQuoteIds", JSON.stringify(filteredQuoteIds)); | ||
notify("💔", "Quote removed from favorites!"); | ||
} else { | ||
setFavoriteQuoteIds([...favoriteQuoteIds, quoteId]) | ||
window.localStorage.setItem("favoriteQuoteIds", JSON.stringify([...favoriteQuoteIds, quoteId])) | ||
notify("❤️", "Quote added to favorites!") | ||
setFavoriteQuoteIds([...favoriteQuoteIds, quoteId]); | ||
window.localStorage.setItem("favoriteQuoteIds", JSON.stringify([...favoriteQuoteIds, quoteId])); | ||
notify("❤️", "Quote added to favorites!"); | ||
} | ||
} | ||
}; | ||
|
||
const setFavoriteQuotes = (quoteIds: number[]) => { | ||
setFavoriteQuoteIds(quoteIds) | ||
window.localStorage.setItem("favoriteQuoteIds", JSON.stringify(quoteIds)) | ||
notify("❤️", "Quotes added to favorites!") | ||
} | ||
setFavoriteQuoteIds(quoteIds); | ||
window.localStorage.setItem("favoriteQuoteIds", JSON.stringify(quoteIds)); | ||
notify("❤️", "Quotes added to favorites!"); | ||
}; | ||
|
||
return { favoriteQuoteIds, addFavoriteQuote, setFavoriteQuotes } | ||
return { favoriteQuoteIds, addFavoriteQuote, setFavoriteQuotes }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
import { useNavigate } from "react-router-dom" | ||
import { useCallback } from "react" | ||
import copy from "copy-to-clipboard" | ||
import { useNavigate } from "react-router-dom"; | ||
import { useCallback } from "react"; | ||
import copy from "copy-to-clipboard"; | ||
|
||
import { notify } from "../util" | ||
import { rawQuotes } from "../data/rawQuotes" | ||
import useFavoriteQuotes from "./useFavoriteQuotes" | ||
import useRandomItem from "./useRandomItem" | ||
import { notify } from "../util"; | ||
import { rawQuotes } from "../data/rawQuotes"; | ||
import useFavoriteQuotes from "./useFavoriteQuotes"; | ||
import useRandomItem from "./useRandomItem"; | ||
|
||
export default function useQuotes(quoteId: string) { | ||
const navigate = useNavigate() | ||
const navigate = useNavigate(); | ||
|
||
const filteredQuotes = rawQuotes.filter(quote => quote.verified) | ||
const { item: randomQuote, change: changeRandomQuote } = useRandomItem(filteredQuotes) | ||
const { favoriteQuoteIds, addFavoriteQuote } = useFavoriteQuotes() | ||
const filteredQuotes = rawQuotes.filter(quote => quote.verified); | ||
const { item: randomQuote, change: changeRandomQuote } = useRandomItem(filteredQuotes); | ||
const { favoriteQuoteIds, addFavoriteQuote } = useFavoriteQuotes(); | ||
|
||
const quote = filteredQuotes.find(quote => quote.id === parseInt(quoteId)) | ||
const quote = filteredQuotes.find(quote => quote.id === parseInt(quoteId)); | ||
|
||
const isFavorite = quote ? favoriteQuoteIds.includes(quote.id) : false | ||
const isFavorite = quote ? favoriteQuoteIds.includes(quote.id) : false; | ||
|
||
const handleCopy = useCallback(() => { | ||
const copied = copy(`${quote?.text} - ${quote?.author || "Unknown"}`) | ||
if (copied) notify("📋", "Quote copied to the clipboard!") | ||
}, [quote?.text, quote?.author]) | ||
const copied = copy(`${quote?.text} - ${quote?.author || "Unknown"}`); | ||
if (copied) notify("📋", "Quote copied to the clipboard!"); | ||
}, [quote?.text, quote?.author]); | ||
|
||
const handleFavorite = useCallback(() => { | ||
if (quote) addFavoriteQuote(quote.id) | ||
}, [addFavoriteQuote, quote]) | ||
if (quote) addFavoriteQuote(quote.id); | ||
}, [addFavoriteQuote, quote]); | ||
|
||
const handleGenerate = () => { | ||
changeRandomQuote() | ||
navigate(`/${randomQuote.id}`) | ||
} | ||
changeRandomQuote(); | ||
navigate(`/${randomQuote.id}`); | ||
}; | ||
|
||
const handleShare = useCallback(() => { | ||
const copied = copy(`${window.location.host}/${quote?.id}`) | ||
if (copied) notify("🔗", "Quote link copied to the clipboard!") | ||
}, [quote?.id]) | ||
const copied = copy(`${window.location.host}/${quote?.id}`); | ||
if (copied) notify("🔗", "Quote link copied to the clipboard!"); | ||
}, [quote?.id]); | ||
|
||
return { quote, isFavorite, handleCopy, handleFavorite, handleGenerate, handleShare } | ||
return { quote, isFavorite, handleCopy, handleFavorite, handleGenerate, handleShare }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import { useCallback, useEffect, useState } from "react" | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
export default function useRandomItem<T>(items: T[]) { | ||
const [pickedIndex, setPickedIndex] = useState(-1) | ||
const [pickedIndex, setPickedIndex] = useState(-1); | ||
|
||
if (items.length < 2) throw new Error("Items must have at least 2 elements") | ||
if (items.length < 2) throw new Error("Items must have at least 2 elements"); | ||
|
||
const change = useCallback(() => { | ||
let randomIndex = Math.floor(Math.random() * items.length) | ||
let randomIndex = Math.floor(Math.random() * items.length); | ||
|
||
while (randomIndex === pickedIndex) { | ||
randomIndex = Math.floor(Math.random() * items.length) | ||
randomIndex = Math.floor(Math.random() * items.length); | ||
} | ||
|
||
setPickedIndex(randomIndex) | ||
}, [items.length, pickedIndex]) | ||
setPickedIndex(randomIndex); | ||
}, [items.length, pickedIndex]); | ||
|
||
useEffect(change, [change]) | ||
useEffect(change, [change]); | ||
|
||
const item = items[pickedIndex] | ||
const item = items[pickedIndex]; | ||
|
||
return { item, change } | ||
return { item, change }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { Analytics } from "@vercel/analytics/react" | ||
import { createRoot } from "react-dom/client" | ||
import { Toaster } from "react-hot-toast" | ||
import { StrictMode } from "react" | ||
import { createBrowserRouter, RouterProvider } from "react-router-dom" | ||
import { Analytics } from "@vercel/analytics/react"; | ||
import { createRoot } from "react-dom/client"; | ||
import { Toaster } from "react-hot-toast"; | ||
import { StrictMode } from "react"; | ||
import { createBrowserRouter, RouterProvider } from "react-router-dom"; | ||
|
||
import "./styles/globals.css" | ||
import { routes } from "./pages" | ||
import "./styles/globals.css"; | ||
import { routes } from "./pages"; | ||
|
||
createRoot(document.querySelector("#root")!).render( | ||
<StrictMode> | ||
<Analytics /> | ||
<Toaster /> | ||
<RouterProvider router={createBrowserRouter(routes)} /> | ||
</StrictMode>, | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export const REPOSITORY_LINK = "https://github.com/sdanialraza/quotes-generator" as const | ||
export const REPOSITORY_LINK = "https://github.com/sdanialraza/quotes-generator" as const; |
Oops, something went wrong.