@prezly/theme-kit-nextjs 1.1.3 → 1.2.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 (56) hide show
  1. package/README.md +167 -3
  2. package/build/adapter-nextjs/getNewsroomServerSideProps.d.ts +9 -0
  3. package/build/adapter-nextjs/{getBasePageProps.js → getNewsroomServerSideProps.js} +9 -9
  4. package/build/adapter-nextjs/getNewsroomServerSideProps.js.map +1 -0
  5. package/build/adapter-nextjs/index.d.ts +1 -1
  6. package/build/adapter-nextjs/index.js +1 -1
  7. package/build/adapter-nextjs/index.js.map +1 -1
  8. package/build/adapter-nextjs/processRequest.d.ts +2 -2
  9. package/build/adapter-nextjs/processRequest.js +19 -14
  10. package/build/adapter-nextjs/processRequest.js.map +1 -1
  11. package/build/data-fetching/api/PrezlyApi.d.ts +2 -2
  12. package/build/data-fetching/api/PrezlyApi.js +11 -11
  13. package/build/data-fetching/api/PrezlyApi.js.map +1 -1
  14. package/build/intl/locale.d.ts +1 -2
  15. package/build/intl/locale.js +1 -2
  16. package/build/intl/locale.js.map +1 -1
  17. package/build/newsroom-context/context.d.ts +8 -10
  18. package/build/newsroom-context/context.js +17 -25
  19. package/build/newsroom-context/context.js.map +1 -1
  20. package/build/newsroom-context/hooks/index.d.ts +2 -2
  21. package/build/newsroom-context/hooks/index.js +2 -2
  22. package/build/newsroom-context/hooks/index.js.map +1 -1
  23. package/build/newsroom-context/hooks/useCurrentCategory.d.ts +1 -0
  24. package/build/newsroom-context/hooks/useCurrentCategory.js +10 -0
  25. package/build/newsroom-context/hooks/useCurrentCategory.js.map +1 -0
  26. package/build/newsroom-context/hooks/useCurrentStory.d.ts +1 -0
  27. package/build/newsroom-context/hooks/{useSelectedStory.js → useCurrentStory.js} +5 -5
  28. package/build/newsroom-context/hooks/useCurrentStory.js.map +1 -0
  29. package/build/newsroom-context/hooks/useGetTranslationUrl.js +12 -12
  30. package/build/newsroom-context/hooks/useGetTranslationUrl.js.map +1 -1
  31. package/build/newsroom-context/hooks/useNewsroomContext.d.ts +2 -2
  32. package/build/newsroom-context/hooks/useNewsroomContext.js.map +1 -1
  33. package/build/newsroom-context/index.d.ts +1 -1
  34. package/build/newsroom-context/index.js.map +1 -1
  35. package/build/types.d.ts +6 -20
  36. package/package.json +21 -4
  37. package/src/adapter-nextjs/{getBasePageProps.ts → getNewsroomServerSideProps.ts} +6 -6
  38. package/src/adapter-nextjs/index.ts +1 -1
  39. package/src/adapter-nextjs/processRequest.ts +20 -12
  40. package/src/data-fetching/api/PrezlyApi.ts +12 -13
  41. package/src/intl/locale.ts +1 -4
  42. package/src/newsroom-context/context.tsx +12 -15
  43. package/src/newsroom-context/hooks/index.ts +2 -2
  44. package/src/newsroom-context/hooks/{useSelectedCategory.ts → useCurrentCategory.ts} +2 -2
  45. package/src/newsroom-context/hooks/{useSelectedStory.ts → useCurrentStory.ts} +2 -2
  46. package/src/newsroom-context/hooks/useGetTranslationUrl.ts +14 -14
  47. package/src/newsroom-context/hooks/useNewsroomContext.ts +2 -2
  48. package/src/newsroom-context/index.ts +1 -1
  49. package/src/types.ts +9 -31
  50. package/build/adapter-nextjs/getBasePageProps.d.ts +0 -9
  51. package/build/adapter-nextjs/getBasePageProps.js.map +0 -1
  52. package/build/newsroom-context/hooks/useSelectedCategory.d.ts +0 -1
  53. package/build/newsroom-context/hooks/useSelectedCategory.js +0 -10
  54. package/build/newsroom-context/hooks/useSelectedCategory.js.map +0 -1
  55. package/build/newsroom-context/hooks/useSelectedStory.d.ts +0 -1
  56. 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.useSelectedStory = void 0;
