@prezly/theme-kit-nextjs 1.3.1 → 1.4.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 (114) hide show
  1. package/README.md +15 -11
  2. package/build/adapter-nextjs/getNewsroomServerSideProps.d.ts +6 -0
  3. package/build/adapter-nextjs/getNewsroomServerSideProps.js +5 -1
  4. package/build/adapter-nextjs/getNewsroomServerSideProps.js.map +1 -1
  5. package/build/adapter-nextjs/processRequest.d.ts +10 -1
  6. package/build/adapter-nextjs/processRequest.js +9 -0
  7. package/build/adapter-nextjs/processRequest.js.map +1 -1
  8. package/build/data-fetching/api/PrezlyApi.d.ts +12 -4
  9. package/build/data-fetching/api/PrezlyApi.js +13 -2
  10. package/build/data-fetching/api/PrezlyApi.js.map +1 -1
  11. package/build/data-fetching/api/getPrezlyApi.d.ts +1 -1
  12. package/build/data-fetching/api/getPrezlyApi.js +5 -5
  13. package/build/data-fetching/api/getPrezlyApi.js.map +1 -1
  14. package/build/data-fetching/api/languages.d.ts +9 -0
  15. package/build/data-fetching/api/languages.js +9 -0
  16. package/build/data-fetching/api/languages.js.map +1 -1
  17. package/build/data-fetching/api/queries.d.ts +5 -5
  18. package/build/data-fetching/api/queries.js +22 -16
  19. package/build/data-fetching/api/queries.js.map +1 -1
  20. package/build/data-fetching/getAssetsUrl.d.ts +4 -0
  21. package/build/data-fetching/getAssetsUrl.js +4 -0
  22. package/build/data-fetching/getAssetsUrl.js.map +1 -1
  23. package/build/data-fetching/getLocalizedCategoryData.d.ts +2 -2
  24. package/build/data-fetching/getPrivacyPortalUrl.d.ts +1 -1
  25. package/build/data-fetching/getStoryPublicationDate.d.ts +1 -1
  26. package/build/data-fetching/getUploadcareGroupUrl.d.ts +7 -0
  27. package/build/data-fetching/getUploadcareGroupUrl.js +7 -0
  28. package/build/data-fetching/getUploadcareGroupUrl.js.map +1 -1
  29. package/build/data-fetching/lib/getAlgoliaSettings.d.ts +2 -2
  30. package/build/data-fetching/lib/getAlgoliaSettings.js +3 -3
  31. package/build/data-fetching/lib/getAlgoliaSettings.js.map +1 -1
  32. package/build/data-fetching/lib/getEnvVariables.d.ts +1 -1
  33. package/build/data-fetching/lib/getEnvVariables.js +7 -7
  34. package/build/data-fetching/lib/getEnvVariables.js.map +1 -1
  35. package/build/data-fetching/types.d.ts +1 -0
  36. package/build/intl/lang.d.ts +5 -3
  37. package/build/intl/lang.js +4 -2
  38. package/build/intl/lang.js.map +1 -1
  39. package/build/intl/locale.d.ts +5 -1
  40. package/build/intl/locale.js +9 -0
  41. package/build/intl/locale.js.map +1 -1
  42. package/build/intl/localeConfig.js +7 -2
  43. package/build/intl/localeConfig.js.map +1 -1
  44. package/build/intl/localeObject.d.ts +23 -0
  45. package/build/intl/localeObject.js +23 -0
  46. package/build/intl/localeObject.js.map +1 -1
  47. package/build/newsroom-context/context.d.ts +40 -3
  48. package/build/newsroom-context/context.js +6 -2
  49. package/build/newsroom-context/context.js.map +1 -1
  50. package/build/newsroom-context/hooks/useAlgoliaSettings.d.ts +1 -1
  51. package/build/newsroom-context/hooks/useAlgoliaSettings.js +2 -2
  52. package/build/newsroom-context/hooks/useAlgoliaSettings.js.map +1 -1
  53. package/build/newsroom-context/hooks/useCategories.d.ts +1 -1
  54. package/build/newsroom-context/hooks/useCategories.js +2 -2
  55. package/build/newsroom-context/hooks/useCategories.js.map +1 -1
  56. package/build/newsroom-context/hooks/useCompanyInformation.d.ts +1 -1
  57. package/build/newsroom-context/hooks/useCompanyInformation.js +2 -2
  58. package/build/newsroom-context/hooks/useCompanyInformation.js.map +1 -1
  59. package/build/newsroom-context/hooks/useCurrentCategory.d.ts +1 -1
  60. package/build/newsroom-context/hooks/useCurrentCategory.js +2 -2
  61. package/build/newsroom-context/hooks/useCurrentCategory.js.map +1 -1
  62. package/build/newsroom-context/hooks/useCurrentLocale.d.ts +1 -1
  63. package/build/newsroom-context/hooks/useCurrentLocale.js +2 -2
  64. package/build/newsroom-context/hooks/useCurrentLocale.js.map +1 -1
  65. package/build/newsroom-context/hooks/useCurrentStory.d.ts +1 -1
  66. package/build/newsroom-context/hooks/useCurrentStory.js +2 -2
  67. package/build/newsroom-context/hooks/useCurrentStory.js.map +1 -1
  68. package/build/newsroom-context/hooks/useGetLinkLocaleSlug.js +6 -1
  69. package/build/newsroom-context/hooks/useGetLinkLocaleSlug.js.map +1 -1
  70. package/build/newsroom-context/hooks/useGetTranslationUrl.d.ts +5 -1
  71. package/build/newsroom-context/hooks/useGetTranslationUrl.js +10 -1
  72. package/build/newsroom-context/hooks/useGetTranslationUrl.js.map +1 -1
  73. package/build/newsroom-context/hooks/useLanguages.d.ts +1 -1
  74. package/build/newsroom-context/hooks/useLanguages.js +2 -2
  75. package/build/newsroom-context/hooks/useLanguages.js.map +1 -1
  76. package/build/newsroom-context/hooks/useNewsroom.d.ts +1 -1
  77. package/build/newsroom-context/hooks/useNewsroom.js +2 -2
  78. package/build/newsroom-context/hooks/useNewsroom.js.map +1 -1
  79. package/build/newsroom-context/hooks/useNewsroomContext.d.ts +2 -2
  80. package/build/newsroom-context/hooks/useNewsroomContext.js +2 -2
  81. package/build/newsroom-context/hooks/useNewsroomContext.js.map +1 -1
  82. package/build/types.d.ts +14 -1
  83. package/package.json +2 -10
  84. package/src/adapter-nextjs/getNewsroomServerSideProps.ts +7 -1
  85. package/src/adapter-nextjs/processRequest.ts +10 -1
  86. package/src/data-fetching/api/PrezlyApi.ts +19 -4
  87. package/src/data-fetching/api/getPrezlyApi.ts +5 -5
  88. package/src/data-fetching/api/languages.ts +9 -0
  89. package/src/data-fetching/api/queries.ts +25 -18
  90. package/src/data-fetching/getAssetsUrl.ts +4 -0
  91. package/src/data-fetching/getLocalizedCategoryData.ts +2 -2
  92. package/src/data-fetching/getPrivacyPortalUrl.ts +1 -1
  93. package/src/data-fetching/getStoryPublicationDate.tsx +1 -1
  94. package/src/data-fetching/getUploadcareGroupUrl.ts +7 -0
  95. package/src/data-fetching/lib/getAlgoliaSettings.ts +4 -4
  96. package/src/data-fetching/lib/getEnvVariables.ts +7 -7
  97. package/src/data-fetching/types.ts +1 -0
  98. package/src/intl/lang.ts +5 -3
  99. package/src/intl/locale.ts +11 -1
  100. package/src/intl/localeConfig.js +7 -2
  101. package/src/intl/localeObject.ts +23 -0
  102. package/src/newsroom-context/context.tsx +43 -5
  103. package/src/newsroom-context/hooks/useAlgoliaSettings.ts +2 -2
  104. package/src/newsroom-context/hooks/useCategories.ts +2 -2
  105. package/src/newsroom-context/hooks/useCompanyInformation.ts +2 -2
  106. package/src/newsroom-context/hooks/useCurrentCategory.ts +2 -2
  107. package/src/newsroom-context/hooks/useCurrentLocale.ts +2 -2
  108. package/src/newsroom-context/hooks/useCurrentStory.ts +2 -2
  109. package/src/newsroom-context/hooks/useGetLinkLocaleSlug.ts +6 -1
  110. package/src/newsroom-context/hooks/useGetTranslationUrl.ts +11 -2
  111. package/src/newsroom-context/hooks/useLanguages.ts +2 -2
  112. package/src/newsroom-context/hooks/useNewsroom.ts +2 -2
  113. package/src/newsroom-context/hooks/useNewsroomContext.ts +4 -3
  114. package/src/types.ts +14 -2
