gt-sanity 0.0.5 → 1.0.0

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.
Files changed (108) hide show
  1. package/LICENSE.md +1 -8
  2. package/README.md +5 -5
  3. package/dist/index.d.mts +122 -95
  4. package/dist/index.d.ts +122 -95
  5. package/dist/index.js +9089 -1119
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +9099 -1100
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +11 -4
  10. package/src/adapter/core.ts +111 -9
  11. package/src/adapter/createTask.ts +1 -1
  12. package/src/adapter/getLocales.ts +2 -2
  13. package/src/adapter/types.ts +9 -0
  14. package/src/components/TranslationsProvider.tsx +942 -0
  15. package/src/components/page/BatchProgress.tsx +27 -0
  16. package/src/components/page/ImportAllDialog.tsx +51 -0
  17. package/src/components/page/ImportMissingDialog.tsx +55 -0
  18. package/src/components/page/TranslateAllDialog.tsx +55 -0
  19. package/src/components/page/TranslationsTable.tsx +81 -0
  20. package/src/components/page/TranslationsTool.tsx +338 -0
  21. package/src/components/shared/BaseTranslationWrapper.tsx +82 -0
  22. package/src/components/{LanguageStatus.tsx → shared/LanguageStatus.tsx} +2 -0
  23. package/src/components/shared/LocaleCheckbox.tsx +47 -0
  24. package/src/components/{ProgressBar.tsx → shared/ProgressBar.tsx} +2 -0
  25. package/src/components/shared/SingleDocumentView.tsx +108 -0
  26. package/src/components/tab/TranslationView.tsx +379 -0
  27. package/src/components/tab/TranslationsTab.tsx +25 -0
  28. package/src/configuration/baseDocumentLevelConfig/documentLevelPatch.ts +21 -11
  29. package/src/configuration/baseDocumentLevelConfig/helpers/createI18nDocAndPatchMetadata.ts +57 -23
  30. package/src/configuration/baseDocumentLevelConfig/helpers/createTranslationMetadata.ts +2 -0
  31. package/src/configuration/baseDocumentLevelConfig/helpers/getOrCreateTranslationMetadata.ts +2 -0
  32. package/src/configuration/baseDocumentLevelConfig/helpers/getTranslationMetadata.ts +2 -0
  33. package/src/configuration/baseDocumentLevelConfig/helpers/patchI18nDoc.ts +31 -8
  34. package/src/configuration/baseDocumentLevelConfig/index.ts +18 -101
  35. package/src/configuration/baseFieldLevelConfig.ts +19 -51
  36. package/src/configuration/utils/checkSerializationVersion.ts +2 -0
  37. package/src/configuration/utils/findDocumentAtRevision.ts +2 -0
  38. package/src/configuration/utils/findLatestDraft.ts +2 -0
  39. package/src/hooks/useClient.ts +3 -1
  40. package/src/hooks/useSecrets.ts +2 -0
  41. package/src/index.ts +91 -67
  42. package/src/sanity-api/findDocuments.ts +44 -0
  43. package/src/sanity-api/publishDocuments.ts +49 -0
  44. package/src/sanity-api/resolveRefs.ts +146 -0
  45. package/src/serialization/BaseDocumentMerger.ts +138 -0
  46. package/src/serialization/BaseSerializationConfig.ts +220 -0
  47. package/src/serialization/__tests__/BaseDocumentDeserializer/__snapshots__/documentLevelDeserialization.test.ts.snap +189 -0
  48. package/src/serialization/__tests__/BaseDocumentDeserializer/__snapshots__/fieldLevelDeserialization.test.ts.snap +107 -0
  49. package/src/serialization/__tests__/BaseDocumentDeserializer/baseDeserialization.test.ts +397 -0
  50. package/src/serialization/__tests__/BaseDocumentDeserializer/documentLevelDeserialization.test.ts +107 -0
  51. package/src/serialization/__tests__/BaseDocumentDeserializer/fieldLevelDeserialization.test.ts +107 -0
  52. package/src/serialization/__tests__/BaseDocumentMerger/__snapshots__/documentLevelMerge.test.ts.snap +193 -0
  53. package/src/serialization/__tests__/BaseDocumentMerger/__snapshots__/fieldLevelMerge.test.ts.snap +97 -0
  54. package/src/serialization/__tests__/BaseDocumentMerger/baseMerge.test.ts +36 -0
  55. package/src/serialization/__tests__/BaseDocumentMerger/documentLevelMerge.test.ts +96 -0
  56. package/src/serialization/__tests__/BaseDocumentMerger/fieldLevelMerge.test.ts +142 -0
  57. package/src/serialization/__tests__/BaseDocumentMerger/utils.ts +52 -0
  58. package/src/serialization/__tests__/BaseDocumentSerializer/__snapshots__/documentInlineMarks.test.ts.snap +39 -0
  59. package/src/serialization/__tests__/BaseDocumentSerializer/__snapshots__/documentLevelSerialization.test.ts.snap +8 -0
  60. package/src/serialization/__tests__/BaseDocumentSerializer/__snapshots__/fieldLevelSerialization.test.ts.snap +8 -0
  61. package/src/serialization/__tests__/BaseDocumentSerializer/baseSerialization.test.ts +345 -0
  62. package/src/serialization/__tests__/BaseDocumentSerializer/documentInlineMarks.test.ts +53 -0
  63. package/src/serialization/__tests__/BaseDocumentSerializer/documentLevelSerialization.test.ts +120 -0
  64. package/src/serialization/__tests__/BaseDocumentSerializer/fieldLevelSerialization.test.ts +153 -0
  65. package/src/serialization/__tests__/BaseDocumentSerializer/utils.ts +27 -0
  66. package/src/serialization/__tests__/README +2 -0
  67. package/src/serialization/__tests__/__fixtures__/annotationAndInlineBlocks.json +140 -0
  68. package/src/serialization/__tests__/__fixtures__/customStyles.json +62 -0
  69. package/src/serialization/__tests__/__fixtures__/documentInlineMarks.json +70 -0
  70. package/src/serialization/__tests__/__fixtures__/documentLevelArticle.json +185 -0
  71. package/src/serialization/__tests__/__fixtures__/fieldLevelArticle.json +107 -0
  72. package/src/serialization/__tests__/__fixtures__/inlineDocumentLevelArticle.json +134 -0
  73. package/src/serialization/__tests__/__fixtures__/inlineSchema.ts +270 -0
  74. package/src/serialization/__tests__/__fixtures__/messy-html.html +26 -0
  75. package/src/serialization/__tests__/__fixtures__/nestedLanguageFields.json +54 -0
  76. package/src/serialization/__tests__/__fixtures__/schema.ts +310 -0
  77. package/src/serialization/__tests__/global.setup.ts +40 -0
  78. package/src/serialization/__tests__/helpers.ts +132 -0
  79. package/src/serialization/data.ts +82 -0
  80. package/src/serialization/deserialize/BaseDocumentDeserializer.ts +171 -0
  81. package/src/serialization/deserialize/helpers.ts +42 -0
  82. package/src/serialization/helpers.ts +18 -0
  83. package/src/serialization/index.ts +11 -0
  84. package/src/serialization/serialize/fieldFilters.ts +124 -0
  85. package/src/serialization/serialize/index.ts +284 -0
  86. package/src/serialization/types.ts +41 -0
  87. package/src/translation/checkTranslationStatus.ts +42 -0
  88. package/src/translation/createJobs.ts +16 -0
  89. package/src/translation/downloadTranslations.ts +68 -0
  90. package/src/translation/importDocument.ts +23 -0
  91. package/src/translation/initProject.ts +61 -0
  92. package/src/translation/uploadFiles.ts +32 -0
  93. package/src/types.ts +7 -20
  94. package/src/utils/applyDocuments.ts +72 -0
  95. package/src/utils/batchProcessor.ts +111 -0
  96. package/src/utils/importUtils.ts +95 -0
  97. package/src/utils/serialize.ts +52 -0
  98. package/src/utils/shared.ts +1 -0
  99. package/src/adapter/index.ts +0 -13
  100. package/src/components/NewTask.tsx +0 -249
  101. package/src/components/TaskView.tsx +0 -255
  102. package/src/components/TranslationContext.tsx +0 -19
  103. package/src/components/TranslationView.tsx +0 -82
  104. package/src/components/TranslationsTab.tsx +0 -177
  105. package/src/configuration/baseDocumentLevelConfig/helpers/index.ts +0 -5
  106. package/src/configuration/baseDocumentLevelConfig/legacyDocumentLevelPatch.ts +0 -69
  107. package/src/configuration/index.ts +0 -18
  108. package/src/configuration/utils/index.ts +0 -3
