Skip to content

Commit

Permalink
feat: show error when failing to import font entries
Browse files Browse the repository at this point in the history
  • Loading branch information
PalmDevs committed Oct 21, 2024
1 parent cc5d908 commit 9511f17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
22 changes: 19 additions & 3 deletions src/core/ui/settings/pages/Fonts/FontEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createProxy, useProxy } from "@core/vendetta/storage";
import { FontDefinition, fonts, removeFont, saveFont, validateFont } from "@lib/addons/fonts";
import { getCurrentTheme } from "@lib/addons/themes";
import { findAssetId } from "@lib/api/assets";
import { semanticColors } from "@lib/ui/color";
import { createStyles, TextStyleSheet } from "@lib/ui/styles";
import { safeFetch } from "@lib/utils";
import { NavigationNative } from "@metro/common";
import { ActionSheet, BottomSheetTitleHeader, Button, IconButton, Stack, TableRow, TableRowGroup, Text, TextInput } from "@metro/common/components";
Expand All @@ -11,6 +13,13 @@ import { ErrorBoundary } from "@ui/components";
import { useMemo, useRef, useState } from "react";
import { ScrollView, View } from "react-native";

const useStyles = createStyles({
errorText: {
...TextStyleSheet["text-xs/medium"],
color: semanticColors.TEXT_DANGER,
},
})

const actionSheet = findByPropsLazy("hideActionSheet");

function guessFontName(urls: string[]) {
Expand Down Expand Up @@ -245,6 +254,9 @@ export default function FontEditor(props: {
const [name, setName] = useState<string | undefined>(props.name);
const [source, setSource] = useState<string>();
const [importing, setIsImporting] = useState<boolean>(false);
const [errors, setErrors] = useState<Array<Error | undefined>>();

const styles = useStyles();

const memoEntry = useMemo(() => {
return createProxy(props.name ? { ...fonts[props.name].main } : {}).proxy;
Expand Down Expand Up @@ -297,11 +309,13 @@ export default function FontEditor(props: {
onChange={setName}
/>
<TableRowGroup title="Font Entries">
{Object.entries(fontEntries).map(([name, url]) => {
{Object.entries(fontEntries).map(([name, url], index) => {
const error = errors?.[index];

return <TableRow
label={name}
subLabel={url}
trailing={<Stack spacing={2} direction="horizontal">
subLabel={error ? <Text style={styles.errorText}>{error.message}</Text> : url}
trailing={<Stack spacing={8} direction="horizontal">
<IconButton
size="sm"
variant="secondary"
Expand All @@ -322,6 +336,7 @@ export default function FontEditor(props: {
})}
<TableRow label={<NewEntryRow fontEntry={fontEntries} />} />
</TableRowGroup>
{errors && <Text style={styles.errorText}>Some font entries cannot be imported. Please modify the entries and try again.</Text>}
<View style={{ flexDirection: "row", justifyContent: "flex-end", bottom: 0, left: 0 }}>
<Button
size="lg"
Expand All @@ -342,6 +357,7 @@ export default function FontEditor(props: {
__source: source
})
.then(() => navigation.goBack())
.catch(es => setErrors(es))
.finally(() => setIsImporting(false));
} else {
Object.assign(fonts[props.name], {
Expand Down
18 changes: 8 additions & 10 deletions src/lib/addons/fonts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ export async function saveFont(data: string | FontDefinition, selected = false)

validateFont(fontDefJson);

try {
await Promise.all(Object.entries(fontDefJson.main).map(async ([font, url]) => {
let ext = url.split(".").pop();
if (ext !== "ttf" && ext !== "otf") ext = "ttf";
const path = `downloads/fonts/${fontDefJson.name}/${font}.${ext}`;
if (!await fileExists(path)) await downloadFile(url, path);
}));
} catch (e) {
throw new Error("Failed to download font assets", { cause: e });
}
const errors = await Promise.allSettled(Object.entries(fontDefJson.main).map(async ([font, url]) => {
let ext = url.split(".").pop();
if (ext !== "ttf" && ext !== "otf") ext = "ttf";
const path = `downloads/fonts/${fontDefJson.name}/${font}.${ext}`;
if (!await fileExists(path)) await downloadFile(url, path);
})).then(it => it.map(it => it.status === 'fulfilled' ? undefined : it.reason));

if (errors.some(it => it)) throw errors

fonts[fontDefJson.name] = fontDefJson;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/native/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function readFile(path: string, { prefix = "pyoncord/" } = {}): Pro
export async function downloadFile(url: string, path: string, { prefix = "pyoncord/" } = {}) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to download file from ${url}: ${response.statusText}`);
throw new Error(`Failed to download file from ${url}: ${response.status}`);
}

const arrayBuffer = await response.arrayBuffer();
Expand Down

0 comments on commit 9511f17

Please sign in to comment.