@@ -7,21 +7,55 @@ import type {
7
7
  NewsroomLanguageSettings,
8
8
  NewsroomThemePreset,
9
9
  } from '@prezly/sdk';
10
- import { createContext, FunctionComponent, useMemo } from 'react';
10
+ import type { PropsWithChildren } from 'react';
11
+ import { createContext, useMemo } from 'react';
11
12
 
12
13
  import type { AlgoliaSettings } from '../data-fetching';
13
14
  import { DEFAULT_LOCALE, LocaleObject } from '../intl';
14
15
 
15
16
  export interface NewsroomContextType {
17
+ /**
18
+ * Basic newsroom information: newsroom logos, settings, etc.
19
+ */
16
20
  newsroom: Newsroom;
21
+ /**
22
+ * Contact information of the newsroom in the default language, usually displayed in the "About" section
23
+ */
17
24
  companyInformation: NewsroomCompanyInformation;
25
+ /**
26
+ * List of all categories enabled on the newsroom
27
+ */
18
28
  categories: Category[];
29
+ /**
30
+ * Optional: Contacts set up to be displayed on the newsroom home page.
31
+ * Will only be loaded if `loadHomepageContacts` option of `getNewsroomServerSideProps` is set to `true`.
32
+ */
19
33
  contacts?: NewsroomContact[];
34
+ /**
35
+ * Optional: Refers to a currently selected category when navigated to `/category/[slug]` page.
36
+ */
20
37
  currentCategory?: Category;
38
+ /**
39
+ * Optional: Refers to a currently selected story when navigated to `/[slug]` page.
40
+ */
21
41
  currentStory?: ExtendedStory;
42
+ /**
43
+ * List of currently enabled newsroom languages, as well as company information translated for each language
44
+ */
22
45
  languages: NewsroomLanguageSettings[];
46
+ /**
47
+ * Currently chosen locale
48
+ */
23
49
  locale: LocaleObject;
24
- themePreset: NewsroomThemePreset;
50
+ /**
51
+ * Theme settings as set in the Prezly app.
52
+ * You need to provide `PREZLY_THEME_UUID` in environment variables in order for the preset to be loaded.
53
+ * Only applicable to themes hosted on the Prezly infrastructure.
54
+ */
55
+ themePreset: NewsroomThemePreset | null;
56
+ /**
57
+ * Environment variables required for Algolia search to work.
58
+ */
25
59
  algoliaSettings: AlgoliaSettings;
26
60
  }
