refactor: improve type definitions and enhance state management in profile and daily components

This commit is contained in:
2026-03-14 16:31:16 +01:00
parent 4e95abf09f
commit e5a21cb0af
5 changed files with 139 additions and 76 deletions

View File

@@ -1,5 +1,5 @@
import { db } from '$lib/server/db';
import { arc, character, characterHistory, characterOverride, devilFruit } from '$lib/server/db/schema';
import { arc, character, characterHistory, characterOverride, devilFruit, type Character, type CharacterOverride } from '$lib/server/db/schema';
import { desc, eq, inArray, and } from 'drizzle-orm';
// Generate or get random seed for daily character selection
@@ -29,14 +29,12 @@ const characterWithRelationsSelect = {
arcName: arc.name
};
export type CharacterWithRelations = typeof character.$inferSelect & {
export type CharacterWithRelations = Character & {
devilFruitName: string | null;
devilFruitType: string | null;
arcName: string | null;
};
type CharacterOverrideRow = typeof characterOverride.$inferSelect;
type RelationMaps = {
arcNameById: Map<string, string | null>;
devilFruitById: Map<string, { name: string | null; type: string | null }>;
@@ -48,7 +46,7 @@ function isNotNullish<T>(value: T | null | undefined): value is T {
function mergeCharacterWithOverride(
baseCharacter: CharacterWithRelations,
overrideRow?: CharacterOverrideRow,
overrideRow?: CharacterOverride,
relationMaps?: RelationMaps
): CharacterWithRelations {
if (!overrideRow) {
@@ -104,7 +102,7 @@ async function applyCharacterOverrides(
return characters;
}
const overrideByCharacterId = new Map<string, CharacterOverrideRow>(
const overrideByCharacterId = new Map<string, CharacterOverride>(
overrideRows.map((overrideRow) => [overrideRow.characterId, overrideRow])
);

View File

@@ -1,5 +1,6 @@
import { integer, sqliteTable, text, real, unique } from 'drizzle-orm/sqlite-core';
import { user } from './auth.schema';
import type { InferSelectModel } from 'drizzle-orm';
// Define devil fruit types
export type DevilFruitType = 'Paramecia' | 'Zoan' | 'Logia' | 'Smile' | 'Unknown';
@@ -23,6 +24,8 @@ export const arc = sqliteTable('arc', {
url: text('url')
});
export type Arc = InferSelectModel<typeof arc>;
// Define the devil fruit table schema
export const devilFruit = sqliteTable('devil_fruit', {
id: text('id').primaryKey(),
@@ -31,6 +34,8 @@ export const devilFruit = sqliteTable('devil_fruit', {
url: text('url')
});
export type DevilFruit = InferSelectModel<typeof devilFruit>;
// Define the character table schema
export const character = sqliteTable('character', {
id: text('id').primaryKey(),
@@ -58,6 +63,8 @@ export const character = sqliteTable('character', {
isInDailyMode: integer('is_in_daily_mode', { mode: 'boolean' }).default(false)
});
export type Character = InferSelectModel<typeof character>;
// Define the character override table schema
export const characterOverride = sqliteTable('character_override', {
characterId: text('character_id').primaryKey().references(() => character.id, { onDelete: 'cascade' }),
@@ -82,6 +89,8 @@ export const characterOverride = sqliteTable('character_override', {
notes: text('notes')
});
export type CharacterOverride = InferSelectModel<typeof characterOverride>;
// Define the character scrape validation table schema
export const characterScrapeValidation = sqliteTable('character_scrape_validation', {
id: text('id').primaryKey(),
@@ -108,6 +117,8 @@ export const characterScrapeValidation = sqliteTable('character_scrape_validatio
frUrl: text('fr_url')
});
export type CharacterScrapeValidation = InferSelectModel<typeof characterScrapeValidation>;
// Define the character history table schema
export const characterHistory = sqliteTable('character_history', {
id: text('id')
@@ -120,6 +131,8 @@ export const characterHistory = sqliteTable('character_history', {
updatedAt: integer('updated_at').notNull().$default(() => Date.now()),
});
export type CharacterHistory = InferSelectModel<typeof characterHistory>;
// Define the user character history table schema
export const userCharacterHistory = sqliteTable('user_character_history', {
id: text('id')
@@ -134,6 +147,8 @@ export const userCharacterHistory = sqliteTable('user_character_history', {
unique().on(table.userId, table.characterHistoryId)
]);
export type UserCharacterHistory = InferSelectModel<typeof userCharacterHistory>;
// Define the friendship table schema (friend requests + accepted friends)
export const friendship = sqliteTable('friendship', {
id: text('id')
@@ -152,5 +167,6 @@ export const friendship = sqliteTable('friendship', {
unique().on(table.requesterId, table.addresseeId)
]);
export type Friendship = InferSelectModel<typeof friendship>;
export * from './auth.schema';