Skip to content

Commit

Permalink
Merge #147 from 7Pawns/development
Browse files Browse the repository at this point in the history
Random Quotes and Failed Image Fetching Handling
  • Loading branch information
the-wright-jamie authored Jan 29, 2024
2 parents 7152179 + 953bae3 commit 7349a50
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/plugins/backgrounds/unsplash/Unsplash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const Unsplash: React.FC<Props> = ({
}
}, [cache]);

const url = item ? buildLink(item.src) : null;
const url = item?.src.length ? buildLink(item.src) : null;

const go = (amount: number) =>
cache && cache.items[cache.cursor + amount]
Expand Down
15 changes: 14 additions & 1 deletion src/plugins/backgrounds/unsplash/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ export const fetchImages = async ({
const res = await fetch(`${url}?${params}`, { headers, cache: "no-cache" });
const body = await res.json();

// TODO: validate types
if (res.status != 200) {
const empty: Image[] = [
{
src: "",
credit: {
imageLink: "",
location: undefined,
userName: "",
userLink: "",
}
}
]
return empty;
};

return body.map((item: any) => ({
src: item.urls.raw,
Expand Down
25 changes: 18 additions & 7 deletions src/plugins/widgets/quote/QuoteSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import { Props, defaultData } from "./types";

const QuoteSettings: React.FC<Props> = ({ data = defaultData, setData }) => (
<div className="QuoteSettings">
<p>
Daily Quotes from 'They Said So' are no longer available, please see{" "}
<a href="https://github.com/the-wright-jamie/tab-nine/issues/29">
this GitHub issue
</a>
. We apologize for the inconvenience this may have caused.
</p>
<h5>Hourly Quotes</h5>
<label>
<input
Expand All @@ -30,6 +23,24 @@ const QuoteSettings: React.FC<Props> = ({ data = defaultData, setData }) => (
Developer Excuses
</a>
</p>
<label>
<input
type="radio"
checked={data.category === "random"}
onChange={() => setData({ category: "random" })}
/>{" "}
Random
</label>
<p>
Powered by{" "}
<a
href="https://github.com/lukePeavey/quotable"
target="_blank"
rel="noopener noreferrer"
>
Quotable
</a>
</p>
</div>
);

Expand Down
30 changes: 28 additions & 2 deletions src/plugins/widgets/quote/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@ import { API } from "../../types";
import { Quote } from "./types";

// Get developer excuse
async function getDeveloperExcuse() {
async function getDeveloperExcuse(): Promise<{quote: string, author: string | undefined}> {
try {
const res = await fetch("https://api.tabliss.io/v1/developer-excuses");
const body = await res.json();

return {
quote: body.data,
author: "Normal Developer"
};
} catch (err) {
return {
quote: "Unable to get a new developer excuse.",
author: undefined
};
}
}

async function getRandomQuote(): Promise<{quote: string, author: string | undefined}> {
try {
const res = await fetch("https://api.quotable.io/quotes/random?limit=1");
const body = await res.json();
const quote = body[0];

return {
quote: quote.content,
author: quote.author
};
} catch (err) {
return {
quote: "Unable to get random quote.",
author: undefined
};
}
}
Expand All @@ -21,17 +41,23 @@ export async function getQuote(
loader: API["loader"],
category: string,
): Promise<Quote> {

loader.push();

const data = await getDeveloperExcuse();
const data =
category === "developerexcuses"
? await getDeveloperExcuse()
: await getRandomQuote();

loader.pop();

const quote = cleanQuote(data.quote);
const author = data.author;

return {
...data,
quote,
author,
timestamp: Date.now(),
};
}
Expand Down

0 comments on commit 7349a50

Please sign in to comment.