27
61
 
@@ -31,7 +65,11 @@ export interface NewsroomContextProps extends Omit<NewsroomContextType, 'locale'
31
65
 
32
66
  export const NewsroomContext = createContext<NewsroomContextType | undefined>(undefined);
33
67
 
34
- export const NewsroomContextProvider: FunctionComponent<NewsroomContextProps> = ({
68
+ /**
69
+ * This context provides common Newsroom information retrieved by `getNewsroomServerSideProps` helper.
70
+ * Please refer to the `NewsroomContextType` definition for more info on what's exposed from the context.
71
+ */
72
+ export function NewsroomContextProvider({
35
73
  categories,
36
74
  contacts,
37
75
  newsroom,
@@ -43,7 +81,7 @@ export const NewsroomContextProvider: FunctionComponent<NewsroomContextProps> =
43
81
  themePreset,
44
82
  algoliaSettings,
45
83
  children,
46
- }) => {
84
+ }: PropsWithChildren<NewsroomContextProps>) {
47
85
  const locale = useMemo(
48
86
  () => LocaleObject.fromAnyCode(localeCode || DEFAULT_LOCALE),
49
87
  [localeCode],
@@ -67,4 +105,4 @@ export const NewsroomContextProvider: FunctionComponent<NewsroomContextProps> =
67
105
  {children}
68
106
  </NewsroomContext.Provider>
69
107
  );
70
- };
108
+ }
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useAlgoliaSettings = () => {
3
+ export function useAlgoliaSettings() {
4
4
  const context = useNewsroomContext();
5
5
 
6
6
  return context.algoliaSettings;
7
- };
7
+ }
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useCategories = () => {
3
+ export function useCategories() {
4
4
  const context = useNewsroomContext();
5
5
 
6
6
  return context.categories;
7
- };
7
+ }
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useCompanyInformation = () => {
3
+ export function useCompanyInformation() {
4
4
  const context = useNewsroomContext();
5
5
 
6
6
  return context.companyInformation;
7
- };
7
+ }
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useCurrentCategory = () => {
3
+ export function useCurrentCategory() {
4
4
  const context = useNewsroomContext();
5
5
 
6
6
  return context.currentCategory;
7
- };
7
+ }
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useCurrentLocale = () => {
3
+ export function useCurrentLocale() {
4
4
  const context = useNewsroomContext();
5
5
 
6
6
  return context.locale;
7
- };
7
+ }
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useCurrentStory = () => {
3
+ export function useCurrentStory() {
4
4
  const context = useNewsroomContext();
5
5
 
6
6
  return context.currentStory;
7
- };
7
+ }
@@ -7,7 +7,12 @@ import { LocaleObject } from '../../intl';
7
7
  import { useCurrentLocale } from './useCurrentLocale';
