clean impl
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useParams, useRouter } from 'next/navigation'
|
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 { 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 { CommunityAppMenu } from '@/features/community/components/community-app-menu'
|
||||||
import { ProjectHeader } from '@/features/community/components/project-header'
|
import { ProjectHeader } from '@/features/community/components/project-header'
|
||||||
import { useAuth } from '@/features/community/lib/auth/hooks'
|
import { useAuth } from '@/features/community/lib/auth/hooks'
|
||||||
@@ -49,60 +50,10 @@ export default function EditorPage() {
|
|||||||
await saveProjectModel(projectId, scene)
|
await saveProjectModel(projectId, scene)
|
||||||
}, [projectId])
|
}, [projectId])
|
||||||
|
|
||||||
const apiPresetsAdapter: PresetsAdapter = {
|
const apiPresetsAdapter = useMemo(
|
||||||
tabs: ['community', 'mine'],
|
() => createApiPresetsAdapter(isAuthenticated),
|
||||||
isAuthenticated,
|
[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 onThumbnailCapture = useCallback(async (blob: Blob) => {
|
const onThumbnailCapture = useCallback(async (blob: Blob) => {
|
||||||
const result = await uploadProjectThumbnail(projectId, blob)
|
const result = await uploadProjectThumbnail(projectId, blob)
|
||||||
|
|||||||
@@ -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
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user