import React from "react" import * as antd from "antd" import { Icons, createIconRender } from "@components/Icons" import MusicModel from "@models/music" import compareObjectsByProperties from "@utils/compareObjectsByProperties" import useUrlQueryActiveKey from "@hooks/useUrlQueryActiveKey" import TrackManifest from "@cores/player/classes/TrackManifest" import { DefaultReleaseEditorState, ReleaseEditorStateContext, } from "@contexts/MusicReleaseEditor" import Tabs from "./tabs" import "./index.less" const ReleaseEditor = (props) => { const { release_id } = props const basicInfoRef = React.useRef() const [submitting, setSubmitting] = React.useState(false) const [loading, setLoading] = React.useState(true) const [submitError, setSubmitError] = React.useState(null) const [loadError, setLoadError] = React.useState(null) const [globalState, setGlobalState] = React.useState( DefaultReleaseEditorState, ) const [initialValues, setInitialValues] = React.useState({}) const [customPage, setCustomPage] = React.useState(null) const [customPageActions, setCustomPageActions] = React.useState([]) const [selectedTab, setSelectedTab] = useUrlQueryActiveKey({ defaultKey: "info", queryKey: "tab", }) async function initialize() { setLoading(true) setLoadError(null) if (release_id !== "new") { try { let releaseData = await MusicModel.getReleaseData(release_id) if (Array.isArray(releaseData.items)) { releaseData.items = releaseData.items.map((item) => { return new TrackManifest(item) }) } setGlobalState({ ...globalState, ...releaseData, }) setInitialValues(releaseData) } catch (error) { setLoadError(error) } } setLoading(false) } function hasChanges() { const stagedChanges = { title: globalState.title, type: globalState.type, public: globalState.public, cover: globalState.cover, items: globalState.items, } return !compareObjectsByProperties( stagedChanges, initialValues, Object.keys(stagedChanges), ) } async function renderCustomPage(page, actions) { setCustomPage(page ?? null) setCustomPageActions(actions ?? []) } async function handleSubmit() { setSubmitting(true) setSubmitError(null) try { console.log("Submitting Tracks") // first sumbit tracks const tracks = await MusicModel.putTrack({ items: globalState.items, }) console.log("Submitting release") // then submit release const result = await MusicModel.putRelease({ _id: globalState._id, title: globalState.title, description: globalState.description, public: globalState.public, cover: globalState.cover, explicit: globalState.explicit, type: globalState.type, items: tracks.items.map((item) => item._id), }) app.location.push(`/studio/music/${result._id}`) } catch (error) { console.error(error) app.message.error(error.message) setSubmitError(error) setSubmitting(false) return false } setSubmitting(false) app.message.success("Release saved") } async function handleDelete() { app.layout.modal.confirm({ headerText: "Are you sure you want to delete this release?", descriptionText: "This action cannot be undone.", onConfirm: async () => { await MusicModel.deleteRelease(globalState._id) app.location.push( window.location.pathname.split("/").slice(0, -1).join("/"), ) }, }) } function canFinish() { return hasChanges() } React.useEffect(() => { initialize() }, []) if (loadError) { return ( ) } if (loading) { return } const Tab = Tabs.find(({ key }) => key === selectedTab) const CustomPageProps = { close: () => { renderCustomPage(null, null) }, } return (
{customPage && (
{customPage.header && (
} onClick={() => renderCustomPage(null, null) } />

{customPage.header}

{Array.isArray(customPageActions) && customPageActions.map((action, index) => { return ( { if ( typeof action.onClick === "function" ) { await action.onClick() } if (action.fireEvent) { app.eventBus.emit( action.fireEvent, ) } }} disabled={action.disabled} > {action.label} ) })}
)} {customPage.content && (React.isValidElement(customPage.content) ? React.cloneElement(customPage.content, { ...CustomPageProps, ...customPage.props, }) : React.createElement(customPage.content, { ...CustomPageProps, ...customPage.props, }))}
)} {!customPage && ( <>
setSelectedTab(e.key)} selectedKeys={[selectedTab]} items={Tabs} mode="vertical" />
) : ( ) } disabled={ submitting || loading || !canFinish() } loading={submitting} > {release_id !== "new" ? "Save" : "Release"} {release_id !== "new" ? ( } disabled={loading} onClick={handleDelete} > Delete ) : null} {release_id !== "new" ? ( } onClick={() => app.location.push( `/music/list/${globalState._id}`, ) } > Go to release ) : null}
{submitError && ( )} {!Tab && ( )} {Tab && React.createElement(Tab.render, { release: globalState, state: globalState, setState: setGlobalState, references: { basic: basicInfoRef, }, })}
)}
) } export default ReleaseEditor