@contember/react-client 1.0.0-rc.1 → 1.0.0-rc.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/components/RichTextRenderer/RichTextRenderer.js +1 -1
- package/dist/development/components/RichTextRenderer/RichTextRenderer.js.map +1 -1
- package/dist/development/upload/fileUploadReducer.js +3 -3
- package/dist/development/upload/fileUploadReducer.js.map +1 -1
- package/dist/development/upload/useFileUpload.js +5 -5
- package/dist/development/upload/useFileUpload.js.map +1 -1
- package/dist/production/components/RichTextRenderer/RichTextRenderer.js +1 -1
- package/dist/production/components/RichTextRenderer/RichTextRenderer.js.map +1 -1
- package/dist/production/upload/fileUploadReducer.js +3 -3
- package/dist/production/upload/fileUploadReducer.js.map +1 -1
- package/dist/production/upload/useFileUpload.js +5 -5
- package/dist/production/upload/useFileUpload.js.map +1 -1
- package/dist/types/ClientError.d.ts +3 -0
- package/dist/types/ClientError.d.ts.map +1 -0
- package/dist/types/ContemberClient.d.ts +12 -0
- package/dist/types/ContemberClient.d.ts.map +1 -0
- package/dist/types/apiRequest/ApiRequestAction.d.ts +13 -0
- package/dist/types/apiRequest/ApiRequestAction.d.ts.map +1 -0
- package/dist/types/apiRequest/ApiRequestState.d.ts +21 -0
- package/dist/types/apiRequest/ApiRequestState.d.ts.map +1 -0
- package/dist/types/apiRequest/apiRequestReducer.d.ts +5 -0
- package/dist/types/apiRequest/apiRequestReducer.d.ts.map +1 -0
- package/dist/types/apiRequest/index.d.ts +5 -0
- package/dist/types/apiRequest/index.d.ts.map +1 -0
- package/dist/types/apiRequest/useApiRequest.d.ts +8 -0
- package/dist/types/apiRequest/useApiRequest.d.ts.map +1 -0
- package/dist/types/auth/SessionTokenContext.d.ts +3 -0
- package/dist/types/auth/SessionTokenContext.d.ts.map +1 -0
- package/dist/types/auth/SetSessionTokenContext.d.ts +3 -0
- package/dist/types/auth/SetSessionTokenContext.d.ts.map +1 -0
- package/dist/types/auth/index.d.ts +5 -0
- package/dist/types/auth/index.d.ts.map +1 -0
- package/dist/types/auth/useSessionToken.d.ts +2 -0
- package/dist/types/auth/useSessionToken.d.ts.map +1 -0
- package/dist/types/auth/useSetSessionToken.d.ts +2 -0
- package/dist/types/auth/useSetSessionToken.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/BuiltinElements.d.ts +49 -0
- package/dist/types/components/RichTextRenderer/BuiltinElements.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/BuiltinLeaves.d.ts +21 -0
- package/dist/types/components/RichTextRenderer/BuiltinLeaves.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/ElementRenderer.d.ts +22 -0
- package/dist/types/components/RichTextRenderer/ElementRenderer.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/LeafRenderer.d.ts +14 -0
- package/dist/types/components/RichTextRenderer/LeafRenderer.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/NormalizedRichTextBlock.d.ts +14 -0
- package/dist/types/components/RichTextRenderer/NormalizedRichTextBlock.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/ReferenceRenderer.d.ts +14 -0
- package/dist/types/components/RichTextRenderer/ReferenceRenderer.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RenderElementFallback.d.ts +12 -0
- package/dist/types/components/RichTextRenderer/RenderElementFallback.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RenderLeafFallback.d.ts +7 -0
- package/dist/types/components/RichTextRenderer/RenderLeafFallback.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextBlock.d.ts +2 -0
- package/dist/types/components/RichTextRenderer/RichTextBlock.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextElement.d.ts +7 -0
- package/dist/types/components/RichTextRenderer/RichTextElement.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextElementMetadata.d.ts +11 -0
- package/dist/types/components/RichTextRenderer/RichTextElementMetadata.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextLeaf.d.ts +4 -0
- package/dist/types/components/RichTextRenderer/RichTextLeaf.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextLeafMetadata.d.ts +4 -0
- package/dist/types/components/RichTextRenderer/RichTextLeafMetadata.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextReference.d.ts +4 -0
- package/dist/types/components/RichTextRenderer/RichTextReference.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextRenderMetadata.d.ts +12 -0
- package/dist/types/components/RichTextRenderer/RichTextRenderMetadata.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextRenderMetadataContext.d.ts +8 -0
- package/dist/types/components/RichTextRenderer/RichTextRenderMetadataContext.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextRenderer.d.ts +29 -0
- package/dist/types/components/RichTextRenderer/RichTextRenderer.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RichTextRendererError.d.ts +3 -0
- package/dist/types/components/RichTextRenderer/RichTextRendererError.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/RootEditorNode.d.ts +9 -0
- package/dist/types/components/RichTextRenderer/RootEditorNode.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/defaultDeserialize.d.ts +5 -0
- package/dist/types/components/RichTextRenderer/defaultDeserialize.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/index.d.ts +16 -0
- package/dist/types/components/RichTextRenderer/index.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/renderChildren.d.ts +14 -0
- package/dist/types/components/RichTextRenderer/renderChildren.d.ts.map +1 -0
- package/dist/types/components/RichTextRenderer/resolveRichTextElementMetadata.d.ts +8 -0
- package/dist/types/components/RichTextRenderer/resolveRichTextElementMetadata.d.ts.map +1 -0
- package/dist/types/components/index.d.ts +2 -0
- package/dist/types/components/index.d.ts.map +1 -0
- package/dist/types/config/ApiBaseUrlContext.d.ts +3 -0
- package/dist/types/config/ApiBaseUrlContext.d.ts.map +1 -0
- package/dist/types/config/LoginTokenContext.d.ts +3 -0
- package/dist/types/config/LoginTokenContext.d.ts.map +1 -0
- package/dist/types/config/index.d.ts +5 -0
- package/dist/types/config/index.d.ts.map +1 -0
- package/dist/types/config/useApiBaseUrl.d.ts +2 -0
- package/dist/types/config/useApiBaseUrl.d.ts.map +1 -0
- package/dist/types/config/useLoginToken.d.ts +2 -0
- package/dist/types/config/useLoginToken.d.ts.map +1 -0
- package/dist/types/content/index.d.ts +4 -0
- package/dist/types/content/index.d.ts.map +1 -0
- package/dist/types/content/useContentApiRequest.d.ts +3 -0
- package/dist/types/content/useContentApiRequest.d.ts.map +1 -0
- package/dist/types/content/useContentGraphQlClient.d.ts +3 -0
- package/dist/types/content/useContentGraphQlClient.d.ts.map +1 -0
- package/dist/types/content/useCurrentContentGraphQlClient.d.ts +3 -0
- package/dist/types/content/useCurrentContentGraphQlClient.d.ts.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/project/ProjectSlugContext.d.ts +3 -0
- package/dist/types/project/ProjectSlugContext.d.ts.map +1 -0
- package/dist/types/project/StageSlugContext.d.ts +3 -0
- package/dist/types/project/StageSlugContext.d.ts.map +1 -0
- package/dist/types/project/index.d.ts +5 -0
- package/dist/types/project/index.d.ts.map +1 -0
- package/dist/types/project/useProjectSlug.d.ts +2 -0
- package/dist/types/project/useProjectSlug.d.ts.map +1 -0
- package/dist/types/project/useStageSlug.d.ts +2 -0
- package/dist/types/project/useStageSlug.d.ts.map +1 -0
- package/dist/types/system/index.d.ts +4 -0
- package/dist/types/system/index.d.ts.map +1 -0
- package/dist/types/system/useCurrentSystemGraphQlClient.d.ts +3 -0
- package/dist/types/system/useCurrentSystemGraphQlClient.d.ts.map +1 -0
- package/dist/types/system/useSystemApiRequest.d.ts +3 -0
- package/dist/types/system/useSystemApiRequest.d.ts.map +1 -0
- package/dist/types/system/useSystemGraphQlClient.d.ts +2 -0
- package/dist/types/system/useSystemGraphQlClient.d.ts.map +1 -0
- package/dist/types/tenant/index.d.ts +3 -0
- package/dist/types/tenant/index.d.ts.map +1 -0
- package/dist/types/tenant/useTenantApiRequest.d.ts +3 -0
- package/dist/types/tenant/useTenantApiRequest.d.ts.map +1 -0
- package/dist/types/tenant/useTenantGraphQlClient.d.ts +2 -0
- package/dist/types/tenant/useTenantGraphQlClient.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/dist/types/upload/FileId.d.ts +2 -0
- package/dist/types/upload/FileId.d.ts.map +1 -0
- package/dist/types/upload/FileUploadAction.d.ts +26 -0
- package/dist/types/upload/FileUploadAction.d.ts.map +1 -0
- package/dist/types/upload/FileUploadCompoundState.d.ts +4 -0
- package/dist/types/upload/FileUploadCompoundState.d.ts.map +1 -0
- package/dist/types/upload/FileUploadMetadata.d.ts +6 -0
- package/dist/types/upload/FileUploadMetadata.d.ts.map +1 -0
- package/dist/types/upload/FileUploadMultiTemporalState.d.ts +10 -0
- package/dist/types/upload/FileUploadMultiTemporalState.d.ts.map +1 -0
- package/dist/types/upload/FileUploadOperations.d.ts +18 -0
- package/dist/types/upload/FileUploadOperations.d.ts.map +1 -0
- package/dist/types/upload/FileWithMetadata.d.ts +8 -0
- package/dist/types/upload/FileWithMetadata.d.ts.map +1 -0
- package/dist/types/upload/SingleFileUploadState.d.ts +31 -0
- package/dist/types/upload/SingleFileUploadState.d.ts.map +1 -0
- package/dist/types/upload/fileUploadReducer.d.ts +5 -0
- package/dist/types/upload/fileUploadReducer.d.ts.map +1 -0
- package/dist/types/upload/index.d.ts +8 -0
- package/dist/types/upload/index.d.ts.map +1 -0
- package/dist/types/upload/toFileId.d.ts +4 -0
- package/dist/types/upload/toFileId.d.ts.map +1 -0
- package/dist/types/upload/useFileUpload.d.ts +11 -0
- package/dist/types/upload/useFileUpload.d.ts.map +1 -0
- package/dist/types/useGraphQlClient.d.ts +3 -0
- package/dist/types/useGraphQlClient.d.ts.map +1 -0
- package/dist/types/utils/assertNever.d.ts +2 -0
- package/dist/types/utils/assertNever.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/package.json +6 -7
- package/src/ClientError.ts +1 -0
- package/src/ContemberClient.tsx +48 -0
- package/src/ambient.d.ts +1 -0
- package/src/apiRequest/ApiRequestAction.ts +17 -0
- package/src/apiRequest/ApiRequestState.ts +25 -0
- package/src/apiRequest/apiRequestReducer.ts +41 -0
- package/src/apiRequest/index.ts +4 -0
- package/src/apiRequest/useApiRequest.ts +73 -0
- package/src/auth/SessionTokenContext.ts +3 -0
- package/src/auth/SetSessionTokenContext.ts +5 -0
- package/src/auth/index.ts +4 -0
- package/src/auth/useSessionToken.ts +4 -0
- package/src/auth/useSetSessionToken.ts +4 -0
- package/src/components/RichTextRenderer/BuiltinElements.ts +112 -0
- package/src/components/RichTextRenderer/BuiltinLeaves.ts +32 -0
- package/src/components/RichTextRenderer/ElementRenderer.tsx +71 -0
- package/src/components/RichTextRenderer/LeafRenderer.tsx +39 -0
- package/src/components/RichTextRenderer/NormalizedRichTextBlock.ts +17 -0
- package/src/components/RichTextRenderer/ReferenceRenderer.ts +26 -0
- package/src/components/RichTextRenderer/RenderElementFallback.tsx +147 -0
- package/src/components/RichTextRenderer/RenderLeafFallback.tsx +43 -0
- package/src/components/RichTextRenderer/RichTextBlock.ts +1 -0
- package/src/components/RichTextRenderer/RichTextElement.ts +10 -0
- package/src/components/RichTextRenderer/RichTextElementMetadata.ts +15 -0
- package/src/components/RichTextRenderer/RichTextLeaf.ts +3 -0
- package/src/components/RichTextRenderer/RichTextLeafMetadata.ts +3 -0
- package/src/components/RichTextRenderer/RichTextReference.ts +3 -0
- package/src/components/RichTextRenderer/RichTextRenderMetadata.ts +16 -0
- package/src/components/RichTextRenderer/RichTextRenderMetadataContext.ts +20 -0
- package/src/components/RichTextRenderer/RichTextRenderer.tsx +165 -0
- package/src/components/RichTextRenderer/RichTextRendererError.ts +1 -0
- package/src/components/RichTextRenderer/RootEditorNode.ts +12 -0
- package/src/components/RichTextRenderer/defaultDeserialize.ts +19 -0
- package/src/components/RichTextRenderer/index.ts +15 -0
- package/src/components/RichTextRenderer/renderChildren.tsx +50 -0
- package/src/components/RichTextRenderer/resolveRichTextElementMetadata.ts +50 -0
- package/src/components/index.ts +1 -0
- package/src/config/ApiBaseUrlContext.ts +3 -0
- package/src/config/LoginTokenContext.ts +3 -0
- package/src/config/index.ts +4 -0
- package/src/config/useApiBaseUrl.ts +4 -0
- package/src/config/useLoginToken.ts +4 -0
- package/src/content/index.ts +3 -0
- package/src/content/useContentApiRequest.ts +6 -0
- package/src/content/useContentGraphQlClient.ts +6 -0
- package/src/content/useCurrentContentGraphQlClient.ts +17 -0
- package/src/index.ts +15 -0
- package/src/project/ProjectSlugContext.ts +3 -0
- package/src/project/StageSlugContext.ts +3 -0
- package/src/project/index.ts +4 -0
- package/src/project/useProjectSlug.ts +4 -0
- package/src/project/useStageSlug.ts +4 -0
- package/src/system/index.ts +3 -0
- package/src/system/useCurrentSystemGraphQlClient.ts +13 -0
- package/src/system/useSystemApiRequest.ts +6 -0
- package/src/system/useSystemGraphQlClient.ts +4 -0
- package/src/tenant/index.ts +2 -0
- package/src/tenant/useTenantApiRequest.ts +6 -0
- package/src/tenant/useTenantGraphQlClient.ts +4 -0
- package/src/tsconfig.json +9 -0
- package/src/upload/FileId.ts +1 -0
- package/src/upload/FileUploadAction.ts +33 -0
- package/src/upload/FileUploadCompoundState.ts +7 -0
- package/src/upload/FileUploadMetadata.ts +6 -0
- package/src/upload/FileUploadMultiTemporalState.ts +10 -0
- package/src/upload/FileUploadOperations.ts +22 -0
- package/src/upload/FileWithMetadata.ts +8 -0
- package/src/upload/SingleFileUploadState.ts +35 -0
- package/src/upload/fileUploadReducer.ts +185 -0
- package/src/upload/index.ts +15 -0
- package/src/upload/toFileId.ts +12 -0
- package/src/upload/useFileUpload.ts +262 -0
- package/src/useGraphQlClient.ts +10 -0
- package/src/utils/assertNever.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/dist/react-client.d.ts +0 -367
|
@@ -49,7 +49,7 @@ const RichTextRenderer = memo(function RichTextRenderer2({
|
|
|
49
49
|
if (typeof Object(references)[Symbol.iterator] !== "function") {
|
|
50
50
|
throw new RichTextRendererError(`The set of references must be an iterable!`);
|
|
51
51
|
}
|
|
52
|
-
normalizedReferences = new Map();
|
|
52
|
+
normalizedReferences = /* @__PURE__ */ new Map();
|
|
53
53
|
for (const reference of references) {
|
|
54
54
|
if (!("id" in reference)) {
|
|
55
55
|
throw new RichTextRendererError(`Found a reference without an id field.`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextRenderer.js","sources":["../../../../src/components/RichTextRenderer/RichTextRenderer.tsx"],"sourcesContent":["import { ComponentType, createElement, memo, ReactElement, ReactNode, useMemo } from 'react'\nimport { defaultDeserialize } from './defaultDeserialize'\nimport type { RenderElement } from './ElementRenderer'\nimport type { RenderLeaf } from './LeafRenderer'\nimport type { NormalizedRichTextBlock } from './NormalizedRichTextBlock'\nimport type { ReferenceRenderer } from './ReferenceRenderer'\nimport { renderChildren } from './renderChildren'\nimport type { RichTextBlock } from './RichTextBlock'\nimport type { RichTextElement } from './RichTextElement'\nimport type { RichTextLeaf } from './RichTextLeaf'\nimport type { RichTextReference } from './RichTextReference'\nimport { RichTextRendererError } from './RichTextRendererError'\nimport { RichTextRenderMetadataContext } from './RichTextRenderMetadataContext'\nimport type { RootEditorNode } from './RootEditorNode'\n\nexport interface RichTextRendererFieldProps {\n\tsource: string | null\n}\n\nexport interface RichTextRendererBlockProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> {\n\tblocks: RichTextBlock[]\n\treferenceRenderers?: Record<string, ReferenceRenderer<any, CustomElements, CustomLeaves>>\n\tsourceField?: string\n\treferencesField?: string\n\treferenceDiscriminationField?: string\n}\n\nexport type RichTextRendererProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> = {\n\tdeserialize?: (source: string) => RootEditorNode<CustomElements, CustomLeaves>\n\trenderElement?: RenderElement<CustomElements, CustomLeaves>\n\trenderLeaf?: RenderLeaf<CustomLeaves>\n\tattributeNamePrefix?: string\n\trenderBlock?: ComponentType<{ block: unknown }>\n} & (RichTextRendererFieldProps | RichTextRendererBlockProps<CustomElements, CustomLeaves>)\n\nexport const RichTextRenderer = memo(function RichTextRenderer<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n>({\n\tdeserialize = defaultDeserialize,\n\trenderElement,\n\trenderLeaf,\n\trenderBlock = ({ children }) => <>{children}</>,\n\tattributeNamePrefix,\n\t...sourceProps\n}: RichTextRendererProps<CustomElements, CustomLeaves>) {\n\tconst normalizedSource = useMemo<NormalizedRichTextBlock<CustomElements, CustomLeaves>[]>(() => {\n\t\tif ('source' in sourceProps && !('blocks' in sourceProps)) {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tcontent: sourceProps.source\n\t\t\t\t\t\t? deserialize(sourceProps.source)\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tformatVersion: 0,\n\t\t\t\t\t\t\t\tchildren: [],\n\t\t\t\t\t\t },\n\t\t\t\t\tid: undefined,\n\t\t\t\t\treferencesField: undefined,\n\t\t\t\t\treferenceDiscriminationField: undefined,\n\t\t\t\t\treferenceRenderers: {},\n\t\t\t\t\treferences: undefined,\n\t\t\t\t},\n\t\t\t]\n\t\t}\n\t\tif ('blocks' in sourceProps) {\n\t\t\tconst sourceField = sourceProps.sourceField ?? 'source'\n\t\t\tconst referencesField = sourceProps.referencesField ?? 'references'\n\t\t\tconst referenceDiscriminationField = sourceProps.referenceDiscriminationField ?? 'type'\n\n\t\t\treturn sourceProps.blocks.map(block => {\n\t\t\t\tif (!(sourceField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`Found a block without a '${sourceField}' field. ` +\n\t\t\t\t\t\t\t(sourceProps.sourceField === undefined\n\t\t\t\t\t\t\t\t? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.`\n\t\t\t\t\t\t\t\t: `That is what the 'sourceField' prop has been set to, and so either this is a typo, ` +\n\t\t\t\t\t\t\t\t `or the data supplied is invalid.`),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst source = block[sourceField]\n\t\t\t\tif (typeof source !== 'string') {\n\t\t\t\t\tthrow new RichTextRendererError(`Found a block whose source is not a string.`)\n\t\t\t\t}\n\n\t\t\t\tif (sourceProps.referencesField !== undefined && !(referencesField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`The 'referencesField' prop is set to '${referencesField}' but a block without such field ` +\n\t\t\t\t\t\t\t`has been encountered. Unless this is just a typo, ` +\n\t\t\t\t\t\t\t`if you do not wish to use references, avoid supplying the 'referencesField' prop.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst references = block[referencesField]\n\n\t\t\t\tlet normalizedReferences: Map<string, RichTextReference> | undefined = undefined\n\t\t\t\tif (references) {\n\t\t\t\t\tif (typeof Object(references)[Symbol.iterator] !== 'function') {\n\t\t\t\t\t\tthrow new RichTextRendererError(`The set of references must be an iterable!`)\n\t\t\t\t\t}\n\n\t\t\t\t\tnormalizedReferences = new Map()\n\t\t\t\t\tfor (const reference of references as Iterable<RichTextReference>) {\n\t\t\t\t\t\tif (!('id' in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(`Found a reference without an id field.`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!(referenceDiscriminationField in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t\t\t`Found a reference without a '${referenceDiscriminationField}' field. ` +\n\t\t\t\t\t\t\t\t\t(sourceProps.referenceDiscriminationField === undefined\n\t\t\t\t\t\t\t\t\t\t? `The 'referenceDiscriminationField' prop has not been supplied, ` +\n\t\t\t\t\t\t\t\t\t\t `and so '${referenceDiscriminationField}' was used as a default.`\n\t\t\t\t\t\t\t\t\t\t: `That is what the 'referenceDiscriminationField' prop has been set to, ` +\n\t\t\t\t\t\t\t\t\t\t `and so either this is a typo, or the data supplied is invalid.`),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnormalizedReferences.set(reference.id, reference)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tid: typeof block.id === 'string' ? block.id : undefined,\n\t\t\t\t\tcontent: deserialize(source),\n\t\t\t\t\treferenceDiscriminationField,\n\t\t\t\t\treferenceRenderers: sourceProps.referenceRenderers ?? {},\n\t\t\t\t\treferencesField,\n\t\t\t\t\treferences: normalizedReferences,\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t\tthrow new RichTextRendererError(`RichTextRenderer: invalid data supplied.`)\n\t}, [deserialize, sourceProps])\n\n\treturn (\n\t\t<>\n\t\t\t{normalizedSource.map((block, i) => (\n\t\t\t\t<RichTextRenderMetadataContext.Provider\n\t\t\t\t\tkey={block.id ?? i}\n\t\t\t\t\tvalue={{\n\t\t\t\t\t\tformatVersion: block.content.formatVersion,\n\t\t\t\t\t\treferenceRenderers: block.referenceRenderers,\n\t\t\t\t\t\treferencesField: block.referencesField,\n\t\t\t\t\t\treferenceDiscriminationField: block.referenceDiscriminationField,\n\t\t\t\t\t\treferences: block.references,\n\t\t\t\t\t}}\n\t\t\t\t>\n\t\t\t\t\t{createElement(\n\t\t\t\t\t\trenderBlock, {\n\t\t\t\t\t\t\tblock,\n\t\t\t\t\t\t}, renderChildren(block.content.children, {\n\t\t\t\t\t\t\trenderLeaf,\n\t\t\t\t\t\t\trenderElement,\n\t\t\t\t\t\t\tattributeNamePrefix,\n\t\t\t\t\t\t}),\n\t\t\t\t\t)}\n\t\t\t\t</RichTextRenderMetadataContext.Provider>\n\t\t\t))}\n\t\t</>\n\t)\n}) as <CustomElements extends RichTextElement = never, CustomLeaves extends RichTextLeaf = never>(\n\tprops: RichTextRendererProps<CustomElements, CustomLeaves>,\n) => ReactElement\n"],"names":[],"mappings":";;;;;;MAyCa,mBAAmB,KAAK,2BAGnC;AAAA,EACD,cAAc;AAAA,EACd;AAAA,EACA;AAAA,EACA,cAAc,CAAC,EAAE,6DAAkB;AAAA,EACnC;AAAA,KACG;AAAA,GACoD;AACvD,QAAM,mBAAmB,QAAiE,MAAM;AAC/F,QAAI,YAAY,eAAe,cAAc,cAAc;AAC1D,aAAO;AAAA,QACN;AAAA,UACC,SAAS,YAAY,SAClB,YAAY,YAAY,UACxB;AAAA,YACA,eAAe;AAAA,YACf,UAAU;AAAA;AAAA,UAEb,IAAI;AAAA,UACJ,iBAAiB;AAAA,UACjB,8BAA8B;AAAA,UAC9B,oBAAoB;AAAA,UACpB,YAAY;AAAA;AAAA;AAAA;AAIf,QAAI,YAAY,aAAa;AAC5B,YAAM,cAAc,YAAY,eAAe;AAC/C,YAAM,kBAAkB,YAAY,mBAAmB;AACvD,YAAM,+BAA+B,YAAY,gCAAgC;AAEjF,aAAO,YAAY,OAAO,IAAI,WAAS;AACtC,YAAI,iBAAiB,QAAQ;AAC5B,gBAAM,IAAI,sBACT,4BAA4B,sCACd,gBAAgB,SAC1B,yDAAyD,wCACzD;AAAA;AAIN,cAAM,SAAS,MAAM;AACrB,YAAI,OAAO,WAAW,UAAU;AAC/B,gBAAM,IAAI,sBAAsB;AAAA;AAGjC,YAAI,YAAY,oBAAoB,UAAa,qBAAqB,QAAQ;AAC7E,gBAAM,IAAI,sBACT,yCAAyC;AAAA;AAK3C,cAAM,aAAa,MAAM;AAEzB,YAAI,uBAAmE;AACvE,YAAI,YAAY;AACf,cAAI,OAAO,OAAO,YAAY,OAAO,cAAc,YAAY;AAC9D,kBAAM,IAAI,sBAAsB;AAAA;AAGjC,
|
|
1
|
+
{"version":3,"file":"RichTextRenderer.js","sources":["../../../../src/components/RichTextRenderer/RichTextRenderer.tsx"],"sourcesContent":["import { ComponentType, createElement, memo, ReactElement, ReactNode, useMemo } from 'react'\nimport { defaultDeserialize } from './defaultDeserialize'\nimport type { RenderElement } from './ElementRenderer'\nimport type { RenderLeaf } from './LeafRenderer'\nimport type { NormalizedRichTextBlock } from './NormalizedRichTextBlock'\nimport type { ReferenceRenderer } from './ReferenceRenderer'\nimport { renderChildren } from './renderChildren'\nimport type { RichTextBlock } from './RichTextBlock'\nimport type { RichTextElement } from './RichTextElement'\nimport type { RichTextLeaf } from './RichTextLeaf'\nimport type { RichTextReference } from './RichTextReference'\nimport { RichTextRendererError } from './RichTextRendererError'\nimport { RichTextRenderMetadataContext } from './RichTextRenderMetadataContext'\nimport type { RootEditorNode } from './RootEditorNode'\n\nexport interface RichTextRendererFieldProps {\n\tsource: string | null\n}\n\nexport interface RichTextRendererBlockProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> {\n\tblocks: RichTextBlock[]\n\treferenceRenderers?: Record<string, ReferenceRenderer<any, CustomElements, CustomLeaves>>\n\tsourceField?: string\n\treferencesField?: string\n\treferenceDiscriminationField?: string\n}\n\nexport type RichTextRendererProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> = {\n\tdeserialize?: (source: string) => RootEditorNode<CustomElements, CustomLeaves>\n\trenderElement?: RenderElement<CustomElements, CustomLeaves>\n\trenderLeaf?: RenderLeaf<CustomLeaves>\n\tattributeNamePrefix?: string\n\trenderBlock?: ComponentType<{ block: unknown }>\n} & (RichTextRendererFieldProps | RichTextRendererBlockProps<CustomElements, CustomLeaves>)\n\nexport const RichTextRenderer = memo(function RichTextRenderer<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n>({\n\tdeserialize = defaultDeserialize,\n\trenderElement,\n\trenderLeaf,\n\trenderBlock = ({ children }) => <>{children}</>,\n\tattributeNamePrefix,\n\t...sourceProps\n}: RichTextRendererProps<CustomElements, CustomLeaves>) {\n\tconst normalizedSource = useMemo<NormalizedRichTextBlock<CustomElements, CustomLeaves>[]>(() => {\n\t\tif ('source' in sourceProps && !('blocks' in sourceProps)) {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tcontent: sourceProps.source\n\t\t\t\t\t\t? deserialize(sourceProps.source)\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tformatVersion: 0,\n\t\t\t\t\t\t\t\tchildren: [],\n\t\t\t\t\t\t },\n\t\t\t\t\tid: undefined,\n\t\t\t\t\treferencesField: undefined,\n\t\t\t\t\treferenceDiscriminationField: undefined,\n\t\t\t\t\treferenceRenderers: {},\n\t\t\t\t\treferences: undefined,\n\t\t\t\t},\n\t\t\t]\n\t\t}\n\t\tif ('blocks' in sourceProps) {\n\t\t\tconst sourceField = sourceProps.sourceField ?? 'source'\n\t\t\tconst referencesField = sourceProps.referencesField ?? 'references'\n\t\t\tconst referenceDiscriminationField = sourceProps.referenceDiscriminationField ?? 'type'\n\n\t\t\treturn sourceProps.blocks.map(block => {\n\t\t\t\tif (!(sourceField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`Found a block without a '${sourceField}' field. ` +\n\t\t\t\t\t\t\t(sourceProps.sourceField === undefined\n\t\t\t\t\t\t\t\t? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.`\n\t\t\t\t\t\t\t\t: `That is what the 'sourceField' prop has been set to, and so either this is a typo, ` +\n\t\t\t\t\t\t\t\t `or the data supplied is invalid.`),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst source = block[sourceField]\n\t\t\t\tif (typeof source !== 'string') {\n\t\t\t\t\tthrow new RichTextRendererError(`Found a block whose source is not a string.`)\n\t\t\t\t}\n\n\t\t\t\tif (sourceProps.referencesField !== undefined && !(referencesField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`The 'referencesField' prop is set to '${referencesField}' but a block without such field ` +\n\t\t\t\t\t\t\t`has been encountered. Unless this is just a typo, ` +\n\t\t\t\t\t\t\t`if you do not wish to use references, avoid supplying the 'referencesField' prop.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst references = block[referencesField]\n\n\t\t\t\tlet normalizedReferences: Map<string, RichTextReference> | undefined = undefined\n\t\t\t\tif (references) {\n\t\t\t\t\tif (typeof Object(references)[Symbol.iterator] !== 'function') {\n\t\t\t\t\t\tthrow new RichTextRendererError(`The set of references must be an iterable!`)\n\t\t\t\t\t}\n\n\t\t\t\t\tnormalizedReferences = new Map()\n\t\t\t\t\tfor (const reference of references as Iterable<RichTextReference>) {\n\t\t\t\t\t\tif (!('id' in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(`Found a reference without an id field.`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!(referenceDiscriminationField in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t\t\t`Found a reference without a '${referenceDiscriminationField}' field. ` +\n\t\t\t\t\t\t\t\t\t(sourceProps.referenceDiscriminationField === undefined\n\t\t\t\t\t\t\t\t\t\t? `The 'referenceDiscriminationField' prop has not been supplied, ` +\n\t\t\t\t\t\t\t\t\t\t `and so '${referenceDiscriminationField}' was used as a default.`\n\t\t\t\t\t\t\t\t\t\t: `That is what the 'referenceDiscriminationField' prop has been set to, ` +\n\t\t\t\t\t\t\t\t\t\t `and so either this is a typo, or the data supplied is invalid.`),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnormalizedReferences.set(reference.id, reference)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tid: typeof block.id === 'string' ? block.id : undefined,\n\t\t\t\t\tcontent: deserialize(source),\n\t\t\t\t\treferenceDiscriminationField,\n\t\t\t\t\treferenceRenderers: sourceProps.referenceRenderers ?? {},\n\t\t\t\t\treferencesField,\n\t\t\t\t\treferences: normalizedReferences,\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t\tthrow new RichTextRendererError(`RichTextRenderer: invalid data supplied.`)\n\t}, [deserialize, sourceProps])\n\n\treturn (\n\t\t<>\n\t\t\t{normalizedSource.map((block, i) => (\n\t\t\t\t<RichTextRenderMetadataContext.Provider\n\t\t\t\t\tkey={block.id ?? i}\n\t\t\t\t\tvalue={{\n\t\t\t\t\t\tformatVersion: block.content.formatVersion,\n\t\t\t\t\t\treferenceRenderers: block.referenceRenderers,\n\t\t\t\t\t\treferencesField: block.referencesField,\n\t\t\t\t\t\treferenceDiscriminationField: block.referenceDiscriminationField,\n\t\t\t\t\t\treferences: block.references,\n\t\t\t\t\t}}\n\t\t\t\t>\n\t\t\t\t\t{createElement(\n\t\t\t\t\t\trenderBlock, {\n\t\t\t\t\t\t\tblock,\n\t\t\t\t\t\t}, renderChildren(block.content.children, {\n\t\t\t\t\t\t\trenderLeaf,\n\t\t\t\t\t\t\trenderElement,\n\t\t\t\t\t\t\tattributeNamePrefix,\n\t\t\t\t\t\t}),\n\t\t\t\t\t)}\n\t\t\t\t</RichTextRenderMetadataContext.Provider>\n\t\t\t))}\n\t\t</>\n\t)\n}) as <CustomElements extends RichTextElement = never, CustomLeaves extends RichTextLeaf = never>(\n\tprops: RichTextRendererProps<CustomElements, CustomLeaves>,\n) => ReactElement\n"],"names":[],"mappings":";;;;;;MAyCa,mBAAmB,KAAK,2BAGnC;AAAA,EACD,cAAc;AAAA,EACd;AAAA,EACA;AAAA,EACA,cAAc,CAAC,EAAE,6DAAkB;AAAA,EACnC;AAAA,KACG;AAAA,GACoD;AACvD,QAAM,mBAAmB,QAAiE,MAAM;AAC/F,QAAI,YAAY,eAAe,cAAc,cAAc;AAC1D,aAAO;AAAA,QACN;AAAA,UACC,SAAS,YAAY,SAClB,YAAY,YAAY,UACxB;AAAA,YACA,eAAe;AAAA,YACf,UAAU;AAAA;AAAA,UAEb,IAAI;AAAA,UACJ,iBAAiB;AAAA,UACjB,8BAA8B;AAAA,UAC9B,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OAAO;AAAA,MACN,eAAe,MAAM,QAAQ;AAAA,MAC7B,oBAAoB,MAAM;AAAA,MAC1B,iBAAiB,MAAM;AAAA,MACvB,8BAA8B,MAAM;AAAA,MACpC,YAAY,MAAM;AAAA;AAAA,KAGlB,cACA,aAAa;AAAA,IACZ;AAAA,KACE,eAAe,MAAM,QAAQ,UAAU;AAAA,IACzC;AAAA,IACA;AAAA,IACA;AAAA;AAAA;;;;"}
|
|
@@ -4,9 +4,9 @@ import { assertNever } from '../utils/assertNever.js';
|
|
|
4
4
|
|
|
5
5
|
const initializeFileUploadState = () => ({
|
|
6
6
|
lastUpdateTime: 0,
|
|
7
|
-
fileIdByFile: new WeakMap(),
|
|
8
|
-
liveState: new Map(),
|
|
9
|
-
publicState: new Map(),
|
|
7
|
+
fileIdByFile: /* @__PURE__ */ new WeakMap(),
|
|
8
|
+
liveState: /* @__PURE__ */ new Map(),
|
|
9
|
+
publicState: /* @__PURE__ */ new Map(),
|
|
10
10
|
isLiveStateDirty: false
|
|
11
11
|
});
|
|
12
12
|
const fileUploadReducer = (previousState, action) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fileUploadReducer.js","sources":["../../../src/upload/fileUploadReducer.ts"],"sourcesContent":["import { FileUploadError } from '@contember/client'\nimport { assertNever } from '../utils'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport { toFileId } from './toFileId'\n\nexport const initializeFileUploadState = <Result = unknown, Metadata = undefined>(): FileUploadMultiTemporalState<\n\tResult,\n\tMetadata\n> => ({\n\tlastUpdateTime: 0,\n\tfileIdByFile: new WeakMap<File, FileId>(),\n\tliveState: new Map(),\n\tpublicState: new Map(),\n\tisLiveStateDirty: false,\n})\n\nexport const fileUploadReducer = <Result = unknown, Metadata = undefined>(\n\tpreviousState: FileUploadMultiTemporalState<Result, Metadata>,\n\taction: FileUploadAction<Result, Metadata>,\n): FileUploadMultiTemporalState<Result, Metadata> => {\n\tconst publishNewState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tpublicState: previousState.liveState,\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tlastUpdateTime: previousState.lastUpdateTime,\n\t\tisLiveStateDirty: false,\n\n\t\t// Immediately make the live state referentially unequal so that this doesn't have to happen during each action\n\t\t// several times between two publishes.\n\t\tliveState: new Map(previousState.liveState),\n\t})\n\tconst getNewDirtyState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tliveState: previousState.liveState,\n\t\tpublicState: previousState.publicState,\n\t\tisLiveStateDirty: true,\n\t\tlastUpdateTime: Date.now(),\n\t})\n\tswitch (action.type) {\n\t\tcase 'publishNewestState': {\n\t\t\treturn publishNewState()\n\t\t}\n\t\tcase 'initialize': {\n\t\t\tfor (const [fileId, metadata] of action.files) {\n\t\t\t\t// Deliberately allowing starting a new upload with the same id\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'initializing',\n\t\t\t\t\tabortController: metadata.abortController,\n\t\t\t\t\tpreviewUrl: metadata.previewUrl,\n\t\t\t\t\tfile: metadata.file,\n\t\t\t\t})\n\t\t\t\tpreviousState.fileIdByFile.set(metadata.file, fileId)\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about new files immediate*/\n\t\t}\n\t\tcase 'startUploading': {\n\t\t\tfor (const [fileOrId, metadata] of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tabortController: fileState.abortController,\n\t\t\t\t\tfile: fileState.file,\n\t\t\t\t\tmetadata: metadata.metadata!, // If the user didn't supply the metadata, then that's on them.\n\t\t\t\t\tpreviewUrl: fileState.previewUrl,\n\t\t\t\t\tprogress: undefined,\n\t\t\t\t\tuploader: metadata.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about started upload immediate*/\n\t\t}\n\t\tcase 'finishSuccessfully': {\n\t\t\tfor (const [fileOrId, result] of action.result) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'success',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tresult,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\t// return publishNewState() // Making the feedback about successful upload immediate.\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t\tcase 'finishWithError': {\n\t\t\tfor (const errorSpec of action.error) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet rawError: unknown\n\n\t\t\t\tif (Array.isArray(errorSpec)) {\n\t\t\t\t\t[fileOrId, rawError] = errorSpec\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = errorSpec\n\t\t\t\t\trawError = undefined\n\t\t\t\t}\n\n\t\t\t\tconst errors: FileUploadError[] = []\n\n\t\t\t\tif (rawError instanceof FileUploadError) {\n\t\t\t\t\terrors.push(rawError)\n\t\t\t\t} else if (Array.isArray(rawError)) {\n\t\t\t\t\tfor (const error of rawError) {\n\t\t\t\t\t\tif (error instanceof FileUploadError) {\n\t\t\t\t\t\t\terrors.push(error)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (\n\t\t\t\t\tpreviousFileState === undefined ||\n\t\t\t\t\t(previousFileState.readyState !== 'initializing' && previousFileState.readyState !== 'uploading')\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst uploader = previousFileState.readyState === 'uploading' ? previousFileState.uploader : undefined\n\t\t\t\tconst metadata = previousFileState.readyState === 'uploading' ? previousFileState.metadata : undefined\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'error',\n\t\t\t\t\terrors: errors.length ? errors : undefined,\n\t\t\t\t\trawError,\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tuploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState() // Bad news can wait.\n\t\t}\n\t\tcase 'purge': {\n\t\t\tlet atLeastOnePurged = false\n\t\t\tfor (const fileOrId of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tatLeastOnePurged = true\n\t\t\t\tif (fileState.readyState === 'initializing' || fileState.readyState === 'uploading') {\n\t\t\t\t\tfileState.abortController.abort() // This is a bit naughty… We shouldn't do this from here.\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.delete(fileId)\n\t\t\t}\n\t\t\tif (atLeastOnePurged) {\n\t\t\t\treturn getNewDirtyState()\n\t\t\t}\n\t\t\treturn previousState\n\t\t}\n\t\tcase 'updateUploadProgress': {\n\t\t\tfor (const [fileOrId, { progress }] of action.progress) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (progress === undefined || previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t\tabortController: previousFileState.abortController,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tprogress,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t}\n\tassertNever(action)\n}\n"],"names":[],"mappings":";;;;MAOa,4BAA4B;AAGnC,EACL,gBAAgB;AAAA,EAChB,
|
|
1
|
+
{"version":3,"file":"fileUploadReducer.js","sources":["../../../src/upload/fileUploadReducer.ts"],"sourcesContent":["import { FileUploadError } from '@contember/client'\nimport { assertNever } from '../utils'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport { toFileId } from './toFileId'\n\nexport const initializeFileUploadState = <Result = unknown, Metadata = undefined>(): FileUploadMultiTemporalState<\n\tResult,\n\tMetadata\n> => ({\n\tlastUpdateTime: 0,\n\tfileIdByFile: new WeakMap<File, FileId>(),\n\tliveState: new Map(),\n\tpublicState: new Map(),\n\tisLiveStateDirty: false,\n})\n\nexport const fileUploadReducer = <Result = unknown, Metadata = undefined>(\n\tpreviousState: FileUploadMultiTemporalState<Result, Metadata>,\n\taction: FileUploadAction<Result, Metadata>,\n): FileUploadMultiTemporalState<Result, Metadata> => {\n\tconst publishNewState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tpublicState: previousState.liveState,\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tlastUpdateTime: previousState.lastUpdateTime,\n\t\tisLiveStateDirty: false,\n\n\t\t// Immediately make the live state referentially unequal so that this doesn't have to happen during each action\n\t\t// several times between two publishes.\n\t\tliveState: new Map(previousState.liveState),\n\t})\n\tconst getNewDirtyState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tliveState: previousState.liveState,\n\t\tpublicState: previousState.publicState,\n\t\tisLiveStateDirty: true,\n\t\tlastUpdateTime: Date.now(),\n\t})\n\tswitch (action.type) {\n\t\tcase 'publishNewestState': {\n\t\t\treturn publishNewState()\n\t\t}\n\t\tcase 'initialize': {\n\t\t\tfor (const [fileId, metadata] of action.files) {\n\t\t\t\t// Deliberately allowing starting a new upload with the same id\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'initializing',\n\t\t\t\t\tabortController: metadata.abortController,\n\t\t\t\t\tpreviewUrl: metadata.previewUrl,\n\t\t\t\t\tfile: metadata.file,\n\t\t\t\t})\n\t\t\t\tpreviousState.fileIdByFile.set(metadata.file, fileId)\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about new files immediate*/\n\t\t}\n\t\tcase 'startUploading': {\n\t\t\tfor (const [fileOrId, metadata] of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tabortController: fileState.abortController,\n\t\t\t\t\tfile: fileState.file,\n\t\t\t\t\tmetadata: metadata.metadata!, // If the user didn't supply the metadata, then that's on them.\n\t\t\t\t\tpreviewUrl: fileState.previewUrl,\n\t\t\t\t\tprogress: undefined,\n\t\t\t\t\tuploader: metadata.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about started upload immediate*/\n\t\t}\n\t\tcase 'finishSuccessfully': {\n\t\t\tfor (const [fileOrId, result] of action.result) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'success',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tresult,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\t// return publishNewState() // Making the feedback about successful upload immediate.\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t\tcase 'finishWithError': {\n\t\t\tfor (const errorSpec of action.error) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet rawError: unknown\n\n\t\t\t\tif (Array.isArray(errorSpec)) {\n\t\t\t\t\t[fileOrId, rawError] = errorSpec\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = errorSpec\n\t\t\t\t\trawError = undefined\n\t\t\t\t}\n\n\t\t\t\tconst errors: FileUploadError[] = []\n\n\t\t\t\tif (rawError instanceof FileUploadError) {\n\t\t\t\t\terrors.push(rawError)\n\t\t\t\t} else if (Array.isArray(rawError)) {\n\t\t\t\t\tfor (const error of rawError) {\n\t\t\t\t\t\tif (error instanceof FileUploadError) {\n\t\t\t\t\t\t\terrors.push(error)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (\n\t\t\t\t\tpreviousFileState === undefined ||\n\t\t\t\t\t(previousFileState.readyState !== 'initializing' && previousFileState.readyState !== 'uploading')\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst uploader = previousFileState.readyState === 'uploading' ? previousFileState.uploader : undefined\n\t\t\t\tconst metadata = previousFileState.readyState === 'uploading' ? previousFileState.metadata : undefined\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'error',\n\t\t\t\t\terrors: errors.length ? errors : undefined,\n\t\t\t\t\trawError,\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tuploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState() // Bad news can wait.\n\t\t}\n\t\tcase 'purge': {\n\t\t\tlet atLeastOnePurged = false\n\t\t\tfor (const fileOrId of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tatLeastOnePurged = true\n\t\t\t\tif (fileState.readyState === 'initializing' || fileState.readyState === 'uploading') {\n\t\t\t\t\tfileState.abortController.abort() // This is a bit naughty… We shouldn't do this from here.\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.delete(fileId)\n\t\t\t}\n\t\t\tif (atLeastOnePurged) {\n\t\t\t\treturn getNewDirtyState()\n\t\t\t}\n\t\t\treturn previousState\n\t\t}\n\t\tcase 'updateUploadProgress': {\n\t\t\tfor (const [fileOrId, { progress }] of action.progress) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (progress === undefined || previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t\tabortController: previousFileState.abortController,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t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,UACA,MAAM,kBAAkB;AAAA,UACxB;AAAA,UACA,YAAY,kBAAkB;AAAA,UAC9B;AAAA;AAAA;AAGF,aAAO;AAAA;AAAA,SAEH,SAAS;AACb,UAAI,mBAAmB;AACvB,iBAAW,YAAY,OAAO,OAAO;AACpC,cAAM,SAAS,SAAS,eAAe;AACvC,cAAM,YAAY,cAAc,UAAU,IAAI;AAE9C,YAAI,cAAc,QAAW;AAC5B;AAAA;AAED,2BAAmB;AACnB,YAAI,UAAU,eAAe,kBAAkB,UAAU,eAAe,aAAa;AACpF,oBAAU,gBAAgB;AAAA;AAE3B,sBAAc,UAAU,OAAO;AAAA;AAEhC,UAAI,kBAAkB;AACrB,eAAO;AAAA;AAER,aAAO;AAAA;AAAA,SAEH,wBAAwB;AAC5B,iBAAW,CAAC,UAAU,EAAE,eAAe,OAAO,UAAU;AACvD,cAAM,SAAS,SAAS,eAAe;AACvC,cAAM,oBAAoB,cAAc,UAAU,IAAI;AACtD,YAAI,aAAa,UAAa,sBAAsB,UAAa,kBAAkB,eAAe,aAAa;AAC9G;AAAA;AAED,sBAAc,UAAU,IAAI,QAAQ;AAAA,UACnC,YAAY;AAAA,UACZ,MAAM,kBAAkB;AAAA,UACxB,UAAU,kBAAkB;AAAA,UAC5B,iBAAiB,kBAAkB;AAAA,UACnC,YAAY,kBAAkB;AAAA,UAC9B,UAAU,kBAAkB;AAAA,UAC5B;AAAA;AAAA;AAGF,aAAO;AAAA;AAAA;AAGT;AAAY;;;;"}
|
|
@@ -29,8 +29,8 @@ const useFileUpload = (options) => {
|
|
|
29
29
|
});
|
|
30
30
|
}, []);
|
|
31
31
|
const initializeUpload = useCallback((files) => {
|
|
32
|
-
const newFileIds = new Set();
|
|
33
|
-
const fileWithMetadataByFileConfig = new Map();
|
|
32
|
+
const newFileIds = /* @__PURE__ */ new Set();
|
|
33
|
+
const fileWithMetadataByFileConfig = /* @__PURE__ */ new Map();
|
|
34
34
|
for (const fileMaybeWithId of files) {
|
|
35
35
|
const abortController = new AbortController();
|
|
36
36
|
let fileId;
|
|
@@ -69,8 +69,8 @@ const useFileUpload = (options) => {
|
|
|
69
69
|
return fileWithMetadataByFileConfig;
|
|
70
70
|
}, [purgeUpload]);
|
|
71
71
|
const startUpload = useCallback((files) => {
|
|
72
|
-
const filesById = new Map();
|
|
73
|
-
const groupedByUploader = new Map();
|
|
72
|
+
const filesById = /* @__PURE__ */ new Map();
|
|
73
|
+
const groupedByUploader = /* @__PURE__ */ new Map();
|
|
74
74
|
const currentState = multiTemporalStateRef.current;
|
|
75
75
|
for (const fileOrIdMaybeWithOptions of files) {
|
|
76
76
|
let fileOrId;
|
|
@@ -94,7 +94,7 @@ const useFileUpload = (options) => {
|
|
|
94
94
|
});
|
|
95
95
|
let filesWithSameUploader = groupedByUploader.get(uploader);
|
|
96
96
|
if (filesWithSameUploader === void 0) {
|
|
97
|
-
groupedByUploader.set(uploader, filesWithSameUploader = new Map());
|
|
97
|
+
groupedByUploader.set(uploader, filesWithSameUploader = /* @__PURE__ */ new Map());
|
|
98
98
|
}
|
|
99
99
|
filesWithSameUploader.set(fileState.file, {
|
|
100
100
|
abortSignal: fileState.abortController.signal
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFileUpload.js","sources":["../../../src/upload/useFileUpload.ts"],"sourcesContent":["import { FileUploader, S3FileUploader, UploadedFileMetadata } from '@contember/client'\nimport { Reducer, useCallback, useEffect, useMemo, useReducer, useRef } from 'react'\nimport { useCurrentContentGraphQlClient } from '../content'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadCompoundState } from './FileUploadCompoundState'\nimport type { FileUploadMetadata } from './FileUploadMetadata'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport type {\n\tPurgeUpload,\n\tFileUploadOperations,\n\tInitializeUpload,\n\tStartUpload,\n\tStartUploadFileOptions,\n\tFailUpload,\n} from './FileUploadOperations'\nimport { fileUploadReducer, initializeFileUploadState } from './fileUploadReducer'\nimport type { FileWithMetadata } from './FileWithMetadata'\nimport { toFileId } from './toFileId'\n\nexport type FileUpload<Result = unknown, Metadata = undefined> = [\n\tFileUploadCompoundState<Result, Metadata>,\n\tFileUploadOperations<Metadata>,\n]\n\nexport interface FileUploadOptions {\n\tmaxUpdateFrequency?: number // This does NOT apply to all kinds of updates.\n}\n\nexport const useFileUpload = <Result = unknown, Metadata = undefined>(\n\toptions?: FileUploadOptions,\n): FileUpload<Result, Metadata> => {\n\tconst maxUpdateFrequency = options?.maxUpdateFrequency ?? 100\n\n\tconst contentApiClient = useCurrentContentGraphQlClient()\n\n\tconst updateTimeoutRef = useRef<number | undefined>(undefined)\n\tconst isFirstRenderRef = useRef(true)\n\tconst fileIdSeedRef = useRef(1)\n\n\tconst [multiTemporalState, dispatch] = useReducer<\n\t\tReducer<FileUploadMultiTemporalState<Result, Metadata>, FileUploadAction<Result, Metadata>>,\n\t\tundefined\n\t>(fileUploadReducer, undefined, initializeFileUploadState)\n\tconst multiTemporalStateRef = useRef(multiTemporalState)\n\n\tconst defaultUploader = useMemo(() => new S3FileUploader(), [])\n\n\tuseEffect(() => {\n\t\tmultiTemporalStateRef.current = multiTemporalState\n\t})\n\n\tconst purgeUpload = useCallback<PurgeUpload>(files => {\n\t\tdispatch({\n\t\t\ttype: 'purge',\n\t\t\tfiles,\n\t\t})\n\t}, [])\n\tconst failUpload = useCallback<FailUpload>(error => {\n\t\tdispatch({\n\t\t\ttype: 'finishWithError',\n\t\t\terror,\n\t\t})\n\t}, [])\n\tconst initializeUpload = useCallback<InitializeUpload>(\n\t\tfiles => {\n\t\t\tconst newFileIds = new Set<FileId>()\n\t\t\tconst fileWithMetadataByFileConfig = new Map<FileId, FileWithMetadata>()\n\n\t\t\tfor (const fileMaybeWithId of files) {\n\t\t\t\tconst abortController = new AbortController()\n\n\t\t\t\tlet fileId: FileId\n\t\t\t\tlet file: File\n\n\t\t\t\tif (fileMaybeWithId instanceof File) {\n\t\t\t\t\tfileId = `__contember__file-${fileIdSeedRef.current++}`\n\t\t\t\t\tfile = fileMaybeWithId\n\t\t\t\t} else {\n\t\t\t\t\tfileId = fileMaybeWithId[0]\n\t\t\t\t\tfile = fileMaybeWithId[1]\n\t\t\t\t}\n\n\t\t\t\tabortController.signal.addEventListener('abort', () => {\n\t\t\t\t\tpurgeUpload([fileId])\n\t\t\t\t})\n\n\t\t\t\tfileWithMetadataByFileConfig.set(fileId, {\n\t\t\t\t\tpreviewUrl: URL.createObjectURL(file),\n\t\t\t\t\tabortController,\n\t\t\t\t\tfile,\n\t\t\t\t\tfileId,\n\t\t\t\t})\n\n\t\t\t\tnewFileIds.add(fileId)\n\t\t\t}\n\t\t\tif (fileWithMetadataByFileConfig.size === 0) {\n\t\t\t\treturn fileWithMetadataByFileConfig\n\t\t\t}\n\n\t\t\tif (newFileIds.size) {\n\t\t\t\tdispatch({\n\t\t\t\t\ttype: 'purge',\n\t\t\t\t\tfiles: newFileIds,\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tdispatch({\n\t\t\t\ttype: 'initialize',\n\t\t\t\tfiles: fileWithMetadataByFileConfig,\n\t\t\t})\n\n\t\t\treturn fileWithMetadataByFileConfig\n\t\t},\n\t\t[purgeUpload],\n\t)\n\tconst startUpload = useCallback<StartUpload<Metadata>>(\n\t\tfiles => {\n\t\t\tconst filesById = new Map<FileId, FileUploadMetadata<Metadata>>()\n\t\t\tconst groupedByUploader: Map<FileUploader, Map<File, UploadedFileMetadata>> = new Map()\n\n\t\t\tconst currentState = multiTemporalStateRef.current\n\n\t\t\tfor (const fileOrIdMaybeWithOptions of files) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet options: StartUploadFileOptions<Metadata>\n\n\t\t\t\tif (Array.isArray(fileOrIdMaybeWithOptions)) {\n\t\t\t\t\tfileOrId = fileOrIdMaybeWithOptions[0]\n\t\t\t\t\toptions = fileOrIdMaybeWithOptions[1]\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = fileOrIdMaybeWithOptions\n\t\t\t\t\toptions = {}\n\t\t\t\t}\n\t\t\t\tconst { uploader = defaultUploader, metadata } = options\n\n\t\t\t\tconst fileId = toFileId(currentState, fileOrId)\n\t\t\t\tconst fileState = currentState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Trying to startUpload a file that hasn't been previously initialized. ` +\n\t\t\t\t\t\t\t`This will be possible in future, but isn't yet.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\tfilesById.set(fileId, {\n\t\t\t\t\tuploader,\n\t\t\t\t\tmetadata,\n\t\t\t\t})\n\n\t\t\t\tlet filesWithSameUploader = groupedByUploader.get(uploader)\n\t\t\t\tif (filesWithSameUploader === undefined) {\n\t\t\t\t\tgroupedByUploader.set(uploader, (filesWithSameUploader = new Map()))\n\t\t\t\t}\n\t\t\t\tfilesWithSameUploader.set(fileState.file, {\n\t\t\t\t\tabortSignal: fileState.abortController.signal,\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tdispatch({\n\t\t\t\ttype: 'startUploading',\n\t\t\t\tfiles: filesById,\n\t\t\t})\n\n\t\t\tfor (const [uploader, filesForUpload] of groupedByUploader) {\n\t\t\t\ttry {\n\t\t\t\t\tuploader.upload(filesForUpload, {\n\t\t\t\t\t\tonProgress: progress => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'updateUploadProgress',\n\t\t\t\t\t\t\t\tprogress,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonSuccess: result => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'finishSuccessfully',\n\t\t\t\t\t\t\t\tresult,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonError: error => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'finishWithError',\n\t\t\t\t\t\t\t\terror,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tcontentApiClient,\n\t\t\t\t\t})\n\t\t\t\t} catch (error) {\n\t\t\t\t\tdispatch({\n\t\t\t\t\t\ttype: 'finishWithError',\n\t\t\t\t\t\terror: Array.from(filesForUpload).map(([file]) => [file, error]),\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t[contentApiClient, defaultUploader],\n\t)\n\n\tconst operations = useMemo<FileUploadOperations<Metadata>>(\n\t\t() => ({\n\t\t\tfailUpload,\n\t\t\tinitializeUpload,\n\t\t\tpurgeUpload,\n\t\t\tstartUpload,\n\t\t}),\n\t\t[failUpload, initializeUpload, purgeUpload, startUpload],\n\t)\n\n\tuseEffect(() => {\n\t\tif (isFirstRenderRef.current) {\n\t\t\treturn\n\t\t}\n\t\tif (multiTemporalState.isLiveStateDirty) {\n\t\t\tconst now = Date.now()\n\t\t\tconst timeDelta = Math.max(now - multiTemporalState.lastUpdateTime, 0) // The max is just a sanity check\n\t\t\tif (timeDelta > maxUpdateFrequency) {\n\t\t\t\tif (updateTimeoutRef.current !== undefined) {\n\t\t\t\t\tclearTimeout(updateTimeoutRef.current)\n\t\t\t\t}\n\t\t\t\tdispatch({\n\t\t\t\t\ttype: 'publishNewestState',\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tif (updateTimeoutRef.current !== undefined) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tupdateTimeoutRef.current = window.setTimeout(() => {\n\t\t\t\t\tdispatch({\n\t\t\t\t\t\ttype: 'publishNewestState',\n\t\t\t\t\t})\n\t\t\t\t\tupdateTimeoutRef.current = undefined\n\t\t\t\t}, maxUpdateFrequency - timeDelta)\n\t\t\t}\n\t\t}\n\t}, [\n\t\tmultiTemporalState.isLiveStateDirty,\n\t\tmultiTemporalState.lastUpdateTime,\n\t\tmultiTemporalState.liveState,\n\t\tmultiTemporalState.publicState,\n\t\tmaxUpdateFrequency,\n\t])\n\n\tuseEffect(\n\t\t() => () => {\n\t\t\tfor (const [, state] of multiTemporalStateRef.current.liveState) {\n\t\t\t\tURL.revokeObjectURL(state.previewUrl)\n\t\t\t}\n\t\t\tfor (const [, state] of multiTemporalStateRef.current.publicState) {\n\t\t\t\tURL.revokeObjectURL(state.previewUrl)\n\t\t\t}\n\t\t},\n\t\t[],\n\t)\n\n\t// For this to work, this effect must be the last one to run.\n\tuseEffect(() => {\n\t\tisFirstRenderRef.current = false\n\t}, [])\n\n\treturn [multiTemporalState.publicState, operations]\n}\n"],"names":[],"mappings":";;;;;;MA6Ba,gBAAgB,CAC5B,YACkC;AAClC,QAAM,qBAAqB,SAAS,sBAAsB;AAE1D,QAAM,mBAAmB;AAEzB,QAAM,mBAAmB,OAA2B;AACpD,QAAM,mBAAmB,OAAO;AAChC,QAAM,gBAAgB,OAAO;AAE7B,QAAM,CAAC,oBAAoB,YAAY,WAGrC,mBAAmB,QAAW;AAChC,QAAM,wBAAwB,OAAO;AAErC,QAAM,kBAAkB,QAAQ,MAAM,IAAI,kBAAkB;AAE5D,YAAU,MAAM;AACf,0BAAsB,UAAU;AAAA;AAGjC,QAAM,cAAc,YAAyB,WAAS;AACrD,aAAS;AAAA,MACR,MAAM;AAAA,MACN;AAAA;AAAA,KAEC;AACH,QAAM,aAAa,YAAwB,WAAS;AACnD,aAAS;AAAA,MACR,MAAM;AAAA,MACN;AAAA;AAAA,KAEC;AACH,QAAM,mBAAmB,YACxB,WAAS;AACR,UAAM,aAAa,IAAI;AACvB,UAAM,+BAA+B,IAAI;AAEzC,eAAW,mBAAmB,OAAO;AACpC,YAAM,kBAAkB,IAAI;AAE5B,UAAI;AACJ,UAAI;AAEJ,UAAI,2BAA2B,MAAM;AACpC,iBAAS,qBAAqB,cAAc;AAC5C,eAAO;AAAA,aACD;AACN,iBAAS,gBAAgB;AACzB,eAAO,gBAAgB;AAAA;AAGxB,sBAAgB,OAAO,iBAAiB,SAAS,MAA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AI,gBAAgB,MAAM;AAAA;AAAA,KAG5B;AAID,YAAU,MAAM;AACf,qBAAiB,UAAU;AAAA,KACzB;AAEH,SAAO,CAAC,mBAAmB,aAAa;AAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"useFileUpload.js","sources":["../../../src/upload/useFileUpload.ts"],"sourcesContent":["import { FileUploader, S3FileUploader, UploadedFileMetadata } from '@contember/client'\nimport { Reducer, useCallback, useEffect, useMemo, useReducer, useRef } from 'react'\nimport { useCurrentContentGraphQlClient } from '../content'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadCompoundState } from './FileUploadCompoundState'\nimport type { FileUploadMetadata } from './FileUploadMetadata'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport type {\n\tPurgeUpload,\n\tFileUploadOperations,\n\tInitializeUpload,\n\tStartUpload,\n\tStartUploadFileOptions,\n\tFailUpload,\n} from './FileUploadOperations'\nimport { fileUploadReducer, initializeFileUploadState } from './fileUploadReducer'\nimport type { FileWithMetadata } from './FileWithMetadata'\nimport { toFileId } from './toFileId'\n\nexport type FileUpload<Result = unknown, Metadata = undefined> = [\n\tFileUploadCompoundState<Result, Metadata>,\n\tFileUploadOperations<Metadata>,\n]\n\nexport interface FileUploadOptions {\n\tmaxUpdateFrequency?: number // This does NOT apply to all kinds of updates.\n}\n\nexport const useFileUpload = <Result = unknown, Metadata = undefined>(\n\toptions?: FileUploadOptions,\n): FileUpload<Result, Metadata> => {\n\tconst maxUpdateFrequency = options?.maxUpdateFrequency ?? 100\n\n\tconst contentApiClient = useCurrentContentGraphQlClient()\n\n\tconst updateTimeoutRef = useRef<number | undefined>(undefined)\n\tconst isFirstRenderRef = useRef(true)\n\tconst fileIdSeedRef = useRef(1)\n\n\tconst [multiTemporalState, dispatch] = useReducer<\n\t\tReducer<FileUploadMultiTemporalState<Result, Metadata>, FileUploadAction<Result, Metadata>>,\n\t\tundefined\n\t>(fileUploadReducer, undefined, initializeFileUploadState)\n\tconst multiTemporalStateRef = useRef(multiTemporalState)\n\n\tconst defaultUploader = useMemo(() => new S3FileUploader(), [])\n\n\tuseEffect(() => {\n\t\tmultiTemporalStateRef.current = multiTemporalState\n\t})\n\n\tconst purgeUpload = useCallback<PurgeUpload>(files => {\n\t\tdispatch({\n\t\t\ttype: 'purge',\n\t\t\tfiles,\n\t\t})\n\t}, [])\n\tconst failUpload = useCallback<FailUpload>(error => {\n\t\tdispatch({\n\t\t\ttype: 'finishWithError',\n\t\t\terror,\n\t\t})\n\t}, [])\n\tconst initializeUpload = useCallback<InitializeUpload>(\n\t\tfiles => {\n\t\t\tconst newFileIds = new Set<FileId>()\n\t\t\tconst fileWithMetadataByFileConfig = new Map<FileId, FileWithMetadata>()\n\n\t\t\tfor (const fileMaybeWithId of files) {\n\t\t\t\tconst abortController = new AbortController()\n\n\t\t\t\tlet fileId: FileId\n\t\t\t\tlet file: File\n\n\t\t\t\tif (fileMaybeWithId instanceof File) {\n\t\t\t\t\tfileId = `__contember__file-${fileIdSeedRef.current++}`\n\t\t\t\t\tfile = fileMaybeWithId\n\t\t\t\t} else {\n\t\t\t\t\tfileId = fileMaybeWithId[0]\n\t\t\t\t\tfile = fileMaybeWithId[1]\n\t\t\t\t}\n\n\t\t\t\tabortController.signal.addEventListener('abort', () => {\n\t\t\t\t\tpurgeUpload([fileId])\n\t\t\t\t})\n\n\t\t\t\tfileWithMetadataByFileConfig.set(fileId, {\n\t\t\t\t\tpreviewUrl: URL.createObjectURL(file),\n\t\t\t\t\tabortController,\n\t\t\t\t\tfile,\n\t\t\t\t\tfileId,\n\t\t\t\t})\n\n\t\t\t\tnewFileIds.add(fileId)\n\t\t\t}\n\t\t\tif (fileWithMetadataByFileConfig.size === 0) {\n\t\t\t\treturn fileWithMetadataByFileConfig\n\t\t\t}\n\n\t\t\tif (newFileIds.size) {\n\t\t\t\tdispatch({\n\t\t\t\t\ttype: 'purge',\n\t\t\t\t\tfiles: newFileIds,\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tdispatch({\n\t\t\t\ttype: 'initialize',\n\t\t\t\tfiles: fileWithMetadataByFileConfig,\n\t\t\t})\n\n\t\t\treturn fileWithMetadataByFileConfig\n\t\t},\n\t\t[purgeUpload],\n\t)\n\tconst startUpload = useCallback<StartUpload<Metadata>>(\n\t\tfiles => {\n\t\t\tconst filesById = new Map<FileId, FileUploadMetadata<Metadata>>()\n\t\t\tconst groupedByUploader: Map<FileUploader, Map<File, UploadedFileMetadata>> = new Map()\n\n\t\t\tconst currentState = multiTemporalStateRef.current\n\n\t\t\tfor (const fileOrIdMaybeWithOptions of files) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet options: StartUploadFileOptions<Metadata>\n\n\t\t\t\tif (Array.isArray(fileOrIdMaybeWithOptions)) {\n\t\t\t\t\tfileOrId = fileOrIdMaybeWithOptions[0]\n\t\t\t\t\toptions = fileOrIdMaybeWithOptions[1]\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = fileOrIdMaybeWithOptions\n\t\t\t\t\toptions = {}\n\t\t\t\t}\n\t\t\t\tconst { uploader = defaultUploader, metadata } = options\n\n\t\t\t\tconst fileId = toFileId(currentState, fileOrId)\n\t\t\t\tconst fileState = currentState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Trying to startUpload a file that hasn't been previously initialized. ` +\n\t\t\t\t\t\t\t`This will be possible in future, but isn't yet.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\tfilesById.set(fileId, {\n\t\t\t\t\tuploader,\n\t\t\t\t\tmetadata,\n\t\t\t\t})\n\n\t\t\t\tlet filesWithSameUploader = groupedByUploader.get(uploader)\n\t\t\t\tif (filesWithSameUploader === undefined) {\n\t\t\t\t\tgroupedByUploader.set(uploader, (filesWithSameUploader = new Map()))\n\t\t\t\t}\n\t\t\t\tfilesWithSameUploader.set(fileState.file, {\n\t\t\t\t\tabortSignal: fileState.abortController.signal,\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tdispatch({\n\t\t\t\ttype: 'startUploading',\n\t\t\t\tfiles: filesById,\n\t\t\t})\n\n\t\t\tfor (const [uploader, filesForUpload] of groupedByUploader) {\n\t\t\t\ttry {\n\t\t\t\t\tuploader.upload(filesForUpload, {\n\t\t\t\t\t\tonProgress: progress => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'updateUploadProgress',\n\t\t\t\t\t\t\t\tprogress,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonSuccess: result => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'finishSuccessfully',\n\t\t\t\t\t\t\t\tresult,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonError: error => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'finishWithError',\n\t\t\t\t\t\t\t\terror,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tcontentApiClient,\n\t\t\t\t\t})\n\t\t\t\t} catch (error) {\n\t\t\t\t\tdispatch({\n\t\t\t\t\t\ttype: 'finishWithError',\n\t\t\t\t\t\terror: Array.from(filesForUpload).map(([file]) => [file, error]),\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t[contentApiClient, defaultUploader],\n\t)\n\n\tconst operations = useMemo<FileUploadOperations<Metadata>>(\n\t\t() => ({\n\t\t\tfailUpload,\n\t\t\tinitializeUpload,\n\t\t\tpurgeUpload,\n\t\t\tstartUpload,\n\t\t}),\n\t\t[failUpload, initializeUpload, purgeUpload, startUpload],\n\t)\n\n\tuseEffect(() => {\n\t\tif (isFirstRenderRef.current) {\n\t\t\treturn\n\t\t}\n\t\tif (multiTemporalState.isLiveStateDirty) {\n\t\t\tconst now = Date.now()\n\t\t\tconst timeDelta = Math.max(now - multiTemporalState.lastUpdateTime, 0) // The max is just a sanity check\n\t\t\tif (timeDelta > maxUpdateFrequency) {\n\t\t\t\tif (updateTimeoutRef.current !== undefined) {\n\t\t\t\t\tclearTimeout(updateTimeoutRef.current)\n\t\t\t\t}\n\t\t\t\tdispatch({\n\t\t\t\t\ttype: 'publishNewestState',\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tif (updateTimeoutRef.current !== undefined) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tupdateTimeoutRef.current = window.setTimeout(() => {\n\t\t\t\t\tdispatch({\n\t\t\t\t\t\ttype: 'publishNewestState',\n\t\t\t\t\t})\n\t\t\t\t\tupdateTimeoutRef.current = undefined\n\t\t\t\t}, maxUpdateFrequency - timeDelta)\n\t\t\t}\n\t\t}\n\t}, [\n\t\tmultiTemporalState.isLiveStateDirty,\n\t\tmultiTemporalState.lastUpdateTime,\n\t\tmultiTemporalState.liveState,\n\t\tmultiTemporalState.publicState,\n\t\tmaxUpdateFrequency,\n\t])\n\n\tuseEffect(\n\t\t() => () => {\n\t\t\tfor (const [, state] of multiTemporalStateRef.current.liveState) {\n\t\t\t\tURL.revokeObjectURL(state.previewUrl)\n\t\t\t}\n\t\t\tfor (const [, state] of multiTemporalStateRef.current.publicState) {\n\t\t\t\tURL.revokeObjectURL(state.previewUrl)\n\t\t\t}\n\t\t},\n\t\t[],\n\t)\n\n\t// For this to work, this effect must be the last one to run.\n\tuseEffect(() => {\n\t\tisFirstRenderRef.current = false\n\t}, [])\n\n\treturn [multiTemporalState.publicState, operations]\n}\n"],"names":[],"mappings":";;;;;;MA6Ba,gBAAgB,CAC5B,YACkC;AAClC,QAAM,qBAAqB,SAAS,sBAAsB;AAE1D,QAAM,mBAAmB;AAEzB,QAAM,mBAAmB,OAA2B;AACpD,QAAM,mBAAmB,OAAO;AAChC,QAAM,gBAAgB,OAAO;AAE7B,QAAM,CAAC,oBAAoB,YAAY,WAGrC,mBAAmB,QAAW;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,MAAM,mBAAmB,gBAAgB;AACpE,UAAI,YAAY,oBAAoB;AACnC,YAAI,iBAAiB,YAAY,QAAW;AAC3C,uBAAa,iBAAiB;AAAA;AAE/B,iBAAS;AAAA,UACR,MAAM;AAAA;AAAA,aAED;AACN,YAAI,iBAAiB,YAAY,QAAW;AAC3C;AAAA;AAED,yBAAiB,UAAU,OAAO,WAAW,MAAM;AAClD,mBAAS;AAAA,YACR,MAAM;AAAA;AAEP,2BAAiB,UAAU;AAAA,WACzB,qBAAqB;AAAA;AAAA;AAAA,KAGxB;AAAA,IACF,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB;AAAA;AAGD,YACC,MAAM,MAAM;AACX,eAAW,GAAG,UAAU,sBAAsB,QAAQ,WAAW;AAChE,UAAI,gBAAgB,MAAM;AAAA;AAE3B,eAAW,GAAG,UAAU,sBAAsB,QAAQ,aAAa;AAClE,UAAI,gBAAgB,MAAM;AAAA;AAAA,KAG5B;AAID,YAAU,MAAM;AACf,qBAAiB,UAAU;AAAA,KACzB;AAEH,SAAO,CAAC,mBAAmB,aAAa;AAAA;;;;"}
|
|
@@ -48,7 +48,7 @@ const RichTextRenderer = memo(function RichTextRenderer2({
|
|
|
48
48
|
if (typeof Object(references)[Symbol.iterator] !== "function") {
|
|
49
49
|
throw new RichTextRendererError(`The set of references must be an iterable!`);
|
|
50
50
|
}
|
|
51
|
-
normalizedReferences = new Map();
|
|
51
|
+
normalizedReferences = /* @__PURE__ */ new Map();
|
|
52
52
|
for (const reference of references) {
|
|
53
53
|
if (!("id" in reference)) {
|
|
54
54
|
throw new RichTextRendererError(`Found a reference without an id field.`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextRenderer.js","sources":["../../../../src/components/RichTextRenderer/RichTextRenderer.tsx"],"sourcesContent":["import { ComponentType, createElement, memo, ReactElement, ReactNode, useMemo } from 'react'\nimport { defaultDeserialize } from './defaultDeserialize'\nimport type { RenderElement } from './ElementRenderer'\nimport type { RenderLeaf } from './LeafRenderer'\nimport type { NormalizedRichTextBlock } from './NormalizedRichTextBlock'\nimport type { ReferenceRenderer } from './ReferenceRenderer'\nimport { renderChildren } from './renderChildren'\nimport type { RichTextBlock } from './RichTextBlock'\nimport type { RichTextElement } from './RichTextElement'\nimport type { RichTextLeaf } from './RichTextLeaf'\nimport type { RichTextReference } from './RichTextReference'\nimport { RichTextRendererError } from './RichTextRendererError'\nimport { RichTextRenderMetadataContext } from './RichTextRenderMetadataContext'\nimport type { RootEditorNode } from './RootEditorNode'\n\nexport interface RichTextRendererFieldProps {\n\tsource: string | null\n}\n\nexport interface RichTextRendererBlockProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> {\n\tblocks: RichTextBlock[]\n\treferenceRenderers?: Record<string, ReferenceRenderer<any, CustomElements, CustomLeaves>>\n\tsourceField?: string\n\treferencesField?: string\n\treferenceDiscriminationField?: string\n}\n\nexport type RichTextRendererProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> = {\n\tdeserialize?: (source: string) => RootEditorNode<CustomElements, CustomLeaves>\n\trenderElement?: RenderElement<CustomElements, CustomLeaves>\n\trenderLeaf?: RenderLeaf<CustomLeaves>\n\tattributeNamePrefix?: string\n\trenderBlock?: ComponentType<{ block: unknown }>\n} & (RichTextRendererFieldProps | RichTextRendererBlockProps<CustomElements, CustomLeaves>)\n\nexport const RichTextRenderer = memo(function RichTextRenderer<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n>({\n\tdeserialize = defaultDeserialize,\n\trenderElement,\n\trenderLeaf,\n\trenderBlock = ({ children }) => <>{children}</>,\n\tattributeNamePrefix,\n\t...sourceProps\n}: RichTextRendererProps<CustomElements, CustomLeaves>) {\n\tconst normalizedSource = useMemo<NormalizedRichTextBlock<CustomElements, CustomLeaves>[]>(() => {\n\t\tif ('source' in sourceProps && !('blocks' in sourceProps)) {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tcontent: sourceProps.source\n\t\t\t\t\t\t? deserialize(sourceProps.source)\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tformatVersion: 0,\n\t\t\t\t\t\t\t\tchildren: [],\n\t\t\t\t\t\t },\n\t\t\t\t\tid: undefined,\n\t\t\t\t\treferencesField: undefined,\n\t\t\t\t\treferenceDiscriminationField: undefined,\n\t\t\t\t\treferenceRenderers: {},\n\t\t\t\t\treferences: undefined,\n\t\t\t\t},\n\t\t\t]\n\t\t}\n\t\tif ('blocks' in sourceProps) {\n\t\t\tconst sourceField = sourceProps.sourceField ?? 'source'\n\t\t\tconst referencesField = sourceProps.referencesField ?? 'references'\n\t\t\tconst referenceDiscriminationField = sourceProps.referenceDiscriminationField ?? 'type'\n\n\t\t\treturn sourceProps.blocks.map(block => {\n\t\t\t\tif (!(sourceField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`Found a block without a '${sourceField}' field. ` +\n\t\t\t\t\t\t\t(sourceProps.sourceField === undefined\n\t\t\t\t\t\t\t\t? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.`\n\t\t\t\t\t\t\t\t: `That is what the 'sourceField' prop has been set to, and so either this is a typo, ` +\n\t\t\t\t\t\t\t\t `or the data supplied is invalid.`),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst source = block[sourceField]\n\t\t\t\tif (typeof source !== 'string') {\n\t\t\t\t\tthrow new RichTextRendererError(`Found a block whose source is not a string.`)\n\t\t\t\t}\n\n\t\t\t\tif (sourceProps.referencesField !== undefined && !(referencesField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`The 'referencesField' prop is set to '${referencesField}' but a block without such field ` +\n\t\t\t\t\t\t\t`has been encountered. Unless this is just a typo, ` +\n\t\t\t\t\t\t\t`if you do not wish to use references, avoid supplying the 'referencesField' prop.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst references = block[referencesField]\n\n\t\t\t\tlet normalizedReferences: Map<string, RichTextReference> | undefined = undefined\n\t\t\t\tif (references) {\n\t\t\t\t\tif (typeof Object(references)[Symbol.iterator] !== 'function') {\n\t\t\t\t\t\tthrow new RichTextRendererError(`The set of references must be an iterable!`)\n\t\t\t\t\t}\n\n\t\t\t\t\tnormalizedReferences = new Map()\n\t\t\t\t\tfor (const reference of references as Iterable<RichTextReference>) {\n\t\t\t\t\t\tif (!('id' in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(`Found a reference without an id field.`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!(referenceDiscriminationField in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t\t\t`Found a reference without a '${referenceDiscriminationField}' field. ` +\n\t\t\t\t\t\t\t\t\t(sourceProps.referenceDiscriminationField === undefined\n\t\t\t\t\t\t\t\t\t\t? `The 'referenceDiscriminationField' prop has not been supplied, ` +\n\t\t\t\t\t\t\t\t\t\t `and so '${referenceDiscriminationField}' was used as a default.`\n\t\t\t\t\t\t\t\t\t\t: `That is what the 'referenceDiscriminationField' prop has been set to, ` +\n\t\t\t\t\t\t\t\t\t\t `and so either this is a typo, or the data supplied is invalid.`),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnormalizedReferences.set(reference.id, reference)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tid: typeof block.id === 'string' ? block.id : undefined,\n\t\t\t\t\tcontent: deserialize(source),\n\t\t\t\t\treferenceDiscriminationField,\n\t\t\t\t\treferenceRenderers: sourceProps.referenceRenderers ?? {},\n\t\t\t\t\treferencesField,\n\t\t\t\t\treferences: normalizedReferences,\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t\tthrow new RichTextRendererError(`RichTextRenderer: invalid data supplied.`)\n\t}, [deserialize, sourceProps])\n\n\treturn (\n\t\t<>\n\t\t\t{normalizedSource.map((block, i) => (\n\t\t\t\t<RichTextRenderMetadataContext.Provider\n\t\t\t\t\tkey={block.id ?? i}\n\t\t\t\t\tvalue={{\n\t\t\t\t\t\tformatVersion: block.content.formatVersion,\n\t\t\t\t\t\treferenceRenderers: block.referenceRenderers,\n\t\t\t\t\t\treferencesField: block.referencesField,\n\t\t\t\t\t\treferenceDiscriminationField: block.referenceDiscriminationField,\n\t\t\t\t\t\treferences: block.references,\n\t\t\t\t\t}}\n\t\t\t\t>\n\t\t\t\t\t{createElement(\n\t\t\t\t\t\trenderBlock, {\n\t\t\t\t\t\t\tblock,\n\t\t\t\t\t\t}, renderChildren(block.content.children, {\n\t\t\t\t\t\t\trenderLeaf,\n\t\t\t\t\t\t\trenderElement,\n\t\t\t\t\t\t\tattributeNamePrefix,\n\t\t\t\t\t\t}),\n\t\t\t\t\t)}\n\t\t\t\t</RichTextRenderMetadataContext.Provider>\n\t\t\t))}\n\t\t</>\n\t)\n}) as <CustomElements extends RichTextElement = never, CustomLeaves extends RichTextLeaf = never>(\n\tprops: RichTextRendererProps<CustomElements, CustomLeaves>,\n) => ReactElement\n"],"names":[],"mappings":";;;;;MAyCa,mBAAmB,KAAK,2BAGnC;AAAA,EACD,cAAc;AAAA,EACd;AAAA,EACA;AAAA,EACA,cAAc,CAAC,EAAE,6DAAkB;AAAA,EACnC;AAAA,KACG;AAAA,GACoD;QACjD,mBAAmB,QAAiE,MAAM;QAC3F,YAAY,eAAe,cAAc,cAAc;aACnD;AAAA,QACN;AAAA,UACC,SAAS,YAAY,SAClB,YAAY,YAAY,UACxB;AAAA,YACA,eAAe;AAAA,YACf,UAAU;AAAA;AAAA,UAEb,IAAI;AAAA,UACJ,iBAAiB;AAAA,UACjB,8BAA8B;AAAA,UAC9B,oBAAoB;AAAA,UACpB,YAAY;AAAA;AAAA;AAAA;QAIX,YAAY,aAAa;YACtB,cAAc,YAAY,eAAe;YACzC,kBAAkB,YAAY,mBAAmB;YACjD,+BAA+B,YAAY,gCAAgC;aAE1E,YAAY,OAAO,IAAI,WAAS;YAClC,iBAAiB,QAAQ;gBACtB,IAAI,sBACT,4BAA4B,sCACd,gBAAgB,SAC1B,yDAAyD,wCACzD;AAAA;cAIA,SAAS,MAAM;YACjB,OAAO,WAAW,UAAU;gBACzB,IAAI,sBAAsB;AAAA;YAG7B,YAAY,oBAAoB,UAAa,qBAAqB,QAAQ;gBACvE,IAAI,sBACT,yCAAyC;AAAA;cAKrC,aAAa,MAAM;YAErB,uBAAmE;YACnE,YAAY;cACX,OAAO,OAAO,YAAY,OAAO,cAAc,YAAY;kBACxD,IAAI,sBAAsB;AAAA;
|
|
1
|
+
{"version":3,"file":"RichTextRenderer.js","sources":["../../../../src/components/RichTextRenderer/RichTextRenderer.tsx"],"sourcesContent":["import { ComponentType, createElement, memo, ReactElement, ReactNode, useMemo } from 'react'\nimport { defaultDeserialize } from './defaultDeserialize'\nimport type { RenderElement } from './ElementRenderer'\nimport type { RenderLeaf } from './LeafRenderer'\nimport type { NormalizedRichTextBlock } from './NormalizedRichTextBlock'\nimport type { ReferenceRenderer } from './ReferenceRenderer'\nimport { renderChildren } from './renderChildren'\nimport type { RichTextBlock } from './RichTextBlock'\nimport type { RichTextElement } from './RichTextElement'\nimport type { RichTextLeaf } from './RichTextLeaf'\nimport type { RichTextReference } from './RichTextReference'\nimport { RichTextRendererError } from './RichTextRendererError'\nimport { RichTextRenderMetadataContext } from './RichTextRenderMetadataContext'\nimport type { RootEditorNode } from './RootEditorNode'\n\nexport interface RichTextRendererFieldProps {\n\tsource: string | null\n}\n\nexport interface RichTextRendererBlockProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> {\n\tblocks: RichTextBlock[]\n\treferenceRenderers?: Record<string, ReferenceRenderer<any, CustomElements, CustomLeaves>>\n\tsourceField?: string\n\treferencesField?: string\n\treferenceDiscriminationField?: string\n}\n\nexport type RichTextRendererProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> = {\n\tdeserialize?: (source: string) => RootEditorNode<CustomElements, CustomLeaves>\n\trenderElement?: RenderElement<CustomElements, CustomLeaves>\n\trenderLeaf?: RenderLeaf<CustomLeaves>\n\tattributeNamePrefix?: string\n\trenderBlock?: ComponentType<{ block: unknown }>\n} & (RichTextRendererFieldProps | RichTextRendererBlockProps<CustomElements, CustomLeaves>)\n\nexport const RichTextRenderer = memo(function RichTextRenderer<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n>({\n\tdeserialize = defaultDeserialize,\n\trenderElement,\n\trenderLeaf,\n\trenderBlock = ({ children }) => <>{children}</>,\n\tattributeNamePrefix,\n\t...sourceProps\n}: RichTextRendererProps<CustomElements, CustomLeaves>) {\n\tconst normalizedSource = useMemo<NormalizedRichTextBlock<CustomElements, CustomLeaves>[]>(() => {\n\t\tif ('source' in sourceProps && !('blocks' in sourceProps)) {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tcontent: sourceProps.source\n\t\t\t\t\t\t? deserialize(sourceProps.source)\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tformatVersion: 0,\n\t\t\t\t\t\t\t\tchildren: [],\n\t\t\t\t\t\t },\n\t\t\t\t\tid: undefined,\n\t\t\t\t\treferencesField: undefined,\n\t\t\t\t\treferenceDiscriminationField: undefined,\n\t\t\t\t\treferenceRenderers: {},\n\t\t\t\t\treferences: undefined,\n\t\t\t\t},\n\t\t\t]\n\t\t}\n\t\tif ('blocks' in sourceProps) {\n\t\t\tconst sourceField = sourceProps.sourceField ?? 'source'\n\t\t\tconst referencesField = sourceProps.referencesField ?? 'references'\n\t\t\tconst referenceDiscriminationField = sourceProps.referenceDiscriminationField ?? 'type'\n\n\t\t\treturn sourceProps.blocks.map(block => {\n\t\t\t\tif (!(sourceField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`Found a block without a '${sourceField}' field. ` +\n\t\t\t\t\t\t\t(sourceProps.sourceField === undefined\n\t\t\t\t\t\t\t\t? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.`\n\t\t\t\t\t\t\t\t: `That is what the 'sourceField' prop has been set to, and so either this is a typo, ` +\n\t\t\t\t\t\t\t\t `or the data supplied is invalid.`),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst source = block[sourceField]\n\t\t\t\tif (typeof source !== 'string') {\n\t\t\t\t\tthrow new RichTextRendererError(`Found a block whose source is not a string.`)\n\t\t\t\t}\n\n\t\t\t\tif (sourceProps.referencesField !== undefined && !(referencesField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`The 'referencesField' prop is set to '${referencesField}' but a block without such field ` +\n\t\t\t\t\t\t\t`has been encountered. Unless this is just a typo, ` +\n\t\t\t\t\t\t\t`if you do not wish to use references, avoid supplying the 'referencesField' prop.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst references = block[referencesField]\n\n\t\t\t\tlet normalizedReferences: Map<string, RichTextReference> | undefined = undefined\n\t\t\t\tif (references) {\n\t\t\t\t\tif (typeof Object(references)[Symbol.iterator] !== 'function') {\n\t\t\t\t\t\tthrow new RichTextRendererError(`The set of references must be an iterable!`)\n\t\t\t\t\t}\n\n\t\t\t\t\tnormalizedReferences = new Map()\n\t\t\t\t\tfor (const reference of references as Iterable<RichTextReference>) {\n\t\t\t\t\t\tif (!('id' in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(`Found a reference without an id field.`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!(referenceDiscriminationField in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t\t\t`Found a reference without a '${referenceDiscriminationField}' field. ` +\n\t\t\t\t\t\t\t\t\t(sourceProps.referenceDiscriminationField === undefined\n\t\t\t\t\t\t\t\t\t\t? `The 'referenceDiscriminationField' prop has not been supplied, ` +\n\t\t\t\t\t\t\t\t\t\t `and so '${referenceDiscriminationField}' was used as a default.`\n\t\t\t\t\t\t\t\t\t\t: `That is what the 'referenceDiscriminationField' prop has been set to, ` +\n\t\t\t\t\t\t\t\t\t\t `and so either this is a typo, or the data supplied is invalid.`),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnormalizedReferences.set(reference.id, reference)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tid: typeof block.id === 'string' ? block.id : undefined,\n\t\t\t\t\tcontent: deserialize(source),\n\t\t\t\t\treferenceDiscriminationField,\n\t\t\t\t\treferenceRenderers: sourceProps.referenceRenderers ?? {},\n\t\t\t\t\treferencesField,\n\t\t\t\t\treferences: normalizedReferences,\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t\tthrow new RichTextRendererError(`RichTextRenderer: invalid data supplied.`)\n\t}, [deserialize, sourceProps])\n\n\treturn (\n\t\t<>\n\t\t\t{normalizedSource.map((block, i) => (\n\t\t\t\t<RichTextRenderMetadataContext.Provider\n\t\t\t\t\tkey={block.id ?? i}\n\t\t\t\t\tvalue={{\n\t\t\t\t\t\tformatVersion: block.content.formatVersion,\n\t\t\t\t\t\treferenceRenderers: block.referenceRenderers,\n\t\t\t\t\t\treferencesField: block.referencesField,\n\t\t\t\t\t\treferenceDiscriminationField: block.referenceDiscriminationField,\n\t\t\t\t\t\treferences: block.references,\n\t\t\t\t\t}}\n\t\t\t\t>\n\t\t\t\t\t{createElement(\n\t\t\t\t\t\trenderBlock, {\n\t\t\t\t\t\t\tblock,\n\t\t\t\t\t\t}, renderChildren(block.content.children, {\n\t\t\t\t\t\t\trenderLeaf,\n\t\t\t\t\t\t\trenderElement,\n\t\t\t\t\t\t\tattributeNamePrefix,\n\t\t\t\t\t\t}),\n\t\t\t\t\t)}\n\t\t\t\t</RichTextRenderMetadataContext.Provider>\n\t\t\t))}\n\t\t</>\n\t)\n}) as <CustomElements extends RichTextElement = never, CustomLeaves extends RichTextLeaf = never>(\n\tprops: RichTextRendererProps<CustomElements, CustomLeaves>,\n) => ReactElement\n"],"names":[],"mappings":";;;;;MAyCa,mBAAmB,KAAK,2BAGnC;AAAA,EACD,cAAc;AAAA,EACd;AAAA,EACA;AAAA,EACA,cAAc,CAAC,EAAE,6DAAkB;AAAA,EACnC;AAAA,KACG;AAAA,GACoD;QACjD,mBAAmB,QAAiE,MAAM;QAC3F,YAAY,eAAe,cAAc,cAAc;aACnD;AAAA,QACN;AAAA,UACC,SAAS,YAAY,SAClB,YAAY,YAAY,UACxB;AAAA,YACA,eAAe;AAAA,YACf,UAAU;AAAA;AAAA,UAEb,IAAI;AAAA,UACJ,iBAAiB;AAAA,UACjB,8BAA8B;AAAA,UAC9B,oBAAoB;AAAA,UACpB,YAAY;AAAA;AAAA;AAAA;QAIX,YAAY,aAAa;YACtB,cAAc,YAAY,eAAe;YACzC,kBAAkB,YAAY,mBAAmB;YACjD,+BAA+B,YAAY,gCAAgC;aAE1E,YAAY,OAAO,IAAI,WAAS;YAClC,iBAAiB,QAAQ;gBACtB,IAAI,sBACT,4BAA4B,sCACd,gBAAgB,SAC1B,yDAAyD,wCACzD;AAAA;cAIA,SAAS,MAAM;YACjB,OAAO,WAAW,UAAU;gBACzB,IAAI,sBAAsB;AAAA;YAG7B,YAAY,oBAAoB,UAAa,qBAAqB,QAAQ;gBACvE,IAAI,sBACT,yCAAyC;AAAA;cAKrC,aAAa,MAAM;YAErB,uBAAmE;YACnE,YAAY;cACX,OAAO,OAAO,YAAY,OAAO,cAAc,YAAY;kBACxD,IAAI,sBAAsB;AAAA;qDAGN;qBAChB,aAAa,YAA2C;gBAC9D,UAAU,YAAY;oBACnB,IAAI,sBAAsB;AAAA;gBAE7B,kCAAkC,YAAY;oBAC3C,IAAI,sBACT,gCAAgC,uDAClB,iCAAiC,SAC3C,0EACW,yDACX;AAAA;iCAIe,IAAI,UAAU,IAAI;AAAA;AAAA;eAGlC;AAAA,UACN,IAAI,OAAO,MAAM,OAAO,WAAW,MAAM,KAAK;AAAA,UAC9C,SAAS,YAAY;AAAA,UACrB;AAAA,UACA,oBAAoB,YAAY,sBAAsB;AAAA,UACtD;AAAA,UACA,YAAY;AAAA;AAAA;AAAA;UAIT,IAAI,sBAAsB;AAAA,KAC9B,CAAC,aAAa;uDAId,iBAAiB,IAAI,CAAC,OAAO,oCAC5B,8BAA8B,UAA/B;AAAA,IACC,KAAK,MAAM,MAAM;AAAA,IACjB,OAAO;AAAA,MACN,eAAe,MAAM,QAAQ;AAAA,MAC7B,oBAAoB,MAAM;AAAA,MAC1B,iBAAiB,MAAM;AAAA,MACvB,8BAA8B,MAAM;AAAA,MACpC,YAAY,MAAM;AAAA;AAAA,KAGlB,cACA,aAAa;AAAA,IACZ;AAAA,KACE,eAAe,MAAM,QAAQ,UAAU;AAAA,IACzC;AAAA,IACA;AAAA,IACA;AAAA;AAAA;;"}
|
|
@@ -3,9 +3,9 @@ import { toFileId } from "./toFileId.js";
|
|
|
3
3
|
import { assertNever } from "../utils/assertNever.js";
|
|
4
4
|
const initializeFileUploadState = () => ({
|
|
5
5
|
lastUpdateTime: 0,
|
|
6
|
-
fileIdByFile: new WeakMap(),
|
|
7
|
-
liveState: new Map(),
|
|
8
|
-
publicState: new Map(),
|
|
6
|
+
fileIdByFile: /* @__PURE__ */ new WeakMap(),
|
|
7
|
+
liveState: /* @__PURE__ */ new Map(),
|
|
8
|
+
publicState: /* @__PURE__ */ new Map(),
|
|
9
9
|
isLiveStateDirty: false
|
|
10
10
|
});
|
|
11
11
|
const fileUploadReducer = (previousState, action) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fileUploadReducer.js","sources":["../../../src/upload/fileUploadReducer.ts"],"sourcesContent":["import { FileUploadError } from '@contember/client'\nimport { assertNever } from '../utils'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport { toFileId } from './toFileId'\n\nexport const initializeFileUploadState = <Result = unknown, Metadata = undefined>(): FileUploadMultiTemporalState<\n\tResult,\n\tMetadata\n> => ({\n\tlastUpdateTime: 0,\n\tfileIdByFile: new WeakMap<File, FileId>(),\n\tliveState: new Map(),\n\tpublicState: new Map(),\n\tisLiveStateDirty: false,\n})\n\nexport const fileUploadReducer = <Result = unknown, Metadata = undefined>(\n\tpreviousState: FileUploadMultiTemporalState<Result, Metadata>,\n\taction: FileUploadAction<Result, Metadata>,\n): FileUploadMultiTemporalState<Result, Metadata> => {\n\tconst publishNewState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tpublicState: previousState.liveState,\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tlastUpdateTime: previousState.lastUpdateTime,\n\t\tisLiveStateDirty: false,\n\n\t\t// Immediately make the live state referentially unequal so that this doesn't have to happen during each action\n\t\t// several times between two publishes.\n\t\tliveState: new Map(previousState.liveState),\n\t})\n\tconst getNewDirtyState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tliveState: previousState.liveState,\n\t\tpublicState: previousState.publicState,\n\t\tisLiveStateDirty: true,\n\t\tlastUpdateTime: Date.now(),\n\t})\n\tswitch (action.type) {\n\t\tcase 'publishNewestState': {\n\t\t\treturn publishNewState()\n\t\t}\n\t\tcase 'initialize': {\n\t\t\tfor (const [fileId, metadata] of action.files) {\n\t\t\t\t// Deliberately allowing starting a new upload with the same id\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'initializing',\n\t\t\t\t\tabortController: metadata.abortController,\n\t\t\t\t\tpreviewUrl: metadata.previewUrl,\n\t\t\t\t\tfile: metadata.file,\n\t\t\t\t})\n\t\t\t\tpreviousState.fileIdByFile.set(metadata.file, fileId)\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about new files immediate*/\n\t\t}\n\t\tcase 'startUploading': {\n\t\t\tfor (const [fileOrId, metadata] of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tabortController: fileState.abortController,\n\t\t\t\t\tfile: fileState.file,\n\t\t\t\t\tmetadata: metadata.metadata!, // If the user didn't supply the metadata, then that's on them.\n\t\t\t\t\tpreviewUrl: fileState.previewUrl,\n\t\t\t\t\tprogress: undefined,\n\t\t\t\t\tuploader: metadata.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about started upload immediate*/\n\t\t}\n\t\tcase 'finishSuccessfully': {\n\t\t\tfor (const [fileOrId, result] of action.result) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'success',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tresult,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\t// return publishNewState() // Making the feedback about successful upload immediate.\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t\tcase 'finishWithError': {\n\t\t\tfor (const errorSpec of action.error) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet rawError: unknown\n\n\t\t\t\tif (Array.isArray(errorSpec)) {\n\t\t\t\t\t[fileOrId, rawError] = errorSpec\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = errorSpec\n\t\t\t\t\trawError = undefined\n\t\t\t\t}\n\n\t\t\t\tconst errors: FileUploadError[] = []\n\n\t\t\t\tif (rawError instanceof FileUploadError) {\n\t\t\t\t\terrors.push(rawError)\n\t\t\t\t} else if (Array.isArray(rawError)) {\n\t\t\t\t\tfor (const error of rawError) {\n\t\t\t\t\t\tif (error instanceof FileUploadError) {\n\t\t\t\t\t\t\terrors.push(error)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (\n\t\t\t\t\tpreviousFileState === undefined ||\n\t\t\t\t\t(previousFileState.readyState !== 'initializing' && previousFileState.readyState !== 'uploading')\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst uploader = previousFileState.readyState === 'uploading' ? previousFileState.uploader : undefined\n\t\t\t\tconst metadata = previousFileState.readyState === 'uploading' ? previousFileState.metadata : undefined\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'error',\n\t\t\t\t\terrors: errors.length ? errors : undefined,\n\t\t\t\t\trawError,\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tuploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState() // Bad news can wait.\n\t\t}\n\t\tcase 'purge': {\n\t\t\tlet atLeastOnePurged = false\n\t\t\tfor (const fileOrId of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tatLeastOnePurged = true\n\t\t\t\tif (fileState.readyState === 'initializing' || fileState.readyState === 'uploading') {\n\t\t\t\t\tfileState.abortController.abort() // This is a bit naughty… We shouldn't do this from here.\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.delete(fileId)\n\t\t\t}\n\t\t\tif (atLeastOnePurged) {\n\t\t\t\treturn getNewDirtyState()\n\t\t\t}\n\t\t\treturn previousState\n\t\t}\n\t\tcase 'updateUploadProgress': {\n\t\t\tfor (const [fileOrId, { progress }] of action.progress) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (progress === undefined || previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t\tabortController: previousFileState.abortController,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tprogress,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t}\n\tassertNever(action)\n}\n"],"names":[],"mappings":";;;MAOa,4BAA4B;EAIxC,gBAAgB;AAAA,EAChB,
|
|
1
|
+
{"version":3,"file":"fileUploadReducer.js","sources":["../../../src/upload/fileUploadReducer.ts"],"sourcesContent":["import { FileUploadError } from '@contember/client'\nimport { assertNever } from '../utils'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport { toFileId } from './toFileId'\n\nexport const initializeFileUploadState = <Result = unknown, Metadata = undefined>(): FileUploadMultiTemporalState<\n\tResult,\n\tMetadata\n> => ({\n\tlastUpdateTime: 0,\n\tfileIdByFile: new WeakMap<File, FileId>(),\n\tliveState: new Map(),\n\tpublicState: new Map(),\n\tisLiveStateDirty: false,\n})\n\nexport const fileUploadReducer = <Result = unknown, Metadata = undefined>(\n\tpreviousState: FileUploadMultiTemporalState<Result, Metadata>,\n\taction: FileUploadAction<Result, Metadata>,\n): FileUploadMultiTemporalState<Result, Metadata> => {\n\tconst publishNewState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tpublicState: previousState.liveState,\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tlastUpdateTime: previousState.lastUpdateTime,\n\t\tisLiveStateDirty: false,\n\n\t\t// Immediately make the live state referentially unequal so that this doesn't have to happen during each action\n\t\t// several times between two publishes.\n\t\tliveState: new Map(previousState.liveState),\n\t})\n\tconst getNewDirtyState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tliveState: previousState.liveState,\n\t\tpublicState: previousState.publicState,\n\t\tisLiveStateDirty: true,\n\t\tlastUpdateTime: Date.now(),\n\t})\n\tswitch (action.type) {\n\t\tcase 'publishNewestState': {\n\t\t\treturn publishNewState()\n\t\t}\n\t\tcase 'initialize': {\n\t\t\tfor (const [fileId, metadata] of action.files) {\n\t\t\t\t// Deliberately allowing starting a new upload with the same id\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'initializing',\n\t\t\t\t\tabortController: metadata.abortController,\n\t\t\t\t\tpreviewUrl: metadata.previewUrl,\n\t\t\t\t\tfile: metadata.file,\n\t\t\t\t})\n\t\t\t\tpreviousState.fileIdByFile.set(metadata.file, fileId)\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about new files immediate*/\n\t\t}\n\t\tcase 'startUploading': {\n\t\t\tfor (const [fileOrId, metadata] of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tabortController: fileState.abortController,\n\t\t\t\t\tfile: fileState.file,\n\t\t\t\t\tmetadata: metadata.metadata!, // If the user didn't supply the metadata, then that's on them.\n\t\t\t\t\tpreviewUrl: fileState.previewUrl,\n\t\t\t\t\tprogress: undefined,\n\t\t\t\t\tuploader: metadata.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about started upload immediate*/\n\t\t}\n\t\tcase 'finishSuccessfully': {\n\t\t\tfor (const [fileOrId, result] of action.result) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'success',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tresult,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\t// return publishNewState() // Making the feedback about successful upload immediate.\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t\tcase 'finishWithError': {\n\t\t\tfor (const errorSpec of action.error) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet rawError: unknown\n\n\t\t\t\tif (Array.isArray(errorSpec)) {\n\t\t\t\t\t[fileOrId, rawError] = errorSpec\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = errorSpec\n\t\t\t\t\trawError = undefined\n\t\t\t\t}\n\n\t\t\t\tconst errors: FileUploadError[] = []\n\n\t\t\t\tif (rawError instanceof FileUploadError) {\n\t\t\t\t\terrors.push(rawError)\n\t\t\t\t} else if (Array.isArray(rawError)) {\n\t\t\t\t\tfor (const error of rawError) {\n\t\t\t\t\t\tif (error instanceof FileUploadError) {\n\t\t\t\t\t\t\terrors.push(error)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (\n\t\t\t\t\tpreviousFileState === undefined ||\n\t\t\t\t\t(previousFileState.readyState !== 'initializing' && previousFileState.readyState !== 'uploading')\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst uploader = previousFileState.readyState === 'uploading' ? previousFileState.uploader : undefined\n\t\t\t\tconst metadata = previousFileState.readyState === 'uploading' ? previousFileState.metadata : undefined\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'error',\n\t\t\t\t\terrors: errors.length ? errors : undefined,\n\t\t\t\t\trawError,\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tuploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState() // Bad news can wait.\n\t\t}\n\t\tcase 'purge': {\n\t\t\tlet atLeastOnePurged = false\n\t\t\tfor (const fileOrId of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tatLeastOnePurged = true\n\t\t\t\tif (fileState.readyState === 'initializing' || fileState.readyState === 'uploading') {\n\t\t\t\t\tfileState.abortController.abort() // This is a bit naughty… We shouldn't do this from here.\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.delete(fileId)\n\t\t\t}\n\t\t\tif (atLeastOnePurged) {\n\t\t\t\treturn getNewDirtyState()\n\t\t\t}\n\t\t\treturn previousState\n\t\t}\n\t\tcase 'updateUploadProgress': {\n\t\t\tfor (const [fileOrId, { progress }] of action.progress) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (progress === undefined || previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t\tabortController: previousFileState.abortController,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tprogress,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t}\n\tassertNever(action)\n}\n"],"names":[],"mappings":";;;MAOa,4BAA4B;EAIxC,gBAAgB;AAAA,EAChB,kCAAkB;AAAA,EAClB,+BAAe;AAAA,EACf,iCAAiB;AAAA,EACjB,kBAAkB;AAAA;MAGN,oBAAoB,CAChC,eACA,WACoD;QAC9C,kBAAkB;IACvB,aAAa,cAAc;AAAA,IAC3B,cAAc,cAAc;AAAA,IAC5B,gBAAgB,cAAc;AAAA,IAC9B,kBAAkB;AAAA,IAIlB,WAAW,IAAI,IAAI,cAAc;AAAA;QAE5B,mBAAmB;IACxB,cAAc,cAAc;AAAA,IAC5B,WAAW,cAAc;AAAA,IACzB,aAAa,cAAc;AAAA,IAC3B,kBAAkB;AAAA,IAClB,gBAAgB,KAAK;AAAA;UAEd,OAAO;AAAA,SACT,sBAAsB;aACnB;AAAA;AAAA,SAEH,cAAc;iBACP,CAAC,QAAQ,aAAa,OAAO,OAAO;sBAGhC,UAAU,IAAI,QAAQ;AAAA,UACnC,YAAY;AAAA,UACZ,iBAAiB,SAAS;AAAA,UAC1B,YAAY,SAAS;AAAA,UACrB,MAAM,SAAS;AAAA;sBAEF,aAAa,IAAI,SAAS,MAAM;AAAA;aAExC;AAAA;AAAA,SAEH,kBAAkB;iBACX,CAAC,UAAU,aAAa,OA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Q,OAAO,SAAS,SAAS;AAAA,UACjC;AAAA,UACA,MAAM,kBAAkB;AAAA,UACxB;AAAA,UACA,YAAY,kBAAkB;AAAA,UAC9B;AAAA;AAAA;aAGK;AAAA;AAAA,SAEH,SAAS;UACT,mBAAmB;iBACZ,YAAY,OAAO,OAAO;cAC9B,SAAS,SAAS,eAAe;cACjC,YAAY,cAAc,UAAU,IAAI;YAE1C,cAAc,QAAW;;;2BAGV;YACf,UAAU,eAAe,kBAAkB,UAAU,eAAe,aAAa;oBAC1E,gBAAgB;AAAA;sBAEb,UAAU,OAAO;AAAA;UAE5B,kBAAkB;eACd;AAAA;aAED;AAAA;AAAA,SAEH,wBAAwB;iBACjB,CAAC,UAAU,EAAE,eAAe,OAAO,UAAU;cACjD,SAAS,SAAS,eAAe;cACjC,oBAAoB,cAAc,UAAU,IAAI;YAClD,aAAa,UAAa,sBAAsB,UAAa,kBAAkB,eAAe,aAAa;;;sBAGjG,UAAU,IAAI,QAAQ;AAAA,UACnC,YAAY;AAAA,UACZ,MAAM,kBAAkB;AAAA,UACxB,UAAU,kBAAkB;AAAA,UAC5B,iBAAiB,kBAAkB;AAAA,UACnC,YAAY,kBAAkB;AAAA,UAC9B,UAAU,kBAAkB;AAAA,UAC5B;AAAA;AAAA;aAGK;AAAA;AAAA;;;;"}
|
|
@@ -28,8 +28,8 @@ const useFileUpload = (options) => {
|
|
|
28
28
|
});
|
|
29
29
|
}, []);
|
|
30
30
|
const initializeUpload = useCallback((files) => {
|
|
31
|
-
const newFileIds = new Set();
|
|
32
|
-
const fileWithMetadataByFileConfig = new Map();
|
|
31
|
+
const newFileIds = /* @__PURE__ */ new Set();
|
|
32
|
+
const fileWithMetadataByFileConfig = /* @__PURE__ */ new Map();
|
|
33
33
|
for (const fileMaybeWithId of files) {
|
|
34
34
|
const abortController = new AbortController();
|
|
35
35
|
let fileId;
|
|
@@ -68,8 +68,8 @@ const useFileUpload = (options) => {
|
|
|
68
68
|
return fileWithMetadataByFileConfig;
|
|
69
69
|
}, [purgeUpload]);
|
|
70
70
|
const startUpload = useCallback((files) => {
|
|
71
|
-
const filesById = new Map();
|
|
72
|
-
const groupedByUploader = new Map();
|
|
71
|
+
const filesById = /* @__PURE__ */ new Map();
|
|
72
|
+
const groupedByUploader = /* @__PURE__ */ new Map();
|
|
73
73
|
const currentState = multiTemporalStateRef.current;
|
|
74
74
|
for (const fileOrIdMaybeWithOptions of files) {
|
|
75
75
|
let fileOrId;
|
|
@@ -93,7 +93,7 @@ const useFileUpload = (options) => {
|
|
|
93
93
|
});
|
|
94
94
|
let filesWithSameUploader = groupedByUploader.get(uploader);
|
|
95
95
|
if (filesWithSameUploader === void 0) {
|
|
96
|
-
groupedByUploader.set(uploader, filesWithSameUploader = new Map());
|
|
96
|
+
groupedByUploader.set(uploader, filesWithSameUploader = /* @__PURE__ */ new Map());
|
|
97
97
|
}
|
|
98
98
|
filesWithSameUploader.set(fileState.file, {
|
|
99
99
|
abortSignal: fileState.abortController.signal
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFileUpload.js","sources":["../../../src/upload/useFileUpload.ts"],"sourcesContent":["import { FileUploader, S3FileUploader, UploadedFileMetadata } from '@contember/client'\nimport { Reducer, useCallback, useEffect, useMemo, useReducer, useRef } from 'react'\nimport { useCurrentContentGraphQlClient } from '../content'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadCompoundState } from './FileUploadCompoundState'\nimport type { FileUploadMetadata } from './FileUploadMetadata'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport type {\n\tPurgeUpload,\n\tFileUploadOperations,\n\tInitializeUpload,\n\tStartUpload,\n\tStartUploadFileOptions,\n\tFailUpload,\n} from './FileUploadOperations'\nimport { fileUploadReducer, initializeFileUploadState } from './fileUploadReducer'\nimport type { FileWithMetadata } from './FileWithMetadata'\nimport { toFileId } from './toFileId'\n\nexport type FileUpload<Result = unknown, Metadata = undefined> = [\n\tFileUploadCompoundState<Result, Metadata>,\n\tFileUploadOperations<Metadata>,\n]\n\nexport interface FileUploadOptions {\n\tmaxUpdateFrequency?: number // This does NOT apply to all kinds of updates.\n}\n\nexport const useFileUpload = <Result = unknown, Metadata = undefined>(\n\toptions?: FileUploadOptions,\n): FileUpload<Result, Metadata> => {\n\tconst maxUpdateFrequency = options?.maxUpdateFrequency ?? 100\n\n\tconst contentApiClient = useCurrentContentGraphQlClient()\n\n\tconst updateTimeoutRef = useRef<number | undefined>(undefined)\n\tconst isFirstRenderRef = useRef(true)\n\tconst fileIdSeedRef = useRef(1)\n\n\tconst [multiTemporalState, dispatch] = useReducer<\n\t\tReducer<FileUploadMultiTemporalState<Result, Metadata>, FileUploadAction<Result, Metadata>>,\n\t\tundefined\n\t>(fileUploadReducer, undefined, initializeFileUploadState)\n\tconst multiTemporalStateRef = useRef(multiTemporalState)\n\n\tconst defaultUploader = useMemo(() => new S3FileUploader(), [])\n\n\tuseEffect(() => {\n\t\tmultiTemporalStateRef.current = multiTemporalState\n\t})\n\n\tconst purgeUpload = useCallback<PurgeUpload>(files => {\n\t\tdispatch({\n\t\t\ttype: 'purge',\n\t\t\tfiles,\n\t\t})\n\t}, [])\n\tconst failUpload = useCallback<FailUpload>(error => {\n\t\tdispatch({\n\t\t\ttype: 'finishWithError',\n\t\t\terror,\n\t\t})\n\t}, [])\n\tconst initializeUpload = useCallback<InitializeUpload>(\n\t\tfiles => {\n\t\t\tconst newFileIds = new Set<FileId>()\n\t\t\tconst fileWithMetadataByFileConfig = new Map<FileId, FileWithMetadata>()\n\n\t\t\tfor (const fileMaybeWithId of files) {\n\t\t\t\tconst abortController = new AbortController()\n\n\t\t\t\tlet fileId: FileId\n\t\t\t\tlet file: File\n\n\t\t\t\tif (fileMaybeWithId instanceof File) {\n\t\t\t\t\tfileId = `__contember__file-${fileIdSeedRef.current++}`\n\t\t\t\t\tfile = fileMaybeWithId\n\t\t\t\t} else {\n\t\t\t\t\tfileId = fileMaybeWithId[0]\n\t\t\t\t\tfile = fileMaybeWithId[1]\n\t\t\t\t}\n\n\t\t\t\tabortController.signal.addEventListener('abort', () => {\n\t\t\t\t\tpurgeUpload([fileId])\n\t\t\t\t})\n\n\t\t\t\tfileWithMetadataByFileConfig.set(fileId, {\n\t\t\t\t\tpreviewUrl: URL.createObjectURL(file),\n\t\t\t\t\tabortController,\n\t\t\t\t\tfile,\n\t\t\t\t\tfileId,\n\t\t\t\t})\n\n\t\t\t\tnewFileIds.add(fileId)\n\t\t\t}\n\t\t\tif (fileWithMetadataByFileConfig.size === 0) {\n\t\t\t\treturn fileWithMetadataByFileConfig\n\t\t\t}\n\n\t\t\tif (newFileIds.size) {\n\t\t\t\tdispatch({\n\t\t\t\t\ttype: 'purge',\n\t\t\t\t\tfiles: newFileIds,\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tdispatch({\n\t\t\t\ttype: 'initialize',\n\t\t\t\tfiles: fileWithMetadataByFileConfig,\n\t\t\t})\n\n\t\t\treturn fileWithMetadataByFileConfig\n\t\t},\n\t\t[purgeUpload],\n\t)\n\tconst startUpload = useCallback<StartUpload<Metadata>>(\n\t\tfiles => {\n\t\t\tconst filesById = new Map<FileId, FileUploadMetadata<Metadata>>()\n\t\t\tconst groupedByUploader: Map<FileUploader, Map<File, UploadedFileMetadata>> = new Map()\n\n\t\t\tconst currentState = multiTemporalStateRef.current\n\n\t\t\tfor (const fileOrIdMaybeWithOptions of files) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet options: StartUploadFileOptions<Metadata>\n\n\t\t\t\tif (Array.isArray(fileOrIdMaybeWithOptions)) {\n\t\t\t\t\tfileOrId = fileOrIdMaybeWithOptions[0]\n\t\t\t\t\toptions = fileOrIdMaybeWithOptions[1]\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = fileOrIdMaybeWithOptions\n\t\t\t\t\toptions = {}\n\t\t\t\t}\n\t\t\t\tconst { uploader = defaultUploader, metadata } = options\n\n\t\t\t\tconst fileId = toFileId(currentState, fileOrId)\n\t\t\t\tconst fileState = currentState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Trying to startUpload a file that hasn't been previously initialized. ` +\n\t\t\t\t\t\t\t`This will be possible in future, but isn't yet.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\tfilesById.set(fileId, {\n\t\t\t\t\tuploader,\n\t\t\t\t\tmetadata,\n\t\t\t\t})\n\n\t\t\t\tlet filesWithSameUploader = groupedByUploader.get(uploader)\n\t\t\t\tif (filesWithSameUploader === undefined) {\n\t\t\t\t\tgroupedByUploader.set(uploader, (filesWithSameUploader = new Map()))\n\t\t\t\t}\n\t\t\t\tfilesWithSameUploader.set(fileState.file, {\n\t\t\t\t\tabortSignal: fileState.abortController.signal,\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tdispatch({\n\t\t\t\ttype: 'startUploading',\n\t\t\t\tfiles: filesById,\n\t\t\t})\n\n\t\t\tfor (const [uploader, filesForUpload] of groupedByUploader) {\n\t\t\t\ttry {\n\t\t\t\t\tuploader.upload(filesForUpload, {\n\t\t\t\t\t\tonProgress: progress => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'updateUploadProgress',\n\t\t\t\t\t\t\t\tprogress,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonSuccess: result => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'finishSuccessfully',\n\t\t\t\t\t\t\t\tresult,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonError: error => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'finishWithError',\n\t\t\t\t\t\t\t\terror,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tcontentApiClient,\n\t\t\t\t\t})\n\t\t\t\t} catch (error) {\n\t\t\t\t\tdispatch({\n\t\t\t\t\t\ttype: 'finishWithError',\n\t\t\t\t\t\terror: Array.from(filesForUpload).map(([file]) => [file, error]),\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t[contentApiClient, defaultUploader],\n\t)\n\n\tconst operations = useMemo<FileUploadOperations<Metadata>>(\n\t\t() => ({\n\t\t\tfailUpload,\n\t\t\tinitializeUpload,\n\t\t\tpurgeUpload,\n\t\t\tstartUpload,\n\t\t}),\n\t\t[failUpload, initializeUpload, purgeUpload, startUpload],\n\t)\n\n\tuseEffect(() => {\n\t\tif (isFirstRenderRef.current) {\n\t\t\treturn\n\t\t}\n\t\tif (multiTemporalState.isLiveStateDirty) {\n\t\t\tconst now = Date.now()\n\t\t\tconst timeDelta = Math.max(now - multiTemporalState.lastUpdateTime, 0) // The max is just a sanity check\n\t\t\tif (timeDelta > maxUpdateFrequency) {\n\t\t\t\tif (updateTimeoutRef.current !== undefined) {\n\t\t\t\t\tclearTimeout(updateTimeoutRef.current)\n\t\t\t\t}\n\t\t\t\tdispatch({\n\t\t\t\t\ttype: 'publishNewestState',\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tif (updateTimeoutRef.current !== undefined) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tupdateTimeoutRef.current = window.setTimeout(() => {\n\t\t\t\t\tdispatch({\n\t\t\t\t\t\ttype: 'publishNewestState',\n\t\t\t\t\t})\n\t\t\t\t\tupdateTimeoutRef.current = undefined\n\t\t\t\t}, maxUpdateFrequency - timeDelta)\n\t\t\t}\n\t\t}\n\t}, [\n\t\tmultiTemporalState.isLiveStateDirty,\n\t\tmultiTemporalState.lastUpdateTime,\n\t\tmultiTemporalState.liveState,\n\t\tmultiTemporalState.publicState,\n\t\tmaxUpdateFrequency,\n\t])\n\n\tuseEffect(\n\t\t() => () => {\n\t\t\tfor (const [, state] of multiTemporalStateRef.current.liveState) {\n\t\t\t\tURL.revokeObjectURL(state.previewUrl)\n\t\t\t}\n\t\t\tfor (const [, state] of multiTemporalStateRef.current.publicState) {\n\t\t\t\tURL.revokeObjectURL(state.previewUrl)\n\t\t\t}\n\t\t},\n\t\t[],\n\t)\n\n\t// For this to work, this effect must be the last one to run.\n\tuseEffect(() => {\n\t\tisFirstRenderRef.current = false\n\t}, [])\n\n\treturn [multiTemporalState.publicState, operations]\n}\n"],"names":[],"mappings":";;;;;MA6Ba,gBAAgB,CAC5B,YACkC;QAC5B,qBAAqB,SAAS,sBAAsB;QAEpD,mBAAmB;QAEnB,mBAAmB,OAA2B;QAC9C,mBAAmB,OAAO;QAC1B,gBAAgB,OAAO;QAEvB,CAAC,oBAAoB,YAAY,WAGrC,mBAAmB,QAAW;QAC1B,wBAAwB,OAAO;QAE/B,kBAAkB,QAAQ,MAAM,IAAI,kBAAkB;YAElD,MAAM;0BACO,UAAU;AAAA;QAG3B,cAAc,YAAyB,WAAS;aAC5C;AAAA,MACR,MAAM;AAAA,MACN;AAAA;AAAA,KAEC;QACG,aAAa,YAAwB,WAAS;aAC1C;AAAA,MACR,MAAM;AAAA,MACN;AAAA;AAAA,KAEC;QACG,mBAAmB,YACxB,WAAS;UACF,aAAa,IAAI;UACjB,+BAA+B,IAAI;eAE9B,mBAAmB,OAAO;YAC9B,kBAAkB,IAAI;UAExB;UACA;UAEA,2BAA2B,MAAM;iBAC3B,qBAAqB,cAAc;eACrC;AAAA,aACD;iBACG,gBAAgB;eAClB,gBAAgB;AAAA;sBAGR,OAAO,iBAAiB,SAAS,MAAM;oBAC1C,CAAC;AAAA;mCAGe,IAAI,QAAQ;AAAA,QACxC,YAAY,IAAI,gBAAgB;AAAA,QAChC;AAAA,QACA;AAAA,QACA;AAAA;iBAGU,IAAI;AAAA;QAEZ,6BAA6B,SAAS,GAAG;aACrC;AAAA;QAGJ,WAAW,MAAM;eACX;AAAA,QACR,MAAM;AAAA,QACN,OAAO;AAAA;AAAA;aAIA;AAAA,MACR,MAAM;AAAA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B,mBAAmB,kBAAkB;YAClC,MAAM,KAAK;YACX,YAAY,KAAK,IAAI,MAAM,mBAAmB,gBAAgB;UAChE,YAAY,oBAAoB;YAC/B,iBAAiB,YAAY,QAAW;uBAC9B,iBAAiB;AAAA;iBAEtB;AAAA,UACR,MAAM;AAAA;AAAA,aAED;YACF,iBAAiB,YAAY,QAAW;;;yBAG3B,UAAU,OAAO,WAAW,MAAM;mBACzC;AAAA,YACR,MAAM;AAAA;2BAEU,UAAU;AAAA,WACzB,qBAAqB;AAAA;AAAA;AAAA,KAGxB;AAAA,IACF,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB;AAAA;YAIA,MAAM,MAAM;eACA,GAAG,UAAU,sBAAsB,QAAQ,WAAW;UAC5D,gBAAgB,MAAM;AAAA;eAEhB,GAAG,UAAU,sBAAsB,QAAQ,aAAa;UAC9D,gBAAgB,MAAM;AAAA;AAAA,KAG5B;YAIS,MAAM;qBACE,UAAU;AAAA,KACzB;SAEI,CAAC,mBAAmB,aAAa;AAAA;;"}
|
|
1
|
+
{"version":3,"file":"useFileUpload.js","sources":["../../../src/upload/useFileUpload.ts"],"sourcesContent":["import { FileUploader, S3FileUploader, UploadedFileMetadata } from '@contember/client'\nimport { Reducer, useCallback, useEffect, useMemo, useReducer, useRef } from 'react'\nimport { useCurrentContentGraphQlClient } from '../content'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadCompoundState } from './FileUploadCompoundState'\nimport type { FileUploadMetadata } from './FileUploadMetadata'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport type {\n\tPurgeUpload,\n\tFileUploadOperations,\n\tInitializeUpload,\n\tStartUpload,\n\tStartUploadFileOptions,\n\tFailUpload,\n} from './FileUploadOperations'\nimport { fileUploadReducer, initializeFileUploadState } from './fileUploadReducer'\nimport type { FileWithMetadata } from './FileWithMetadata'\nimport { toFileId } from './toFileId'\n\nexport type FileUpload<Result = unknown, Metadata = undefined> = [\n\tFileUploadCompoundState<Result, Metadata>,\n\tFileUploadOperations<Metadata>,\n]\n\nexport interface FileUploadOptions {\n\tmaxUpdateFrequency?: number // This does NOT apply to all kinds of updates.\n}\n\nexport const useFileUpload = <Result = unknown, Metadata = undefined>(\n\toptions?: FileUploadOptions,\n): FileUpload<Result, Metadata> => {\n\tconst maxUpdateFrequency = options?.maxUpdateFrequency ?? 100\n\n\tconst contentApiClient = useCurrentContentGraphQlClient()\n\n\tconst updateTimeoutRef = useRef<number | undefined>(undefined)\n\tconst isFirstRenderRef = useRef(true)\n\tconst fileIdSeedRef = useRef(1)\n\n\tconst [multiTemporalState, dispatch] = useReducer<\n\t\tReducer<FileUploadMultiTemporalState<Result, Metadata>, FileUploadAction<Result, Metadata>>,\n\t\tundefined\n\t>(fileUploadReducer, undefined, initializeFileUploadState)\n\tconst multiTemporalStateRef = useRef(multiTemporalState)\n\n\tconst defaultUploader = useMemo(() => new S3FileUploader(), [])\n\n\tuseEffect(() => {\n\t\tmultiTemporalStateRef.current = multiTemporalState\n\t})\n\n\tconst purgeUpload = useCallback<PurgeUpload>(files => {\n\t\tdispatch({\n\t\t\ttype: 'purge',\n\t\t\tfiles,\n\t\t})\n\t}, [])\n\tconst failUpload = useCallback<FailUpload>(error => {\n\t\tdispatch({\n\t\t\ttype: 'finishWithError',\n\t\t\terror,\n\t\t})\n\t}, [])\n\tconst initializeUpload = useCallback<InitializeUpload>(\n\t\tfiles => {\n\t\t\tconst newFileIds = new Set<FileId>()\n\t\t\tconst fileWithMetadataByFileConfig = new Map<FileId, FileWithMetadata>()\n\n\t\t\tfor (const fileMaybeWithId of files) {\n\t\t\t\tconst abortController = new AbortController()\n\n\t\t\t\tlet fileId: FileId\n\t\t\t\tlet file: File\n\n\t\t\t\tif (fileMaybeWithId instanceof File) {\n\t\t\t\t\tfileId = `__contember__file-${fileIdSeedRef.current++}`\n\t\t\t\t\tfile = fileMaybeWithId\n\t\t\t\t} else {\n\t\t\t\t\tfileId = fileMaybeWithId[0]\n\t\t\t\t\tfile = fileMaybeWithId[1]\n\t\t\t\t}\n\n\t\t\t\tabortController.signal.addEventListener('abort', () => {\n\t\t\t\t\tpurgeUpload([fileId])\n\t\t\t\t})\n\n\t\t\t\tfileWithMetadataByFileConfig.set(fileId, {\n\t\t\t\t\tpreviewUrl: URL.createObjectURL(file),\n\t\t\t\t\tabortController,\n\t\t\t\t\tfile,\n\t\t\t\t\tfileId,\n\t\t\t\t})\n\n\t\t\t\tnewFileIds.add(fileId)\n\t\t\t}\n\t\t\tif (fileWithMetadataByFileConfig.size === 0) {\n\t\t\t\treturn fileWithMetadataByFileConfig\n\t\t\t}\n\n\t\t\tif (newFileIds.size) {\n\t\t\t\tdispatch({\n\t\t\t\t\ttype: 'purge',\n\t\t\t\t\tfiles: newFileIds,\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tdispatch({\n\t\t\t\ttype: 'initialize',\n\t\t\t\tfiles: fileWithMetadataByFileConfig,\n\t\t\t})\n\n\t\t\treturn fileWithMetadataByFileConfig\n\t\t},\n\t\t[purgeUpload],\n\t)\n\tconst startUpload = useCallback<StartUpload<Metadata>>(\n\t\tfiles => {\n\t\t\tconst filesById = new Map<FileId, FileUploadMetadata<Metadata>>()\n\t\t\tconst groupedByUploader: Map<FileUploader, Map<File, UploadedFileMetadata>> = new Map()\n\n\t\t\tconst currentState = multiTemporalStateRef.current\n\n\t\t\tfor (const fileOrIdMaybeWithOptions of files) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet options: StartUploadFileOptions<Metadata>\n\n\t\t\t\tif (Array.isArray(fileOrIdMaybeWithOptions)) {\n\t\t\t\t\tfileOrId = fileOrIdMaybeWithOptions[0]\n\t\t\t\t\toptions = fileOrIdMaybeWithOptions[1]\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = fileOrIdMaybeWithOptions\n\t\t\t\t\toptions = {}\n\t\t\t\t}\n\t\t\t\tconst { uploader = defaultUploader, metadata } = options\n\n\t\t\t\tconst fileId = toFileId(currentState, fileOrId)\n\t\t\t\tconst fileState = currentState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Trying to startUpload a file that hasn't been previously initialized. ` +\n\t\t\t\t\t\t\t`This will be possible in future, but isn't yet.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\tfilesById.set(fileId, {\n\t\t\t\t\tuploader,\n\t\t\t\t\tmetadata,\n\t\t\t\t})\n\n\t\t\t\tlet filesWithSameUploader = groupedByUploader.get(uploader)\n\t\t\t\tif (filesWithSameUploader === undefined) {\n\t\t\t\t\tgroupedByUploader.set(uploader, (filesWithSameUploader = new Map()))\n\t\t\t\t}\n\t\t\t\tfilesWithSameUploader.set(fileState.file, {\n\t\t\t\t\tabortSignal: fileState.abortController.signal,\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tdispatch({\n\t\t\t\ttype: 'startUploading',\n\t\t\t\tfiles: filesById,\n\t\t\t})\n\n\t\t\tfor (const [uploader, filesForUpload] of groupedByUploader) {\n\t\t\t\ttry {\n\t\t\t\t\tuploader.upload(filesForUpload, {\n\t\t\t\t\t\tonProgress: progress => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'updateUploadProgress',\n\t\t\t\t\t\t\t\tprogress,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonSuccess: result => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'finishSuccessfully',\n\t\t\t\t\t\t\t\tresult,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonError: error => {\n\t\t\t\t\t\t\tdispatch({\n\t\t\t\t\t\t\t\ttype: 'finishWithError',\n\t\t\t\t\t\t\t\terror,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t},\n\t\t\t\t\t\tcontentApiClient,\n\t\t\t\t\t})\n\t\t\t\t} catch (error) {\n\t\t\t\t\tdispatch({\n\t\t\t\t\t\ttype: 'finishWithError',\n\t\t\t\t\t\terror: Array.from(filesForUpload).map(([file]) => [file, error]),\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t[contentApiClient, defaultUploader],\n\t)\n\n\tconst operations = useMemo<FileUploadOperations<Metadata>>(\n\t\t() => ({\n\t\t\tfailUpload,\n\t\t\tinitializeUpload,\n\t\t\tpurgeUpload,\n\t\t\tstartUpload,\n\t\t}),\n\t\t[failUpload, initializeUpload, purgeUpload, startUpload],\n\t)\n\n\tuseEffect(() => {\n\t\tif (isFirstRenderRef.current) {\n\t\t\treturn\n\t\t}\n\t\tif (multiTemporalState.isLiveStateDirty) {\n\t\t\tconst now = Date.now()\n\t\t\tconst timeDelta = Math.max(now - multiTemporalState.lastUpdateTime, 0) // The max is just a sanity check\n\t\t\tif (timeDelta > maxUpdateFrequency) {\n\t\t\t\tif (updateTimeoutRef.current !== undefined) {\n\t\t\t\t\tclearTimeout(updateTimeoutRef.current)\n\t\t\t\t}\n\t\t\t\tdispatch({\n\t\t\t\t\ttype: 'publishNewestState',\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tif (updateTimeoutRef.current !== undefined) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tupdateTimeoutRef.current = window.setTimeout(() => {\n\t\t\t\t\tdispatch({\n\t\t\t\t\t\ttype: 'publishNewestState',\n\t\t\t\t\t})\n\t\t\t\t\tupdateTimeoutRef.current = undefined\n\t\t\t\t}, maxUpdateFrequency - timeDelta)\n\t\t\t}\n\t\t}\n\t}, [\n\t\tmultiTemporalState.isLiveStateDirty,\n\t\tmultiTemporalState.lastUpdateTime,\n\t\tmultiTemporalState.liveState,\n\t\tmultiTemporalState.publicState,\n\t\tmaxUpdateFrequency,\n\t])\n\n\tuseEffect(\n\t\t() => () => {\n\t\t\tfor (const [, state] of multiTemporalStateRef.current.liveState) {\n\t\t\t\tURL.revokeObjectURL(state.previewUrl)\n\t\t\t}\n\t\t\tfor (const [, state] of multiTemporalStateRef.current.publicState) {\n\t\t\t\tURL.revokeObjectURL(state.previewUrl)\n\t\t\t}\n\t\t},\n\t\t[],\n\t)\n\n\t// For this to work, this effect must be the last one to run.\n\tuseEffect(() => {\n\t\tisFirstRenderRef.current = false\n\t}, [])\n\n\treturn [multiTemporalState.publicState, operations]\n}\n"],"names":[],"mappings":";;;;;MA6Ba,gBAAgB,CAC5B,YACkC;QAC5B,qBAAqB,SAAS,sBAAsB;QAEpD,mBAAmB;QAEnB,mBAAmB,OAA2B;QAC9C,mBAAmB,OAAO;QAC1B,gBAAgB,OAAO;QAEvB,CAAC,oBAAoB,YAAY,WAGrC,mBAAmB,QAAW;QAC1B,wBAAwB,OAAO;QAE/B,kBAAkB,QAAQ,MAAM,IAAI,kBAAkB;YAElD,MAAM;0BACO,UAAU;AAAA;QAG3B,cAAc,YAAyB,WAAS;aAC5C;AAAA,MACR,MAAM;AAAA,MACN;AAAA;AAAA,KAEC;QACG,aAAa,YAAwB,WAAS;aAC1C;AAAA,MACR,MAAM;AAAA,MACN;AAAA;AAAA,KAEC;QACG,mBAAmB,YACxB,WAAS;UACF,iCAAiB;UACjB,mDAAmC;eAE9B,mBAAmB,OAAO;YAC9B,kBAAkB,IAAI;UAExB;UACA;UAEA,2BAA2B,MAAM;iBAC3B,qBAAqB,cAAc;eACrC;AAAA,aACD;iBACG,gBAAgB;eAClB,gBAAgB;AAAA;sBAGR,OAAO,iBAAiB,SAAS,MAAM;oBAC1C,CAAC;AAAA;mCAGe,IAAI,QAAQ;AAAA,QACxC,YAAY,IAAI,gBAAgB;AAAA,QAChC;AAAA,QACA;AAAA,QACA;AAAA;iBAGU,IAAI;AAAA;QAEZ,6BAA6B,SAAS,GAAG;aACrC;AAAA;QAGJ,WAAW,MAAM;eACX;AAAA,QACR,MAAM;AAAA,QACN,OA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;QACX,iBAAiB,SAAS;;;QAG1B,mBAAmB,kBAAkB;YAClC,MAAM,KAAK;YACX,YAAY,KAAK,IAAI,MAAM,mBAAmB,gBAAgB;UAChE,YAAY,oBAAoB;YAC/B,iBAAiB,YAAY,QAAW;uBAC9B,iBAAiB;AAAA;iBAEtB;AAAA,UACR,MAAM;AAAA;AAAA,aAED;YACF,iBAAiB,YAAY,QAAW;;;yBAG3B,UAAU,OAAO,WAAW,MAAM;mBACzC;AAAA,YACR,MAAM;AAAA;2BAEU,UAAU;AAAA,WACzB,qBAAqB;AAAA;AAAA;AAAA,KAGxB;AAAA,IACF,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB,mBAAmB;AAAA,IACnB;AAAA;YAIA,MAAM,MAAM;eACA,GAAG,UAAU,sBAAsB,QAAQ,WAAW;UAC5D,gBAAgB,MAAM;AAAA;eAEhB,GAAG,UAAU,sBAAsB,QAAQ,aAAa;UAC9D,gBAAgB,MAAM;AAAA;AAAA,KAG5B;YAIS,MAAM;qBACE,UAAU;AAAA,KACzB;SAEI,CAAC,mBAAmB,aAAa;AAAA;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClientError.d.ts","sourceRoot":"","sources":["../../src/ClientError.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAY,SAAQ,KAAK;CAAG"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export interface ContemberClientProps {
|
|
3
|
+
apiBaseUrl: string;
|
|
4
|
+
sessionToken?: string;
|
|
5
|
+
loginToken?: string;
|
|
6
|
+
project?: string;
|
|
7
|
+
stage?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const ContemberClient: import("react").NamedExoticComponent<ContemberClientProps & {
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
}>;
|
|
12
|
+
//# sourceMappingURL=ContemberClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContemberClient.d.ts","sourceRoot":"","sources":["../../src/ContemberClient.tsx"],"names":[],"mappings":";AAKA,MAAM,WAAW,oBAAoB;IACpC,UAAU,EAAE,MAAM,CAAA;IAClB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;CACd;AAID,eAAO,MAAM,eAAe;cAA2C,MAAM,SAAS;EAgCpF,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { GraphQlClientFailedRequestMetadata } from '@contember/client';
|
|
2
|
+
export declare type ApiRequestAction<SuccessData> = {
|
|
3
|
+
type: 'uninitialize';
|
|
4
|
+
} | {
|
|
5
|
+
type: 'initialize';
|
|
6
|
+
} | {
|
|
7
|
+
type: 'resolveSuccessfully';
|
|
8
|
+
data: SuccessData;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'resolveWithError';
|
|
11
|
+
error: GraphQlClientFailedRequestMetadata;
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=ApiRequestAction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ApiRequestAction.d.ts","sourceRoot":"","sources":["../../../src/apiRequest/ApiRequestAction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,mBAAmB,CAAA;AAE3E,oBAAY,gBAAgB,CAAC,WAAW,IACrC;IACA,IAAI,EAAE,cAAc,CAAA;CACnB,GACD;IACA,IAAI,EAAE,YAAY,CAAA;CACjB,GACD;IACA,IAAI,EAAE,qBAAqB,CAAA;IAC3B,IAAI,EAAE,WAAW,CAAA;CAChB,GACD;IACA,IAAI,EAAE,kBAAkB,CAAA;IACxB,KAAK,EAAE,kCAAkC,CAAA;CACxC,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { GraphQlClientFailedRequestMetadata } from '@contember/client';
|
|
2
|
+
export declare type ApiRequestState<SuccessData> = {
|
|
3
|
+
isLoading: false;
|
|
4
|
+
isFinished: false;
|
|
5
|
+
readyState: 'uninitialized';
|
|
6
|
+
} | {
|
|
7
|
+
isLoading: true;
|
|
8
|
+
isFinished: false;
|
|
9
|
+
readyState: 'pending';
|
|
10
|
+
} | {
|
|
11
|
+
isLoading: false;
|
|
12
|
+
isFinished: true;
|
|
13
|
+
readyState: 'networkSuccess';
|
|
14
|
+
data: SuccessData;
|
|
15
|
+
} | {
|
|
16
|
+
isLoading: false;
|
|
17
|
+
isFinished: true;
|
|
18
|
+
readyState: 'networkError';
|
|
19
|
+
data: GraphQlClientFailedRequestMetadata;
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=ApiRequestState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ApiRequestState.d.ts","sourceRoot":"","sources":["../../../src/apiRequest/ApiRequestState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,mBAAmB,CAAA;AAE3E,oBAAY,eAAe,CAAC,WAAW,IACpC;IACA,SAAS,EAAE,KAAK,CAAA;IAChB,UAAU,EAAE,KAAK,CAAA;IACjB,UAAU,EAAE,eAAe,CAAA;CAC1B,GACD;IACA,SAAS,EAAE,IAAI,CAAA;IACf,UAAU,EAAE,KAAK,CAAA;IACjB,UAAU,EAAE,SAAS,CAAA;CACpB,GACD;IACA,SAAS,EAAE,KAAK,CAAA;IAChB,UAAU,EAAE,IAAI,CAAA;IAChB,UAAU,EAAE,gBAAgB,CAAA;IAC5B,IAAI,EAAE,WAAW,CAAA;CAChB,GACD;IACA,SAAS,EAAE,KAAK,CAAA;IAChB,UAAU,EAAE,IAAI,CAAA;IAChB,UAAU,EAAE,cAAc,CAAA;IAC1B,IAAI,EAAE,kCAAkC,CAAA;CACvC,CAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ApiRequestAction } from './ApiRequestAction';
|
|
2
|
+
import type { ApiRequestState } from './ApiRequestState';
|
|
3
|
+
export declare type ApiRequestReducer<SuccessData> = (previousState: ApiRequestState<SuccessData>, action: ApiRequestAction<SuccessData>) => ApiRequestState<SuccessData>;
|
|
4
|
+
export declare const apiRequestReducer: <SuccessData>(previousState: ApiRequestState<SuccessData>, action: ApiRequestAction<SuccessData>) => ApiRequestState<SuccessData>;
|
|
5
|
+
//# sourceMappingURL=apiRequestReducer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apiRequestReducer.d.ts","sourceRoot":"","sources":["../../../src/apiRequest/apiRequestReducer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAExD,oBAAY,iBAAiB,CAAC,WAAW,IAAI,CAC5C,aAAa,EAAE,eAAe,CAAC,WAAW,CAAC,EAC3C,MAAM,EAAE,gBAAgB,CAAC,WAAW,CAAC,KACjC,eAAe,CAAC,WAAW,CAAC,CAAA;AAEjC,eAAO,MAAM,iBAAiB,mIAgC7B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/apiRequest/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA;AACnC,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GraphQlClient, GraphQlClientRequestOptions, GraphQlClientVariables } from '@contember/client';
|
|
2
|
+
import type { ApiRequestState } from './ApiRequestState';
|
|
3
|
+
export declare type UseApiRequestResult<SuccessData> = [
|
|
4
|
+
ApiRequestState<SuccessData>,
|
|
5
|
+
(query: string, variables?: GraphQlClientVariables, options?: Omit<GraphQlClientRequestOptions, 'variables'>) => Promise<SuccessData>
|
|
6
|
+
];
|
|
7
|
+
export declare const useApiRequest: <SuccessData>(client: GraphQlClient) => UseApiRequestResult<SuccessData>;
|
|
8
|
+
//# sourceMappingURL=useApiRequest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useApiRequest.d.ts","sourceRoot":"","sources":["../../../src/apiRequest/useApiRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,aAAa,EAEb,2BAA2B,EAC3B,sBAAsB,EACtB,MAAM,mBAAmB,CAAA;AAG1B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAQxD,oBAAY,mBAAmB,CAAC,WAAW,IAAI;IAC9C,eAAe,CAAC,WAAW,CAAC;IAC5B,CACC,KAAK,EAAE,MAAM,EACb,SAAS,CAAC,EAAE,sBAAsB,EAClC,OAAO,CAAC,EAAE,IAAI,CAAC,2BAA2B,EAAE,WAAW,CAAC,KACpD,OAAO,CAAC,WAAW,CAAC;CACzB,CAAA;AAED,eAAO,MAAM,aAAa,wBAAyB,aAAa,qCA+C/D,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SessionTokenContext.d.ts","sourceRoot":"","sources":["../../../src/auth/SessionTokenContext.ts"],"names":[],"mappings":";AAEA,eAAO,MAAM,mBAAmB,6CAA+C,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SetSessionTokenContext.d.ts","sourceRoot":"","sources":["../../../src/auth/SetSessionTokenContext.ts"],"names":[],"mappings":";AAEA,eAAO,MAAM,sBAAsB,kCAAyB,MAAM,GAAG,SAAS,KAAK,IAAI,CAErF,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/auth/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,0BAA0B,CAAA;AACxC,cAAc,mBAAmB,CAAA;AACjC,cAAc,sBAAsB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSessionToken.d.ts","sourceRoot":"","sources":["../../../src/auth/useSessionToken.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,eAAe,0BAAwC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSetSessionToken.d.ts","sourceRoot":"","sources":["../../../src/auth/useSetSessionToken.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,kBAAkB,2CAA2C,CAAA"}
|