Skip to content

Commit b3ef0bb

Browse files
authored
fix space id extraction (#316)
1 parent 7e4df7c commit b3ef0bb

File tree

6 files changed

+20
-15
lines changed

6 files changed

+20
-15
lines changed

apps/events/src/components/playground.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useState } from 'react';
99
import { Event } from '../schema';
1010
import { Button } from './ui/button';
1111

12-
export const Playground = () => {
12+
export const Playground = ({ spaceId }: { spaceId: string }) => {
1313
const { data, isLoading, isError } = useQuery(Event, {
1414
mode: 'public',
1515
include: {
@@ -18,12 +18,13 @@ export const Playground = () => {
1818
},
1919
},
2020
first: 2,
21+
space: spaceId,
2122
});
2223
const [isDeleting, setIsDeleting] = useState(false);
2324
const [isCreating, setIsCreating] = useState(false);
2425
const { getSmartSessionClient } = useHypergraphApp();
2526

26-
const { name, id: spaceId } = useSpace({ mode: 'public' });
27+
const { name } = useSpace({ mode: 'public', space: spaceId });
2728

2829
const deleteEntity = _useDeleteEntityPublic(Event, { space: spaceId });
2930
const createEntity = _useCreateEntityPublic(Event, { space: spaceId });

apps/events/src/routes/playground.lazy.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ export const Route = createLazyFileRoute('/playground')({
1111
function RouteComponent() {
1212
const space = 'a393e509-ae56-4d99-987c-bed71d9db631';
1313
return (
14-
<HypergraphSpaceProvider space={space}>
15-
<div className="flex flex-col gap-4 max-w-(--breakpoint-sm) mx-auto py-8">
16-
<h1 className="text-2xl font-bold">Playground</h1>
17-
<Playground />
18-
<CreatePropertiesAndTypesEvent space={space} />
19-
<CreateEvents space={space} />
20-
</div>
21-
</HypergraphSpaceProvider>
14+
<>
15+
<Playground spaceId={space} />
16+
<HypergraphSpaceProvider space={space}>
17+
<div className="flex flex-col gap-4 max-w-(--breakpoint-sm) mx-auto py-8">
18+
<h1 className="text-2xl font-bold">Playground</h1>
19+
<CreatePropertiesAndTypesEvent space={space} />
20+
<CreateEvents space={space} />
21+
</div>
22+
</HypergraphSpaceProvider>
23+
</>
2224
);
2325
}

packages/hypergraph-react/src/HypergraphSpaceContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const HypergraphReactContext = createContext<HypergraphContext | undefine
2323

2424
export function useHypergraphSpaceInternal() {
2525
const context = useContext(HypergraphReactContext);
26-
return context as HypergraphContext;
26+
return (context as HypergraphContext) || { space: '' };
2727
}
2828

2929
export function HypergraphSpaceProvider({ space, children }: { space: string; children: ReactNode }) {

packages/hypergraph-react/src/internal/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type * as Schema from 'effect/Schema';
33

44
export type QueryPublicParams<S extends Entity.AnyNoContext> = {
55
enabled: boolean;
6+
space?: string | undefined;
67
// TODO: for multi-level nesting it should only allow the allowed properties instead of Record<string, Record<string, never>>
78
include?: { [K in keyof Schema.Schema.Type<S>]?: Record<string, Record<string, never>> } | undefined;
89
first?: number | undefined;

packages/hypergraph-react/src/internal/use-query-public.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,9 @@ export const parseResult = <S extends Entity.AnyNoContext>(
295295
};
296296

297297
export const useQueryPublic = <S extends Entity.AnyNoContext>(type: S, params?: QueryPublicParams<S>) => {
298-
const { enabled = true, include, first = 100 } = params ?? {};
299-
const { space } = useHypergraphSpaceInternal();
298+
const { enabled = true, include, space: spaceFromParams, first = 100 } = params ?? {};
299+
const { space: spaceFromContext } = useHypergraphSpaceInternal();
300+
const space = spaceFromParams ?? spaceFromContext;
300301
const mapping = useSelector(store, (state) => state.context.mapping);
301302

302303
// @ts-expect-error TODO should use the actual type instead of the name in the mapping

packages/hypergraph-react/src/use-query.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type QueryParams<S extends Entity.AnyNoContext> = {
99
filter?: { [K in keyof Schema.Schema.Type<S>]?: Entity.EntityFieldFilter<Schema.Schema.Type<S>[K]> } | undefined;
1010
// TODO: for multi-level nesting it should only allow the allowed properties instead of Record<string, Record<string, never>>
1111
include?: { [K in keyof Schema.Schema.Type<S>]?: Record<string, Record<string, never>> } | undefined;
12-
space?: string;
12+
space?: string | undefined;
1313
first?: number | undefined;
1414
};
1515

@@ -146,7 +146,7 @@ const preparePublishDummy = () => undefined;
146146

147147
export function useQuery<const S extends Entity.AnyNoContext>(type: S, params: QueryParams<S>) {
148148
const { mode, filter, include, space, first } = params;
149-
const publicResult = useQueryPublic(type, { enabled: mode === 'public', include, first });
149+
const publicResult = useQueryPublic(type, { enabled: mode === 'public', include, first, space });
150150
const localResult = useQueryLocal(type, { enabled: mode === 'private', filter, include, space });
151151
// const mapping = useSelector(store, (state) => state.context.mapping);
152152
// const generateUpdateOps = useGenerateUpdateOps(type, mode === 'merged');

0 commit comments

Comments
 (0)