8
8
  import { useLanguages } from './useLanguages';
9
9
 
10
- // Determine correct locale slug for the link (trying to shorten the locale code if possible)
10
+ /**
11
+ * Determines correct locale slug for the link (trying to shorten the locale code if possible).
12
+ * E.g. if newsroom has a single locale for Dutch (`nl_BE`), it will be shortened to `nl`.
13
+ * If there are more than one locale with the same language part, a full locale code will be used instead.
14
+ * `false` means that a default locale should be used (no locale in URL required).
15
+ */
11
16
  function getLinkLocaleSlug(
12
17
  languages: NewsroomLanguageSettings[],
13
18
  locale: LocaleObject,
@@ -3,12 +3,17 @@ import { useRouter } from 'next/router';
3
3
  import { useCallback } from 'react';
4
4
 
5
5
  import { getCategoryHasTranslation, getCategoryUrl } from '../../data-fetching';
6
- import { LocaleObject } from '../../intl';
6
+ import type { LocaleObject } from '../../intl';
7
7
 
8
8
  import { useCurrentCategory } from './useCurrentCategory';
9
9
  import { useCurrentStory } from './useCurrentStory';
10
10
 
11
- // Determine correct URL for translated stories/categories with a fallback to homepage
11
+ /**
12
+ * Determine correct URL for translated stories/categories with a fallback to homepage.
13
+ * E.g. if a story has a translation, the function will return the localized slug URL.
14
+ * If there is no translation for the story, it will return the index page URL (`/`),
15
+ * or empty string, if `noFallback` is set to `true` (this is useful for alternate locale links).
16
+ */
12
17
  function getTranslationUrl(
13
18
  locale: LocaleObject,
14
19
  path: string,
@@ -48,6 +53,10 @@ function getTranslationUrl(
48
53
  return path;
49
54
  }
50
55
 
56
+ /**
57
+ * This hook returns a function that is meant to be used in language pickers and alternate locales list for the pages.
58
+ * `noFallback` parameter is useful for alternate language links in the `head` section of the document (so that there are no unwanted links to the homepage).
59
+ */
51
60
  export function useGetTranslationUrl() {
52
61
  const { asPath } = useRouter();
53
62
  const currentCategory = useCurrentCategory();
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useLanguages = () => {
3
+ export function useLanguages() {
4
4
  const context = useNewsroomContext();
5
5
 
6
6
  return context.languages;
7
- };
7
+ }
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useNewsroom = () => {
3
+ export function useNewsroom() {
4
4
  const context = useNewsroomContext();
5
5
 
6
6
  return context.newsroom;
7
- };
7
+ }
@@ -1,8 +1,9 @@
1
1
  import { useContext } from 'react';
2
2
 
3
- import { NewsroomContext, NewsroomContextType } from '../context';
3
+ import type { NewsroomContextType } from '../context';
4
+ import { NewsroomContext } from '../context';
4
5
 
5
- export const useNewsroomContext = () => {
6
+ export function useNewsroomContext() {
6
7
  // TS Compiler is having a hard time figuring this type implicitly, so we have to set it explicitly
7
8
  // See https://github.com/microsoft/TypeScript/issues/5711#issuecomment-340283439
8
9
  const newsroomContext: NewsroomContextType | undefined = useContext(NewsroomContext);
@@ -11,4 +12,4 @@ export const useNewsroomContext = () => {
11
12
  }
12
13
 
13
14
  return newsroomContext;
14
- };
15
+ }
package/src/types.ts CHANGED
@@ -1,22 +1,34 @@
1
- // TODO: Re-arrange the types to make them isolated to respective libs where possible
2
1
  import type { CultureRef, ExtraStoryFields, Story } from '@prezly/sdk';
3
2
 
4
- import { NewsroomContextProps } from './newsroom-context';
3
+ import type { NewsroomContextProps } from './newsroom-context';
5
4
 
6
5
  export interface ServerSidePageProps {
6
+ /**
7
+ * Indicates whether the locale requested by the Next application was correctly resolved to an active newsroom locale.
8
+ * This is used by `processRequest` method to redirect to the default locale if the property is `false`.
9
+ */
7
10
  localeResolved: boolean;
8
11
  }
9
12
 
13
+ /**
14
+ * These are the props returned by the `processRequest` method that are meant to be passed to your `_app` to be injected into the `NewsroomContextProvider`.
15
+ */
10
16
  export interface PageProps {
11
17
  newsroomContextProps: NewsroomContextProps;
12
18
  }
13
19
 
20
+ /**
21
+ * Categories in Algolia index are stored in a simplified format, with `slug` and `name` properties already saved in the correct locale for the story search result.
22
+ */
14
23
  export interface AlgoliaCategoryRef {
15
24
  id: number;
16
25
  name: string;
17
26
  slug: string;
18
27
  }
19
28
 
29
+ /**
30
+ * Stories in Algolia index are stored in a simplified format, with only minimal subset of fields.
31
+ */
20
32
  export type AlgoliaStory = Pick<Story, 'uuid' | 'slug' | 'title' | 'subtitle'> &
21
33
  Pick<ExtraStoryFields, 'thumbnail_image'> & {
22
34
  content_text: string;