Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Mar 30, 2024
2 parents a382539 + 6a03645 commit 7a624b2
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 35 deletions.
8 changes: 6 additions & 2 deletions src/components/ErrorComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,25 @@ import { useNavigate } from "@tanstack/react-router";
export default function ErrorComponent({
error,
}: {
error: any;
error: unknown;
}) {
const navigate = useNavigate();

return (
<Stack p="md">
<Title>An error ocurred</Title>
{error instanceof Error && (
{error instanceof Error ? (
<>
<Text>
<b>{error.name}:</b> {error.message}
</Text>
<Code>{error.stack}</Code>
{error.cause}
</>
) : (
<Text>
<b>Unexpected Error:</b> {JSON.stringify(error)}
</Text>
)}
<Group>
{error instanceof Error && (
Expand Down
166 changes: 154 additions & 12 deletions src/components/boards/AnnotationHint.tsx

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions src/components/files/FilesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { useToggle } from "@mantine/hooks";
import { IconPlus, IconSearch, IconX } from "@tabler/icons-react";
import { useLoaderData } from "@tanstack/react-router";
import { readDir, removeFile } from "@tauri-apps/api/fs";
import { type FileEntry, readDir, removeFile } from "@tauri-apps/api/fs";
import React, { useEffect, useState } from "react";
import useSWR from "swr";
import ConfirmModal from "../common/ConfirmModal";
Expand All @@ -39,9 +39,16 @@ export type MetadataOrEntry = {
async function processFiles(
files: MetadataOrEntry[],
): Promise<MetadataOrEntry[]> {
const filesInfo = await Promise.all(
files.map((f) => readFileMetadata(f.name, f.path, f.children)),
);
const filesInfo = (
await Promise.allSettled(
files.map((f) => readFileMetadata(f.name, f.path, f.children)),
)
)
.filter((r) => r.status === "fulfilled")
.map(
(r) =>
(r as PromiseFulfilledResult<FileMetadata | FileEntry | null>).value,
);
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (file.children) {
Expand Down
8 changes: 5 additions & 3 deletions src/components/home/Databases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function Databases() {
async () => {
const playerDbs = playerDbNames.find((p) => p.name === name)?.databases;
if (!databases || !playerDbs) return [];
const newInfo: PersonalInfo[] = await Promise.all(
const results = await Promise.allSettled(
databases
.filter((db) => playerDbs.includes(db.title || ""))
.map(async (db, i) => {
Expand All @@ -172,15 +172,17 @@ function Databases() {
if (players.data.length > 0) {
player = players.data[0];
} else {
throw "Player not found in database";
throw new Error("Player not found in database");
}
const info = unwrap(
await commands.getPlayersGameInfo(db.file, player.id),
);
return { db, info };
}),
);
return newInfo;
return results
.filter((r) => r.status === "fulfilled")
.map((r) => (r as PromiseFulfilledResult<PersonalInfo>).value);
},
);

Expand Down
14 changes: 7 additions & 7 deletions src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import TopBar from "@/components/TopBar";
import { openFile } from "@/utils/files";
import { createTab } from "@/utils/tabs";
import { AppShell } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import {
Outlet,
createRootRouteWithContext,
useNavigate,
} from "@tanstack/react-router";
import { ask, message, open } from "@tauri-apps/api/dialog";
import { listen } from "@tauri-apps/api/event";
import { appDataDir, resolve } from "@tauri-apps/api/path";
import { appLogDir, resolve } from "@tauri-apps/api/path";
import { open as shellOpen } from "@tauri-apps/api/shell";
import { checkUpdate, installUpdate } from "@tauri-apps/api/updater";
import { appWindow } from "@tauri-apps/api/window";
Expand Down Expand Up @@ -143,12 +144,11 @@ function RootLayout() {
label: "Open Logs",
id: "logs",
action: async () => {
const appDataDirPath = await appDataDir();
const path = await resolve(
appDataDirPath,
"logs",
"en-croissant.log",
);
const path = await resolve(await appLogDir(), "en-croissant.log");
notifications.show({
title: "Logs",
message: `Opened logs in ${path}`,
});
await shellOpen(path);
},
},
Expand Down
4 changes: 3 additions & 1 deletion src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ export async function query_tournaments(
export async function getDatabases(): Promise<DatabaseInfo[]> {
const files = await readDir("db", { dir: BaseDirectory.AppData });
const dbs = files.filter((file) => file.name?.endsWith(".db3"));
return await Promise.all(dbs.map((db) => getDatabase(db.path)));
return (await Promise.allSettled(dbs.map((db) => getDatabase(db.path))))
.filter((r) => r.status === "fulfilled")
.map((r) => (r as PromiseFulfilledResult<DatabaseInfo>).value);
}

export async function getDatabase(path: string): Promise<DatabaseInfo> {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/invoke.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export function unwrap<T>(result: Result<T, string>): T {
color: "red",
icon: <IconX />,
});
throw result.error;
throw new Error(result.error);
}
8 changes: 3 additions & 5 deletions src/utils/puzzles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export async function getPuzzleDatabase(path: string): Promise<PuzzleDatabase> {
export async function getPuzzleDatabases(): Promise<PuzzleDatabase[]> {
const files = await readDir("puzzles", { dir: BaseDirectory.AppData });
const dbs = files.filter((file) => file.name?.endsWith(".db3"));
return (
await Promise.all(
dbs.map((db) => getPuzzleDatabase(db.path).catch(() => null)),
)
).filter((db) => db !== null) as PuzzleDatabase[];
return (await Promise.allSettled(dbs.map((db) => getPuzzleDatabase(db.path))))
.filter((r) => r.status === "fulfilled")
.map((r) => (r as PromiseFulfilledResult<PuzzleDatabase>).value);
}

0 comments on commit 7a624b2

Please sign in to comment.