lemma-sdk 0.2.30 → 0.2.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +211 -51
  2. package/dist/react/index.d.ts +20 -0
  3. package/dist/react/index.js +10 -0
  4. package/dist/react/useAgentInputSchema.d.ts +19 -0
  5. package/dist/react/useAgentInputSchema.js +73 -0
  6. package/dist/react/useAgentRun.js +18 -20
  7. package/dist/react/useAgentRuns.d.ts +33 -0
  8. package/dist/react/useAgentRuns.js +149 -0
  9. package/dist/react/useAssistantRun.js +10 -9
  10. package/dist/react/useAssistantSession.js +21 -25
  11. package/dist/react/useBulkRecords.js +9 -16
  12. package/dist/react/useConversation.js +24 -8
  13. package/dist/react/useConversations.d.ts +4 -0
  14. package/dist/react/useConversations.js +49 -3
  15. package/dist/react/useCreateRecord.js +9 -16
  16. package/dist/react/useCurrentUser.d.ts +14 -0
  17. package/dist/react/useCurrentUser.js +68 -0
  18. package/dist/react/useDeleteRecord.js +9 -16
  19. package/dist/react/useFlowRunHistory.js +1 -5
  20. package/dist/react/useFlowSession.js +41 -33
  21. package/dist/react/useForeignKeyOptions.js +26 -15
  22. package/dist/react/useFunctionRun.d.ts +19 -0
  23. package/dist/react/useFunctionRun.js +30 -0
  24. package/dist/react/useFunctionRuns.d.ts +33 -0
  25. package/dist/react/useFunctionRuns.js +149 -0
  26. package/dist/react/useFunctionSession.js +37 -29
  27. package/dist/react/useJoinedRecords.js +24 -23
  28. package/dist/react/useMembers.d.ts +4 -0
  29. package/dist/react/useMembers.js +55 -16
  30. package/dist/react/useOrganizationMembers.d.ts +26 -0
  31. package/dist/react/useOrganizationMembers.js +113 -0
  32. package/dist/react/usePodAccess.d.ts +22 -0
  33. package/dist/react/usePodAccess.js +128 -0
  34. package/dist/react/useRecord.js +24 -13
  35. package/dist/react/useRecordForm.js +1 -18
  36. package/dist/react/useRecords.d.ts +2 -0
  37. package/dist/react/useRecords.js +62 -22
  38. package/dist/react/useRelatedRecords.js +28 -21
  39. package/dist/react/useReverseRelatedRecords.js +30 -21
  40. package/dist/react/useSchemaForm.js +1 -13
  41. package/dist/react/useTable.js +24 -13
  42. package/dist/react/useTables.d.ts +4 -0
  43. package/dist/react/useTables.js +57 -15
  44. package/dist/react/useTaskSession.js +11 -22
  45. package/dist/react/useUpdateRecord.js +9 -16
  46. package/dist/react/useWorkflowResume.d.ts +18 -0
  47. package/dist/react/useWorkflowResume.js +45 -0
  48. package/dist/react/useWorkflowRun.d.ts +21 -0
  49. package/dist/react/useWorkflowRun.js +49 -0
  50. package/dist/react/useWorkflowRuns.d.ts +33 -0
  51. package/dist/react/useWorkflowRuns.js +149 -0
  52. package/dist/react/useWorkflowStart.js +20 -27
  53. package/dist/react/utils.d.ts +5 -0
  54. package/dist/react/utils.js +25 -0
  55. package/dist/types.d.ts +1 -0
  56. package/package.json +2 -4
  57. package/dist/react/components/AssistantChrome.d.ts +0 -86
  58. package/dist/react/components/AssistantChrome.js +0 -48
  59. package/dist/react/components/AssistantEmbedded.d.ts +0 -10
  60. package/dist/react/components/AssistantEmbedded.js +0 -15
  61. package/dist/react/components/AssistantExperience.d.ts +0 -96
  62. package/dist/react/components/AssistantExperience.js +0 -1294
  63. package/dist/react/components/assistant-types.d.ts +0 -80
  64. package/dist/react/components/assistant-types.js +0 -1
@@ -1,12 +1,6 @@
1
- import { useCallback, useEffect, useState } from "react";
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
2
  import { isTerminalFunctionStatus, normalizeRunStatus, sleep } from "../run-utils.js";
