@prezly/theme-kit-nextjs 4.2.0-alpha.1 → 4.2.0-alpha.3
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/build/cjs/components-nextjs/PageSeo/PageSeo.js +1 -0
- package/build/cjs/components-nextjs/PageSeo/PageSeo.js.map +1 -1
- package/build/cjs/components-nextjs/StorySeo/StorySeo.js +1 -0
- package/build/cjs/components-nextjs/StorySeo/StorySeo.js.map +1 -1
- package/build/cjs/components-nextjs/document.js +1 -0
- package/build/cjs/components-nextjs/document.js.map +1 -1
- package/build/cjs/newsroom-context/context.js +1 -0
- package/build/cjs/newsroom-context/context.js.map +1 -1
- package/build/esm/components-nextjs/PageSeo/PageSeo.js +1 -0
- package/build/esm/components-nextjs/PageSeo/PageSeo.js.map +1 -1
- package/build/esm/components-nextjs/StorySeo/StorySeo.js +1 -0
- package/build/esm/components-nextjs/StorySeo/StorySeo.js.map +1 -1
- package/build/esm/components-nextjs/document.js +1 -0
- package/build/esm/components-nextjs/document.js.map +1 -1
- package/build/esm/newsroom-context/context.js +1 -0
- package/build/esm/newsroom-context/context.js.map +1 -1
- package/build/tsconfig.cjs.tsbuildinfo +1 -1
- package/build/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/__mocks__/languages.ts +0 -479
- package/src/adapter-nextjs/api-handlers/fetchGalleries.ts +0 -24
- package/src/adapter-nextjs/api-handlers/fetchStories.ts +0 -43
- package/src/adapter-nextjs/api-handlers/index.ts +0 -2
- package/src/adapter-nextjs/getNewsroomServerSideProps.ts +0 -29
- package/src/adapter-nextjs/getNewsroomStaticProps.ts +0 -29
- package/src/adapter-nextjs/index.ts +0 -6
- package/src/adapter-nextjs/page-props/category.ts +0 -156
- package/src/adapter-nextjs/page-props/gallery.ts +0 -114
- package/src/adapter-nextjs/page-props/galleryAlbum.ts +0 -89
- package/src/adapter-nextjs/page-props/home.ts +0 -131
- package/src/adapter-nextjs/page-props/index.ts +0 -26
- package/src/adapter-nextjs/page-props/lib/types.ts +0 -8
- package/src/adapter-nextjs/page-props/search.ts +0 -53
- package/src/adapter-nextjs/page-props/sitemap/SitemapBuilder.ts +0 -56
- package/src/adapter-nextjs/page-props/sitemap/createPaths.ts +0 -21
- package/src/adapter-nextjs/page-props/sitemap/getServerSideProps.ts +0 -45
- package/src/adapter-nextjs/page-props/sitemap/index.ts +0 -5
- package/src/adapter-nextjs/page-props/sitemap/types.ts +0 -5
- package/src/adapter-nextjs/page-props/story.ts +0 -96
- package/src/adapter-nextjs/page-props/storyPreview.ts +0 -33
- package/src/adapter-nextjs/processRequest.ts +0 -50
- package/src/adapter-nextjs/processStaticRequest.ts +0 -32
- package/src/components-nextjs/PageSeo/PageSeo.tsx +0 -132
- package/src/components-nextjs/PageSeo/index.ts +0 -1
- package/src/components-nextjs/PageSeo/lib/getAbsoluteUrl.ts +0 -7
- package/src/components-nextjs/PageSeo/lib/index.ts +0 -1
- package/src/components-nextjs/StorySeo/StorySeo.tsx +0 -87
- package/src/components-nextjs/StorySeo/index.ts +0 -1
- package/src/components-nextjs/document.tsx +0 -37
- package/src/components-nextjs/index.ts +0 -3
- package/src/data-fetching/NextPrezlyApi.ts +0 -57
- package/src/data-fetching/getNextPrezlyApi.ts +0 -23
- package/src/data-fetching/index.ts +0 -1
- package/src/index.ts +0 -8
- package/src/infinite-loading/index.ts +0 -4
- package/src/infinite-loading/types.ts +0 -9
- package/src/infinite-loading/useInfiniteGalleriesLoading.ts +0 -54
- package/src/infinite-loading/useInfiniteLoading.ts +0 -67
- package/src/infinite-loading/useInfiniteStoriesLoading.ts +0 -94
- package/src/intl/index.ts +0 -2
- package/src/intl/languages.test.ts +0 -37
- package/src/intl/languages.ts +0 -40
- package/src/intl/locale.test.ts +0 -42
- package/src/intl/locale.ts +0 -39
- package/src/newsroom-context/context.tsx +0 -114
- package/src/newsroom-context/hooks/index.ts +0 -11
- package/src/newsroom-context/hooks/useAlgoliaSettings.ts +0 -7
- package/src/newsroom-context/hooks/useCategories.ts +0 -7
- package/src/newsroom-context/hooks/useCompanyInformation.ts +0 -7
- package/src/newsroom-context/hooks/useCurrentCategory.ts +0 -7
- package/src/newsroom-context/hooks/useCurrentLocale.ts +0 -7
- package/src/newsroom-context/hooks/useCurrentStory.ts +0 -7
- package/src/newsroom-context/hooks/useGetLinkLocaleSlug.ts +0 -36
- package/src/newsroom-context/hooks/useGetTranslationUrl.ts +0 -99
- package/src/newsroom-context/hooks/useLanguages.ts +0 -7
- package/src/newsroom-context/hooks/useNewsroom.ts +0 -7
- package/src/newsroom-context/hooks/useNewsroomContext.ts +0 -15
- package/src/newsroom-context/index.ts +0 -2
- package/src/sentry/client.ts +0 -10
- package/src/sentry/common.ts +0 -82
- package/src/sentry/index.ts +0 -2
- package/src/sentry/server.ts +0 -10
- package/src/types.ts +0 -16
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { NewsroomGallery } from '@prezly/sdk';
|
|
2
|
-
|
|
3
|
-
import type { PaginationProps } from './types';
|
|
4
|
-
import { useInfiniteLoading } from './useInfiniteLoading';
|
|
5
|
-
|
|
6
|
-
async function fetchGalleries(
|
|
7
|
-
page: number,
|
|
8
|
-
pageSize: number,
|
|
9
|
-
): Promise<{ galleries: NewsroomGallery[] }> {
|
|
10
|
-
const result = await fetch('/api/fetch-galleries', {
|
|
11
|
-
method: 'POST',
|
|
12
|
-
headers: {
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
14
|
-
'Content-Type': 'application/json',
|
|
15
|
-
},
|
|
16
|
-
body: JSON.stringify({
|
|
17
|
-
page,
|
|
18
|
-
pageSize,
|
|
19
|
-
}),
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
if (!result.ok) {
|
|
23
|
-
const { message } = await result.json();
|
|
24
|
-
throw new Error(message);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return result.json();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* NOTE: This hook depends on the presence of a Next API route (/api/fetch-galleries)!
|
|
32
|
-
* The handler for this route is exported from `apiHandlers/fetchGalleries.ts` file.
|
|
33
|
-
* See https://github.com/prezly/theme-nextjs-bea/blob/d381e9878187ca98815d240d3ffdf10a75993c14/pages/media/index.tsx#L23 for usage example
|
|
34
|
-
*/
|
|
35
|
-
export function useInfiniteGalleriesLoading(
|
|
36
|
-
initialGalleries: NewsroomGallery[],
|
|
37
|
-
pagination: PaginationProps,
|
|
38
|
-
) {
|
|
39
|
-
const { canLoadMore, data, isLoading, loadMore } = useInfiniteLoading<NewsroomGallery>({
|
|
40
|
-
fetchingFn: async (nextPage, pageSize) => {
|
|
41
|
-
const { galleries } = await fetchGalleries(nextPage, pageSize);
|
|
42
|
-
return galleries;
|
|
43
|
-
},
|
|
44
|
-
initialData: initialGalleries,
|
|
45
|
-
pagination,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
canLoadMore,
|
|
50
|
-
galleries: data,
|
|
51
|
-
isLoading,
|
|
52
|
-
loadMoreGalleries: loadMore,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { useAsync } from '@react-hookz/web';
|
|
2
|
-
import { useCallback, useEffect, useState } from 'react';
|
|
3
|
-
|
|
4
|
-
import type { PaginationProps } from './types';
|
|
5
|
-
|
|
6
|
-
interface Parameters<T> {
|
|
7
|
-
fetchingFn: (page: number, pageSize: number) => Promise<T[]>;
|
|
8
|
-
initialData: T[];
|
|
9
|
-
pagination: PaginationProps;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface State<T> {
|
|
13
|
-
canLoadMore: boolean;
|
|
14
|
-
currentPage: number;
|
|
15
|
-
data: T[];
|
|
16
|
-
isLoading: boolean;
|
|
17
|
-
loadMore: () => void;
|
|
18
|
-
resetData: () => void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function useInfiniteLoading<T>({
|
|
22
|
-
fetchingFn,
|
|
23
|
-
initialData,
|
|
24
|
-
pagination,
|
|
25
|
-
}: Parameters<T>): State<T> {
|
|
26
|
-
const [data, setData] = useState<T[]>(initialData);
|
|
27
|
-
const [currentPage, setCurrentPage] = useState(pagination.currentPage);
|
|
28
|
-
|
|
29
|
-
const { itemsTotal, pageSize, withHighlightedStory } = pagination;
|
|
30
|
-
const adjustedItemsTotal = withHighlightedStory ? itemsTotal - 1 : itemsTotal;
|
|
31
|
-
const pagesTotal = Math.ceil(adjustedItemsTotal / pageSize);
|
|
32
|
-
const canLoadMore = currentPage < pagesTotal;
|
|
33
|
-
|
|
34
|
-
const [{ error, status }, { execute: loadMoreFn }] = useAsync(async () => {
|
|
35
|
-
const nextPage = currentPage + 1;
|
|
36
|
-
const newData = await fetchingFn(nextPage, pageSize);
|
|
37
|
-
setCurrentPage(nextPage);
|
|
38
|
-
setData((currentData) => [...currentData, ...newData]);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const loadMore = useCallback(() => {
|
|
42
|
-
if (canLoadMore) {
|
|
43
|
-
loadMoreFn();
|
|
44
|
-
}
|
|
45
|
-
}, [canLoadMore, loadMoreFn]);
|
|
46
|
-
|
|
47
|
-
const resetData = useCallback(() => {
|
|
48
|
-
setData(initialData);
|
|
49
|
-
setCurrentPage(pagination.currentPage);
|
|
50
|
-
}, [pagination.currentPage, initialData]);
|
|
51
|
-
|
|
52
|
-
useEffect(() => {
|
|
53
|
-
if (error) {
|
|
54
|
-
// eslint-disable-next-line no-console
|
|
55
|
-
console.error(error);
|
|
56
|
-
}
|
|
57
|
-
}, [error]);
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
canLoadMore,
|
|
61
|
-
currentPage,
|
|
62
|
-
data,
|
|
63
|
-
isLoading: status === 'loading',
|
|
64
|
-
loadMore,
|
|
65
|
-
resetData,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import type { Category, Story } from '@prezly/sdk';
|
|
2
|
-
import type { LocaleObject } from '@prezly/theme-kit-core';
|
|
3
|
-
import { useEffect } from 'react';
|
|
4
|
-
|
|
5
|
-
import { useCurrentLocale } from '../newsroom-context';
|
|
6
|
-
|
|
7
|
-
import type { PaginationProps } from './types';
|
|
8
|
-
import { useInfiniteLoading } from './useInfiniteLoading';
|
|
9
|
-
|
|
10
|
-
async function fetchStories<T extends Story = Story>(
|
|
11
|
-
page: number,
|
|
12
|
-
pageSize: number,
|
|
13
|
-
withHighlightedStory: boolean,
|
|
14
|
-
category?: Category,
|
|
15
|
-
locale?: LocaleObject,
|
|
16
|
-
include?: (keyof Story.ExtraFields)[],
|
|
17
|
-
pinning?: boolean,
|
|
18
|
-
filterQuery?: Object,
|
|
19
|
-
): Promise<{ stories: T[] }> {
|
|
20
|
-
const result = await fetch('/api/fetch-stories', {
|
|
21
|
-
method: 'POST',
|
|
22
|
-
headers: {
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
24
|
-
'Content-Type': 'application/json',
|
|
25
|
-
},
|
|
26
|
-
body: JSON.stringify({
|
|
27
|
-
page,
|
|
28
|
-
pageSize,
|
|
29
|
-
withHighlightedStory,
|
|
30
|
-
category,
|
|
31
|
-
include,
|
|
32
|
-
pinning,
|
|
33
|
-
...(locale && {
|
|
34
|
-
localeCode: locale.toUnderscoreCode(),
|
|
35
|
-
}),
|
|
36
|
-
filterQuery,
|
|
37
|
-
}),
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
if (!result.ok) {
|
|
41
|
-
const { message } = await result.json();
|
|
42
|
-
throw new Error(message);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return result.json();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* NOTE: This hook depends on the presence of a Next API route (/api/fetch-stories)!
|
|
50
|
-
* The handler for this route is exported from `apiHandlers/fetchStories.ts` file.
|
|
51
|
-
* See https://github.com/prezly/theme-nextjs-bea/blob/d381e9878187ca98815d240d3ffdf10a75993c14/pages/index.tsx#L24 for usage example
|
|
52
|
-
*/
|
|
53
|
-
export function useInfiniteStoriesLoading<T extends Story = Story>(
|
|
54
|
-
initialStories: T[],
|
|
55
|
-
pagination: PaginationProps,
|
|
56
|
-
category?: Category,
|
|
57
|
-
include?: (keyof Story.ExtraFields)[],
|
|
58
|
-
pinning?: boolean,
|
|
59
|
-
filterQuery?: Object,
|
|
60
|
-
) {
|
|
61
|
-
const currentLocale = useCurrentLocale();
|
|
62
|
-
const { withHighlightedStory = false } = pagination;
|
|
63
|
-
|
|
64
|
-
const { canLoadMore, data, isLoading, loadMore, resetData } = useInfiniteLoading<T>({
|
|
65
|
-
fetchingFn: async (nextPage, pageSize) => {
|
|
66
|
-
const { stories } = await fetchStories<T>(
|
|
67
|
-
nextPage,
|
|
68
|
-
pageSize,
|
|
69
|
-
withHighlightedStory,
|
|
70
|
-
category,
|
|
71
|
-
currentLocale,
|
|
72
|
-
include,
|
|
73
|
-
pinning,
|
|
74
|
-
filterQuery,
|
|
75
|
-
);
|
|
76
|
-
return stories;
|
|
77
|
-
},
|
|
78
|
-
initialData: initialStories,
|
|
79
|
-
pagination,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
useEffect(() => {
|
|
83
|
-
if (category?.id) {
|
|
84
|
-
resetData();
|
|
85
|
-
}
|
|
86
|
-
}, [category?.id, resetData]);
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
canLoadMore,
|
|
90
|
-
isLoading,
|
|
91
|
-
loadMoreStories: loadMore,
|
|
92
|
-
stories: data,
|
|
93
|
-
};
|
|
94
|
-
}
|
package/src/intl/index.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { LANGUAGES } from '../__mocks__/languages';
|
|
2
|
-
|
|
3
|
-
import { getLanguageFromNextLocaleIsoCode } from './languages';
|
|
4
|
-
import { DUMMY_DEFAULT_LOCALE } from './locale';
|
|
5
|
-
|
|
6
|
-
const ALL_LANGUAGES = Object.values(LANGUAGES);
|
|
7
|
-
|
|
8
|
-
describe('getLanguageFromNextLocaleIsoCode', () => {
|
|
9
|
-
it('returns default language when no locale code provided or when dummy locale provided', () => {
|
|
10
|
-
expect(getLanguageFromNextLocaleIsoCode(ALL_LANGUAGES)).toEqual(LANGUAGES.en);
|
|
11
|
-
expect(getLanguageFromNextLocaleIsoCode(ALL_LANGUAGES, DUMMY_DEFAULT_LOCALE)).toEqual(
|
|
12
|
-
LANGUAGES.en,
|
|
13
|
-
);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('returns language by exact code first', () => {
|
|
17
|
-
expect(getLanguageFromNextLocaleIsoCode(ALL_LANGUAGES, 'en')).toEqual(LANGUAGES.en);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('returns language by region code second', () => {
|
|
21
|
-
expect(getLanguageFromNextLocaleIsoCode(ALL_LANGUAGES, 'be')).toEqual(LANGUAGES.nl_BE);
|
|
22
|
-
expect(getLanguageFromNextLocaleIsoCode(ALL_LANGUAGES, 'nl')).toEqual(LANGUAGES.nl_NL);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('returns language by neutral code thrid', () => {
|
|
26
|
-
expect(
|
|
27
|
-
getLanguageFromNextLocaleIsoCode(
|
|
28
|
-
ALL_LANGUAGES.filter(({ code }) => code !== 'fr'),
|
|
29
|
-
'fr',
|
|
30
|
-
),
|
|
31
|
-
).toEqual(LANGUAGES.fr_BE);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('returns undefined when no language found', () => {
|
|
35
|
-
expect(getLanguageFromNextLocaleIsoCode(ALL_LANGUAGES, 'zh-Hant')).toEqual(undefined);
|
|
36
|
-
});
|
|
37
|
-
});
|
package/src/intl/languages.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { NewsroomLanguageSettings } from '@prezly/sdk';
|
|
2
|
-
import {
|
|
3
|
-
getDefaultLanguage,
|
|
4
|
-
getLanguageByExactLocaleCode,
|
|
5
|
-
getLanguageByNeutralLocaleCode,
|
|
6
|
-
getLanguageByShortRegionCode,
|
|
7
|
-
LocaleObject,
|
|
8
|
-
} from '@prezly/theme-kit-core';
|
|
9
|
-
|
|
10
|
-
import { DUMMY_DEFAULT_LOCALE } from './locale';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Retrieve a culture with fallback to legacy codes support.
|
|
14
|
-
* Pulled from https://github.com/prezly/prezly/blob/de9900d9890a33502780494aa3fb85c9a732b3c3/lib/model/CulturePeer.php#L91-L114
|
|
15
|
-
*/
|
|
16
|
-
export function getLanguageFromNextLocaleIsoCode(
|
|
17
|
-
languages: NewsroomLanguageSettings[],
|
|
18
|
-
nextLocaleIsoCode?: string,
|
|
19
|
-
): NewsroomLanguageSettings | undefined {
|
|
20
|
-
const defaultLanguage = getDefaultLanguage(languages);
|
|
21
|
-
|
|
22
|
-
if (!nextLocaleIsoCode || nextLocaleIsoCode === DUMMY_DEFAULT_LOCALE) {
|
|
23
|
-
return defaultLanguage;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const locale = LocaleObject.fromAnyCode(nextLocaleIsoCode);
|
|
27
|
-
let targetLanguage: NewsroomLanguageSettings | undefined;
|
|
28
|
-
|
|
29
|
-
if (nextLocaleIsoCode.length >= 2 && nextLocaleIsoCode.length <= 4) {
|
|
30
|
-
// The order of methods here is reversed from `getShortestLocaleCode`, so that the logic "unwraps" the possible variants with no collisions
|
|
31
|
-
targetLanguage =
|
|
32
|
-
getLanguageByExactLocaleCode(languages, locale) ||
|
|
33
|
-
getLanguageByShortRegionCode(languages, locale) ||
|
|
34
|
-
getLanguageByNeutralLocaleCode(languages, locale);
|
|
35
|
-
} else {
|
|
36
|
-
targetLanguage = getLanguageByExactLocaleCode(languages, locale);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return targetLanguage;
|
|
40
|
-
}
|
package/src/intl/locale.test.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { DUMMY_DEFAULT_LOCALE, getRedirectToCanonicalLocale } from './locale';
|
|
2
|
-
|
|
3
|
-
describe('getRedirectToCanonicalLocale', () => {
|
|
4
|
-
it('returns undefined when no locale or dummy locale is provided', () => {
|
|
5
|
-
expect(getRedirectToCanonicalLocale('en', undefined, '/')).toBe(undefined);
|
|
6
|
-
expect(getRedirectToCanonicalLocale('en', DUMMY_DEFAULT_LOCALE, '/')).toBe(undefined);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('returns undefined when locale is already canonical', () => {
|
|
10
|
-
expect(getRedirectToCanonicalLocale('en', 'en', '/')).toBe(undefined);
|
|
11
|
-
expect(getRedirectToCanonicalLocale('nl_BE', 'nl-be', '/')).toBe(undefined);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('returns a redirect object when requested locale is not canonical', () => {
|
|
15
|
-
expect(getRedirectToCanonicalLocale(false, 'en-us', '/test')).toEqual({
|
|
16
|
-
destination: '/test',
|
|
17
|
-
permanent: false,
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
expect(getRedirectToCanonicalLocale('en', 'en-us', '/test')).toEqual({
|
|
21
|
-
destination: '/en/test',
|
|
22
|
-
permanent: false,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
expect(getRedirectToCanonicalLocale('en', 'en-us', 'test')).toEqual({
|
|
26
|
-
destination: '/en/test',
|
|
27
|
-
permanent: false,
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('preserves query parameters', () => {
|
|
32
|
-
expect(
|
|
33
|
-
getRedirectToCanonicalLocale('en', 'en-us', '/test', {
|
|
34
|
-
page: '1',
|
|
35
|
-
tags: ['story', 'media'],
|
|
36
|
-
}),
|
|
37
|
-
).toEqual({
|
|
38
|
-
destination: '/en/test?page=1&tags=story&tags=media',
|
|
39
|
-
permanent: false,
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
});
|
package/src/intl/locale.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { LocaleObject } from '@prezly/theme-kit-core';
|
|
2
|
-
import type { Redirect } from 'next';
|
|
3
|
-
import type { ParsedUrlQuery } from 'querystring';
|
|
4
|
-
import { stringify } from 'querystring';
|
|
5
|
-
|
|
6
|
-
// We use pseudo locale used for localization testing, to reliably determine if we need to fallback to the default newsroom language
|
|
7
|
-
export const DUMMY_DEFAULT_LOCALE = 'qps-ploc';
|
|
8
|
-
|
|
9
|
-
export function getRedirectToCanonicalLocale(
|
|
10
|
-
shortestLocaleCode: string | false,
|
|
11
|
-
nextLocaleIsoCode: string | undefined,
|
|
12
|
-
redirectPath: string,
|
|
13
|
-
query?: ParsedUrlQuery,
|
|
14
|
-
): Redirect | undefined {
|
|
15
|
-
// No need to redirect if no particular locale was requested by the application
|
|
16
|
-
if (!nextLocaleIsoCode || nextLocaleIsoCode === DUMMY_DEFAULT_LOCALE) {
|
|
17
|
-
return undefined;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const shortestLocaleSlug = shortestLocaleCode
|
|
21
|
-
? LocaleObject.fromAnyCode(shortestLocaleCode).toUrlSlug()
|
|
22
|
-
: shortestLocaleCode;
|
|
23
|
-
|
|
24
|
-
if (shortestLocaleSlug !== nextLocaleIsoCode) {
|
|
25
|
-
const prefixedPath =
|
|
26
|
-
redirectPath && !redirectPath.startsWith('/') ? `/${redirectPath}` : redirectPath;
|
|
27
|
-
|
|
28
|
-
const urlQuery = query ? `?${stringify(query)}` : '';
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
destination: shortestLocaleSlug
|
|
32
|
-
? `/${shortestLocaleSlug}${prefixedPath}${urlQuery}`
|
|
33
|
-
: `${prefixedPath}${urlQuery}`,
|
|
34
|
-
permanent: false,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Category,
|
|
3
|
-
ExtendedStory,
|
|
4
|
-
Newsroom,
|
|
5
|
-
NewsroomCompanyInformation,
|
|
6
|
-
NewsroomContact,
|
|
7
|
-
NewsroomLanguageSettings,
|
|
8
|
-
NewsroomThemePreset,
|
|
9
|
-
Notification,
|
|
10
|
-
} from '@prezly/sdk';
|
|
11
|
-
import type { AlgoliaSettings } from '@prezly/theme-kit-core';
|
|
12
|
-
import { DEFAULT_LOCALE, LocaleObject } from '@prezly/theme-kit-core';
|
|
13
|
-
import type { PropsWithChildren } from 'react';
|
|
14
|
-
import { createContext, useMemo } from 'react';
|
|
15
|
-
|
|
16
|
-
export interface NewsroomContextType {
|
|
17
|
-
/**
|
|
18
|
-
* Basic newsroom information: newsroom logos, settings, etc.
|
|
19
|
-
*/
|
|
20
|
-
newsroom: Newsroom;
|
|
21
|
-
/**
|
|
22
|
-
* Contact information of the newsroom in the default language, usually displayed in the "About" section
|
|
23
|
-
*/
|
|
24
|
-
companyInformation: NewsroomCompanyInformation;
|
|
25
|
-
/**
|
|
26
|
-
* List of all categories enabled on the newsroom
|
|
27
|
-
*/
|
|
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
|
-
*/
|
|
33
|
-
contacts?: NewsroomContact[];
|
|
34
|
-
/**
|
|
35
|
-
* Optional: Refers to a currently selected category when navigated to `/category/[slug]` page.
|
|
36
|
-
*/
|
|
37
|
-
currentCategory?: Category;
|
|
38
|
-
/**
|
|
39
|
-
* Optional: Refers to a currently selected story when navigated to `/[slug]` page.
|
|
40
|
-
*/
|
|
41
|
-
currentStory?: ExtendedStory;
|
|
42
|
-
/**
|
|
43
|
-
* List of currently enabled newsroom languages, as well as company information translated for each language
|
|
44
|
-
*/
|
|
45
|
-
languages: NewsroomLanguageSettings[];
|
|
46
|
-
/**
|
|
47
|
-
* Currently chosen locale
|
|
48
|
-
*/
|
|
49
|
-
locale: LocaleObject;
|
|
50
|
-
/**
|
|
51
|
-
* Newsroom user-facing notifications (usually displayed as top-edge banners)
|
|
52
|
-
*/
|
|
53
|
-
notifications: Notification[];
|
|
54
|
-
/**
|
|
55
|
-
* Theme settings as set in the Prezly app.
|
|
56
|
-
* You need to provide `PREZLY_THEME_UUID` in environment variables in order for the preset to be loaded.
|
|
57
|
-
* Only applicable to themes hosted on the Prezly infrastructure.
|
|
58
|
-
*/
|
|
59
|
-
themePreset: NewsroomThemePreset | null;
|
|
60
|
-
/**
|
|
61
|
-
* Environment variables required for Algolia search to work.
|
|
62
|
-
*/
|
|
63
|
-
algoliaSettings: AlgoliaSettings;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export interface NewsroomContextProps extends Omit<NewsroomContextType, 'locale'> {
|
|
67
|
-
localeCode: string;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export const NewsroomContext = createContext<NewsroomContextType | undefined>(undefined);
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* This context provides common Newsroom information retrieved by `getNewsroomServerSideProps` helper.
|
|
74
|
-
* Please refer to the `NewsroomContextType` definition for more info on what's exposed from the context.
|
|
75
|
-
*/
|
|
76
|
-
export function NewsroomContextProvider({
|
|
77
|
-
categories,
|
|
78
|
-
contacts,
|
|
79
|
-
newsroom,
|
|
80
|
-
currentCategory,
|
|
81
|
-
currentStory,
|
|
82
|
-
companyInformation,
|
|
83
|
-
languages,
|
|
84
|
-
localeCode,
|
|
85
|
-
notifications,
|
|
86
|
-
themePreset,
|
|
87
|
-
algoliaSettings,
|
|
88
|
-
children,
|
|
89
|
-
}: PropsWithChildren<NewsroomContextProps>) {
|
|
90
|
-
const locale = useMemo(
|
|
91
|
-
() => LocaleObject.fromAnyCode(localeCode || DEFAULT_LOCALE),
|
|
92
|
-
[localeCode],
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
return (
|
|
96
|
-
<NewsroomContext.Provider
|
|
97
|
-
value={{
|
|
98
|
-
categories,
|
|
99
|
-
contacts,
|
|
100
|
-
newsroom,
|
|
101
|
-
currentCategory,
|
|
102
|
-
currentStory,
|
|
103
|
-
companyInformation,
|
|
104
|
-
languages,
|
|
105
|
-
locale,
|
|
106
|
-
notifications,
|
|
107
|
-
themePreset,
|
|
108
|
-
algoliaSettings,
|
|
109
|
-
}}
|
|
110
|
-
>
|
|
111
|
-
{children}
|
|
112
|
-
</NewsroomContext.Provider>
|
|
113
|
-
);
|
|
114
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from './useAlgoliaSettings';
|
|
2
|
-
export * from './useCategories';
|
|
3
|
-
export * from './useCompanyInformation';
|
|
4
|
-
export * from './useCurrentCategory';
|
|
5
|
-
export * from './useCurrentLocale';
|
|
6
|
-
export * from './useCurrentStory';
|
|
7
|
-
export * from './useGetLinkLocaleSlug';
|
|
8
|
-
export * from './useGetTranslationUrl';
|
|
9
|
-
export * from './useLanguages';
|
|
10
|
-
export * from './useNewsroom';
|
|
11
|
-
export * from './useNewsroomContext';
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { NewsroomLanguageSettings } from '@prezly/sdk';
|
|
2
|
-
import { getShortestLocaleCode, LocaleObject } from '@prezly/theme-kit-core';
|
|
3
|
-
import { useCallback } from 'react';
|
|
4
|
-
|
|
5
|
-
import { useCurrentLocale } from './useCurrentLocale';
|
|
6
|
-
import { useLanguages } from './useLanguages';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Determines correct locale slug for the link (trying to shorten the locale code if possible).
|
|
10
|
-
* E.g. if newsroom has a single locale for Dutch (`nl_BE`), it will be shortened to `nl`.
|
|
11
|
-
* If there are more than one locale with the same language part, a full locale code will be used instead.
|
|
12
|
-
* `false` means that a default locale should be used (no locale in URL required).
|
|
13
|
-
*/
|
|
14
|
-
function getLinkLocaleSlug(
|
|
15
|
-
languages: NewsroomLanguageSettings[],
|
|
16
|
-
locale: LocaleObject,
|
|
17
|
-
): string | false {
|
|
18
|
-
const shortestLocaleCode = getShortestLocaleCode(languages, locale);
|
|
19
|
-
// When navigating to default language, we don't append the locale to the URL.
|
|
20
|
-
if (!shortestLocaleCode) {
|
|
21
|
-
return shortestLocaleCode;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return LocaleObject.fromAnyCode(shortestLocaleCode).toUrlSlug();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function useGetLinkLocaleSlug() {
|
|
28
|
-
const currentLocale = useCurrentLocale();
|
|
29
|
-
const languages = useLanguages();
|
|
30
|
-
|
|
31
|
-
// The `locale` parameter is mainly used for Language Dropdown
|
|
32
|
-
return useCallback(
|
|
33
|
-
(locale?: LocaleObject) => getLinkLocaleSlug(languages, locale || currentLocale),
|
|
34
|
-
[languages, currentLocale],
|
|
35
|
-
);
|
|
36
|
-
}
|