import { PageLayoutComponentInstanceContext } from '@/page-layout/states/contexts/PageLayoutComponentInstanceContext'; import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow'; import { useAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentState'; import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue'; import { isDeeplyEqual } from '~/utils/isDeeplyEqual'; import { pageLayoutDraftComponentState } from '@/page-layout/states/pageLayoutDraftComponentState'; import { pageLayoutPersistedComponentState } from '@/page-layout/states/pageLayoutPersistedComponentState'; export const usePageLayoutDraftState = (pageLayoutIdFromProps?: string) => { const pageLayoutId = useAvailableComponentInstanceIdOrThrow( PageLayoutComponentInstanceContext, pageLayoutIdFromProps, ); const [pageLayoutDraft, setPageLayoutDraft] = useAtomComponentState( pageLayoutDraftComponentState, pageLayoutId, ); const pageLayoutPersisted = useAtomComponentStateValue( pageLayoutPersistedComponentState, pageLayoutId, ); const isDirty = pageLayoutPersisted ? !isDeeplyEqual(pageLayoutDraft, { id: pageLayoutPersisted.id, name: pageLayoutPersisted.name, type: pageLayoutPersisted.type, objectMetadataId: pageLayoutPersisted.objectMetadataId, tabs: pageLayoutPersisted.tabs, }) : pageLayoutDraft.name.trim().length > 0 || pageLayoutDraft.tabs.length > 0; const canSave = pageLayoutDraft.name?.trim().length > 0; return { pageLayoutDraft, setPageLayoutDraft, isDirty, canSave, }; };