사소한 리팩토링 2

This commit is contained in:
static
2025-12-29 18:14:42 +09:00
parent 174305ca1b
commit 15b6a53710
12 changed files with 37 additions and 39 deletions

View File

@@ -92,7 +92,7 @@
{#key info} {#key info}
<RowVirtualizer <RowVirtualizer
count={files.length} count={files.length}
itemHeight={(index) => 56 + (index + 1 < files.length ? 4 : 0)} itemHeight={(index) => 48 + (index + 1 < files.length ? 4 : 0)}
> >
{#snippet item(index)} {#snippet item(index)}
{@const { info, isRecursive } = files[index]!} {@const { info, isRecursive } = files[index]!}

View File

@@ -1,4 +1,3 @@
import { TRPCClientError } from "@trpc/client";
import { get, writable, type Writable } from "svelte/store"; import { get, writable, type Writable } from "svelte/store";
import { import {
getDirectoryInfos as getDirectoryInfosFromIndexedDB, getDirectoryInfos as getDirectoryInfosFromIndexedDB,
@@ -18,7 +17,7 @@ import {
type CategoryId, type CategoryId,
} from "$lib/indexedDB"; } from "$lib/indexedDB";
import { unwrapDataKey, decryptString } from "$lib/modules/crypto"; import { unwrapDataKey, decryptString } from "$lib/modules/crypto";
import { trpc } from "$trpc/client"; import { trpc, isTRPCClientError } from "$trpc/client";
export type DirectoryInfo = export type DirectoryInfo =
| { | {
@@ -114,7 +113,7 @@ const fetchDirectoryInfoFromServer = async (
try { try {
data = await trpc().directory.get.query({ id }); data = await trpc().directory.get.query({ id });
} catch (e) { } catch (e) {
if (e instanceof TRPCClientError && e.data?.code === "NOT_FOUND") { if (isTRPCClientError(e) && e.data?.code === "NOT_FOUND") {
info.set(null); info.set(null);
await deleteDirectoryInfo(id as number); await deleteDirectoryInfo(id as number);
return; return;
@@ -187,7 +186,7 @@ const fetchFileInfoFromServer = async (
try { try {
metadata = await trpc().file.get.query({ id }); metadata = await trpc().file.get.query({ id });
} catch (e) { } catch (e) {
if (e instanceof TRPCClientError && e.data?.code === "NOT_FOUND") { if (isTRPCClientError(e) && e.data?.code === "NOT_FOUND") {
info.set(null); info.set(null);
await deleteFileInfo(id); await deleteFileInfo(id);
return; return;
@@ -283,7 +282,7 @@ const fetchCategoryInfoFromServer = async (
try { try {
data = await trpc().category.get.query({ id }); data = await trpc().category.get.query({ id });
} catch (e) { } catch (e) {
if (e instanceof TRPCClientError && e.data?.code === "NOT_FOUND") { if (isTRPCClientError(e) && e.data?.code === "NOT_FOUND") {
info.set(null); info.set(null);
await deleteCategoryInfo(id as number); await deleteCategoryInfo(id as number);
return; return;

View File

@@ -1,6 +1,5 @@
import { TRPCClientError } from "@trpc/client";
import { encodeToBase64, decryptChallenge, signMessageRSA } from "$lib/modules/crypto"; import { encodeToBase64, decryptChallenge, signMessageRSA } from "$lib/modules/crypto";
import { trpc } from "$trpc/client"; import { trpc, isTRPCClientError } from "$trpc/client";
export const requestSessionUpgrade = async ( export const requestSessionUpgrade = async (
encryptKeyBase64: string, encryptKeyBase64: string,
@@ -16,7 +15,7 @@ export const requestSessionUpgrade = async (
sigPubKey: verifyKeyBase64, sigPubKey: verifyKeyBase64,
})); }));
} catch (e) { } catch (e) {
if (e instanceof TRPCClientError && e.data?.code === "FORBIDDEN") { if (isTRPCClientError(e) && e.data?.code === "FORBIDDEN") {
return [false, "Unregistered client"] as const; return [false, "Unregistered client"] as const;
} }
return [false] as const; return [false] as const;
@@ -31,7 +30,7 @@ export const requestSessionUpgrade = async (
force, force,
}); });
} catch (e) { } catch (e) {
if (e instanceof TRPCClientError && e.data?.code === "CONFLICT") { if (isTRPCClientError(e) && e.data?.code === "CONFLICT") {
return [false, "Already logged in"] as const; return [false, "Already logged in"] as const;
} }
return [false] as const; return [false] as const;

View File

@@ -1,4 +1,3 @@
import { TRPCClientError } from "@trpc/client";
import { storeMasterKeys } from "$lib/indexedDB"; import { storeMasterKeys } from "$lib/indexedDB";
import { import {
encodeToBase64, encodeToBase64,
@@ -11,7 +10,7 @@ import {
} from "$lib/modules/crypto"; } from "$lib/modules/crypto";
import { requestSessionUpgrade } from "$lib/services/auth"; import { requestSessionUpgrade } from "$lib/services/auth";
import { masterKeyStore, type ClientKeys } from "$lib/stores"; import { masterKeyStore, type ClientKeys } from "$lib/stores";
import { trpc } from "$trpc/client"; import { trpc, isTRPCClientError } from "$trpc/client";
export const requestClientRegistration = async ( export const requestClientRegistration = async (
encryptKeyBase64: string, encryptKeyBase64: string,
@@ -112,10 +111,7 @@ export const requestInitialMasterKeyAndHmacSecretRegistration = async (
mekSig: await signMasterKeyWrapped(masterKeyWrapped, 1, signKey), mekSig: await signMasterKeyWrapped(masterKeyWrapped, 1, signKey),
}); });
} catch (e) { } catch (e) {
if ( if (isTRPCClientError(e) && (e.data?.code === "FORBIDDEN" || e.data?.code === "CONFLICT")) {
e instanceof TRPCClientError &&
(e.data?.code === "FORBIDDEN" || e.data?.code === "CONFLICT")
) {
return true; return true;
} }
// TODO: Error Handling // TODO: Error Handling

View File

@@ -0,0 +1,7 @@
import { createCaller } from "$trpc/router.server";
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async (event) => {
const files = await createCaller(event).file.list();
return { files };
};

View File

@@ -1,7 +0,0 @@
import { trpc } from "$trpc/client";
import type { PageLoad } from "./$types";
export const load: PageLoad = async ({ fetch }) => {
const files = await trpc(fetch).file.list.query();
return { files };
};

View File

@@ -0,0 +1,7 @@
import { createCaller } from "$trpc/router.server";
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async (event) => {
const files = await createCaller(event).file.listWithoutThumbnail();
return { files };
};

View File

@@ -1,7 +0,0 @@
import { trpc } from "$trpc/client";
import type { PageLoad } from "./$types";
export const load: PageLoad = async ({ fetch }) => {
const files = await trpc(fetch).file.listWithoutThumbnail.query();
return { files };
};

View File

@@ -130,7 +130,7 @@
{#if files.length > 0} {#if files.length > 0}
<RowVirtualizer <RowVirtualizer
count={files.length} count={files.length}
itemHeight={(index) => 48 + (index + 1 < files.length ? 4 : 0)} itemHeight={(index) => 56 + (index + 1 < files.length ? 4 : 0)}
> >
{#snippet item(index)} {#snippet item(index)}
{@const file = files[index]!} {@const file = files[index]!}

View File

@@ -0,0 +1,7 @@
import { createCaller } from "$trpc/router.server";
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async (event) => {
const { nickname } = await createCaller(event).user.get();
return { nickname };
};

View File

@@ -1,7 +0,0 @@
import { trpc } from "$trpc/client";
import type { PageLoad } from "./$types";
export const load: PageLoad = async ({ fetch }) => {
const { nickname } = await trpc(fetch).user.get.query();
return { nickname };
};

View File

@@ -1,4 +1,4 @@
import { createTRPCClient, httpBatchLink } from "@trpc/client"; import { createTRPCClient, httpBatchLink, TRPCClientError } from "@trpc/client";
import superjson from "superjson"; import superjson from "superjson";
import { browser } from "$app/environment"; import { browser } from "$app/environment";
import type { AppRouter } from "./router.server"; import type { AppRouter } from "./router.server";
@@ -24,3 +24,7 @@ export const trpc = (fetch = globalThis.fetch) => {
} }
return client; return client;
}; };
export const isTRPCClientError = (e: unknown): e is TRPCClientError<AppRouter> => {
return e instanceof TRPCClientError;
};