-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from sanity-io/facelift
configure 'locate'
- Loading branch information
Showing
5 changed files
with
168 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// URL for the front end from this Studio build | ||
// This works because this one repo builds both a Studio and the Next.js front end separately, but using the same branch | ||
// demo-course-platform.sanity.build is the frontend domain | ||
// demo-course-platform-studio.sanity.build is the studio domain | ||
export const enableUrl = process.env.SANITY_STUDIO_VERCEL_ENV | ||
? `https://${ | ||
process.env.SANITY_STUDIO_VERCEL_ENV === 'production' | ||
? 'demo-course-platform.sanity.build' // I don't understand why the primary domain doesn't have a variable | ||
: process.env.SANITY_STUDIO_VERCEL_BRANCH_URL?.replace( | ||
'demo-course-platform-studio', | ||
'demo-course-platform' | ||
) | ||
}/api/draft` | ||
: 'http://localhost:3000/api/draft' | ||
|
||
export {locate} from './locate' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import groq from 'groq' | ||
import {map} from 'rxjs' | ||
import {ListenQueryOptions} from 'sanity' | ||
import type {DocumentLocationResolver} from 'sanity/presentation' | ||
|
||
const DEFAULT_LANG = 'en' | ||
|
||
// See: https://www.sanity.io/docs/configuring-the-presentation-tool#7dce82cbe90b | ||
export const locate: DocumentLocationResolver = (params, context) => { | ||
let doc$ = null | ||
const queryParams = {...params, lang: DEFAULT_LANG} | ||
const listenOptions: ListenQueryOptions = {perspective: 'previewDrafts'} | ||
|
||
if (params.type === 'presenter') { | ||
doc$ = context.documentStore.listenQuery( | ||
groq`*[_id == $id][0]{ | ||
slug, | ||
"title": name | ||
}`, | ||
queryParams, | ||
listenOptions | ||
) | ||
|
||
// Return a streaming list of locations | ||
return doc$.pipe( | ||
map((doc) => { | ||
if (!doc || !doc.slug?.current) { | ||
return null | ||
} | ||
|
||
return { | ||
locations: [ | ||
{ | ||
title: doc.title || 'Untitled', | ||
href: `/${DEFAULT_LANG}/presenter/${doc.slug.current}`, | ||
}, | ||
], | ||
} | ||
}) | ||
) | ||
} else if (params.type === 'legal') { | ||
doc$ = context.documentStore.listenQuery( | ||
groq`*[_id == $id][0]{slug, title}`, | ||
queryParams, | ||
listenOptions | ||
) | ||
|
||
return doc$.pipe( | ||
map((doc) => { | ||
if (!doc || !doc.slug?.current) { | ||
return null | ||
} | ||
|
||
return { | ||
locations: [ | ||
{ | ||
title: doc.title || 'Untitled', | ||
href: `/${DEFAULT_LANG}/legal/${doc.slug.current}`, | ||
}, | ||
], | ||
} | ||
}) | ||
) | ||
} else if (params.type === 'course') { | ||
doc$ = context.documentStore.listenQuery( | ||
groq`*[_id == $id][0]{ | ||
"slug": slug[$lang], | ||
"title": title[$lang] | ||
}`, | ||
queryParams, | ||
listenOptions | ||
) | ||
|
||
return doc$.pipe( | ||
map((doc) => { | ||
console.log(doc) | ||
if (!doc || !doc.slug?.current) { | ||
return null | ||
} | ||
|
||
return { | ||
locations: [ | ||
{ | ||
title: doc.title || 'Untitled', | ||
href: `/${DEFAULT_LANG}/${doc.slug.current}`, | ||
}, | ||
{ | ||
title: 'Home', | ||
href: `/${DEFAULT_LANG}`, | ||
}, | ||
], | ||
} | ||
}) | ||
) | ||
} else if (params.type === 'lesson') { | ||
doc$ = context.documentStore.listenQuery( | ||
groq`*[_id == $id][0]{ | ||
slug, | ||
// TODO: Perform lookup for non-base-language lessons to lookup metadata document | ||
"courseSlug": *[_type == "course" && references(^._id)][0].slug, | ||
title, | ||
language | ||
}`, | ||
queryParams, | ||
listenOptions | ||
) | ||
|
||
return doc$.pipe( | ||
map((doc) => { | ||
if (!doc || !doc?.language || !doc.slug?.current || !doc.courseSlug) { | ||
return null | ||
} | ||
|
||
// Cannot retrieve values in GROQ dynamically based on the value of a field | ||
const courseSlug = doc.courseSlug[doc.language] || doc.courseSlug[DEFAULT_LANG] | ||
|
||
return { | ||
locations: [ | ||
{ | ||
title: doc.title || 'Untitled', | ||
href: `/${doc.language}/${courseSlug.current}/${doc.slug.current}`, | ||
}, | ||
], | ||
} | ||
}) | ||
) | ||
} | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52fc0e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
demo-course-platform – ./web
demo-course-platform-git-main.sanity.build
demo-course-platform.sanity.build
52fc0e0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
demo-course-platform-studio – ./studio
demo-course-platform-studio.sanity.build
demo-course-platform-studio-git-main.sanity.build