@@ -3,6 +3,7 @@ import { getDraftId } from 'sanity'
33import type { DocumentLocationResolver } from 'sanity/presentation'
44import blocksToText from '@/helpers/blocksToText'
55import { capitalizeFirstLetter } from '@/helpers/formatters'
6+ import { defaultWebLang } from '@/languages'
67import { getIdFromName , getLocaleFromName } from '../src/lib/localization'
78
89export const locations : DocumentLocationResolver = ( params , context ) => {
@@ -16,26 +17,35 @@ export const locations: DocumentLocationResolver = (params, context) => {
1617 'localNews' ,
1718 ]
1819
19- if ( routePages ?. includes ( params . type ) ) {
20+ if ( params . type === 'homePage' ) {
2021 query = {
2122 fetch : `*[_id==$id][0]{
22- "slugs": *[_type match "route*" && references(^._id)].slug.current,
23+ lang,
24+ "isActive": count(
25+ *[_type match "route_homepage" && (
26+ references(
27+ *[_type == "translation.metadata" && references($id)][0]
28+ .translations[_key == $defaultLang][0].value._ref
29+ ) || references($id)
30+ )]
31+ ) > 0,
2332 "translationSlugs": *[_type == "translation.metadata" && references(^._id)].translations[].value->{
24- "slug" : *[_type match "route*" && references(^._id)][0].slug.current,
2533 lang,
26- title,
2734 },
2835 ...
2936 }
3037 ` ,
3138 listen : `*[_id in [$id,$draftId]]` ,
3239 }
33- } else if ( params . type === 'homePage' ) {
40+ } else if ( routePages ?. includes ( params . type ) ) {
3441 query = {
3542 fetch : `*[_id==$id][0]{
36- "isActive": count(*[_type match "route_homepage" && references(^._id)]) > 0,
43+ lang,
44+ "slugs": *[_type match "route*" && references(^._id)].slug.current,
3745 "translationSlugs": *[_type == "translation.metadata" && references(^._id)].translations[].value->{
46+ "slug" : *[_type match "route*" && references(^._id)][0].slug.current,
3847 lang,
48+ title,
3949 },
4050 ...
4151 }
@@ -46,6 +56,7 @@ export const locations: DocumentLocationResolver = (params, context) => {
4656 //pages with slugs on them
4757 query = {
4858 fetch : `*[_id==$id][0]{
59+ lang,
4960 "slugs": *[_type match "route*" && references(^._id)][0].slug.current,
5061 "translationSlugs": *[_type == "translation.metadata" && references(^._id)].translations[].value->{
5162 "slug": slug.current,
@@ -58,10 +69,16 @@ export const locations: DocumentLocationResolver = (params, context) => {
5869 }
5970
6071 if ( query ) {
61- const pageParams = { id : params . id , draftId : getDraftId ( params . id ) }
72+ const pageParams = {
73+ id : params . id ,
74+ draftId : getDraftId ( params . id ) ,
75+ publishedId : params . id . replace ( / ^ d r a f t s \. / , '' ) ,
76+ defaultLang : defaultWebLang ?. name ,
77+ }
6278
6379 const doc$ = context . documentStore . listenQuery (
6480 query ,
81+ //@ts -ignore
6582 pageParams ,
6683 { perspective : 'drafts' } , // returns a draft article if it exists
6784 )
@@ -71,10 +88,8 @@ export const locations: DocumentLocationResolver = (params, context) => {
7188 map ( doc => {
7289 // If the document doesn't exist or have a slug, return null
7390 if ( ! doc || ( doc ?. _type === 'homePage' && ! doc ?. isActive ) ) {
74- console . log ( 'return no locations' )
7591 return null
7692 }
77-
7893 let locs = [ ]
7994 if ( routePages ?. includes ( params . type ) ) {
8095 if (
@@ -91,7 +106,7 @@ export const locations: DocumentLocationResolver = (params, context) => {
91106 const locale = getLocaleFromName ( translation ?. lang )
92107 return {
93108 title : `${ blocksToText ( translation ?. title ) } ` ,
94- href : `${ translation ?. lang !== 'en_GB' ? `${ locale } ` : '' } ${ translation ?. slug } ` ,
109+ href : `${ translation ?. lang !== defaultWebLang ?. name ? `${ locale } ` : '' } ${ translation ?. slug } ` ,
95110 }
96111 } )
97112 } else if ( doc ?. slugs ?. filter ( ( slug : string ) => slug ) ?. length > 0 ) {
@@ -101,7 +116,7 @@ export const locations: DocumentLocationResolver = (params, context) => {
101116 const locale = getLocaleFromName ( doc ?. lang )
102117 return {
103118 title : `${ blocksToText ( doc ?. title ) } ` ,
104- href : `${ doc ?. lang !== 'en_GB' ? `${ locale } ` : '' } ${ slug } ` ,
119+ href : `${ doc ?. lang !== defaultWebLang ?. name ? `${ locale } ` : '' } ${ slug } ` ,
105120 }
106121 } )
107122 } else {
@@ -113,17 +128,32 @@ export const locations: DocumentLocationResolver = (params, context) => {
113128 ]
114129 }
115130 } else if ( doc ?. _type === 'homePage' && doc ?. isActive ) {
116- locs = doc ?. translationSlugs ?. map ( ( item : any ) => {
117- const itemLocale = getLocaleFromName ( item ?. lang )
118- const localeId = capitalizeFirstLetter ( getIdFromName ( item ?. lang ) )
119-
120- return {
121- title : `${ localeId } homepage` ,
122- href : `${ item ?. lang !== 'en_GB' ? `/${ itemLocale } ` : '/' } ` ,
123- }
124- } )
131+ if ( ! doc ?. translationSlugs || doc ?. translationSlugs ?. length === 0 ) {
132+ const localeId = capitalizeFirstLetter (
133+ getIdFromName ( defaultWebLang ?. name ) ,
134+ )
135+ locs = [
136+ {
137+ title : `${ localeId } homepage` ,
138+ href : `/` ,
139+ } ,
140+ ]
141+ }
142+ if ( doc ?. translationSlugs ?. length > 0 ) {
143+ locs = doc ?. translationSlugs
144+ ?. filter ( ( item : any ) => item ?. lang === doc ?. lang )
145+ ?. map ( ( item : any ) => {
146+ const itemLocale = getLocaleFromName ( item ?. lang )
147+ const localeId = capitalizeFirstLetter (
148+ getIdFromName ( item ?. lang ) ,
149+ )
150+ return {
151+ title : `${ localeId } homepage` ,
152+ href : `${ item ?. lang !== defaultWebLang ?. name ? `/${ itemLocale } ` : '/' } ` ,
153+ }
154+ } )
155+ }
125156 } else if ( pagesWithSlugOnThem ?. includes ( params . type ) ) {
126- console . log ( 'pagesWithSlugOnThem' )
127157 const locale = getLocaleFromName ( doc ?. lang )
128158 const plainTitle = Array . isArray ( doc . title )
129159 ? blocksToText ( doc . title )
@@ -132,7 +162,7 @@ export const locations: DocumentLocationResolver = (params, context) => {
132162 locs = [
133163 {
134164 title : doc . title ? plainTitle : 'Untitled' ,
135- href : `${ doc ?. lang !== 'en_GB' ? `${ locale } /` : '' } ${ doc ?. slug ?. current } ` ,
165+ href : `${ doc ?. lang !== defaultWebLang ?. name ? `${ locale } /` : '' } ${ doc ?. slug ?. current } ` ,
136166 } ,
137167 ]
138168 }
0 commit comments