Skip to content

Commit 8823d40

Browse files
committed
simplify a couple of things
1 parent 17bd068 commit 8823d40

File tree

6 files changed

+16
-24
lines changed

6 files changed

+16
-24
lines changed

apps/studio/src/perspective-example.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ import type {ClientPerspective} from '@sanity/client'
1010
*/
1111
export function PerspectiveExample() {
1212
const client = useClient({apiVersion: '2025-03-04'})
13-
// Get the global perspective from Sanity Studio
1413
const {perspectiveStack} = usePerspective()
15-
const globalPerspective = perspectiveStack[0] || 'published'
1614

1715
return (
18-
<LiveQueryProvider client={client} perspective={globalPerspective}>
16+
<LiveQueryProvider client={client} perspective={perspectiveStack}>
1917
<Box padding={4} height="fill">
2018
<Stack space={5}>
2119
<Card padding={4} shadow={1} radius={2}>
@@ -27,7 +25,7 @@ export function PerspectiveExample() {
2725
<Code>LiveQueryProvider</Code>.
2826
</Text>
2927
<Text size={1} muted>
30-
Global Studio Perspective: <Code>{globalPerspective}</Code>
28+
Global Studio Perspective: <Code>{JSON.stringify(perspectiveStack)}</Code>
3129
</Text>
3230
</Stack>
3331
</Card>
@@ -44,13 +42,10 @@ export function PerspectiveExample() {
4442
<Text size={1}>
4543
This query uses the global Studio perspective from <Code>usePerspective</Code>:
4644
</Text>
47-
{globalPerspective !== 'raw' && (
48-
<PerspectiveQuery
49-
perspective={globalPerspective as Exclude<ClientPerspective, 'raw'>}
50-
title={`Global (${globalPerspective})`}
51-
/>
52-
)}
53-
45+
<PerspectiveQuery
46+
perspective={perspectiveStack}
47+
title={`Global (${perspectiveStack})`}
48+
/>
5449
<Heading size={1}>Provider Default Perspective</Heading>
5550
<Text size={1}>
5651
This query doesn't specify a perspective, so it uses the provider's default:

packages/preview-kit/src/LiveQueryProvider/LiveQueryProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ export default function LiveStoreProvider(props: LiveQueryProviderProps): React.
5858
initialSnapshot: QueryResult,
5959
query: string,
6060
params: QueryParams,
61-
hookPerspective?: Exclude<ClientPerspective, 'raw'>,
61+
hookPerspective: Exclude<ClientPerspective, 'raw'> | null,
6262
) {
63-
const effectivePerspective = hookPerspective || perspective
63+
const effectivePerspective = hookPerspective === null ? perspective : hookPerspective
6464
const snapshotsKey = getQueryCacheKey(query, params, effectivePerspective)
6565
const contextSubscribe: ListenerSubscribe = (onStoreChange) => {
6666
const unsubscribe = subscribe({

packages/preview-kit/src/LiveQueryProvider/usePerspective.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {type ClientPerspective} from '@sanity/client'
1+
import type {ClientPerspective} from '@sanity/client'
22
import {createNode, createNodeMachine} from '@sanity/comlink'
33
import {
44
createCompatibilityActors,

packages/preview-kit/src/hooks.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type isEqualFn<QueryResult> = (a: QueryResult, b: QueryResult) => boolean
1717
/** @public */
1818
export interface LiveQueryHookOptions<QueryResult> {
1919
isEqual?: isEqualFn<QueryResult>
20-
perspective?: Exclude<ClientPerspective, 'raw'>
20+
perspective?: Exclude<ClientPerspective, 'raw'> | null
2121
}
2222

2323
/** @public */
@@ -30,7 +30,7 @@ export function useLiveQuery<
3030
queryParams?: QueryParams,
3131
options?: LiveQueryHookOptions<QueryResult>,
3232
): [QueryResult, QueryLoading, QueryEnabled] {
33-
const {isEqual = isFastEqual, perspective: perspectiveOption} = options || {}
33+
const {isEqual = isFastEqual, perspective: perspectiveOption = null} = options || {}
3434

3535
const defineStore = useContext(defineStoreContext)
3636
const params = useQueryParams(queryParams)
@@ -117,14 +117,11 @@ export function useQueryParams(params?: undefined | null | QueryParams): QueryPa
117117
* @internal
118118
*/
119119
export function useQueryPerspective(
120-
perspective?: undefined | null | Exclude<ClientPerspective, 'raw'>,
121-
): Exclude<ClientPerspective, 'raw'> | undefined {
120+
perspective: null | Exclude<ClientPerspective, 'raw'>,
121+
): Exclude<ClientPerspective, 'raw'> | null {
122122
const stringifiedPerspective = useMemo(() => JSON.stringify(perspective), [perspective])
123123
return useMemo(
124-
() =>
125-
stringifiedPerspective === 'null' || stringifiedPerspective === 'undefined'
126-
? undefined
127-
: (JSON.parse(stringifiedPerspective) as Exclude<ClientPerspective, 'raw'>),
124+
() => JSON.parse(stringifiedPerspective) as Exclude<ClientPerspective, 'raw'>,
128125
[stringifiedPerspective],
129126
)
130127
}

packages/preview-kit/src/live-query/client-component/useLiveQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function useLiveQuery<
2020
const defineStore = useContext(defineStoreContext)
2121
const queryParams = useQueryParams(queryParams2)
2222
const store = useMemo(
23-
() => defineStore?.<QueryResult>(initialData, query, queryParams),
23+
() => defineStore?.<QueryResult>(initialData, query, queryParams, null),
2424
[defineStore, initialData, queryParams, query],
2525
)
2626
// initialSnapshot might change before hydration is done, so deep cloning it on the first hook call

packages/preview-kit/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type DefineListenerContext = <QueryResult>(
1717
initialSnapshot: QueryResult,
1818
query: string,
1919
params: QueryParams,
20-
perspective?: Exclude<ClientPerspective, 'raw'>,
20+
perspective: Exclude<ClientPerspective, 'raw'> | null,
2121
) => {
2222
subscribe: ListenerSubscribe
2323
getSnapshot: ListenerGetSnapshot<QueryResult>

0 commit comments

Comments
 (0)