Spaces:
Paused

File size: 2,534 Bytes
5eb4857
 
d1e80eb
5eb4857
 
 
 
 
 
 
 
 
 
ad4fe35
 
 
 
f3a0c24
 
ad4fe35
f3a0c24
ad4fe35
5eb4857
ad4fe35
 
 
f3a0c24
ad4fe35
 
f3a0c24
ad4fe35
 
 
f3a0c24
ad4fe35
 
 
 
 
 
 
 
 
5eb4857
ad4fe35
 
5eb4857
0d8550d
5eb4857
ad4fe35
 
f3a0c24
5eb4857
ad4fe35
5eb4857
ad4fe35
 
5eb4857
 
 
 
 
0d8550d
ad4fe35
5eb4857
 
 
 
 
 
 
 
d1e80eb
 
5eb4857
f3a0c24
5eb4857
 
d1e80eb
5eb4857
f3a0c24
5eb4857
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
import { useState, useCallback, useEffect } from 'react';
import api from '../utils/api';
import { useAuth } from './useAuth';

export interface User {
  id: string;
  email: string;
  full_name: string;
  role: string;
  is_active: boolean;
  avatar_url?: string;
}

// Global singleton for user list
let globalUsers: User[] = [];
let globalUsersLoading = false;
let globalUsersError: string | null = null;
let globalUsersLoaded = false;
const usersListeners = new Set<(u: User[], l: boolean, e: string | null, d: boolean) => void>();

const notifyUsers = () => usersListeners.forEach(l => l(globalUsers, globalUsersLoading, globalUsersError, globalUsersLoaded));

export const useUsers = (workspaceId?: string) => {
  const [localUsers, setLocalUsers] = useState(globalUsers);
  const [loading, setLoading] = useState(globalUsersLoading);
  const [error, setError] = useState(globalUsersError);
  const [loaded, setLoaded] = useState(globalUsersLoaded);

  useEffect(() => {
    const listener = (u: User[], l: boolean, e: string | null, d: boolean) => {
      setLocalUsers(u);
      setLoading(l);
      setError(e);
      setLoaded(d);
    };
    usersListeners.add(listener);
    return () => {
      usersListeners.delete(listener);
    };
  }, []);

  const fetchUsers = useCallback(async (force: boolean = false) => {
    if (!force && globalUsers.length > 0) return;

    globalUsersLoading = true;
    notifyUsers();
    try {
      const url = workspaceId ? `users/?workspace_id=${workspaceId}` : 'users/';
      const response = await api.get(url);
      globalUsers = response.data;
      globalUsersError = null;
      globalUsersLoaded = true;
    } catch (err: any) {
      globalUsersError = err.response?.data?.detail || 'Failed to fetch users';
    } finally {
      globalUsersLoading = false;
      notifyUsers();
    }
  }, [workspaceId]);

  const addUser = async (payload: any) => {
    try {
      const response = await api.post('users/', payload);
      await fetchUsers(true);
      return response.data;
    } catch (err: any) {
      const detail = err.response?.data?.detail;
      const msg = Array.isArray(detail) ? detail.map((d: any) => d.msg).join(', ') : detail;
      throw new Error(msg || 'Failed to add user');
    }
  };

  const { token } = useAuth();

  useEffect(() => {
    if (token && !globalUsersLoaded && !globalUsersLoading) {
      fetchUsers();
    }
  }, [fetchUsers, token]);

  return { users: localUsers, loading, error, loaded, refresh: () => fetchUsers(true), addUser };
};