@@ -1,177 +0,0 @@
1
- import { useMemo } from 'react';
2
- import { SanityDocument, useSchema } from 'sanity';
3
- import { randomKey } from '@sanity/util/content';
4
- import {
5
- ThemeProvider,
6
- ToastProvider,
7
- Stack,
8
- Text,
9
- Layer,
10
- Box,
11
- Card,
12
- Flex,
13
- Spinner,
14
- } from '@sanity/ui';
15
-
16
- import { TranslationContext } from './TranslationContext';
17
- import { TranslationView } from './TranslationView';
18
- import { useClient } from '../hooks/useClient';
19
- import { useSecrets } from '../hooks/useSecrets';
20
- import { GTFile, Secrets, TranslationsTabConfigOptions } from '../types';
21
-
22
- type TranslationTabProps = {
23
- document: {
24
- displayed: SanityDocument;
25
- };
26
- options: TranslationsTabConfigOptions;
27
- };
28
-
29
- const TranslationTab = (props: TranslationTabProps) => {
30
- const { displayed } = props.document;
31
- const client = useClient();
32
- const schema = useSchema();
33
-
34
- const documentId =
35
- displayed && displayed._id
36
- ? (displayed._id.split('drafts.').pop() as string)
37
- : '';
38
-
39
- const revisionId = displayed && displayed._rev ? displayed._rev : undefined;
40
-
41
- const { errors, importTranslation, exportForTranslation } = useMemo(() => {
42
- const { serializationOptions, languageField, mergeWithTargetLocale } =
43
- props.options;
44
- const ctx = {
45
- client,
46
- schema,
47
- };
48
-
49
- const allErrors = [];
50
-
51
- const importTranslationFunc = props.options.importTranslation;
52
- if (!importTranslationFunc) {
53
- allErrors.push({
54
- key: randomKey(12),
55
- text: (
56
- <>
57
- You need to provide an <code>importTranslation</code> function. See
58
- documentation.
59
- </>
60
- ),
61
- });
62
- }
63
-
64
- const contextImportTranslation = (localeId: string, doc: string) => {
65
- return importTranslationFunc(
66
- { documentId, versionId: revisionId },
67
- localeId,
68
- doc,
69
- ctx,
70
- serializationOptions,
71
- languageField,
72
- mergeWithTargetLocale
73
- );
74
- };
75
-
76
- const exportTranslationFunc = props.options.exportForTranslation;
77
- if (!exportTranslationFunc) {
78
- allErrors.push({
79
- key: randomKey(12),
80
- text: (
81
- <>
82
- You need to provide an <code>exportForTranslation</code> function.
83
- See documentation.
84
- </>
85
- ),
86
- });
87
- }
88
-
89
- const contextExportForTranslation = (docInfo: GTFile) => {
90
- return exportTranslationFunc(
91
- docInfo,
92
- ctx,
93
- serializationOptions,
94
- languageField
95
- );
96
- };
97
-
98
- return {
99
- errors: allErrors,
100
- importTranslation: contextImportTranslation,
101
- exportForTranslation: contextExportForTranslation,
102
- };
103
- }, [props.options, documentId, revisionId, client, schema]);
104
-
105
- const { loading, secrets } = useSecrets<Secrets>(
106
- `${props.options.secretsNamespace || 'translationService'}.secrets`
107
- );
108
-
109
- const hasErrors = errors.length > 0;
110
-
111
- if (loading || !secrets) {
112
- return (
113
- <ThemeProvider>
114
- <Flex padding={5} align='center' justify='center'>
115
- <Spinner />
116
- </Flex>
117
- </ThemeProvider>
118
- );
119
- } else if (!secrets) {
120
- return (
121
- <ThemeProvider>
122
- <Box padding={4}>
123
- <Card tone='caution' padding={[2, 3, 4, 4]} shadow={1} radius={2}>
124
- <Text>
125
- Can't find secrets for your translation service. Did you load them
126
- into this dataset?
127
- </Text>
128
- </Card>
129
- </Box>
130
- </ThemeProvider>
131
- );
132
- }
133
- return (
134
- <ThemeProvider>
135
- <Box padding={4}>
136
- <Layer>
137
- <ToastProvider paddingY={7}>
138
- {hasErrors && (
139
- <Stack space={3}>
140
- {errors.map((error) => (
141
- <Card
142
- key={error.key}
143
- tone='caution'
144
- padding={[2, 3, 4, 4]}
145
- shadow={1}
146
- radius={2}
147
- >
148
- <Text>{error.text}</Text>
149
- </Card>
150
- ))}
151
- </Stack>
152
- )}
153
- {!hasErrors && (
154
- <TranslationContext.Provider
155
- value={{
156
- documentInfo: { documentId, versionId: revisionId },
157
- secrets,
158
- importTranslation,
159
- exportForTranslation,
160
- adapter: props.options.adapter,
161
- workflowOptions: props.options.workflowOptions,
162
- localeIdAdapter: props.options.localeIdAdapter,
163
- callbackUrl: props.options.callbackUrl,
164
- mergeWithTargetLocale: props.options.mergeWithTargetLocale,
165
- }}
166
- >
167
- <TranslationView />
168
- </TranslationContext.Provider>
169
- )}
170
- </ToastProvider>
171
- </Layer>
172
- </Box>
173
- </ThemeProvider>
174
- );
175
- };
176
-
177
- export default TranslationTab;
@@ -1,5 +0,0 @@
1
- export { createI18nDocAndPatchMetadata } from './createI18nDocAndPatchMetadata';
2
- export { createTranslationMetadata } from './createTranslationMetadata';
3
- export { getTranslationMetadata } from './getTranslationMetadata';
4
- export { getOrCreateTranslationMetadata } from './getOrCreateTranslationMetadata';
5
- export { patchI18nDoc } from './patchI18nDoc';
@@ -1,69 +0,0 @@
1
- import { SanityClient, SanityDocument, SanityDocumentLike } from 'sanity';
2
- import { BaseDocumentMerger } from 'sanity-naive-html-serializer';
3
-
4
- import { findLatestDraft, findDocumentAtRevision } from '../utils';
5
- import type { GTFile } from '../../types';
6
-
7
- export const legacyDocumentLevelPatch = async (
8
- docInfo: GTFile,
9
- translatedFields: SanityDocument,
10
- localeId: string,
11
- client: SanityClient
12
- ): Promise<void> => {
13
- let baseDoc: SanityDocument | null = null;
14
-
15
- /*
16
- * we send over the _rev with our translation file so we can
17
- * accurately coalesce the translations in case something has
18
- * changed in the base document since translating
19
- */
20
- if (docInfo.documentId && docInfo.versionId) {
21
- baseDoc = await findDocumentAtRevision(
22
- docInfo.documentId,
23
- docInfo.versionId,
24
- client
25
- );
26
- }
27
- if (!baseDoc) {
28
- baseDoc = await findLatestDraft(docInfo.documentId, client);
29
- }
30
-
31
- /*
32
- * we then merge the translation with the base document
33
- * to create a document that contains the translation and everything
34
- * that wasn't sent over for translation
35
- */
36
- const merged = BaseDocumentMerger.documentLevelMerge(
37
- translatedFields,
38
- baseDoc
39
- ) as SanityDocumentLike;
40
-
41
- /* we now need to check if we have a translated document
42
- * if not, we create it
43
- */
44
- const targetId = `drafts.${docInfo.documentId}__i18n_${localeId}`;
45
- const i18nDoc = await findLatestDraft(targetId, client);
46
- if (i18nDoc) {
47
- const cleanedMerge: Record<string, any> = {};
48
- //don't overwrite any existing system values on the i18n doc
49
- Object.entries(merged).forEach(([key, value]) => {
50
- if (
51
- Object.keys(translatedFields).includes(key) &&
52
- !['_id', '_rev', '_updatedAt'].includes(key)
53
- ) {
54
- cleanedMerge[key] = value;
55
- }
56
- });
57
-
58
- await client
59
- .transaction()
60
- //@ts-ignore
61
- .patch(i18nDoc._id, (p) => p.set(cleanedMerge))
62
- .commit();
63
- } else {
64
- merged._id = targetId;
65
-
66
- merged.__i18n_lang = localeId;
67
- await client.create(merged);
68
- }
69
- };
@@ -1,18 +0,0 @@
1
- import {
2
- baseDocumentLevelConfig,
3
- documentLevelPatch,
4
- legacyDocumentLevelConfig,
5
- legacyDocumentLevelPatch,
6
- } from './baseDocumentLevelConfig';
7
- import { baseFieldLevelConfig, fieldLevelPatch } from './baseFieldLevelConfig';
8
- import { findLatestDraft } from './utils';
9
-
10
- export {
11
- baseDocumentLevelConfig,
12
- legacyDocumentLevelConfig,
13
- baseFieldLevelConfig,
14
- findLatestDraft,
15
- legacyDocumentLevelPatch,
16
- documentLevelPatch,
17
- fieldLevelPatch,
18
- };
@@ -1,3 +0,0 @@
1
- export * from './findDocumentAtRevision';
2
- export * from './findLatestDraft';
3
- export * from './checkSerializationVersion';