3
- function resolvePodId(client, podId) {
4
- const resolved = podId ?? client.podId;
5
- if (!resolved) {
6
- throw new Error("podId is required. Pass podId or set it on LemmaClient.");
7
- }
8
- return resolved;
9
- }
3
+ import { normalizeError, resolvePodClient, resolvePodId } from "./utils.js";
10
4
  function resolveFunctionName(base, override) {
11
5
  const resolved = override ?? base;
12
6
  if (!resolved) {
@@ -14,17 +8,16 @@ function resolveFunctionName(base, override) {
14
8
  }
15
9
  return resolved;
16
10
  }
17
- function normalizeError(error, fallback) {
18
- if (error instanceof Error)
19
- return error;
20
- return new Error(fallback);
21
- }
22
11
  export function useFunctionSession({ client, podId, functionName, runId: externalRunId = null, autoPoll = true, pollIntervalMs = 2000, onRun, onError, }) {
23
12
  const [runId, setRunIdState] = useState(externalRunId);
24
13
  const [run, setRun] = useState(null);
25
14
  const [status, setStatus] = useState(undefined);
26
15
  const [isPolling, setIsPolling] = useState(false);
27
16
  const [error, setError] = useState(null);
17
+ const onRunRef = useRef(onRun);
18
+ const onErrorRef = useRef(onError);
19
+ useEffect(() => { onRunRef.current = onRun; }, [onRun]);
20
+ useEffect(() => { onErrorRef.current = onError; }, [onError]);
28
21
  const setRunId = useCallback((nextRunId) => {
29
22
  setRunIdState(nextRunId);
30
23
  if (!nextRunId) {
@@ -44,27 +37,27 @@ export function useFunctionSession({ client, podId, functionName, runId: externa
44
37
  if (!id)
45
38
  return null;
46
39
  try {
47
- client.setPodId(resolvePodId(client, podId));
40
+ const scopedClient = resolvePodClient(client, resolvePodId(client, podId));
48
41
  const name = resolveFunctionName(functionName);
49
- const nextRun = await client.functions.runs.get(name, id);
42
+ const nextRun = await scopedClient.functions.runs.get(name, id);
50
43
  setRun(nextRun);
51
44
  const nextStatus = normalizeRunStatus(nextRun.status);
52
45
  setStatus(nextStatus);
53
- onRun?.(nextRun);
46
+ onRunRef.current?.(nextRun);
54
47
  return nextRun;
55
48
  }
56
49
  catch (refreshError) {
57
50
  const normalized = normalizeError(refreshError, "Failed to fetch function run.");
58
51
  setError(normalized);
59
- onError?.(refreshError);
52
+ onErrorRef.current?.(refreshError);
60
53
  return null;
61
54
  }
62
- }, [client, functionName, onError, onRun, podId, runId]);
55
+ }, [client, functionName, podId, runId]);
63
56
  const listHistory = useCallback(async (options = {}) => {
64
57
  try {
65
- client.setPodId(resolvePodId(client, podId));
58
+ const scopedClient = resolvePodClient(client, resolvePodId(client, podId));
66
59
  const name = resolveFunctionName(functionName, options.functionName);
67
- const response = await client.functions.runs.list(name, {
60
+ const response = await scopedClient.functions.runs.list(name, {
68
61
  limit: options.limit,
69
62
  pageToken: options.pageToken,
70
63
  });
@@ -73,32 +66,47 @@ export function useFunctionSession({ client, podId, functionName, runId: externa
73
66
  catch (listError) {
74
67
  const normalized = normalizeError(listError, "Failed to list function runs.");
75
68
  setError(normalized);
76
- onError?.(listError);
69
+ onErrorRef.current?.(listError);
77
70
  return [];
78
71
  }
79
- }, [client, functionName, onError, podId]);
72
+ }, [client, functionName, podId]);
80
73
  const start = useCallback(async (options = {}) => {
81
74
  setError(null);
82
- client.setPodId(resolvePodId(client, podId));
75
+ const scopedClient = resolvePodClient(client, resolvePodId(client, podId));
83
76
  const name = resolveFunctionName(functionName, options.functionName);
84
- const created = await client.functions.runs.create(name, {
77
+ const created = await scopedClient.functions.runs.create(name, {
85
78
  input: options.input,
86
79
  });
87
80
  setRun(created);
88
81
  setRunIdState(created.id);
89
82
  const nextStatus = normalizeRunStatus(created.status);
90
83
  setStatus(nextStatus);
91
- onRun?.(created);
84
+ onRunRef.current?.(created);
92
85
  if (options.connect !== false) {
93
86
  await refresh(created.id);
94
87
  }
95
88
  return created;
96
- }, [client, functionName, onRun, podId, refresh]);
89
+ }, [client, functionName, podId, refresh]);
97
90
  useEffect(() => {
98
91
  if (!runId) {
99
92
  return;
100
93
  }
101
- void refresh(runId);
94
+ const controller = new AbortController();
95
+ let cancelled = false;
96
+ (async () => {
97
+ try {
98
+ await refresh(runId);
99
+ }
100
+ catch {
101
+ if (!cancelled) {
102
+ // refresh handles errors internally
103
+ }
104
+ }
105
+ })();
106
+ return () => {
107
+ cancelled = true;
108
+ controller.abort();
109
+ };
102
110
  }, [refresh, runId]);
103
111
  useEffect(() => {
104
112
  if (!autoPoll || !runId) {
@@ -132,14 +140,14 @@ export function useFunctionSession({ client, podId, functionName, runId: externa
132
140
  void loop().catch((pollError) => {
133
141
  const normalized = normalizeError(pollError, "Failed while polling function run.");
134
142
  setError(normalized);
135
- onError?.(pollError);
143
+ onErrorRef.current?.(pollError);
136
144
  setIsPolling(false);
137
145
  });
138
146
  return () => {
139
147
  active = false;
140
148
  abortController.abort();
141
149
  };
142
- }, [autoPoll, onError, pollIntervalMs, refresh, runId]);
150
+ }, [autoPoll, pollIntervalMs, refresh, runId]);
143
151
  return {
144
152
  runId,
145
153
  run,
@@ -1,25 +1,6 @@
1
1
  import { useCallback, useEffect, useMemo, useState } from "react";
2
2
  import { buildJoinedRecordsQuery } from "../datastore-query.js";
3
- function resolvePodId(client, podId) {
4
- const resolved = podId ?? client.podId;
5
- if (!resolved) {
6
- throw new Error("podId is required. Pass podId or set it on LemmaClient.");
7
- }
8
- return resolved;
9
- }
10
- function normalizeError(error, fallback) {
11
- if (error instanceof Error)
12
- return error;
13
- return new Error(fallback);
14
- }
15
- function stringifyComparable(value) {
16
- try {
17
- return JSON.stringify(value);
18
- }
19
- catch {
20
- return String(value);
21
- }
22
- }
3
+ import { normalizeError, resolvePodId, stringifyComparable } from "./utils.js";
23
4
  export function useJoinedRecords({ client, podId, query, enabled = true, autoLoad = true, }) {
24
5
  const [records, setRecords] = useState([]);
25
6
  const [total, setTotal] = useState(0);
@@ -28,7 +9,7 @@ export function useJoinedRecords({ client, podId, query, enabled = true, autoLoa
28
9
  const queryKey = stringifyComparable(query);
29
10
  const stableQuery = useMemo(() => query, [queryKey]);
30
11
  const sql = useMemo(() => buildJoinedRecordsQuery(stableQuery), [stableQuery]);
31
- const refresh = useCallback(async () => {
12
+ const refresh = useCallback(async (signal) => {
32
13
  if (!enabled) {
33
14
  setRecords([]);
34
15
  setTotal(0);
@@ -42,18 +23,23 @@ export function useJoinedRecords({ client, podId, query, enabled = true, autoLoa
42
23
  const resolvedPodId = resolvePodId(client, podId);
43
24
  const scopedClient = resolvedPodId === client.podId ? client : client.withPod(resolvedPodId);
44
25
  const response = await scopedClient.datastore.query(sql);
26
+ if (signal?.aborted)
27
+ return [];
45
28
  const nextRecords = (response.items ?? []);
46
29
  setRecords(nextRecords);
47
30
  setTotal(response.total ?? nextRecords.length);
48
31
  return nextRecords;
49
32
  }
50
33
  catch (refreshError) {
34
+ if (signal?.aborted)
35
+ return [];
51
36
  const normalized = normalizeError(refreshError, "Failed to load joined records.");
52
37
  setError(normalized);
53
38
  return [];
54
39
  }
55
40
  finally {
56
- setIsLoading(false);
41
+ if (!signal?.aborted)
42
+ setIsLoading(false);
57
43
  }
58
44
  }, [client, enabled, podId, sql]);
59
45
  useEffect(() => {
@@ -66,7 +52,22 @@ export function useJoinedRecords({ client, podId, query, enabled = true, autoLoa
66
52
  }
67
53
  if (!autoLoad)
68
54
  return;
69
- void refresh();
55
+ const controller = new AbortController();
56
+ let cancelled = false;
57
+ (async () => {
58
+ try {
59
+ await refresh(controller.signal);
60
+ }
61
+ catch {
62
+ if (!cancelled) {
63
+ setError(normalizeError(new Error("Failed to load joined records."), "Failed to load joined records."));
64
+ }
65
+ }
66
+ })();
67
+ return () => {
68
+ cancelled = true;
69
+ controller.abort();
70
+ };
70
71
  }, [autoLoad, enabled, refresh]);
71
72
  return useMemo(() => ({
72
73
  records,
@@ -13,10 +13,14 @@ export interface UseMembersResult {
13
13
  total: number;
14
14
  nextPageToken: string | null;
15
15
  isLoading: boolean;
16
+ isLoadingMore: boolean;
16
17
  error: Error | null;
17
18
  refresh: (overrides?: {
18
19
  limit?: number;
19
20
  pageToken?: string;
20
21
  }) => Promise<PodMember[]>;
22
+ loadMore: (overrides?: {
23
+ limit?: number;
24
+ }) => Promise<PodMember[]>;
21
25
  }
22
26
  export declare function useMembers({ client, podId, enabled, autoLoad, limit, pageToken, }: UseMembersOptions): UseMembersResult;
@@ -1,23 +1,13 @@
1
1
  import { useCallback, useEffect, useMemo, useState } from "react";
2
- function resolvePodId(client, podId) {
3
- const resolved = podId ?? client.podId;
4
- if (!resolved) {
5
- throw new Error("podId is required. Pass podId or set it on LemmaClient.");
6
- }
7
- return resolved;
8
- }
9
- function normalizeError(error, fallback) {
10
- if (error instanceof Error)
11
- return error;
12
- return new Error(fallback);
13
- }
2
+ import { normalizeError, resolvePodId } from "./utils.js";
14
3
  export function useMembers({ client, podId, enabled = true, autoLoad = true, limit = 100, pageToken, }) {
15
4
  const [members, setMembers] = useState([]);
16
5
  const [total, setTotal] = useState(0);
17
6
  const [nextPageToken, setNextPageToken] = useState(null);
18
7
  const [isLoading, setIsLoading] = useState(false);
8
+ const [isLoadingMore, setIsLoadingMore] = useState(false);
19
9
  const [error, setError] = useState(null);
20
- const refresh = useCallback(async (overrides = {}) => {
10
+ const refresh = useCallback(async (overrides = {}, signal) => {
21
11
  if (!enabled)
22
12
  return [];
23
13
  setIsLoading(true);
@@ -28,6 +18,8 @@ export function useMembers({ client, podId, enabled = true, autoLoad = true, lim
28
18
  limit: overrides.limit ?? limit,
29
19
  pageToken: overrides.pageToken ?? pageToken,
30
20
  });
21
+ if (signal?.aborted)
22
+ return [];
31
23
  const nextMembers = response.items ?? [];
32
24
  setMembers(nextMembers);
33
25
  setTotal(response.total ?? nextMembers.length);
@@ -35,25 +27,72 @@ export function useMembers({ client, podId, enabled = true, autoLoad = true, lim
35
27
  return nextMembers;
36
28
  }
37
29
  catch (refreshError) {
30
+ if (signal?.aborted)
31
+ return [];
38
32
  const normalized = normalizeError(refreshError, "Failed to load pod members.");
39
33
  setError(normalized);
40
34
  return [];
41
35
  }
42
36
  finally {
43
- setIsLoading(false);
37
+ if (!signal?.aborted)
38
+ setIsLoading(false);
44
39
  }
45
40
  }, [client, enabled, limit, pageToken, podId]);
41
+ const loadMore = useCallback(async (overrides = {}) => {
42
+ if (!enabled || !nextPageToken || isLoading || isLoadingMore) {
43
+ return [];
44
+ }
45
+ setIsLoadingMore(true);
46
+ setError(null);
47
+ try {
48
+ const resolvedPodId = resolvePodId(client, podId);
49
+ const response = await client.podMembers.list(resolvedPodId, {
50
+ limit: overrides.limit ?? limit,
51
+ pageToken: nextPageToken,
52
+ });
53
+ const moreMembers = response.items ?? [];
54
+ setMembers((previous) => [...previous, ...moreMembers]);
55
+ setTotal(response.total ?? members.length + moreMembers.length);
56
+ setNextPageToken(response.next_page_token ?? null);
57
+ return moreMembers;
58
+ }
59
+ catch (loadError) {
60
+ const normalized = normalizeError(loadError, "Failed to load more pod members.");
61
+ setError(normalized);
62
+ return [];
63
+ }
64
+ finally {
65
+ setIsLoadingMore(false);
66
+ }
67
+ }, [client, enabled, isLoading, isLoadingMore, limit, members.length, nextPageToken, podId]);
46
68
  useEffect(() => {
47
69
  if (!enabled || !autoLoad)
48
70
  return;
49
- void refresh();
71
+ const controller = new AbortController();
72
+ let cancelled = false;
73
+ (async () => {
74
+ try {
75
+ await refresh({}, controller.signal);
76
+ }
77
+ catch {
78
+ if (!cancelled) {
79
+ setError(normalizeError(new Error("Failed to load pod members."), "Failed to load pod members."));
80
+ }
81
+ }
82
+ })();
83
+ return () => {
84
+ cancelled = true;
85
+ controller.abort();
86
+ };
50
87
  }, [autoLoad, enabled, refresh]);
51
88
  return useMemo(() => ({
52
89
  members,
53
90
  total,
54
91
  nextPageToken,
55
92
  isLoading,
93
+ isLoadingMore,
56
94
  error,
57
95
  refresh,
58
- }), [error, isLoading, members, nextPageToken, refresh, total]);
96
+ loadMore,
97
+ }), [error, isLoading, isLoadingMore, loadMore, members, nextPageToken, refresh, total]);
59
98
  }
@@ -0,0 +1,26 @@
1
+ import type { LemmaClient } from "../client.js";
2
+ import type { OrganizationMember } from "../types.js";
3
+ export interface UseOrganizationMembersOptions {
4
+ client: LemmaClient;
5
+ organizationId: string;
6
+ enabled?: boolean;
7
+ autoLoad?: boolean;
8
+ limit?: number;
9
+ pageToken?: string;
10
+ }
11
+ export interface UseOrganizationMembersResult {
12
+ members: OrganizationMember[];
13
+ total: number;
14
+ nextPageToken: string | null;
15
+ isLoading: boolean;
16
+ isLoadingMore: boolean;
17
+ error: Error | null;
18
+ refresh: (overrides?: {
19
+ limit?: number;
20
+ pageToken?: string;
21
+ }) => Promise<OrganizationMember[]>;
22
+ loadMore: (overrides?: {
23
+ limit?: number;
24
+ }) => Promise<OrganizationMember[]>;
25
+ }
26
+ export declare function useOrganizationMembers({ client, organizationId, enabled, autoLoad, limit, pageToken, }: UseOrganizationMembersOptions): UseOrganizationMembersResult;
@@ -0,0 +1,113 @@
1
+ import { useCallback, useEffect, useMemo, useState } from "react";
2
+ import { normalizeError } from "./utils.js";
3
+ export function useOrganizationMembers({ client, organizationId, enabled = true, autoLoad = true, limit = 100, pageToken, }) {
4
+ const [members, setMembers] = useState([]);
5
+ const [total, setTotal] = useState(0);
6
+ const [nextPageToken, setNextPageToken] = useState(null);
7
+ const [isLoading, setIsLoading] = useState(false);
8
+ const [isLoadingMore, setIsLoadingMore] = useState(false);
9
+ const [error, setError] = useState(null);
10
+ const trimmedOrganizationId = organizationId.trim();
11
+ const isEnabled = enabled && trimmedOrganizationId.length > 0;
12
+ const refresh = useCallback(async (overrides = {}, signal) => {
13
+ if (!isEnabled) {
14
+ setMembers([]);
15
+ setTotal(0);
16
+ setNextPageToken(null);
17
+ setError(null);
18
+ setIsLoading(false);
19
+ return [];
20
+ }
21
+ setIsLoading(true);
22
+ setError(null);
23
+ try {
24
+ const response = await client.organizations.members.list(trimmedOrganizationId, {
25
+ limit: overrides.limit ?? limit,
26
+ pageToken: overrides.pageToken ?? pageToken,
27
+ });
28
+ if (signal?.aborted)
29
+ return [];
30
+ const nextMembers = response.items ?? [];
31
+ setMembers(nextMembers);
32
+ setTotal(response.total ?? nextMembers.length);
33
+ setNextPageToken(response.next_page_token ?? null);
34
+ return nextMembers;
35
+ }
36
+ catch (refreshError) {
37
+ if (signal?.aborted)
38
+ return [];
39
+ const normalized = normalizeError(refreshError, "Failed to load organization members.");
40
+ setError(normalized);
41
+ return [];
42
+ }
43
+ finally {
44
+ if (!signal?.aborted)
45
+ setIsLoading(false);
46
+ }
47
+ }, [client, isEnabled, limit, pageToken, trimmedOrganizationId]);
48
+ const loadMore = useCallback(async (overrides = {}) => {
49
+ if (!isEnabled || !nextPageToken || isLoading || isLoadingMore) {
50
+ return [];
51
+ }
52
+ setIsLoadingMore(true);
53
+ setError(null);
54
+ try {
55
+ const response = await client.organizations.members.list(trimmedOrganizationId, {
56
+ limit: overrides.limit ?? limit,
57
+ pageToken: nextPageToken,
58
+ });
59
+ const moreMembers = response.items ?? [];
60
+ setMembers((previous) => [...previous, ...moreMembers]);
61
+ setTotal(response.total ?? members.length + moreMembers.length);
62
+ setNextPageToken(response.next_page_token ?? null);
63
+ return moreMembers;
64
+ }
65
+ catch (loadError) {
66
+ const normalized = normalizeError(loadError, "Failed to load more organization members.");
67
+ setError(normalized);
68
+ return [];
69
+ }
70
+ finally {
71
+ setIsLoadingMore(false);
72
+ }
73
+ }, [client, isEnabled, isLoading, isLoadingMore, limit, members.length, nextPageToken, trimmedOrganizationId]);
74
+ useEffect(() => {
75
+ if (!isEnabled) {
76
+ setMembers([]);
77
+ setTotal(0);
78
+ setNextPageToken(null);
79
+ setError(null);
80
+ setIsLoading(false);
81
+ setIsLoadingMore(false);
82
+ return;
83
+ }
84
+ if (!autoLoad)
85
+ return;
86
+ const controller = new AbortController();
87
+ let cancelled = false;
88
+ (async () => {
89
+ try {
90
+ await refresh({}, controller.signal);
91
+ }
92
+ catch {
93
+ if (!cancelled) {
94
+ setError(normalizeError(new Error("Failed to load organization members."), "Failed to load organization members."));
95
+ }
96
+ }
97
+ })();
98
+ return () => {
99
+ cancelled = true;
100
+ controller.abort();
101
+ };
102
+ }, [autoLoad, isEnabled, refresh]);
103
+ return useMemo(() => ({
104
+ members,
105
+ total,
106
+ nextPageToken,
107
+ isLoading,
108
+ isLoadingMore,
109
+ error,
110
+ refresh,
111
+ loadMore,
112
+ }), [error, isLoading, isLoadingMore, loadMore, members, nextPageToken, refresh, total]);
113
+ }
@@ -0,0 +1,22 @@
1
+ import type { LemmaClient } from "../client.js";
2
+ import type { PodJoinRequest, PodMember, User } from "../types.js";
3
+ export type PodAccessStatus = "idle" | "checking" | "member" | "missing" | "pending" | "error";
4
+ export interface UsePodAccessOptions {
5
+ client: LemmaClient;
6
+ podId?: string;
7
+ enabled?: boolean;
8
+ autoLoad?: boolean;
9
+ }
10
+ export interface UsePodAccessResult {
11
+ status: PodAccessStatus;
12
+ hasAccess: boolean;
13
+ user: User | null;
14
+ member: PodMember | null;
15
+ joinRequest: PodJoinRequest | null;
16
+ isLoading: boolean;
17
+ isRequestingAccess: boolean;
18
+ error: Error | null;
19
+ refresh: () => Promise<PodAccessStatus>;
20
+ requestAccess: () => Promise<PodJoinRequest>;
21
+ }
22
+ export declare function usePodAccess({ client, podId, enabled, autoLoad, }: UsePodAccessOptions): UsePodAccessResult;
@@ -0,0 +1,128 @@
1
+ import { useCallback, useEffect, useMemo, useState } from "react";
2
+ import { ApiError } from "../http.js";
3
+ import { normalizeError, resolvePodId } from "./utils.js";
4
+ function isMissingAccessError(error) {
5
+ return error instanceof ApiError && (error.statusCode === 403 || error.statusCode === 404);
6
+ }
7
+ export function usePodAccess({ client, podId, enabled = true, autoLoad = true, }) {
8
+ const [status, setStatus] = useState("idle");
9
+ const [user, setUser] = useState(null);
10
+ const [member, setMember] = useState(null);
11
+ const [joinRequest, setJoinRequest] = useState(null);
12
+ const [isLoading, setIsLoading] = useState(false);
13
+ const [isRequestingAccess, setIsRequestingAccess] = useState(false);
14
+ const [error, setError] = useState(null);
15
+ const refresh = useCallback(async () => {
16
+ if (!enabled) {
17
+ setStatus("idle");
18
+ setUser(null);
19
+ setMember(null);
20
+ setJoinRequest(null);
21
+ setError(null);
22
+ setIsLoading(false);
23
+ return "idle";
24
+ }
25
+ setStatus("checking");
26
+ setIsLoading(true);
27
+ setError(null);
28
+ try {
29
+ const resolvedPodId = resolvePodId(client, podId);
30
+ const currentUser = await client.users.current();
31
+ setUser(currentUser);
32
+ try {
33
+ const nextMember = await client.podMembers.get(resolvedPodId, currentUser.id);
34
+ setMember(nextMember);
35
+ setJoinRequest(null);
36
+ setStatus("member");
37
+ return "member";
38
+ }
39
+ catch (membershipError) {
40
+ if (!isMissingAccessError(membershipError)) {
41
+ throw membershipError;
42
+ }
43
+ }
44
+ setMember(null);
45
+ try {
46
+ const request = await client.podJoinRequests.me(resolvedPodId);
47
+ setJoinRequest(request);
48
+ const nextStatus = request?.status === "PENDING" ? "pending" : "missing";
49
+ setStatus(nextStatus);
50
+ return nextStatus;
51
+ }
52
+ catch (joinRequestError) {
53
+ if (!isMissingAccessError(joinRequestError)) {
54
+ throw joinRequestError;
55
+ }
56
+ }
57
+ setJoinRequest(null);
58
+ setStatus("missing");
59
+ return "missing";
60
+ }
61
+ catch (refreshError) {
62
+ const normalized = normalizeError(refreshError, "Failed to check pod access.");
63
+ setError(normalized);
64
+ setStatus("error");
65
+ return "error";
66
+ }
67
+ finally {
68
+ setIsLoading(false);
69
+ }
70
+ }, [client, enabled, podId]);
71
+ const requestAccess = useCallback(async () => {
72
+ setIsRequestingAccess(true);
73
+ setError(null);
74
+ try {
75
+ const resolvedPodId = resolvePodId(client, podId);
76
+ const request = await client.podJoinRequests.create(resolvedPodId);
77
+ setJoinRequest(request);
78
+ setMember(null);
79
+ setStatus(request.status === "PENDING" ? "pending" : "missing");
80
+ return request;
81
+ }
82
+ catch (requestError) {
83
+ const normalized = normalizeError(requestError, "Failed to request pod access.");
84
+ setError(normalized);
85
+ setStatus("error");
86
+ throw normalized;
87
+ }
88
+ finally {
89
+ setIsRequestingAccess(false);
90
+ }
91
+ }, [client, podId]);
92
+ useEffect(() => {
93
+ if (!enabled) {
94
+ setStatus("idle");
95
+ setUser(null);
96
+ setMember(null);
97
+ setJoinRequest(null);
98
+ setError(null);
99
+ setIsLoading(false);
100
+ return;
101
+ }
102
+ if (!autoLoad)
103
+ return;
104
+ void refresh();
105
+ }, [autoLoad, enabled, refresh]);
106
+ return useMemo(() => ({
107
+ status,
108
+ hasAccess: status === "member",
109
+ user,
110
+ member,
111
+ joinRequest,
112
+ isLoading,
113
+ isRequestingAccess,
114
+ error,
115
+ refresh,
116
+ requestAccess,
117
+ }), [
118
+ error,
119
+ isLoading,
120
+ isRequestingAccess,
121
+ joinRequest,
122
+ member,
123
+ refresh,
124
+ requestAccess,
125
+ status,
126
+ user,
127
+ ]);
128
+ }