Skip to content

Commit e810c35

Browse files
feat: read server data from the preference dialog
1 parent ddedaf4 commit e810c35

File tree

7 files changed

+58
-49
lines changed

7 files changed

+58
-49
lines changed

src/constants/preference.ts

+33-29
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ export interface Preference {
99
core: Partial<Record<CoreName, Record<string, string>>>
1010
platform: Record<PlatformName, { core: CoreName; shader?: string }>
1111
}
12-
libraryCoverType: 'boxart'
13-
platformInfoDisplayType: 0
14-
platforms: PlatformName[]
15-
theme: 'rose'
12+
ui: {
13+
libraryCoverType: 'boxart'
14+
platformInfoDisplayType: 0
15+
platforms: PlatformName[]
16+
theme: 'rose'
17+
}
1618
}
1719

1820
export const defaultPreference: Preference = {
@@ -50,29 +52,31 @@ export const defaultPreference: Preference = {
5052
wonderswancolor: { core: 'mednafen_wswan' },
5153
},
5254
},
53-
libraryCoverType: 'boxart',
54-
platformInfoDisplayType: 0,
55-
platforms: [
56-
'arcade',
57-
'atari2600',
58-
'gb',
59-
'gba',
60-
'gbc',
61-
'megadrive',
62-
'nes',
63-
'snes',
64-
'vb',
65-
'wonderswan',
66-
'wonderswancolor',
67-
'sms',
68-
'ngp',
69-
'ngpc',
70-
'gamegear',
71-
'atari5200',
72-
'atari7800',
73-
'atarilynx',
74-
'sega32x',
75-
'sg-1000',
76-
],
77-
theme: 'rose',
55+
ui: {
56+
libraryCoverType: 'boxart',
57+
platformInfoDisplayType: 0,
58+
platforms: [
59+
'arcade',
60+
'atari2600',
61+
'gb',
62+
'gba',
63+
'gbc',
64+
'megadrive',
65+
'nes',
66+
'snes',
67+
'vb',
68+
'wonderswan',
69+
'wonderswancolor',
70+
'sms',
71+
'ngp',
72+
'ngpc',
73+
'gamegear',
74+
'atari5200',
75+
'atari7800',
76+
'atarilynx',
77+
'sega32x',
78+
'sg-1000',
79+
],
80+
theme: 'rose',
81+
},
7882
}

src/controllers/get-preference.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ export async function getPreference() {
88
const { currentUser, db } = getContextData()
99

1010
const where = eq(userPreferenceTable.user_id, currentUser.id)
11-
const results = await db.library.select().from(userPreferenceTable).where(where)
11+
const results = await db.library
12+
.select({ emulator: userPreferenceTable.emulator, ui: userPreferenceTable.ui })
13+
.from(userPreferenceTable)
14+
.where(where)
1215
const preference = structuredClone(defaultPreference)
13-
const userPreference = results[0].content
14-
merge(preference, userPreference as any)
16+
const userPreference = results[0]
17+
merge(preference, (userPreference as any) || {})
1518
return preference
1619
}

src/middlewares/waku/globals.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { eq } from 'drizzle-orm'
2-
import { merge } from 'es-toolkit'
31
import type { Middleware } from 'waku/config'
4-
import { defaultPreference } from '../../constants/preference.ts'
5-
import { userPreferenceTable } from '../../databases/library/schema.ts'
2+
import type { defaultPreference } from '../../constants/preference.ts'
3+
import { getPreference } from '../../controllers/get-preference.ts'
64
import { createDrizzle } from '../../utils/drizzle.ts'
75
import { createStorage } from '../../utils/storage.ts'
86
import { createSupabase } from '../../utils/supabase.ts'
@@ -33,22 +31,18 @@ export default (function globalsMiddleware() {
3331
// const { data } = await ctx.data.supabase.auth.getUser()
3432
// const currentUser = data?.user
3533
const currentUser = { id: '567a53eb-c109-4142-8700-00f58db9853f' }
36-
const customPreference = await db.library
37-
.select()
38-
.from(userPreferenceTable)
39-
.where(eq(userPreferenceTable.user_id, currentUser.id))
40-
const preference = merge(defaultPreference, customPreference)
4134

4235
function redirect(location: string, status?: number) {
4336
ctx.res.status = status ?? 302
4437
ctx.res.headers ??= {}
4538
ctx.res.headers.location = location
4639
}
4740

48-
const contextData: ContextData = { currentUser, db, preference, redirect, storage, supabase }
41+
const contextData: Omit<ContextData, 'preference'> = { currentUser, db, redirect, storage, supabase }
4942

5043
Object.assign(ctx.data, contextData)
51-
44+
const preference = await getPreference()
45+
Object.assign(ctx.data, { preference })
5246
await next()
5347
}
5448
} as Middleware)

src/pages/library/actions.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use server'
2+
3+
export function handleChange(e) {
4+
console.log(process)
5+
}

src/pages/library/components/emulating-settings.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import { usePreference } from '../hooks/use-preference.ts'
77

88
export function EmulatingSettings() {
99
const preference = usePreference()
10-
const [selectedPlatform, setSelectedPlatform] = useState(preference.platforms?.[0])
11-
console.log(preference)
10+
const [selectedPlatform, setSelectedPlatform] = useState(preference.ui.platforms?.[0])
1211

13-
if (!preference.platforms?.length) {
12+
if (!preference.ui.platforms?.length) {
1413
return
1514
}
1615

@@ -31,7 +30,7 @@ export function EmulatingSettings() {
3130
>
3231
<Select.Trigger variant='ghost' />
3332
<Select.Content>
34-
{preference.platforms.map((platform) => (
33+
{preference.ui.platforms.map((platform) => (
3534
<Select.Item key={platformMap[platform].name} value={platformMap[platform].name}>
3635
<div className='flex items-center gap-2'>
3736
<img

src/pages/library/components/platform-checkbox.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { CheckboxCards } from '@radix-ui/themes'
22
import { platforms } from '@/constants/platform.ts'
33
import { getPlatformIcon } from '@/utils/rom.ts'
4+
import { handleChange } from '../actions.ts'
5+
import { usePreference } from '../hooks/use-preference.ts'
46

57
export function PlatformCheckbox() {
8+
const { ui } = usePreference()
9+
610
return (
7-
<CheckboxCards.Root columns='4' size='1'>
11+
<CheckboxCards.Root columns='4' onValueChange={handleChange} size='1' value={ui?.platforms || []}>
812
{platforms.map((platform) => (
913
<CheckboxCards.Item key={platform.name} value={platform.name}>
1014
<div className='flex items-center gap-2'>

src/pages/library/components/sidebar-links.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SidebarLink } from './sidebar-link.tsx'
66
export function SidebarLinks() {
77
const { preference } = getContextData()
88

9-
const platformLinks = preference.platforms.map((platform) => ({
9+
const platformLinks = preference.ui.platforms.map((platform) => ({
1010
href: `/library/platform/${encodeURIComponent(platform)}`,
1111
icon: getPlatformIcon(platform, ''),
1212
name: platform,

0 commit comments

Comments
 (0)