From e0d389b9ce3369c5fd41c3a686d2064754f8ee4a Mon Sep 17 00:00:00 2001 From: Maximilian Harz Date: Fri, 11 Jul 2025 16:35:53 +0200 Subject: [PATCH] Improve typing; get inviting profile specifically --- app/src/App.tsx | 15 ++++++++++++--- app/src/api/itemsApi.ts | 6 +++--- lib/src/Components/Onboarding/InvitePage.tsx | 14 ++++++-------- lib/src/index.tsx | 1 + lib/src/types/FullItemsApi.d.ts | 11 +++++++++++ 5 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 lib/src/types/FullItemsApi.d.ts diff --git a/app/src/App.tsx b/app/src/App.tsx index af496860..1e093b0d 100644 --- a/app/src/App.tsx +++ b/app/src/App.tsx @@ -12,7 +12,7 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-call */ -import type { Tag } from 'utopia-ui' +import type { Item, Tag } from 'utopia-ui' import { AppShell, @@ -66,6 +66,7 @@ function App() { const [mapApiInstance, setMapApiInstance] = useState() const [layersApiInstance, setLayersApiInstance] = useState() const [attestationApi, setAttestationApi] = useState>() + const [itemsApiInstance, setItemsApiInstance] = useState>() const [map, setMap] = useState() const [layers, setLayers] = useState() @@ -140,10 +141,15 @@ function App() { setLoading(false) }, [map]) + useEffect(() => { + if (!map) return + setItemsApiInstance(new itemsApi('items', undefined, map.id)) + }, [map]) + const currentUrl = window.location.href const bottomRoutes = getBottomRoutes(currentUrl) - if (map && layers) + if (map && layers && itemsApiInstance) return (
@@ -169,7 +175,10 @@ function App() { path='/*' element={} > - } /> + } + /> } /> } /> implements ItemsApi { +export class itemsApi implements FullItemsApi { collectionName: keyof MyCollections filter: any layerId: string | undefined @@ -70,7 +70,7 @@ export class itemsApi implements ItemsApi { async getItem(id: string): Promise { try { - const result = await directusClient.request(readItem(this.collectionName as never, id)) + const result = await directusClient.request(readItem(this.collectionName, id)) return result as T } catch (error: any) { console.log(error) diff --git a/lib/src/Components/Onboarding/InvitePage.tsx b/lib/src/Components/Onboarding/InvitePage.tsx index 41398849..bc5aa5a2 100644 --- a/lib/src/Components/Onboarding/InvitePage.tsx +++ b/lib/src/Components/Onboarding/InvitePage.tsx @@ -3,28 +3,27 @@ import { useNavigate, useParams } from 'react-router-dom' import { toast } from 'react-toastify' import { useAuth } from '#components/Auth/useAuth' -import { useAllItemsLoaded, useItems } from '#components/Map/hooks/useItems' import { useMyProfile } from '#components/Map/hooks/useMyProfile' import { MapOverlayPage } from '#components/Templates/MapOverlayPage' +import type { FullItemsApi } from '#types/FullItemsApi' import type { InviteApi } from '#types/InviteApi' import type { Item } from '#types/Item' interface Props { inviteApi: InviteApi + itemsApi: FullItemsApi } /** * @category Onboarding */ -export function InvitePage({ inviteApi }: Props) { +export function InvitePage({ inviteApi, itemsApi }: Props) { const { isAuthenticated, isInitialized: isAuthenticationInitialized } = useAuth() const { id } = useParams<{ id: string }>() const navigate = useNavigate() const { myProfile, isMyProfileLoaded } = useMyProfile() - const items = useItems() - const allItemsLoaded = useAllItemsLoaded() && items.length > 0 if (!id) throw new Error('Invite ID is required') @@ -63,7 +62,7 @@ export function InvitePage({ inviteApi }: Props) { return } - const invitingProfile = items.find((item) => item.id === invitingProfileId) + const invitingProfile = await itemsApi.getItem(invitingProfileId) if (!invitingProfile) { toast.error('Inviting profile not found') @@ -74,7 +73,7 @@ export function InvitePage({ inviteApi }: Props) { setInvitingProfile(invitingProfile) } - if (!isAuthenticationInitialized || !allItemsLoaded) return + if (!isAuthenticationInitialized) return if (isAuthenticated) { void redeemInvite() @@ -92,8 +91,7 @@ export function InvitePage({ inviteApi }: Props) { isAuthenticationInitialized, myProfile, isMyProfileLoaded, - items, - allItemsLoaded, + itemsApi, ]) const goToSignup = () => { diff --git a/lib/src/index.tsx b/lib/src/index.tsx index f3239c11..98c68cd6 100644 --- a/lib/src/index.tsx +++ b/lib/src/index.tsx @@ -10,6 +10,7 @@ export * from './Components/Input' export * from './Components/Item' export * from './Components/Onboarding' export * from './Components/Profile' +export type * from './types/FullItemsApi' declare global { interface Window { diff --git a/lib/src/types/FullItemsApi.d.ts b/lib/src/types/FullItemsApi.d.ts new file mode 100644 index 00000000..7bdc3707 --- /dev/null +++ b/lib/src/types/FullItemsApi.d.ts @@ -0,0 +1,11 @@ +/** + * @category Types + */ +export interface FullItemsApi { + getItems(): Promise + getItem(id: string): Promise + createItem(item: T): Promise + updateItem(item: Partial): Promise + deleteItem(id: string): Promise + collectionName?: string +}