From 3df8a006f2cbd4038f596c8f2d09d84c96ba15c7 Mon Sep 17 00:00:00 2001 From: wass08 Date: Wed, 11 Mar 2026 13:08:33 +0100 Subject: [PATCH] clean impl --- .../community/app/editor/[projectId]/page.tsx | 63 +++---------------- apps/community/lib/presets-adapter.ts | 58 +++++++++++++++++ 2 files changed, 65 insertions(+), 56 deletions(-) create mode 100644 apps/community/lib/presets-adapter.ts diff --git a/apps/community/app/editor/[projectId]/page.tsx b/apps/community/app/editor/[projectId]/page.tsx index 6fb47e15..fb66cdc4 100644 --- a/apps/community/app/editor/[projectId]/page.tsx +++ b/apps/community/app/editor/[projectId]/page.tsx @@ -1,9 +1,10 @@ 'use client' import { useParams, useRouter } from 'next/navigation' -import { useCallback, useEffect, useState } from 'react' +import { useCallback, useEffect, useMemo, useState } from 'react' import { Editor, SceneLoader } from '@pascal-app/editor' -import type { SceneGraph, PresetsAdapter } from '@pascal-app/editor' +import type { SceneGraph } from '@pascal-app/editor' +import { createApiPresetsAdapter } from '@/lib/presets-adapter' import { CommunityAppMenu } from '@/features/community/components/community-app-menu' import { ProjectHeader } from '@/features/community/components/project-header' import { useAuth } from '@/features/community/lib/auth/hooks' @@ -49,60 +50,10 @@ export default function EditorPage() { await saveProjectModel(projectId, scene) }, [projectId]) - const apiPresetsAdapter: PresetsAdapter = { - tabs: ['community', 'mine'], - isAuthenticated, - fetchPresets: async (type, tab) => { - const res = await fetch(`/api/presets?type=${type}&tab=${tab}`) - if (!res.ok) return [] - const json = await res.json() - return json.presets ?? [] - }, - savePreset: async (type, name, data) => { - const res = await fetch('/api/presets', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ type, name, data }), - }) - if (!res.ok) return null - const json = await res.json() - return json.preset?.id ?? null - }, - overwritePreset: async (_type, id, data) => { - await fetch(`/api/presets/${id}`, { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ data }), - }) - }, - renamePreset: async (id, name) => { - await fetch(`/api/presets/${id}`, { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ name }), - }) - }, - deletePreset: async (id) => { - await fetch(`/api/presets/${id}`, { method: 'DELETE' }) - }, - togglePresetCommunity: async (id, current) => { - await fetch(`/api/presets/${id}`, { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ is_community: !current }), - }) - }, - uploadPresetThumbnail: async (presetId, blob) => { - const res = await fetch(`/api/presets/${presetId}/thumbnail`, { - method: 'POST', - body: blob, - headers: { 'Content-Type': 'image/png' }, - }) - if (!res.ok) return null - const json = await res.json() - return json.thumbnail_url ?? null - }, - } + const apiPresetsAdapter = useMemo( + () => createApiPresetsAdapter(isAuthenticated), + [isAuthenticated], + ) const onThumbnailCapture = useCallback(async (blob: Blob) => { const result = await uploadProjectThumbnail(projectId, blob) diff --git a/apps/community/lib/presets-adapter.ts b/apps/community/lib/presets-adapter.ts new file mode 100644 index 00000000..a9d8f814 --- /dev/null +++ b/apps/community/lib/presets-adapter.ts @@ -0,0 +1,58 @@ +import type { PresetsAdapter } from '@pascal-app/editor' + +export function createApiPresetsAdapter(isAuthenticated: boolean): PresetsAdapter { + return { + tabs: ['community', 'mine'], + isAuthenticated, + fetchPresets: async (type, tab) => { + const res = await fetch(`/api/presets?type=${type}&tab=${tab}`) + if (!res.ok) return [] + const json = await res.json() + return json.presets ?? [] + }, + savePreset: async (type, name, data) => { + const res = await fetch('/api/presets', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ type, name, data }), + }) + if (!res.ok) return null + const json = await res.json() + return json.preset?.id ?? null + }, + overwritePreset: async (_type, id, data) => { + await fetch(`/api/presets/${id}`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ data }), + }) + }, + renamePreset: async (id, name) => { + await fetch(`/api/presets/${id}`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ name }), + }) + }, + deletePreset: async (id) => { + await fetch(`/api/presets/${id}`, { method: 'DELETE' }) + }, + togglePresetCommunity: async (id, current) => { + await fetch(`/api/presets/${id}`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ is_community: !current }), + }) + }, + uploadPresetThumbnail: async (presetId, blob) => { + const res = await fetch(`/api/presets/${presetId}/thumbnail`, { + method: 'POST', + body: blob, + headers: { 'Content-Type': 'image/png' }, + }) + if (!res.ok) return null + const json = await res.json() + return json.thumbnail_url ?? null + }, + } +}