@prezly/theme-kit-nextjs 1.1.4 → 1.2.1
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 +36 -64
- package/build/adapter-nextjs/getNewsroomServerSideProps.d.ts +9 -0
- package/build/adapter-nextjs/{getBasePageProps.js → getNewsroomServerSideProps.js} +9 -9
- package/build/adapter-nextjs/getNewsroomServerSideProps.js.map +1 -0
- package/build/adapter-nextjs/index.d.ts +1 -1
- package/build/adapter-nextjs/index.js +1 -1
- package/build/adapter-nextjs/index.js.map +1 -1
- package/build/adapter-nextjs/processRequest.d.ts +2 -2
- package/build/adapter-nextjs/processRequest.js +19 -14
- package/build/adapter-nextjs/processRequest.js.map +1 -1
- package/build/data-fetching/api/PrezlyApi.d.ts +2 -2
- package/build/data-fetching/api/PrezlyApi.js +11 -11
- package/build/data-fetching/api/PrezlyApi.js.map +1 -1
- package/build/data-fetching/getNewsroomLogoUrl.js +1 -1
- package/build/data-fetching/getNewsroomLogoUrl.js.map +1 -1
- package/build/intl/locale.d.ts +1 -2
- package/build/intl/locale.js +1 -2
- package/build/intl/locale.js.map +1 -1
- package/build/newsroom-context/context.d.ts +7 -9
- package/build/newsroom-context/context.js +17 -25
- package/build/newsroom-context/context.js.map +1 -1
- package/build/newsroom-context/hooks/index.d.ts +2 -2
- package/build/newsroom-context/hooks/index.js +2 -2
- package/build/newsroom-context/hooks/index.js.map +1 -1
- package/build/newsroom-context/hooks/useCurrentCategory.d.ts +1 -0
- package/build/newsroom-context/hooks/useCurrentCategory.js +10 -0
- package/build/newsroom-context/hooks/useCurrentCategory.js.map +1 -0
- package/build/newsroom-context/hooks/useCurrentStory.d.ts +1 -0
- package/build/newsroom-context/hooks/{useSelectedStory.js → useCurrentStory.js} +5 -5
- package/build/newsroom-context/hooks/useCurrentStory.js.map +1 -0
- package/build/newsroom-context/hooks/useGetTranslationUrl.js +12 -12
- package/build/newsroom-context/hooks/useGetTranslationUrl.js.map +1 -1
- package/build/newsroom-context/hooks/useNewsroomContext.d.ts +2 -2
- package/build/newsroom-context/hooks/useNewsroomContext.js.map +1 -1
- package/build/newsroom-context/index.d.ts +1 -1
- package/build/newsroom-context/index.js.map +1 -1
- package/build/types.d.ts +6 -20
- package/package.json +21 -4
- package/src/adapter-nextjs/{getBasePageProps.ts → getNewsroomServerSideProps.ts} +6 -6
- package/src/adapter-nextjs/index.ts +1 -1
- package/src/adapter-nextjs/processRequest.ts +20 -12
- package/src/data-fetching/api/PrezlyApi.ts +12 -13
- package/src/data-fetching/getNewsroomLogoUrl.ts +1 -1
- package/src/intl/locale.ts +1 -4
- package/src/newsroom-context/context.tsx +11 -14
- package/src/newsroom-context/hooks/index.ts +2 -2
- package/src/newsroom-context/hooks/{useSelectedCategory.ts → useCurrentCategory.ts} +2 -2
- package/src/newsroom-context/hooks/{useSelectedStory.ts → useCurrentStory.ts} +2 -2
- package/src/newsroom-context/hooks/useGetTranslationUrl.ts +13 -13
- package/src/newsroom-context/hooks/useNewsroomContext.ts +2 -2
- package/src/newsroom-context/index.ts +1 -1
- package/src/types.ts +9 -32
- package/build/adapter-nextjs/getBasePageProps.d.ts +0 -9
- package/build/adapter-nextjs/getBasePageProps.js.map +0 -1
- package/build/newsroom-context/hooks/useSelectedCategory.d.ts +0 -1
- package/build/newsroom-context/hooks/useSelectedCategory.js +0 -10
- package/build/newsroom-context/hooks/useSelectedCategory.js.map +0 -1
- package/build/newsroom-context/hooks/useSelectedStory.d.ts +0 -1
- package/build/newsroom-context/hooks/useSelectedStory.js.map +0 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.useCurrentStory = void 0;
|
|
4
4
|
var useNewsroomContext_1 = require("./useNewsroomContext");
|
|
5
|
-
var
|
|
5
|
+
var useCurrentStory = function () {
|
|
6
6
|
var context = (0, useNewsroomContext_1.useNewsroomContext)();
|
|
7
|
-
return context.
|
|
7
|
+
return context.currentStory;
|
|
8
8
|
};
|
|
9
|
-
exports.
|
|
10
|
-
//# sourceMappingURL=
|
|
9
|
+
exports.useCurrentStory = useCurrentStory;
|
|
10
|
+
//# sourceMappingURL=useCurrentStory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCurrentStory.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useCurrentStory.ts"],"names":[],"mappings":";;;AAAA,2DAA0D;AAEnD,IAAM,eAAe,GAAG;IAC3B,IAAM,OAAO,GAAG,IAAA,uCAAkB,GAAE,CAAC;IAErC,OAAO,OAAO,CAAC,YAAY,CAAC;AAChC,CAAC,CAAC;AAJW,QAAA,eAAe,mBAI1B"}
|
|
@@ -4,13 +4,13 @@ exports.useGetTranslationUrl = void 0;
|
|
|
4
4
|
var router_1 = require("next/router");
|
|
5
5
|
var react_1 = require("react");
|
|
6
6
|
var data_fetching_1 = require("../../data-fetching");
|
|
7
|
-
var
|
|
8
|
-
var
|
|
7
|
+
var useCurrentCategory_1 = require("./useCurrentCategory");
|
|
8
|
+
var useCurrentStory_1 = require("./useCurrentStory");
|
|
9
9
|
// Determine correct URL for translated stories/categories with a fallback to homepage
|
|
10
|
-
function getTranslationUrl(locale, path,
|
|
11
|
-
if (
|
|
12
|
-
if ((0, data_fetching_1.getCategoryHasTranslation)(
|
|
13
|
-
return (0, data_fetching_1.getCategoryUrl)(
|
|
10
|
+
function getTranslationUrl(locale, path, currentCategory, currentStory, noFallback) {
|
|
11
|
+
if (currentCategory) {
|
|
12
|
+
if ((0, data_fetching_1.getCategoryHasTranslation)(currentCategory, locale)) {
|
|
13
|
+
return (0, data_fetching_1.getCategoryUrl)(currentCategory, locale);
|
|
14
14
|
}
|
|
15
15
|
if (noFallback) {
|
|
16
16
|
return '';
|
|
@@ -18,8 +18,8 @@ function getTranslationUrl(locale, path, selectedCategory, selectedStory, noFall
|
|
|
18
18
|
return '/';
|
|
19
19
|
}
|
|
20
20
|
var localeCode = locale.toUnderscoreCode();
|
|
21
|
-
if (
|
|
22
|
-
var translatedStory =
|
|
21
|
+
if (currentStory && currentStory.culture.locale !== localeCode) {
|
|
22
|
+
var translatedStory = currentStory.translations.find(function (_a) {
|
|
23
23
|
var culture = _a.culture;
|
|
24
24
|
return culture.locale === localeCode;
|
|
25
25
|
});
|
|
@@ -35,11 +35,11 @@ function getTranslationUrl(locale, path, selectedCategory, selectedStory, noFall
|
|
|
35
35
|
}
|
|
36
36
|
function useGetTranslationUrl() {
|
|
37
37
|
var asPath = (0, router_1.useRouter)().asPath;
|
|
38
|
-
var
|
|
39
|
-
var
|
|
38
|
+
var currentCategory = (0, useCurrentCategory_1.useCurrentCategory)();
|
|
39
|
+
var currentStory = (0, useCurrentStory_1.useCurrentStory)();
|
|
40
40
|
return (0, react_1.useCallback)(function (locale, noFallback) {
|
|
41
|
-
return getTranslationUrl(locale, asPath,
|
|
42
|
-
}, [asPath,
|
|
41
|
+
return getTranslationUrl(locale, asPath, currentCategory, currentStory, noFallback);
|
|
42
|
+
}, [asPath, currentCategory, currentStory]);
|
|
43
43
|
}
|
|
44
44
|
exports.useGetTranslationUrl = useGetTranslationUrl;
|
|
45
45
|
//# sourceMappingURL=useGetTranslationUrl.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGetTranslationUrl.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useGetTranslationUrl.ts"],"names":[],"mappings":";;;AACA,sCAAwC;AACxC,+BAAoC;AAEpC,qDAAgF;AAGhF,
|
|
1
|
+
{"version":3,"file":"useGetTranslationUrl.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useGetTranslationUrl.ts"],"names":[],"mappings":";;;AACA,sCAAwC;AACxC,+BAAoC;AAEpC,qDAAgF;AAGhF,2DAA0D;AAC1D,qDAAoD;AAEpD,sFAAsF;AACtF,SAAS,iBAAiB,CACtB,MAAoB,EACpB,IAAY,EACZ,eAA0B,EAC1B,YAA4B,EAC5B,UAAoB;IAEpB,IAAI,eAAe,EAAE;QACjB,IAAI,IAAA,yCAAyB,EAAC,eAAe,EAAE,MAAM,CAAC,EAAE;YACpD,OAAO,IAAA,8BAAc,EAAC,eAAe,EAAE,MAAM,CAAC,CAAC;SAClD;QAED,IAAI,UAAU,EAAE;YACZ,OAAO,EAAE,CAAC;SACb;QAED,OAAO,GAAG,CAAC;KACd;IAED,IAAM,UAAU,GAAG,MAAM,CAAC,gBAAgB,EAAE,CAAC;IAE7C,IAAI,YAAY,IAAI,YAAY,CAAC,OAAO,CAAC,MAAM,KAAK,UAAU,EAAE;QAC5D,IAAM,eAAe,GAAG,YAAY,CAAC,YAAY,CAAC,IAAI,CAClD,UAAC,EAAW;gBAAT,OAAO,aAAA;YAAO,OAAA,OAAO,CAAC,MAAM,KAAK,UAAU;QAA7B,CAA6B,CACjD,CAAC;QACF,IAAI,eAAe,EAAE;YACjB,OAAO,WAAI,eAAe,CAAC,IAAI,CAAE,CAAC;SACrC;QAED,IAAI,UAAU,EAAE;YACZ,OAAO,EAAE,CAAC;SACb;QAED,OAAO,GAAG,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,SAAgB,oBAAoB;IACxB,IAAA,MAAM,GAAK,IAAA,kBAAS,GAAE,OAAhB,CAAiB;IAC/B,IAAM,eAAe,GAAG,IAAA,uCAAkB,GAAE,CAAC;IAC7C,IAAM,YAAY,GAAG,IAAA,iCAAe,GAAE,CAAC;IAEvC,OAAO,IAAA,mBAAW,EACd,UAAC,MAAoB,EAAE,UAAoB;QACvC,OAAA,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,YAAY,EAAE,UAAU,CAAC;IAA5E,CAA4E,EAChF,CAAC,MAAM,EAAE,eAAe,EAAE,YAAY,CAAC,CAC1C,CAAC;AACN,CAAC;AAVD,oDAUC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const useNewsroomContext: () =>
|
|
1
|
+
import { NewsroomContextType } from '../context';
|
|
2
|
+
export declare const useNewsroomContext: () => NewsroomContextType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useNewsroomContext.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useNewsroomContext.ts"],"names":[],"mappings":";;;AAAA,+BAAmC;AAEnC,
|
|
1
|
+
{"version":3,"file":"useNewsroomContext.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useNewsroomContext.ts"],"names":[],"mappings":";;;AAAA,+BAAmC;AAEnC,sCAAkE;AAE3D,IAAM,kBAAkB,GAAG;IAC9B,mGAAmG;IACnG,iFAAiF;IACjF,IAAM,eAAe,GAAoC,IAAA,kBAAU,EAAC,yBAAe,CAAC,CAAC;IACrF,IAAI,CAAC,eAAe,EAAE;QAClB,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAC;KAC3F;IAED,OAAO,eAAe,CAAC;AAC3B,CAAC,CAAC;AATW,QAAA,kBAAkB,sBAS7B"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { NewsroomContext, NewsroomContextProvider } from './context';
|
|
1
|
+
export { NewsroomContext, NewsroomContextProvider, NewsroomContextProps } from './context';
|
|
2
2
|
export * from './hooks';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/newsroom-context/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/newsroom-context/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,qCAA2F;AAAlF,0GAAA,eAAe,OAAA;AAAE,kHAAA,uBAAuB,OAAA;AACjD,0CAAwB"}
|
package/build/types.d.ts
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
3
|
-
export interface
|
|
4
|
-
newsroom: Newsroom;
|
|
5
|
-
companyInformation: NewsroomCompanyInformation;
|
|
6
|
-
categories: Category[];
|
|
7
|
-
languages: NewsroomLanguageSettings[];
|
|
8
|
-
localeCode: string;
|
|
9
|
-
/**
|
|
10
|
-
* `false` means it's the default locale
|
|
11
|
-
*/
|
|
12
|
-
shortestLocaleCode: string | false;
|
|
1
|
+
import type { CultureRef, ExtraStoryFields, Story } from '@prezly/sdk';
|
|
2
|
+
import { NewsroomContextProps } from './newsroom-context';
|
|
3
|
+
export interface ServerSidePageProps {
|
|
13
4
|
localeResolved: boolean;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
contacts?: NewsroomContact[];
|
|
18
|
-
isTrackingEnabled?: boolean;
|
|
19
|
-
selectedCategory?: Category;
|
|
20
|
-
selectedStory?: ExtendedStory;
|
|
5
|
+
}
|
|
6
|
+
export interface PageProps {
|
|
7
|
+
newsroomContextProps: NewsroomContextProps;
|
|
21
8
|
}
|
|
22
9
|
export interface AlgoliaCategoryRef {
|
|
23
10
|
id: number;
|
|
@@ -31,4 +18,3 @@ export declare type AlgoliaStory = Pick<Story, 'uuid' | 'slug' | 'title' | 'subt
|
|
|
31
18
|
culture: Pick<CultureRef, 'code' | 'name' | 'native_name' | 'language_code'>;
|
|
32
19
|
categories: AlgoliaCategoryRef[];
|
|
33
20
|
};
|
|
34
|
-
export declare type Translations = Record<string, string>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prezly/theme-kit-nextjs",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Data layer and utility library for developing Prezly themes with NextJS",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -29,12 +29,18 @@
|
|
|
29
29
|
"homepage": "https://github.com/prezly/theme-kit-nextjs#readme",
|
|
30
30
|
"scripts": {
|
|
31
31
|
"clean": "rimraf build/ node_modules/",
|
|
32
|
-
"typecheck": "tsc --noEmit --incremental --project .",
|
|
33
32
|
"prebuild": "npm run clean && npm install",
|
|
34
33
|
"build": "tsc --project .",
|
|
35
34
|
"test": "echo \"Tests? maybe next time ;)\" && exit 0",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
35
|
+
"typecheck": "tsc --noEmit --incremental --project .",
|
|
36
|
+
"lint": "eslint src",
|
|
37
|
+
"lint:fix": "npm run lint -- --fix",
|
|
38
|
+
"prettier": "prettier --check './**/*.(js|ts|tsx|css)'",
|
|
39
|
+
"prettier:fix": "npm run prettier -- --write --no-list-different",
|
|
40
|
+
"check": "npm run lint && npm run typecheck",
|
|
41
|
+
"prerelease": "npm run check && npm run build",
|
|
42
|
+
"release": "np",
|
|
43
|
+
"release:preview": "npm run prerelease && np prerelease --any-branch --no-release-draft --tag=preview"
|
|
38
44
|
},
|
|
39
45
|
"peerDependencies": {
|
|
40
46
|
"next": "^12.x",
|
|
@@ -50,7 +56,18 @@
|
|
|
50
56
|
"@types/parse-data-url": "^3.0.0",
|
|
51
57
|
"@types/react": "^17.0.38",
|
|
52
58
|
"@types/react-dom": "^17.0.11",
|
|
59
|
+
"eslint": "^8.8.0",
|
|
60
|
+
"eslint-config-airbnb": "^19.0.4",
|
|
61
|
+
"eslint-config-airbnb-typescript": "^16.1.0",
|
|
62
|
+
"eslint-config-prettier": "^8.3.0",
|
|
63
|
+
"eslint-import-resolver-typescript": "^2.5.0",
|
|
64
|
+
"eslint-plugin-import": "^2.25.4",
|
|
65
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
66
|
+
"eslint-plugin-react": "^7.28.0",
|
|
67
|
+
"eslint-plugin-react-hooks": "^4.3.0",
|
|
68
|
+
"eslint-plugin-sort-export-all": "^1.2.1",
|
|
53
69
|
"np": "^7.6.0",
|
|
70
|
+
"prettier": "^2.5.1",
|
|
54
71
|
"rimraf": "^3.0.2",
|
|
55
72
|
"typescript": "^4.5.5"
|
|
56
73
|
},
|
|
@@ -2,22 +2,22 @@ import type { GetServerSidePropsContext } from 'next';
|
|
|
2
2
|
|
|
3
3
|
import { getPrezlyApi } from '../data-fetching';
|
|
4
4
|
|
|
5
|
-
interface
|
|
5
|
+
interface Options {
|
|
6
6
|
loadHomepageContacts?: boolean;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export async function
|
|
9
|
+
export async function getNewsroomServerSideProps(
|
|
10
10
|
context: GetServerSidePropsContext,
|
|
11
|
-
options:
|
|
11
|
+
options: Options = {},
|
|
12
12
|
) {
|
|
13
13
|
const { req: request, locale } = context;
|
|
14
14
|
|
|
15
15
|
const api = getPrezlyApi(request);
|
|
16
|
-
const
|
|
16
|
+
const serverSideProps = await api.getNewsroomServerSideProps(request, locale);
|
|
17
17
|
|
|
18
18
|
if (options.loadHomepageContacts) {
|
|
19
|
-
|
|
19
|
+
serverSideProps.newsroomContextProps.contacts = await api.getNewsroomContacts();
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
return { api,
|
|
22
|
+
return { api, serverSideProps };
|
|
23
23
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './getNewsroomServerSideProps';
|
|
2
2
|
export * from './processRequest';
|
|
@@ -1,31 +1,39 @@
|
|
|
1
1
|
import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { getShortestLocaleCode } from '../data-fetching';
|
|
4
|
+
import { getRedirectToCanonicalLocale, LocaleObject } from '../intl';
|
|
5
|
+
import { PageProps, ServerSidePageProps } from '../types';
|
|
5
6
|
|
|
6
|
-
export function processRequest<Props
|
|
7
|
+
export function processRequest<Props>(
|
|
7
8
|
context: GetServerSidePropsContext,
|
|
8
|
-
|
|
9
|
+
props: Props & PageProps & ServerSidePageProps,
|
|
9
10
|
canonicalUrl?: string,
|
|
10
|
-
customProps?: Omit<Props, keyof BasePageProps>,
|
|
11
11
|
): GetServerSidePropsResult<Props> {
|
|
12
|
-
const { locale, query } = context;
|
|
12
|
+
const { locale: nextLocale, query } = context;
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
const { localeResolved, ...pageProps } = props;
|
|
15
|
+
|
|
16
|
+
if (!localeResolved) {
|
|
15
17
|
return { notFound: true };
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
if (canonicalUrl) {
|
|
19
|
-
const
|
|
21
|
+
const { languages, localeCode } = pageProps.newsroomContextProps;
|
|
22
|
+
const currentLocale = LocaleObject.fromAnyCode(localeCode);
|
|
23
|
+
const shortestLocaleCode = getShortestLocaleCode(languages, currentLocale);
|
|
24
|
+
|
|
25
|
+
const redirect = getRedirectToCanonicalLocale(
|
|
26
|
+
shortestLocaleCode,
|
|
27
|
+
nextLocale,
|
|
28
|
+
canonicalUrl,
|
|
29
|
+
query,
|
|
30
|
+
);
|
|
20
31
|
if (redirect) {
|
|
21
32
|
return { redirect };
|
|
22
33
|
}
|
|
23
34
|
}
|
|
24
35
|
|
|
25
36
|
return {
|
|
26
|
-
props:
|
|
27
|
-
...basePageProps,
|
|
28
|
-
...customProps,
|
|
29
|
-
} as Props,
|
|
37
|
+
props: pageProps as Props & PageProps,
|
|
30
38
|
};
|
|
31
39
|
}
|
|
@@ -8,7 +8,7 @@ import PrezlySDK, {
|
|
|
8
8
|
import type { IncomingMessage } from 'http';
|
|
9
9
|
|
|
10
10
|
import { LocaleObject } from '../../intl';
|
|
11
|
-
import {
|
|
11
|
+
import { PageProps, ServerSidePageProps } from '../../types';
|
|
12
12
|
import { DEFAULT_PAGE_SIZE } from '../constants';
|
|
13
13
|
import { getAlgoliaSettings } from '../lib/getAlgoliaSettings';
|
|
14
14
|
|
|
@@ -17,7 +17,6 @@ import {
|
|
|
17
17
|
getDefaultLanguage,
|
|
18
18
|
getLanguageFromNextLocaleIsoCode,
|
|
19
19
|
getLanguageFromStory,
|
|
20
|
-
getShortestLocaleCode,
|
|
21
20
|
} from './languages';
|
|
22
21
|
import {
|
|
23
22
|
getContactsQuery,
|
|
@@ -199,11 +198,11 @@ export class PrezlyApi {
|
|
|
199
198
|
return this.sdk.newsroomThemes.getActive(this.newsroomUuid);
|
|
200
199
|
}
|
|
201
200
|
|
|
202
|
-
async
|
|
201
|
+
async getNewsroomServerSideProps(
|
|
203
202
|
request: IncomingMessage | undefined,
|
|
204
203
|
nextLocaleIsoCode?: string,
|
|
205
204
|
story?: Story,
|
|
206
|
-
): Promise<
|
|
205
|
+
): Promise<PageProps & ServerSidePageProps> {
|
|
207
206
|
const [newsroom, languages, categories, themePreset] = await Promise.all([
|
|
208
207
|
this.getNewsroom(),
|
|
209
208
|
this.getNewsroomLanguages(),
|
|
@@ -218,7 +217,6 @@ export class PrezlyApi {
|
|
|
218
217
|
|
|
219
218
|
const { code: localeCode } = currentLanguage || defaultLanguage;
|
|
220
219
|
const locale = LocaleObject.fromAnyCode(localeCode);
|
|
221
|
-
const shortestLocaleCode = getShortestLocaleCode(languages, locale);
|
|
222
220
|
|
|
223
221
|
// TODO: if no information given for current language, show boilerplate from default language
|
|
224
222
|
const companyInformation = getCompanyInformation(languages, locale);
|
|
@@ -226,15 +224,16 @@ export class PrezlyApi {
|
|
|
226
224
|
const algoliaSettings = getAlgoliaSettings(request);
|
|
227
225
|
|
|
228
226
|
return {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
227
|
+
newsroomContextProps: {
|
|
228
|
+
newsroom,
|
|
229
|
+
companyInformation,
|
|
230
|
+
categories,
|
|
231
|
+
languages,
|
|
232
|
+
localeCode,
|
|
233
|
+
themePreset,
|
|
234
|
+
algoliaSettings,
|
|
235
|
+
},
|
|
235
236
|
localeResolved: Boolean(currentLanguage),
|
|
236
|
-
themePreset,
|
|
237
|
-
algoliaSettings,
|
|
238
237
|
};
|
|
239
238
|
}
|
|
240
239
|
}
|
package/src/intl/locale.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { Redirect } from 'next';
|
|
2
2
|
import { ParsedUrlQuery, stringify } from 'querystring';
|
|
3
3
|
|
|
4
|
-
import { BasePageProps } from '../types';
|
|
5
|
-
|
|
6
4
|
import { LocaleObject } from './localeObject';
|
|
7
5
|
|
|
8
6
|
export const DEFAULT_LOCALE = 'en';
|
|
@@ -76,12 +74,11 @@ export function getSupportedLocaleIsoCode(locale: LocaleObject): string {
|
|
|
76
74
|
}
|
|
77
75
|
|
|
78
76
|
export function getRedirectToCanonicalLocale(
|
|
79
|
-
|
|
77
|
+
shortestLocaleCode: string | false,
|
|
80
78
|
nextLocaleIsoCode: string | undefined,
|
|
81
79
|
redirectPath: string,
|
|
82
80
|
query?: ParsedUrlQuery,
|
|
83
81
|
): Redirect | undefined {
|
|
84
|
-
const { shortestLocaleCode } = basePageProps;
|
|
85
82
|
const shortestLocaleSlug = shortestLocaleCode
|
|
86
83
|
? LocaleObject.fromAnyCode(shortestLocaleCode).toUrlSlug()
|
|
87
84
|
: shortestLocaleCode;
|
|
@@ -7,41 +7,39 @@ import type {
|
|
|
7
7
|
NewsroomLanguageSettings,
|
|
8
8
|
NewsroomThemePreset,
|
|
9
9
|
} from '@prezly/sdk';
|
|
10
|
-
import
|
|
10
|
+
import { createContext, FunctionComponent, useMemo } from 'react';
|
|
11
11
|
|
|
12
12
|
import type { AlgoliaSettings } from '../data-fetching';
|
|
13
13
|
import { DEFAULT_LOCALE, LocaleObject } from '../intl';
|
|
14
14
|
|
|
15
|
-
export interface
|
|
15
|
+
export interface NewsroomContextType {
|
|
16
16
|
newsroom: Newsroom;
|
|
17
17
|
companyInformation: NewsroomCompanyInformation;
|
|
18
18
|
categories: Category[];
|
|
19
19
|
contacts?: NewsroomContact[];
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
currentCategory?: Category;
|
|
21
|
+
currentStory?: ExtendedStory;
|
|
22
22
|
languages: NewsroomLanguageSettings[];
|
|
23
23
|
locale: LocaleObject;
|
|
24
|
-
hasError?: boolean;
|
|
25
24
|
themePreset: NewsroomThemePreset;
|
|
26
25
|
algoliaSettings: AlgoliaSettings;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
interface
|
|
28
|
+
export interface NewsroomContextProps extends Omit<NewsroomContextType, 'locale'> {
|
|
30
29
|
localeCode: string;
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
export const NewsroomContext = createContext<
|
|
32
|
+
export const NewsroomContext = createContext<NewsroomContextType | undefined>(undefined);
|
|
34
33
|
|
|
35
|
-
export const NewsroomContextProvider: FunctionComponent<
|
|
34
|
+
export const NewsroomContextProvider: FunctionComponent<NewsroomContextProps> = ({
|
|
36
35
|
categories,
|
|
37
36
|
contacts,
|
|
38
37
|
newsroom,
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
currentCategory,
|
|
39
|
+
currentStory,
|
|
41
40
|
companyInformation,
|
|
42
41
|
languages,
|
|
43
42
|
localeCode,
|
|
44
|
-
hasError,
|
|
45
43
|
themePreset,
|
|
46
44
|
algoliaSettings,
|
|
47
45
|
children,
|
|
@@ -57,12 +55,11 @@ export const NewsroomContextProvider: FunctionComponent<Props> = ({
|
|
|
57
55
|
categories,
|
|
58
56
|
contacts,
|
|
59
57
|
newsroom,
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
currentCategory,
|
|
59
|
+
currentStory,
|
|
62
60
|
companyInformation,
|
|
63
61
|
languages,
|
|
64
62
|
locale,
|
|
65
|
-
hasError,
|
|
66
63
|
themePreset,
|
|
67
64
|
algoliaSettings,
|
|
68
65
|
}}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export * from './useAlgoliaSettings';
|
|
2
2
|
export * from './useCategories';
|
|
3
3
|
export * from './useCompanyInformation';
|
|
4
|
+
export * from './useCurrentCategory';
|
|
4
5
|
export * from './useCurrentLocale';
|
|
6
|
+
export * from './useCurrentStory';
|
|
5
7
|
export * from './useGetLinkLocaleSlug';
|
|
6
8
|
export * from './useGetTranslationUrl';
|
|
7
9
|
export * from './useLanguages';
|
|
8
10
|
export * from './useNewsroom';
|
|
9
11
|
export * from './useNewsroomContext';
|
|
10
|
-
export * from './useSelectedCategory';
|
|
11
|
-
export * from './useSelectedStory';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useNewsroomContext } from './useNewsroomContext';
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const useCurrentCategory = () => {
|
|
4
4
|
const context = useNewsroomContext();
|
|
5
5
|
|
|
6
|
-
return context.
|
|
6
|
+
return context.currentCategory;
|
|
7
7
|
};
|
|
@@ -5,20 +5,20 @@ import { useCallback } from 'react';
|
|
|
5
5
|
import { getCategoryHasTranslation, getCategoryUrl } from '../../data-fetching';
|
|
6
6
|
import { LocaleObject } from '../../intl';
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { useCurrentCategory } from './useCurrentCategory';
|
|
9
|
+
import { useCurrentStory } from './useCurrentStory';
|
|
10
10
|
|
|
11
11
|
// Determine correct URL for translated stories/categories with a fallback to homepage
|
|
12
12
|
function getTranslationUrl(
|
|
13
13
|
locale: LocaleObject,
|
|
14
14
|
path: string,
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
currentCategory?: Category,
|
|
16
|
+
currentStory?: ExtendedStory,
|
|
17
17
|
noFallback?: boolean,
|
|
18
18
|
) {
|
|
19
|
-
if (
|
|
20
|
-
if (getCategoryHasTranslation(
|
|
21
|
-
return getCategoryUrl(
|
|
19
|
+
if (currentCategory) {
|
|
20
|
+
if (getCategoryHasTranslation(currentCategory, locale)) {
|
|
21
|
+
return getCategoryUrl(currentCategory, locale);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
if (noFallback) {
|
|
@@ -30,8 +30,8 @@ function getTranslationUrl(
|
|
|
30
30
|
|
|
31
31
|
const localeCode = locale.toUnderscoreCode();
|
|
32
32
|
|
|
33
|
-
if (
|
|
34
|
-
const translatedStory =
|
|
33
|
+
if (currentStory && currentStory.culture.locale !== localeCode) {
|
|
34
|
+
const translatedStory = currentStory.translations.find(
|
|
35
35
|
({ culture }) => culture.locale === localeCode,
|
|
36
36
|
);
|
|
37
37
|
if (translatedStory) {
|
|
@@ -50,12 +50,12 @@ function getTranslationUrl(
|
|
|
50
50
|
|
|
51
51
|
export function useGetTranslationUrl() {
|
|
52
52
|
const { asPath } = useRouter();
|
|
53
|
-
const
|
|
54
|
-
const
|
|
53
|
+
const currentCategory = useCurrentCategory();
|
|
54
|
+
const currentStory = useCurrentStory();
|
|
55
55
|
|
|
56
56
|
return useCallback(
|
|
57
57
|
(locale: LocaleObject, noFallback?: boolean) =>
|
|
58
|
-
getTranslationUrl(locale, asPath,
|
|
59
|
-
[asPath,
|
|
58
|
+
getTranslationUrl(locale, asPath, currentCategory, currentStory, noFallback),
|
|
59
|
+
[asPath, currentCategory, currentStory],
|
|
60
60
|
);
|
|
61
61
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { useContext } from 'react';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { NewsroomContext, NewsroomContextType } from '../context';
|
|
4
4
|
|
|
5
5
|
export const useNewsroomContext = () => {
|
|
6
6
|
// TS Compiler is having a hard time figuring this type implicitly, so we have to set it explicitly
|
|
7
7
|
// See https://github.com/microsoft/TypeScript/issues/5711#issuecomment-340283439
|
|
8
|
-
const newsroomContext:
|
|
8
|
+
const newsroomContext: NewsroomContextType | undefined = useContext(NewsroomContext);
|
|
9
9
|
if (!newsroomContext) {
|
|
10
10
|
throw new Error('No `NewsroomContextProvider` found when calling `useNewsroomContext`');
|
|
11
11
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { NewsroomContext, NewsroomContextProvider } from './context';
|
|
1
|
+
export { NewsroomContext, NewsroomContextProvider, NewsroomContextProps } from './context';
|
|
2
2
|
export * from './hooks';
|
package/src/types.ts
CHANGED
|
@@ -1,37 +1,16 @@
|
|
|
1
1
|
// TODO: Re-arrange the types to make them isolated to respective libs where possible
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
Category,
|
|
5
|
-
CultureRef,
|
|
6
|
-
ExtendedStory,
|
|
7
|
-
ExtraStoryFields,
|
|
8
|
-
Newsroom,
|
|
9
|
-
NewsroomCompanyInformation,
|
|
10
|
-
NewsroomContact,
|
|
11
|
-
NewsroomLanguageSettings,
|
|
12
|
-
NewsroomThemePreset,
|
|
13
|
-
Story,
|
|
14
|
-
} from '@prezly/sdk';
|
|
2
|
+
import type { CultureRef, ExtraStoryFields, Story } from '@prezly/sdk';
|
|
15
3
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
categories: Category[];
|
|
20
|
-
languages: NewsroomLanguageSettings[];
|
|
21
|
-
localeCode: string;
|
|
22
|
-
/**
|
|
23
|
-
* `false` means it's the default locale
|
|
24
|
-
*/
|
|
25
|
-
shortestLocaleCode: string | false;
|
|
4
|
+
import { NewsroomContextProps } from './newsroom-context';
|
|
5
|
+
|
|
6
|
+
export interface ServerSidePageProps {
|
|
26
7
|
localeResolved: boolean;
|
|
27
|
-
themePreset: NewsroomThemePreset;
|
|
28
|
-
algoliaSettings: AlgoliaSettings;
|
|
29
|
-
translations?: Translations;
|
|
30
|
-
contacts?: NewsroomContact[];
|
|
31
|
-
isTrackingEnabled?: boolean;
|
|
32
|
-
selectedCategory?: Category;
|
|
33
|
-
selectedStory?: ExtendedStory;
|
|
34
8
|
}
|
|
9
|
+
|
|
10
|
+
export interface PageProps {
|
|
11
|
+
newsroomContextProps: NewsroomContextProps;
|
|
12
|
+
}
|
|
13
|
+
|
|
35
14
|
export interface AlgoliaCategoryRef {
|
|
36
15
|
id: number;
|
|
37
16
|
name: string;
|
|
@@ -46,5 +25,3 @@ export type AlgoliaStory = Pick<Story, 'uuid' | 'slug' | 'title' | 'subtitle'> &
|
|
|
46
25
|
culture: Pick<CultureRef, 'code' | 'name' | 'native_name' | 'language_code'>;
|
|
47
26
|
categories: AlgoliaCategoryRef[];
|
|
48
27
|
};
|
|
49
|
-
|
|
50
|
-
export type Translations = Record<string, string>;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { GetServerSidePropsContext } from 'next';
|
|
2
|
-
interface BasePagePropsOptions {
|
|
3
|
-
loadHomepageContacts?: boolean;
|
|
4
|
-
}
|
|
5
|
-
export declare function getBasePageProps(context: GetServerSidePropsContext, options?: BasePagePropsOptions): Promise<{
|
|
6
|
-
api: import("../data-fetching/api/PrezlyApi").PrezlyApi;
|
|
7
|
-
basePageProps: import("..").BasePageProps;
|
|
8
|
-
}>;
|
|
9
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getBasePageProps.js","sourceRoot":"","sources":["../../src/adapter-nextjs/getBasePageProps.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,kDAAgD;AAMhD,SAAsB,gBAAgB,CAClC,OAAkC,EAClC,OAAkC;IAAlC,wBAAA,EAAA,YAAkC;;;;;;oBAErB,OAAO,GAAa,OAAO,IAApB,EAAE,MAAM,GAAK,OAAO,OAAZ,CAAa;oBAEnC,GAAG,GAAG,IAAA,4BAAY,EAAC,OAAO,CAAC,CAAC;oBACZ,qBAAM,GAAG,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,EAAA;;oBAA3D,aAAa,GAAG,SAA2C;yBAE7D,OAAO,CAAC,oBAAoB,EAA5B,wBAA4B;oBAC5B,KAAA,aAAa,CAAA;oBAAY,qBAAM,GAAG,CAAC,mBAAmB,EAAE,EAAA;;oBAAxD,GAAc,QAAQ,GAAG,SAA+B,CAAC;;wBAG7D,sBAAO,EAAE,GAAG,KAAA,EAAE,aAAa,eAAA,EAAE,EAAC;;;;CACjC;AAdD,4CAcC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useSelectedCategory: () => import("@prezly/sdk").Category | undefined;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useSelectedCategory = void 0;
|
|
4
|
-
var useNewsroomContext_1 = require("./useNewsroomContext");
|
|
5
|
-
var useSelectedCategory = function () {
|
|
6
|
-
var context = (0, useNewsroomContext_1.useNewsroomContext)();
|
|
7
|
-
return context.selectedCategory;
|
|
8
|
-
};
|
|
9
|
-
exports.useSelectedCategory = useSelectedCategory;
|
|
10
|
-
//# sourceMappingURL=useSelectedCategory.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useSelectedCategory.js","sourceRoot":"","sources":["../../../src/newsroom-context/hooks/useSelectedCategory.ts"],"names":[],"mappings":";;;AAAA,2DAA0D;AAEnD,IAAM,mBAAmB,GAAG;IAC/B,IAAM,OAAO,GAAG,IAAA,uCAAkB,GAAE,CAAC;IAErC,OAAO,OAAO,CAAC,gBAAgB,CAAC;AACpC,CAAC,CAAC;AAJW,QAAA,mBAAmB,uBAI9B"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useSelectedStory: () => import("@prezly/sdk").ExtendedStory | undefined;
|