3
+ exports.useCurrentStory = void 0;
4
4
  var useNewsroomContext_1 = require("./useNewsroomContext");
5
- var useSelectedStory = function () {
5
+ var useCurrentStory = function () {
6
6
  var context = (0, useNewsroomContext_1.useNewsroomContext)();
7
- return context.selectedStory;
7
+ return context.currentStory;
8
8
  };
9
- exports.useSelectedStory = useSelectedStory;
10
- //# sourceMappingURL=useSelectedStory.js.map
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 useSelectedCategory_1 = require("./useSelectedCategory");
8
- var useSelectedStory_1 = require("./useSelectedStory");
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, selectedCategory, selectedStory, noFallback) {
11
- if (selectedCategory) {
12
- if ((0, data_fetching_1.getCategoryHasTranslation)(selectedCategory, locale)) {
13
- return (0, data_fetching_1.getCategoryUrl)(selectedCategory, locale);
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 (selectedStory && selectedStory.culture.locale !== localeCode) {
22
- var translatedStory = selectedStory.translations.find(function (_a) {
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 selectedCategory = (0, useSelectedCategory_1.useSelectedCategory)();
39
- var selectedStory = (0, useSelectedStory_1.useSelectedStory)();
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, selectedCategory, selectedStory, noFallback);
42
- }, [asPath, selectedCategory, selectedStory]);
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,6DAA4D;AAC5D,uDAAsD;AAEtD,sFAAsF;AACtF,SAAS,iBAAiB,CACtB,MAAoB,EACpB,IAAY,EACZ,gBAA2B,EAC3B,aAAqB,EACrB,UAAoB;IAEpB,IAAI,gBAAgB,EAAE;QAClB,IAAI,IAAA,yCAAyB,EAAC,gBAAgB,EAAE,MAAM,CAAC,EAAE;YACrD,OAAO,IAAA,8BAAc,EAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;SACnD;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,aAAa,IAAI,aAAa,CAAC,OAAO,CAAC,MAAM,KAAK,UAAU,EAAE;QAC9D,IAAM,eAAe,GAAG,aAAa,CAAC,YAAY,CAAC,IAAI,CACnD,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,gBAAgB,GAAG,IAAA,yCAAmB,GAAE,CAAC;IAC/C,IAAM,aAAa,GAAG,IAAA,mCAAgB,GAAE,CAAC;IAEzC,OAAO,IAAA,mBAAW,EACd,UAAC,MAAoB,EAAE,UAAoB;QACvC,OAAA,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,aAAa,EAAE,UAAU,CAAC;IAA9E,CAA8E,EAClF,CAAC,MAAM,EAAE,gBAAgB,EAAE,aAAa,CAAC,CAC5C,CAAC;AACN,CAAC;AAVD,oDAUC"}
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 { INewsroomContext } from '../context';
2
- export declare const useNewsroomContext: () => INewsroomContext;
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,sCAA+D;AAExD,IAAM,kBAAkB,GAAG;IAC9B,mGAAmG;IACnG,iFAAiF;IACjF,IAAM,eAAe,GAAiC,IAAA,kBAAU,EAAC,yBAAe,CAAC,CAAC;IAClF,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
+ {"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,qCAAqE;AAA5D,0GAAA,eAAe,OAAA;AAAE,kHAAA,uBAAuB,OAAA;AACjD,0CAAwB"}
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 { AlgoliaSettings } from './data-fetching';
2
- import type { Category, CultureRef, ExtraStoryFields, Newsroom, NewsroomCompanyInformation, NewsroomContact, NewsroomLanguageSettings, NewsroomThemePreset, Story } from '@prezly/sdk';
3
- export interface BasePageProps {
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
- themePreset: NewsroomThemePreset;
15
- algoliaSettings: AlgoliaSettings;
16
- translations?: Translations;
17
- contacts?: NewsroomContact[];
18
- isTrackingEnabled?: boolean;
19
- selectedCategory?: Category;
20
- selectedStory?: Story;
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",
3
+ "version": "1.2.0",
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
- "prerelease": "npm run typecheck && npm run build",
37
- "release": "np"
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 BasePagePropsOptions {
5
+ interface Options {
6
6
  loadHomepageContacts?: boolean;
7
7
  }
8
8
 
9
- export async function getBasePageProps(
9
+ export async function getNewsroomServerSideProps(
10
10
  context: GetServerSidePropsContext,
11
- options: BasePagePropsOptions = {},
11
+ options: Options = {},
12
12
  ) {
13
13
  const { req: request, locale } = context;
14
14
 
15
15
  const api = getPrezlyApi(request);
16
- const basePageProps = await api.getBasePageProps(request, locale);
16
+ const serverSideProps = await api.getNewsroomServerSideProps(request, locale);
17
17
 
18
18
  if (options.loadHomepageContacts) {
19
- basePageProps.contacts = await api.getNewsroomContacts();
19
+ serverSideProps.newsroomContextProps.contacts = await api.getNewsroomContacts();
20
20
  }
21
21
 
22
- return { api, basePageProps };
22
+ return { api, serverSideProps };
23
23
  }
@@ -1,2 +1,2 @@
1
- export * from './getBasePageProps';
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 { getRedirectToCanonicalLocale } from '../intl';
4
- import { BasePageProps } from '../types';
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 extends BasePageProps>(
7
+ export function processRequest<Props>(
7
8
  context: GetServerSidePropsContext,
8
- basePageProps: BasePageProps,
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
- if (!basePageProps.localeResolved) {
14
+ const { localeResolved, ...pageProps } = props;
15
+
16
+ if (!localeResolved) {
15
17
  return { notFound: true };
16
18
  }
17
19
 
18
20
  if (canonicalUrl) {
19
- const redirect = getRedirectToCanonicalLocale(basePageProps, locale, canonicalUrl, query);
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 { BasePageProps } from '../../types';
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 getBasePageProps(
201
+ async getNewsroomServerSideProps(
203
202
  request: IncomingMessage | undefined,
204
203
  nextLocaleIsoCode?: string,
205
204
  story?: Story,
206
- ): Promise<BasePageProps> {
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
- newsroom,
230
- companyInformation,
231
- categories,
232
- languages,
233
- localeCode,
234
- shortestLocaleCode,
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
  }
@@ -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
- basePageProps: BasePageProps,
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;
@@ -1,47 +1,45 @@
1
1
  import type {
2
2
  Category,
3
+ ExtendedStory,
3
4
  Newsroom,
4
5
  NewsroomCompanyInformation,
5
6
  NewsroomContact,
6
7
  NewsroomLanguageSettings,
7
8
  NewsroomThemePreset,
8
- Story,
9
9
  } from '@prezly/sdk';
10
- import React, { createContext, FunctionComponent, useMemo } from 'react';
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 INewsroomContext {
15
+ export interface NewsroomContextType {
16
16
  newsroom: Newsroom;
17
17
  companyInformation: NewsroomCompanyInformation;
18
18
  categories: Category[];
19
19
  contacts?: NewsroomContact[];
20
- selectedCategory?: Category;
21
- selectedStory?: Story;
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 Props extends Omit<INewsroomContext, 'locale'> {
28
+ export interface NewsroomContextProps extends Omit<NewsroomContextType, 'locale'> {
30
29
  localeCode: string;
31
30
  }
32
31
 
33
- export const NewsroomContext = createContext<INewsroomContext | undefined>(undefined);
32
+ export const NewsroomContext = createContext<NewsroomContextType | undefined>(undefined);
34
33
 
35
- export const NewsroomContextProvider: FunctionComponent<Props> = ({
34
+ export const NewsroomContextProvider: FunctionComponent<NewsroomContextProps> = ({
36
35
  categories,
37
36
  contacts,
38
37
  newsroom,
39
- selectedCategory,
40
- selectedStory,
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
- selectedCategory,
61
- selectedStory,
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 useSelectedCategory = () => {
3
+ export const useCurrentCategory = () => {
4
4
  const context = useNewsroomContext();
5
5
 
6
- return context.selectedCategory;
6
+ return context.currentCategory;
7
7
  };
@@ -1,7 +1,7 @@
1
1
  import { useNewsroomContext } from './useNewsroomContext';
2
2
 
3
- export const useSelectedStory = () => {
3
+ export const useCurrentStory = () => {
4
4
  const context = useNewsroomContext();
5
5
 
6
- return context.selectedStory;
6
+ return context.currentStory;
7
7
  };
@@ -1,24 +1,24 @@
1
- import type { Category, Story } from '@prezly/sdk';
1
+ import type { Category, ExtendedStory } from '@prezly/sdk';
2
2
  import { useRouter } from 'next/router';
3
3
  import { useCallback } from 'react';
4
4
 
5
5
  import { getCategoryHasTranslation, getCategoryUrl } from '../../data-fetching';
6
6
  import { LocaleObject } from '../../intl';
7
7
 
8
- import { useSelectedCategory } from './useSelectedCategory';
9
- import { useSelectedStory } from './useSelectedStory';
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
- selectedCategory?: Category,
16
- selectedStory?: Story,
15
+ currentCategory?: Category,
16
+ currentStory?: ExtendedStory,
17
17
  noFallback?: boolean,
18
18
  ) {
19
- if (selectedCategory) {
20
- if (getCategoryHasTranslation(selectedCategory, locale)) {
21
- return getCategoryUrl(selectedCategory, locale);
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 (selectedStory && selectedStory.culture.locale !== localeCode) {
34
- const translatedStory = selectedStory.translations.find(
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 selectedCategory = useSelectedCategory();
54
- const selectedStory = useSelectedStory();
53
+ const currentCategory = useCurrentCategory();
54
+ const currentStory = useCurrentStory();
55
55
 
56
56
  return useCallback(
57
57
  (locale: LocaleObject, noFallback?: boolean) =>
58
- getTranslationUrl(locale, asPath, selectedCategory, selectedStory, noFallback),
59
- [asPath, selectedCategory, selectedStory],
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 { INewsroomContext, NewsroomContext } from '../context';
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: INewsroomContext | undefined = useContext(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,36 +1,16 @@
1
1
  // TODO: Re-arrange the types to make them isolated to respective libs where possible
2
- import type { AlgoliaSettings } from './data-fetching';
3
- import type {
4
- Category,
5
- CultureRef,
6
- ExtraStoryFields,
7
- Newsroom,
8
- NewsroomCompanyInformation,
9
- NewsroomContact,
10
- NewsroomLanguageSettings,
11
- NewsroomThemePreset,
12
- Story,
13
- } from '@prezly/sdk';
2
+ import type { CultureRef, ExtraStoryFields, Story } from '@prezly/sdk';
14
3
 
15
- export interface BasePageProps {
16
- newsroom: Newsroom;
17
- companyInformation: NewsroomCompanyInformation;
18
- categories: Category[];
19
- languages: NewsroomLanguageSettings[];
20
- localeCode: string;
21
- /**
22
- * `false` means it's the default locale
23
- */
24
- shortestLocaleCode: string | false;
4
+ import { NewsroomContextProps } from './newsroom-context';
5
+
6
+ export interface ServerSidePageProps {
25
7
  localeResolved: boolean;
26
- themePreset: NewsroomThemePreset;
27
- algoliaSettings: AlgoliaSettings;
28
- translations?: Translations;
29
- contacts?: NewsroomContact[];
30
- isTrackingEnabled?: boolean;
31
- selectedCategory?: Category;
32
- selectedStory?: Story;
33
8
  }
9
+
10
+ export interface PageProps {
11
+ newsroomContextProps: NewsroomContextProps;
12
+ }
13
+
34
14
  export interface AlgoliaCategoryRef {
35
15
  id: number;
36
16
  name: string;
@@ -45,5 +25,3 @@ export type AlgoliaStory = Pick<Story, 'uuid' | 'slug' | 'title' | 'subtitle'> &
45
25
  culture: Pick<CultureRef, 'code' | 'name' | 'native_name' | 'language_code'>;
46
26
  categories: AlgoliaCategoryRef[];
47
27
  };
48
-
49
- 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").Story | undefined;