파일 페이지에 카테고리 목록 및 카테고리에 추가 버튼 구현

This commit is contained in:
static
2025-01-22 13:50:36 +09:00
parent a2402f37a0
commit 4c0d668cc1
7 changed files with 80 additions and 28 deletions

View File

@@ -46,6 +46,7 @@ export interface FileInfo {
name: string;
createdAt?: Date;
lastModifiedAt: Date;
categoryIds: number[];
}
type CategoryId = "root" | number;
@@ -160,7 +161,7 @@ const fetchFileInfoFromIndexedDB = async (id: number, info: Writable<FileInfo |
const file = await getFileInfoFromIndexedDB(id);
if (!file) return;
info.set(file);
info.set({ ...file, categoryIds: [] });
};
const decryptDate = async (ciphertext: string, iv: string, dataKey: CryptoKey) => {
@@ -204,6 +205,7 @@ const fetchFileInfoFromServer = async (
name,
createdAt,
lastModifiedAt,
categoryIds: metadata.categories,
});
await storeFileInfo({
id,

View File

@@ -12,8 +12,10 @@
let { categories, onCategoryClick }: Props = $props();
</script>
<div class="space-y-1">
{#each categories as category}
<Category info={category} onclick={onCategoryClick} />
{/each}
</div>
{#if categories.length > 0}
<div class="space-y-1">
{#each categories as category}
<Category info={category} onclick={onCategoryClick} />
{/each}
</div>
{/if}

View File

@@ -441,6 +441,15 @@ export const addFileToCategory = async (fileId: number, categoryId: number) => {
});
};
export const getAllFileCategories = async (fileId: number) => {
const categories = await db
.selectFrom("file_category")
.select("category_id")
.where("file_id", "=", fileId)
.execute();
return categories.map(({ category_id }) => ({ id: category_id }));
};
export const removeFileFromCategory = async (fileId: number, categoryId: number) => {
await db.transaction().execute(async (trx) => {
const res = await trx

View File

@@ -18,6 +18,7 @@ export const fileInfoResponse = z.object({
createdAtIv: z.string().base64().nonempty().optional(),
lastModifiedAt: z.string().base64().nonempty(),
lastModifiedAtIv: z.string().base64().nonempty(),
categories: z.number().int().positive().array(),
});
export type FileInfoResponse = z.infer<typeof fileInfoResponse>;

View File

@@ -13,6 +13,7 @@ import {
getFile,
setFileEncName,
unregisterFile,
getAllFileCategories,
type NewFile,
} from "$lib/server/db/file";
import type { Ciphertext } from "$lib/server/db/schema";
@@ -24,6 +25,7 @@ export const getFileInformation = async (userId: number, fileId: number) => {
error(404, "Invalid file id");
}
const categories = await getAllFileCategories(fileId);
return {
parentId: file.parentId ?? ("root" as const),
mekVersion: file.mekVersion,
@@ -34,6 +36,7 @@ export const getFileInformation = async (userId: number, fileId: number) => {
encName: file.encName,
encCreatedAt: file.encCreatedAt,
encLastModifiedAt: file.encLastModifiedAt,
categories: categories.map(({ id }) => id),
};
};