Skip to content

Commit

Permalink
useDocumentRoot returns the documentRootModel
Browse files Browse the repository at this point in the history
  • Loading branch information
lebalz committed Aug 2, 2024
1 parent 3caaf9f commit d6bdc16
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/hooks/useDocumentRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@ import DocumentRoot, { TypeMeta } from '../models/DocumentRoot';
export const useDocumentRoot = <Type extends DocumentType>(id: string | undefined, meta: TypeMeta<Type>) => {
const defaultRootDocId = useId();
const defaultDocId = useId();
const store = rootStore.documentRootStore;
const [dummyDocumentRoot] = React.useState<DocumentRoot<Type>>(
new DocumentRoot(
{ id: id || defaultRootDocId, access: Access.RW },
meta,
rootStore.documentRootStore,
store,
true
)
);

/** initial load */
React.useEffect(() => {
const rootDoc = rootStore.documentRootStore.find(dummyDocumentRoot.id);
const rootDoc = store.find(dummyDocumentRoot.id);
if (
rootDoc ||
rootStore.documentRootStore.apiStateFor(`load-${dummyDocumentRoot.id}`) === ApiState.LOADING
store.apiStateFor(`load-${dummyDocumentRoot.id}`) === ApiState.LOADING
) {
return;
}
if (dummyDocumentRoot.isDummy) {
rootStore.documentRootStore.addDocumentRoot(dummyDocumentRoot);
store.addDocumentRoot(dummyDocumentRoot);
/** add default document when there are no mainDocs */
if (dummyDocumentRoot.mainDocuments.length === 0) {
const now = new Date().toISOString();
Expand All @@ -50,11 +51,11 @@ export const useDocumentRoot = <Type extends DocumentType>(id: string | undefine
/**
* load the documentRoot and it's documents from the api.
*/
rootStore.documentRootStore
store
.load(id, meta)
.then((docRoot) => {
if (!docRoot) {
return rootStore.documentRootStore.create(id, meta, {}).then((docRoot) => {
return store.create(id, meta, {}).then((docRoot) => {
return docRoot;
});
}
Expand All @@ -77,5 +78,5 @@ export const useDocumentRoot = <Type extends DocumentType>(id: string | undefine
});
}, [meta, id]);

return dummyDocumentRoot.id;
return store.find<Type>(dummyDocumentRoot.id);
};
1 change: 0 additions & 1 deletion src/stores/DocumentStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class DocumentStore extends iStore {
return;
}
const model = this.createModel(data);
console.log('add model', data, model);

this.removeFromStore(model.id);
this.documents.push(model);
Expand Down
4 changes: 1 addition & 3 deletions src/theme/CodeEditor/WithScript/ScriptContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export class ScriptMeta extends TypeMeta<DocumentType.Script> {

const ScriptContext = observer((props: InitState & { children: React.ReactNode }) => {
const [meta] = React.useState(new ScriptMeta(props));
const documentRootId = useDocumentRoot(props.id, meta);
const documentRootStore = useStore('documentRootStore');
const documentRoot = documentRootStore.find<DocumentType.Script>(documentRootId);
const documentRoot = useDocumentRoot(props.id, meta);

if (!documentRoot || !documentRoot.firstMainDocument) {
return <div>Load</div>;
Expand Down

0 comments on commit d6bdc16

Please sign in to comment.