mirror of
https://github.com/get-drexa/drive.git
synced 2026-02-03 01:01:17 +00:00
feat(fronend): wip org prefixed routing
This commit is contained in:
32
apps/drive-web/src/organization/api.ts
Normal file
32
apps/drive-web/src/organization/api.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { queryOptions, skipToken } from "@tanstack/react-query"
|
||||
import { fetchApi } from "@/lib/api"
|
||||
import { Organization } from "./organization"
|
||||
import { Drive } from "@/drive/drive"
|
||||
|
||||
export const organizationQuery = (orgSlug: string | null) =>
|
||||
queryOptions({
|
||||
queryKey: ["organizations", orgSlug],
|
||||
queryFn: orgSlug
|
||||
? () =>
|
||||
fetchApi("GET", `/organizations/${orgSlug}`, {
|
||||
returns: Organization,
|
||||
}).then(([, data]) => data)
|
||||
: skipToken,
|
||||
})
|
||||
|
||||
export const listOrganizationsQuery = queryOptions({
|
||||
queryKey: ["organizations"],
|
||||
queryFn: () =>
|
||||
fetchApi("GET", "/users/me/organizations", {
|
||||
returns: Organization.array(),
|
||||
}).then(([, data]) => data),
|
||||
})
|
||||
|
||||
export const listOrganizationDrivesQuery = (orgSlug: string) =>
|
||||
queryOptions({
|
||||
queryKey: ["organizations", orgSlug, "drives"],
|
||||
queryFn: () =>
|
||||
fetchApi("GET", `/${orgSlug}/drives`, {
|
||||
returns: Drive.array(),
|
||||
}).then(([, data]) => data),
|
||||
})
|
||||
16
apps/drive-web/src/organization/context.ts
Normal file
16
apps/drive-web/src/organization/context.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { createContext, useContext } from "react"
|
||||
import type { Organization } from "./organization"
|
||||
|
||||
export const OrganizationContext = createContext<Organization>(
|
||||
null as unknown as Organization,
|
||||
)
|
||||
|
||||
export function useCurrentOrganization() {
|
||||
const org = useContext(OrganizationContext)
|
||||
if (!org) {
|
||||
throw new Error(
|
||||
"useCurrentOrganization must be used under /$orgSlug routes",
|
||||
)
|
||||
}
|
||||
return org
|
||||
}
|
||||
18
apps/drive-web/src/organization/organization.ts
Normal file
18
apps/drive-web/src/organization/organization.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { type } from "arktype"
|
||||
|
||||
export const ORGANIZATION_KIND = {
|
||||
personal: "personal",
|
||||
team: "team",
|
||||
} as const
|
||||
export type OrganizationKind =
|
||||
(typeof ORGANIZATION_KIND)[keyof typeof ORGANIZATION_KIND]
|
||||
|
||||
export const Organization = type({
|
||||
id: "string",
|
||||
kind: type.valueOf(ORGANIZATION_KIND),
|
||||
name: "string",
|
||||
slug: "string",
|
||||
createdAt: "string.date.iso.parse",
|
||||
updatedAt: "string.date.iso.parse",
|
||||
})
|
||||
export type Organization = typeof Organization.infer
|
||||
Reference in New Issue
Block a user