All files / src/hooks useOwners.ts

66.66% Statements 20/30
45% Branches 9/20
81.81% Functions 9/11
74.07% Lines 20/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                            22x                   157x 1x 156x 156x                                 167x 167x   52x   167x   52x     167x 167x   167x 97x 2x                 2x 2x               97x 101x   97x                          
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import { Resource } from "@/objects/Doc";
import { AccessMap, Organisation } from "@/objects/Organisation";
import { Project } from "@/objects/Project";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
 
export type OwnerKind = "personal" | "organisation" | "project";
 
/** One owner a resource can have, from the caller's point of view. */
export interface OwnerOption {
  /** `personal`, `organisation:<id>` or `project:<id>`. */
  key: string;
  kind: OwnerKind;
  id: string | null;
  name: string;
  /** The caller's level in each area there; null where unknown (a
   *  superuser outside it). Personal work has no areas: anything goes. */
  access: AccessMap | null;
}
 
export const PERSONAL: OwnerOption = {
  key: "personal",
  kind: "personal",
  id: null,
  name: "Personal",
  access: null,
};
 
/** Which owner a resource has, as an `OwnerOption` key. */
export function ownerKeyOf(resource: Resource): string {
  if (resource.owner_organisation_id)
    return `organisation:${resource.owner_organisation_id}`;
  Iif (resource.owner_project_id) return `project:${resource.owner_project_id}`;
  return PERSONAL.key;
}
 
/** The owner columns a create request carries for `option`. */
export function ownerFields(option: OwnerOption): Partial<Resource> {
  if (option.kind === "organisation")
    return { owner_organisation_id: option.id };
  if (option.kind === "project") return { owner_project_id: option.id };
  return {};
}
 
/**
 * The organisations and projects the caller belongs to, as owners: what a
 * list labels a shared card with, what a create form offers, and where work
 * can move. Personal is always first.
 */
export function useOwners() {
  const { getApiHandler } = useApiHandler();
  const orgsQuery = useQuery<Organisation[] | null>({
    queryKey: [QUERY_KEYS.GET.ORGANISATIONS],
    queryFn: () => getApiHandler().organisation_router.list(),
  });
  const projectsQuery = useQuery<Project[] | null>({
    queryKey: [QUERY_KEYS.GET.PROJECTS],
    queryFn: () => getApiHandler().project_router.list() as Promise<Project[] | null>,
  });
  // Typed here: TypeScript 5.0 reads useQuery's data as `any`.
  const orgs: Organisation[] = orgsQuery.data ?? [];
  const projects: Project[] = projectsQuery.data ?? [];
 
  return useMemo(() => {
    const shared: OwnerOption[] = [
      ...orgs.map((o) => ({
        key: `organisation:${o.id}`,
        kind: "organisation" as const,
        id: o.id,
        name: o.name,
        access: o.access ?? null,
      })),
      // Only projects the caller is a member of can own new work of theirs.
      ...projects
        .filter((p) => p.access)
        .map((p) => ({
          key: `project:${p.id}`,
          kind: "project" as const,
          id: p.id,
          name: p.name,
          access: (p.access ?? null) as AccessMap | null,
        })),
    ];
    const byKey = new Map<string, OwnerOption>(
      [PERSONAL, ...shared].map((o) => [o.key, o]),
    );
    return {
      options: [PERSONAL, ...shared],
      shared,
      byKey,
      /** The owner's name for a shared resource, or null for a personal one. */
      label: (resource: Resource): string | null => {
        const key = ownerKeyOf(resource);
        if (key === PERSONAL.key) return null;
        return byKey.get(key)?.name ?? "Shared";
      },
    };
  }, [orgs, projects]);
}