-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1976 Document Builder. Lists #2077
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c907212
Rrndering deferred array
BALEHOK 4cb8cea
better editing
BALEHOK 0ed4353
Edit and Preview list
BALEHOK 943688d
Brick serialization test
BALEHOK 4a062e2
Rendering the nested list items
BALEHOK 7e0989c
Code cleanup
BALEHOK c44d76d
Some improvements
BALEHOK 83351a1
Element key input
BALEHOK aa11331
ErrorBoundary around List
BALEHOK a35f7a2
Linting
BALEHOK e42b5e7
Merge branch 'main' into feature/1976-doc-builder-lists
BALEHOK 7e61baf
Linting
BALEHOK c0d3c03
Merge remote-tracking branch 'origin/main' into feature/1976-doc-buil…
twschiller 829777c
#1976: show list error
twschiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright (C) 2021 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React, { useContext } from "react"; | ||
import DocumentContext from "./DocumentContext"; | ||
import { UnknownObject } from "@/types"; | ||
import { Args, mapArgs } from "@/runtime/mapArgs"; | ||
import { useAsyncState } from "@/hooks/common"; | ||
import { GridLoader } from "react-spinners"; | ||
import { BuildDocumentBranch, DocumentElement } from "./documentBuilderTypes"; | ||
import { produce } from "immer"; | ||
import ErrorBoundary from "@/components/ErrorBoundary"; | ||
import { getErrorMessage } from "@/errors"; | ||
|
||
type DocumentListProps = { | ||
array: UnknownObject[]; | ||
elementKey?: string; | ||
config: Args; | ||
buildDocumentBranch: BuildDocumentBranch; | ||
}; | ||
|
||
const DocumentListInternal: React.FC<DocumentListProps> = ({ | ||
array, | ||
elementKey, | ||
config, | ||
buildDocumentBranch, | ||
}) => { | ||
// Should be 'element' for any falsy value including empty string. | ||
elementKey = elementKey || "element"; | ||
|
||
const documentContext = useContext(DocumentContext); | ||
|
||
const [rootDefinitions, isLoading, error] = useAsyncState( | ||
async () => | ||
Promise.all( | ||
array.map(async (itemData) => { | ||
const elementContext = produce(documentContext, (draft) => { | ||
draft.options.ctxt[`@${elementKey}`] = itemData; | ||
}); | ||
|
||
return (mapArgs(config, elementContext.options.ctxt, { | ||
implicitRender: null, | ||
autoescape: true, | ||
}) as Promise<DocumentElement>).then((documentElement) => ({ | ||
documentElement, | ||
elementContext, | ||
})); | ||
}) | ||
), | ||
[array, elementKey, config, documentContext] | ||
); | ||
|
||
if (isLoading) { | ||
return <GridLoader />; | ||
} | ||
|
||
if (error) { | ||
return ( | ||
<details> | ||
<summary className="text-danger">{getErrorMessage(error)}</summary> | ||
|
||
<pre className="mt-2"> | ||
{((error as Error).stack ?? "").replaceAll( | ||
`chrome-extension://${process.env.CHROME_EXTENSION_ID}/`, | ||
"" | ||
)} | ||
</pre> | ||
</details> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{rootDefinitions.map(({ documentElement, elementContext }, i) => { | ||
const { Component, props } = buildDocumentBranch(documentElement); | ||
return ( | ||
<DocumentContext.Provider key={i} value={elementContext}> | ||
<Component {...props} /> | ||
</DocumentContext.Provider> | ||
); | ||
})} | ||
</> | ||
); | ||
}; | ||
|
||
const DocumentList: React.FC<DocumentListProps> = (props) => ( | ||
<ErrorBoundary> | ||
<DocumentListInternal {...props} /> | ||
</ErrorBoundary> | ||
); | ||
|
||
export default DocumentList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow I'm getting an
Cannot read properties of undefined (reading 'map')
error hereI think it happens when there's an error calculating the asyncState?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this to display the error when the factory of useAsyncState fails