@prezly/theme-kit-nextjs 1.3.0 → 1.3.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.
- package/README.md +15 -11
- package/build/adapter-nextjs/getNewsroomServerSideProps.d.ts +6 -0
- package/build/adapter-nextjs/getNewsroomServerSideProps.js +5 -1
- package/build/adapter-nextjs/getNewsroomServerSideProps.js.map +1 -1
- package/build/adapter-nextjs/processRequest.d.ts +10 -1
- package/build/adapter-nextjs/processRequest.js +9 -0
- package/build/adapter-nextjs/processRequest.js.map +1 -1
- package/build/data-fetching/api/PrezlyApi.d.ts +12 -4
- package/build/data-fetching/api/PrezlyApi.js +13 -2
- package/build/data-fetching/api/PrezlyApi.js.map +1 -1
- package/build/data-fetching/api/getPrezlyApi.d.ts +1 -1
- package/build/data-fetching/api/getPrezlyApi.js +5 -5
- package/build/data-fetching/api/getPrezlyApi.js.map +1 -1
- package/build/data-fetching/api/languages.d.ts +9 -0
- package/build/data-fetching/api/languages.js +9 -0
- package/build/data-fetching/api/languages.js.map +1 -1
- package/build/data-fetching/api/queries.d.ts +5 -5
- package/build/data-fetching/api/queries.js +22 -16
- package/build/data-fetching/api/queries.js.map +1 -1
- package/build/data-fetching/getAssetsUrl.d.ts +4 -0
- package/build/data-fetching/getAssetsUrl.js +4 -0
- package/build/data-fetching/getAssetsUrl.js.map +1 -1
- package/build/data-fetching/getLocalizedCategoryData.d.ts +2 -2
- package/build/data-fetching/getNewsroomLogoUrl.js +3 -2
- package/build/data-fetching/getNewsroomLogoUrl.js.map +1 -1
- package/build/data-fetching/getPrivacyPortalUrl.d.ts +1 -1
- package/build/data-fetching/getStoryPublicationDate.d.ts +1 -1
- package/build/data-fetching/getUploadcareGroupUrl.d.ts +7 -0
- package/build/data-fetching/getUploadcareGroupUrl.js +7 -0
- package/build/data-fetching/getUploadcareGroupUrl.js.map +1 -1
- package/build/data-fetching/lib/getAlgoliaSettings.d.ts +2 -2
- package/build/data-fetching/lib/getAlgoliaSettings.js +3 -3
- package/build/data-fetching/lib/getAlgoliaSettings.js.map +1 -1
- package/build/data-fetching/lib/getEnvVariables.d.ts +1 -1
- package/build/data-fetching/lib/getEnvVariables.js +7 -7
- package/build/data-fetching/lib/getEnvVariables.js.map +1 -1
- package/build/data-fetching/types.d.ts +1 -0
- package/build/intl/lang.d.ts +5 -3
- package/build/intl/lang.js +4 -2
- package/build/intl/lang.js.map +1 -1
- package/build/intl/locale.d.ts +5 -1
- package/build/intl/locale.js +9 -0
- package/build/intl/locale.js.map +1 -1
- package/build/intl/localeConfig.js +7 -2
- package/build/intl/localeConfig.js.map +1 -1
- package/build/intl/localeObject.d.ts +23 -0
- package/build/intl/localeObject.js +23 -0
- package/build/intl/localeObject.js.map +1 -1
- package/build/newsroom-context/context.d.ts +40 -3
- package/build/newsroom-context/context.js +6 -2
- package/build/newsroom-context/context.js.map +1 -1
- package/build/newsroom-context/hooks/useAlgoliaSettings.d.ts +1 -1
- package/build/newsroom-context/hooks/useAlgoliaSettings.js +2 -2
- package/build/newsroom-context/hooks/useAlgoliaSettings.js.map +1 -1
- package/build/newsroom-context/hooks/useCategories.d.ts +1 -1
- package/build/newsroom-context/hooks/useCategories.js +2 -2
- package/build/newsroom-context/hooks/useCategories.js.map +1 -1
- package/build/newsroom-context/hooks/useCompanyInformation.d.ts +1 -1
- package/build/newsroom-context/hooks/useCompanyInformation.js +2 -2
- package/build/newsroom-context/hooks/useCompanyInformation.js.map +1 -1
- package/build/newsroom-context/hooks/useCurrentCategory.d.ts +1 -1
- package/build/newsroom-context/hooks/useCurrentCategory.js +2 -2
- package/build/newsroom-context/hooks/useCurrentCategory.js.map +1 -1
- package/build/newsroom-context/hooks/useCurrentLocale.d.ts +1 -1
- package/build/newsroom-context/hooks/useCurrentLocale.js +2 -2
- package/build/newsroom-context/hooks/useCurrentLocale.js.map +1 -1
- package/build/newsroom-context/hooks/useCurrentStory.d.ts +1 -1
- package/build/newsroom-context/hooks/useCurrentStory.js +2 -2
- package/build/newsroom-context/hooks/useCurrentStory.js.map +1 -1
- package/build/newsroom-context/hooks/useGetLinkLocaleSlug.js +6 -1
- package/build/newsroom-context/hooks/useGetLinkLocaleSlug.js.map +1 -1
- package/build/newsroom-context/hooks/useGetTranslationUrl.d.ts +5 -1
- package/build/newsroom-context/hooks/useGetTranslationUrl.js +10 -1
- package/build/newsroom-context/hooks/useGetTranslationUrl.js.map +1 -1
- package/build/newsroom-context/hooks/useLanguages.d.ts +1 -1
- package/build/newsroom-context/hooks/useLanguages.js +2 -2
- package/build/newsroom-context/hooks/useLanguages.js.map +1 -1
- package/build/newsroom-context/hooks/useNewsroom.d.ts +1 -1
- package/build/newsroom-context/hooks/useNewsroom.js +2 -2
- package/build/newsroom-context/hooks/useNewsroom.js.map +1 -1
- package/build/newsroom-context/hooks/useNewsroomContext.d.ts +2 -2
- package/build/newsroom-context/hooks/useNewsroomContext.js +2 -2
- package/build/newsroom-context/hooks/useNewsroomContext.js.map +1 -1
- package/build/types.d.ts +14 -1
- package/package.json +2 -10
- package/src/adapter-nextjs/getNewsroomServerSideProps.ts +7 -1
- package/src/adapter-nextjs/processRequest.ts +10 -1
- package/src/data-fetching/api/PrezlyApi.ts +19 -4
- package/src/data-fetching/api/getPrezlyApi.ts +5 -5
- package/src/data-fetching/api/languages.ts +9 -0
- package/src/data-fetching/api/queries.ts +25 -18
- package/src/data-fetching/getAssetsUrl.ts +4 -0
- package/src/data-fetching/getLocalizedCategoryData.ts +2 -2
- package/src/data-fetching/getNewsroomLogoUrl.ts +3 -4
- package/src/data-fetching/getPrivacyPortalUrl.ts +1 -1
- package/src/data-fetching/getStoryPublicationDate.tsx +1 -1
- package/src/data-fetching/getUploadcareGroupUrl.ts +7 -0
- package/src/data-fetching/lib/getAlgoliaSettings.ts +4 -4
- package/src/data-fetching/lib/getEnvVariables.ts +7 -7
- package/src/data-fetching/types.ts +1 -0
- package/src/intl/lang.ts +5 -3
- package/src/intl/locale.ts +11 -1
- package/src/intl/localeConfig.js +7 -2
- package/src/intl/localeObject.ts +23 -0
- package/src/newsroom-context/context.tsx +43 -5
- package/src/newsroom-context/hooks/useAlgoliaSettings.ts +2 -2
- package/src/newsroom-context/hooks/useCategories.ts +2 -2
- package/src/newsroom-context/hooks/useCompanyInformation.ts +2 -2
- package/src/newsroom-context/hooks/useCurrentCategory.ts +2 -2
- package/src/newsroom-context/hooks/useCurrentLocale.ts +2 -2
- package/src/newsroom-context/hooks/useCurrentStory.ts +2 -2
- package/src/newsroom-context/hooks/useGetLinkLocaleSlug.ts +6 -1
- package/src/newsroom-context/hooks/useGetTranslationUrl.ts +11 -2
- package/src/newsroom-context/hooks/useLanguages.ts +2 -2
- package/src/newsroom-context/hooks/useNewsroom.ts +2 -2
- package/src/newsroom-context/hooks/useNewsroomContext.ts +4 -3
- package/src/types.ts +14 -2
package/src/intl/localeObject.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import localeConfig from './localeConfig';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* This class is created to simplify working with locales and transforming between different locale code formats.
|
|
5
|
+
* Prezly API uses underscore-codes, while most frontend application tend to use hyphen-codes or slugs.
|
|
6
|
+
* Note that `fromAnyCode` constructor only accepts locales supported by Prezly backend. See `localeConfig` for reference.
|
|
7
|
+
*/
|
|
3
8
|
export class LocaleObject {
|
|
4
9
|
/**
|
|
5
10
|
* Hyphen-separated locale code.
|
|
@@ -39,24 +44,39 @@ export class LocaleObject {
|
|
|
39
44
|
return new LocaleObject(foundCode);
|
|
40
45
|
}
|
|
41
46
|
|
|
47
|
+
/**
|
|
48
|
+
* @returns Code formatted into hyphen format: `nl-BE`, `en-US`, `fr`, `fil`
|
|
49
|
+
*/
|
|
42
50
|
public toHyphenCode(): string {
|
|
43
51
|
return this.localeCode;
|
|
44
52
|
}
|
|
45
53
|
|
|
54
|
+
/**
|
|
55
|
+
* @returns Code formatted into underscore format, as expected by Prezly API: `nl_BE`, `en_US`, `fr`, `fil`
|
|
56
|
+
*/
|
|
46
57
|
public toUnderscoreCode(): string {
|
|
47
58
|
return this.localeCode.replace('-', '_');
|
|
48
59
|
}
|
|
49
60
|
|
|
61
|
+
/**
|
|
62
|
+
* @returns Code formatted to a URL-friendly lowercase slug format: `nl-be`, `en-us`, `fr`, `fil`
|
|
63
|
+
*/
|
|
50
64
|
public toUrlSlug(): string {
|
|
51
65
|
return this.localeCode.toLowerCase();
|
|
52
66
|
}
|
|
53
67
|
|
|
68
|
+
/**
|
|
69
|
+
* @returns Only the language part of the code without culture/region part: `nl`, `en`, `fr`
|
|
70
|
+
*/
|
|
54
71
|
public toNeutralLanguageCode(): string {
|
|
55
72
|
const [language] = this.localeCode.split('-');
|
|
56
73
|
|
|
57
74
|
return language;
|
|
58
75
|
}
|
|
59
76
|
|
|
77
|
+
/**
|
|
78
|
+
* @returns Only the culture/regtion part without the language part: `be`, `us`, `fr`
|
|
79
|
+
*/
|
|
60
80
|
public toRegionCode(): string {
|
|
61
81
|
const [language, region] = this.localeCode.split('-');
|
|
62
82
|
|
|
@@ -67,6 +87,9 @@ export class LocaleObject {
|
|
|
67
87
|
return region.toUpperCase();
|
|
68
88
|
}
|
|
69
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Check if two LocaleObject instances are representing the same locale code.
|
|
92
|
+
*/
|
|
70
93
|
public isEqual(anotherLocale: LocaleObject) {
|
|
71
94
|
return anotherLocale.toHyphenCode() === this.localeCode;
|
|
72
95
|
}
|
|
@@ -7,21 +7,55 @@ import type {
|
|
|
7
7
|
NewsroomLanguageSettings,
|
|
8
8
|
NewsroomThemePreset,
|
|
9
9
|
} from '@prezly/sdk';
|
|
10
|
-
import {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
}
|
|
@@ -7,7 +7,12 @@ import { LocaleObject } from '../../intl';
|
|
|
7
7
|
import { useCurrentLocale } from './useCurrentLocale';
|
|
8
8
|
import { useLanguages } from './useLanguages';
|
|
9
9
|
|
|
10
|
-
|
|
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
|
-
|
|
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,8 +1,9 @@
|
|
|
1
1
|
import { useContext } from 'react';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import type { NewsroomContextType } from '../context';
|
|
4
|
+
import { NewsroomContext } from '../context';
|
|
4
5
|
|
|
5
|
-
export
|
|
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;
|