@danainnovations/directory 0.1.1
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.
- package/README.md +105 -0
- package/dist/index.d.mts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +245 -0
- package/dist/index.mjs +211 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @danainnovations/directory
|
|
2
|
+
|
|
3
|
+
Sonance employee directory SDK. Zero-config access to the company's active directory (synced from Okta/AD via SCIM).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @danainnovations/directory
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
No environment variables or API keys needed. Just install and use.
|
|
12
|
+
|
|
13
|
+
## React Hooks
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { useUsers, useUser, useDepartments, useOrgChart } from '@danainnovations/directory'
|
|
17
|
+
|
|
18
|
+
// Search and browse employees
|
|
19
|
+
const { users, total, loading, error } = useUsers({ search: 'john', department: 'Engineering', page: 1, limit: 20 })
|
|
20
|
+
|
|
21
|
+
// Look up a single user by email or ID
|
|
22
|
+
const { user, loading, error } = useUser('john@sonance.com')
|
|
23
|
+
|
|
24
|
+
// Get all departments with employee counts
|
|
25
|
+
const { departments, loading, error } = useDepartments()
|
|
26
|
+
|
|
27
|
+
// Get the full org chart (manager hierarchy tree)
|
|
28
|
+
const { tree, loading, error } = useOrgChart()
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Async Functions
|
|
32
|
+
|
|
33
|
+
For server-side, API routes, scripts, or non-React code:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { getUsers, getUser, getDepartments, getOrgChart } from '@danainnovations/directory'
|
|
37
|
+
|
|
38
|
+
const result = await getUsers({ search: 'sales', department: 'Sales', page: 1, limit: 50 })
|
|
39
|
+
// result = { data: DirectoryUser[], total: number, page: number, limit: number, totalPages: number }
|
|
40
|
+
|
|
41
|
+
const user = await getUser('john@sonance.com') // or pass a user ID
|
|
42
|
+
// user = DirectoryUser | null
|
|
43
|
+
|
|
44
|
+
const departments = await getDepartments()
|
|
45
|
+
// departments = [{ name: 'Engineering', count: 42 }, ...]
|
|
46
|
+
|
|
47
|
+
const tree = await getOrgChart()
|
|
48
|
+
// tree = OrgNode[] (recursive tree with .reports[])
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Types
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
interface DirectoryUser {
|
|
55
|
+
id: string
|
|
56
|
+
email: string
|
|
57
|
+
full_name: string | null
|
|
58
|
+
given_name: string | null
|
|
59
|
+
family_name: string | null
|
|
60
|
+
department: string | null
|
|
61
|
+
job_title: string | null
|
|
62
|
+
location: string | null
|
|
63
|
+
phone_number: string | null
|
|
64
|
+
avatar_url: string | null
|
|
65
|
+
manager_email: string | null
|
|
66
|
+
role: string | null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface Department {
|
|
70
|
+
name: string
|
|
71
|
+
count: number
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface OrgNode {
|
|
75
|
+
id: string
|
|
76
|
+
email: string
|
|
77
|
+
name: string
|
|
78
|
+
title: string | null
|
|
79
|
+
department: string | null
|
|
80
|
+
reports: OrgNode[] // direct reports (recursive)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface UserSearchParams {
|
|
84
|
+
search?: string // searches name, email, department
|
|
85
|
+
department?: string // exact department match
|
|
86
|
+
page?: number // default: 1
|
|
87
|
+
limit?: number // default: 50, max: 100
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## How It Works
|
|
92
|
+
|
|
93
|
+
This package connects directly to a Supabase database that receives live SCIM provisioning from Okta. The data updates automatically whenever IT makes changes in Okta/Active Directory (new hires, terminations, department changes, etc.). Only active employees are returned — non-person accounts (service accounts, rooms, test users) are filtered out by row-level security.
|
|
94
|
+
|
|
95
|
+
## AI Integration Notes
|
|
96
|
+
|
|
97
|
+
When integrating this package into a project:
|
|
98
|
+
- Use the React hooks (`useUsers`, `useUser`, `useDepartments`, `useOrgChart`) for client components
|
|
99
|
+
- Use the async functions (`getUsers`, `getUser`, `getDepartments`, `getOrgChart`) for server components, API routes, or scripts
|
|
100
|
+
- All hooks return `{ loading, error }` alongside the data
|
|
101
|
+
- The `useUsers` hook re-fetches automatically when search params change
|
|
102
|
+
- No providers, context, or setup needed — just import and use
|
|
103
|
+
- For people pickers / autocomplete, use `useUsers({ search: inputValue })` with a debounced input
|
|
104
|
+
- For org charts, `useOrgChart()` returns a tree — each node has a `.reports[]` array of direct reports
|
|
105
|
+
- For department filters, `useDepartments()` returns departments sorted alphabetically with employee counts
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
interface DirectoryUser {
|
|
2
|
+
id: string;
|
|
3
|
+
email: string;
|
|
4
|
+
full_name: string | null;
|
|
5
|
+
given_name: string | null;
|
|
6
|
+
family_name: string | null;
|
|
7
|
+
department: string | null;
|
|
8
|
+
job_title: string | null;
|
|
9
|
+
location: string | null;
|
|
10
|
+
phone_number: string | null;
|
|
11
|
+
avatar_url: string | null;
|
|
12
|
+
manager_email: string | null;
|
|
13
|
+
role: string | null;
|
|
14
|
+
}
|
|
15
|
+
interface UserSearchParams {
|
|
16
|
+
search?: string;
|
|
17
|
+
department?: string;
|
|
18
|
+
page?: number;
|
|
19
|
+
limit?: number;
|
|
20
|
+
}
|
|
21
|
+
interface UserSearchResult {
|
|
22
|
+
data: DirectoryUser[];
|
|
23
|
+
total: number;
|
|
24
|
+
page: number;
|
|
25
|
+
limit: number;
|
|
26
|
+
totalPages: number;
|
|
27
|
+
}
|
|
28
|
+
interface Department {
|
|
29
|
+
name: string;
|
|
30
|
+
count: number;
|
|
31
|
+
}
|
|
32
|
+
interface OrgNode {
|
|
33
|
+
id: string;
|
|
34
|
+
email: string;
|
|
35
|
+
name: string;
|
|
36
|
+
title: string | null;
|
|
37
|
+
department: string | null;
|
|
38
|
+
reports: OrgNode[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare function getUsers(params?: UserSearchParams): Promise<UserSearchResult>;
|
|
42
|
+
declare function getUser(emailOrId: string): Promise<DirectoryUser | null>;
|
|
43
|
+
declare function getDepartments(): Promise<Department[]>;
|
|
44
|
+
declare function getOrgChart(): Promise<OrgNode[]>;
|
|
45
|
+
|
|
46
|
+
declare function useUsers(params?: UserSearchParams): {
|
|
47
|
+
users: DirectoryUser[];
|
|
48
|
+
total: number;
|
|
49
|
+
loading: boolean;
|
|
50
|
+
error: Error | null;
|
|
51
|
+
};
|
|
52
|
+
declare function useUser(emailOrId: string | undefined | null): {
|
|
53
|
+
user: DirectoryUser | null;
|
|
54
|
+
loading: boolean;
|
|
55
|
+
error: Error | null;
|
|
56
|
+
};
|
|
57
|
+
declare function useDepartments(): {
|
|
58
|
+
departments: Department[];
|
|
59
|
+
loading: boolean;
|
|
60
|
+
error: Error | null;
|
|
61
|
+
};
|
|
62
|
+
declare function useOrgChart(): {
|
|
63
|
+
tree: OrgNode[];
|
|
64
|
+
loading: boolean;
|
|
65
|
+
error: Error | null;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export { type Department, type DirectoryUser, type OrgNode, type UserSearchParams, type UserSearchResult, getDepartments, getOrgChart, getUser, getUsers, useDepartments, useOrgChart, useUser, useUsers };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
interface DirectoryUser {
|
|
2
|
+
id: string;
|
|
3
|
+
email: string;
|
|
4
|
+
full_name: string | null;
|
|
5
|
+
given_name: string | null;
|
|
6
|
+
family_name: string | null;
|
|
7
|
+
department: string | null;
|
|
8
|
+
job_title: string | null;
|
|
9
|
+
location: string | null;
|
|
10
|
+
phone_number: string | null;
|
|
11
|
+
avatar_url: string | null;
|
|
12
|
+
manager_email: string | null;
|
|
13
|
+
role: string | null;
|
|
14
|
+
}
|
|
15
|
+
interface UserSearchParams {
|
|
16
|
+
search?: string;
|
|
17
|
+
department?: string;
|
|
18
|
+
page?: number;
|
|
19
|
+
limit?: number;
|
|
20
|
+
}
|
|
21
|
+
interface UserSearchResult {
|
|
22
|
+
data: DirectoryUser[];
|
|
23
|
+
total: number;
|
|
24
|
+
page: number;
|
|
25
|
+
limit: number;
|
|
26
|
+
totalPages: number;
|
|
27
|
+
}
|
|
28
|
+
interface Department {
|
|
29
|
+
name: string;
|
|
30
|
+
count: number;
|
|
31
|
+
}
|
|
32
|
+
interface OrgNode {
|
|
33
|
+
id: string;
|
|
34
|
+
email: string;
|
|
35
|
+
name: string;
|
|
36
|
+
title: string | null;
|
|
37
|
+
department: string | null;
|
|
38
|
+
reports: OrgNode[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare function getUsers(params?: UserSearchParams): Promise<UserSearchResult>;
|
|
42
|
+
declare function getUser(emailOrId: string): Promise<DirectoryUser | null>;
|
|
43
|
+
declare function getDepartments(): Promise<Department[]>;
|
|
44
|
+
declare function getOrgChart(): Promise<OrgNode[]>;
|
|
45
|
+
|
|
46
|
+
declare function useUsers(params?: UserSearchParams): {
|
|
47
|
+
users: DirectoryUser[];
|
|
48
|
+
total: number;
|
|
49
|
+
loading: boolean;
|
|
50
|
+
error: Error | null;
|
|
51
|
+
};
|
|
52
|
+
declare function useUser(emailOrId: string | undefined | null): {
|
|
53
|
+
user: DirectoryUser | null;
|
|
54
|
+
loading: boolean;
|
|
55
|
+
error: Error | null;
|
|
56
|
+
};
|
|
57
|
+
declare function useDepartments(): {
|
|
58
|
+
departments: Department[];
|
|
59
|
+
loading: boolean;
|
|
60
|
+
error: Error | null;
|
|
61
|
+
};
|
|
62
|
+
declare function useOrgChart(): {
|
|
63
|
+
tree: OrgNode[];
|
|
64
|
+
loading: boolean;
|
|
65
|
+
error: Error | null;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export { type Department, type DirectoryUser, type OrgNode, type UserSearchParams, type UserSearchResult, getDepartments, getOrgChart, getUser, getUsers, useDepartments, useOrgChart, useUser, useUsers };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
getDepartments: () => getDepartments,
|
|
24
|
+
getOrgChart: () => getOrgChart,
|
|
25
|
+
getUser: () => getUser,
|
|
26
|
+
getUsers: () => getUsers,
|
|
27
|
+
useDepartments: () => useDepartments,
|
|
28
|
+
useOrgChart: () => useOrgChart,
|
|
29
|
+
useUser: () => useUser,
|
|
30
|
+
useUsers: () => useUsers
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(index_exports);
|
|
33
|
+
|
|
34
|
+
// src/client.ts
|
|
35
|
+
var import_supabase_js = require("@supabase/supabase-js");
|
|
36
|
+
var SUPABASE_URL = "https://vfwtukipsinfkfjtivbw.supabase.co";
|
|
37
|
+
var SUPABASE_ANON_KEY = "sb_publishable_V0y4tzcVuftRGEAdvwHloA_iQf7tPUz";
|
|
38
|
+
var client = null;
|
|
39
|
+
function getDirectoryClient() {
|
|
40
|
+
if (!client) {
|
|
41
|
+
client = (0, import_supabase_js.createClient)(SUPABASE_URL, SUPABASE_ANON_KEY, {
|
|
42
|
+
auth: {
|
|
43
|
+
autoRefreshToken: false,
|
|
44
|
+
persistSession: false
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return client;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/queries.ts
|
|
52
|
+
var USER_FIELDS = "id, email, full_name, given_name, family_name, department, job_title, location, phone_number, avatar_url, manager_email, role";
|
|
53
|
+
async function getUsers(params) {
|
|
54
|
+
const supabase = getDirectoryClient();
|
|
55
|
+
const page = params?.page ?? 1;
|
|
56
|
+
const limit = Math.min(params?.limit ?? 50, 100);
|
|
57
|
+
const from = (page - 1) * limit;
|
|
58
|
+
const to = from + limit - 1;
|
|
59
|
+
let query = supabase.from("user_profiles").select(USER_FIELDS, { count: "exact" }).eq("scim_active", true).order("full_name").range(from, to);
|
|
60
|
+
if (params?.department) {
|
|
61
|
+
query = query.eq("department", params.department);
|
|
62
|
+
}
|
|
63
|
+
if (params?.search) {
|
|
64
|
+
query = query.or(
|
|
65
|
+
`full_name.ilike.%${params.search}%,email.ilike.%${params.search}%,department.ilike.%${params.search}%`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
const { data, error, count } = await query;
|
|
69
|
+
if (error) throw new Error(error.message);
|
|
70
|
+
const total = count ?? 0;
|
|
71
|
+
return {
|
|
72
|
+
data: data ?? [],
|
|
73
|
+
total,
|
|
74
|
+
page,
|
|
75
|
+
limit,
|
|
76
|
+
totalPages: Math.ceil(total / limit)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
async function getUser(emailOrId) {
|
|
80
|
+
const supabase = getDirectoryClient();
|
|
81
|
+
if (emailOrId.includes("@")) {
|
|
82
|
+
const { data: data2, error: error2 } = await supabase.from("user_profiles").select(USER_FIELDS).ilike("email", emailOrId).eq("scim_active", true).single();
|
|
83
|
+
if (!error2 && data2) return data2;
|
|
84
|
+
}
|
|
85
|
+
const { data, error } = await supabase.from("user_profiles").select(USER_FIELDS).eq("id", emailOrId).eq("scim_active", true).single();
|
|
86
|
+
if (error) return null;
|
|
87
|
+
return data;
|
|
88
|
+
}
|
|
89
|
+
async function getDepartments() {
|
|
90
|
+
const supabase = getDirectoryClient();
|
|
91
|
+
const { data, error } = await supabase.from("user_profiles").select("department").eq("scim_active", true).not("department", "is", null);
|
|
92
|
+
if (error) throw new Error(error.message);
|
|
93
|
+
const counts = /* @__PURE__ */ new Map();
|
|
94
|
+
for (const row of data ?? []) {
|
|
95
|
+
if (row.department) {
|
|
96
|
+
counts.set(row.department, (counts.get(row.department) ?? 0) + 1);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return Array.from(counts.entries()).map(([name, count]) => ({ name, count })).sort((a, b) => a.name.localeCompare(b.name));
|
|
100
|
+
}
|
|
101
|
+
async function getOrgChart() {
|
|
102
|
+
const supabase = getDirectoryClient();
|
|
103
|
+
const { data, error } = await supabase.from("user_profiles").select("id, email, full_name, job_title, department, manager_email").eq("scim_active", true);
|
|
104
|
+
if (error) throw new Error(error.message);
|
|
105
|
+
if (!data) return [];
|
|
106
|
+
const emailMap = /* @__PURE__ */ new Map();
|
|
107
|
+
for (const user of data) {
|
|
108
|
+
emailMap.set(user.email.toLowerCase(), user);
|
|
109
|
+
}
|
|
110
|
+
const childrenMap = /* @__PURE__ */ new Map();
|
|
111
|
+
const roots = [];
|
|
112
|
+
for (const user of data) {
|
|
113
|
+
const managerEmail = user.manager_email?.toLowerCase();
|
|
114
|
+
if (managerEmail && emailMap.has(managerEmail)) {
|
|
115
|
+
const children = childrenMap.get(managerEmail) ?? [];
|
|
116
|
+
children.push(user);
|
|
117
|
+
childrenMap.set(managerEmail, children);
|
|
118
|
+
} else {
|
|
119
|
+
roots.push(user);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function buildNode(user) {
|
|
123
|
+
const children = childrenMap.get(user.email.toLowerCase()) ?? [];
|
|
124
|
+
return {
|
|
125
|
+
id: user.id,
|
|
126
|
+
email: user.email,
|
|
127
|
+
name: user.full_name ?? user.email,
|
|
128
|
+
title: user.job_title,
|
|
129
|
+
department: user.department,
|
|
130
|
+
reports: children.map(buildNode)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return roots.map(buildNode);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/hooks.ts
|
|
137
|
+
var import_react = require("react");
|
|
138
|
+
function useUsers(params) {
|
|
139
|
+
const [users, setUsers] = (0, import_react.useState)([]);
|
|
140
|
+
const [total, setTotal] = (0, import_react.useState)(0);
|
|
141
|
+
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
142
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
143
|
+
const search = params?.search;
|
|
144
|
+
const department = params?.department;
|
|
145
|
+
const page = params?.page;
|
|
146
|
+
const limit = params?.limit;
|
|
147
|
+
(0, import_react.useEffect)(() => {
|
|
148
|
+
let cancelled = false;
|
|
149
|
+
setLoading(true);
|
|
150
|
+
setError(null);
|
|
151
|
+
getUsers({ search, department, page, limit }).then((result) => {
|
|
152
|
+
if (!cancelled) {
|
|
153
|
+
setUsers(result.data);
|
|
154
|
+
setTotal(result.total);
|
|
155
|
+
}
|
|
156
|
+
}).catch((err) => {
|
|
157
|
+
if (!cancelled) setError(err);
|
|
158
|
+
}).finally(() => {
|
|
159
|
+
if (!cancelled) setLoading(false);
|
|
160
|
+
});
|
|
161
|
+
return () => {
|
|
162
|
+
cancelled = true;
|
|
163
|
+
};
|
|
164
|
+
}, [search, department, page, limit]);
|
|
165
|
+
return { users, total, loading, error };
|
|
166
|
+
}
|
|
167
|
+
function useUser(emailOrId) {
|
|
168
|
+
const [user, setUser] = (0, import_react.useState)(null);
|
|
169
|
+
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
170
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
171
|
+
(0, import_react.useEffect)(() => {
|
|
172
|
+
if (!emailOrId) {
|
|
173
|
+
setUser(null);
|
|
174
|
+
setLoading(false);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
let cancelled = false;
|
|
178
|
+
setLoading(true);
|
|
179
|
+
setError(null);
|
|
180
|
+
getUser(emailOrId).then((result) => {
|
|
181
|
+
if (!cancelled) setUser(result);
|
|
182
|
+
}).catch((err) => {
|
|
183
|
+
if (!cancelled) setError(err);
|
|
184
|
+
}).finally(() => {
|
|
185
|
+
if (!cancelled) setLoading(false);
|
|
186
|
+
});
|
|
187
|
+
return () => {
|
|
188
|
+
cancelled = true;
|
|
189
|
+
};
|
|
190
|
+
}, [emailOrId]);
|
|
191
|
+
return { user, loading, error };
|
|
192
|
+
}
|
|
193
|
+
function useDepartments() {
|
|
194
|
+
const [departments, setDepartments] = (0, import_react.useState)([]);
|
|
195
|
+
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
196
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
197
|
+
(0, import_react.useEffect)(() => {
|
|
198
|
+
let cancelled = false;
|
|
199
|
+
setLoading(true);
|
|
200
|
+
setError(null);
|
|
201
|
+
getDepartments().then((result) => {
|
|
202
|
+
if (!cancelled) setDepartments(result);
|
|
203
|
+
}).catch((err) => {
|
|
204
|
+
if (!cancelled) setError(err);
|
|
205
|
+
}).finally(() => {
|
|
206
|
+
if (!cancelled) setLoading(false);
|
|
207
|
+
});
|
|
208
|
+
return () => {
|
|
209
|
+
cancelled = true;
|
|
210
|
+
};
|
|
211
|
+
}, []);
|
|
212
|
+
return { departments, loading, error };
|
|
213
|
+
}
|
|
214
|
+
function useOrgChart() {
|
|
215
|
+
const [tree, setTree] = (0, import_react.useState)([]);
|
|
216
|
+
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
217
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
218
|
+
(0, import_react.useEffect)(() => {
|
|
219
|
+
let cancelled = false;
|
|
220
|
+
setLoading(true);
|
|
221
|
+
setError(null);
|
|
222
|
+
getOrgChart().then((result) => {
|
|
223
|
+
if (!cancelled) setTree(result);
|
|
224
|
+
}).catch((err) => {
|
|
225
|
+
if (!cancelled) setError(err);
|
|
226
|
+
}).finally(() => {
|
|
227
|
+
if (!cancelled) setLoading(false);
|
|
228
|
+
});
|
|
229
|
+
return () => {
|
|
230
|
+
cancelled = true;
|
|
231
|
+
};
|
|
232
|
+
}, []);
|
|
233
|
+
return { tree, loading, error };
|
|
234
|
+
}
|
|
235
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
236
|
+
0 && (module.exports = {
|
|
237
|
+
getDepartments,
|
|
238
|
+
getOrgChart,
|
|
239
|
+
getUser,
|
|
240
|
+
getUsers,
|
|
241
|
+
useDepartments,
|
|
242
|
+
useOrgChart,
|
|
243
|
+
useUser,
|
|
244
|
+
useUsers
|
|
245
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { createClient } from "@supabase/supabase-js";
|
|
3
|
+
var SUPABASE_URL = "https://vfwtukipsinfkfjtivbw.supabase.co";
|
|
4
|
+
var SUPABASE_ANON_KEY = "sb_publishable_V0y4tzcVuftRGEAdvwHloA_iQf7tPUz";
|
|
5
|
+
var client = null;
|
|
6
|
+
function getDirectoryClient() {
|
|
7
|
+
if (!client) {
|
|
8
|
+
client = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, {
|
|
9
|
+
auth: {
|
|
10
|
+
autoRefreshToken: false,
|
|
11
|
+
persistSession: false
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return client;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/queries.ts
|
|
19
|
+
var USER_FIELDS = "id, email, full_name, given_name, family_name, department, job_title, location, phone_number, avatar_url, manager_email, role";
|
|
20
|
+
async function getUsers(params) {
|
|
21
|
+
const supabase = getDirectoryClient();
|
|
22
|
+
const page = params?.page ?? 1;
|
|
23
|
+
const limit = Math.min(params?.limit ?? 50, 100);
|
|
24
|
+
const from = (page - 1) * limit;
|
|
25
|
+
const to = from + limit - 1;
|
|
26
|
+
let query = supabase.from("user_profiles").select(USER_FIELDS, { count: "exact" }).eq("scim_active", true).order("full_name").range(from, to);
|
|
27
|
+
if (params?.department) {
|
|
28
|
+
query = query.eq("department", params.department);
|
|
29
|
+
}
|
|
30
|
+
if (params?.search) {
|
|
31
|
+
query = query.or(
|
|
32
|
+
`full_name.ilike.%${params.search}%,email.ilike.%${params.search}%,department.ilike.%${params.search}%`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
const { data, error, count } = await query;
|
|
36
|
+
if (error) throw new Error(error.message);
|
|
37
|
+
const total = count ?? 0;
|
|
38
|
+
return {
|
|
39
|
+
data: data ?? [],
|
|
40
|
+
total,
|
|
41
|
+
page,
|
|
42
|
+
limit,
|
|
43
|
+
totalPages: Math.ceil(total / limit)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async function getUser(emailOrId) {
|
|
47
|
+
const supabase = getDirectoryClient();
|
|
48
|
+
if (emailOrId.includes("@")) {
|
|
49
|
+
const { data: data2, error: error2 } = await supabase.from("user_profiles").select(USER_FIELDS).ilike("email", emailOrId).eq("scim_active", true).single();
|
|
50
|
+
if (!error2 && data2) return data2;
|
|
51
|
+
}
|
|
52
|
+
const { data, error } = await supabase.from("user_profiles").select(USER_FIELDS).eq("id", emailOrId).eq("scim_active", true).single();
|
|
53
|
+
if (error) return null;
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
async function getDepartments() {
|
|
57
|
+
const supabase = getDirectoryClient();
|
|
58
|
+
const { data, error } = await supabase.from("user_profiles").select("department").eq("scim_active", true).not("department", "is", null);
|
|
59
|
+
if (error) throw new Error(error.message);
|
|
60
|
+
const counts = /* @__PURE__ */ new Map();
|
|
61
|
+
for (const row of data ?? []) {
|
|
62
|
+
if (row.department) {
|
|
63
|
+
counts.set(row.department, (counts.get(row.department) ?? 0) + 1);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return Array.from(counts.entries()).map(([name, count]) => ({ name, count })).sort((a, b) => a.name.localeCompare(b.name));
|
|
67
|
+
}
|
|
68
|
+
async function getOrgChart() {
|
|
69
|
+
const supabase = getDirectoryClient();
|
|
70
|
+
const { data, error } = await supabase.from("user_profiles").select("id, email, full_name, job_title, department, manager_email").eq("scim_active", true);
|
|
71
|
+
if (error) throw new Error(error.message);
|
|
72
|
+
if (!data) return [];
|
|
73
|
+
const emailMap = /* @__PURE__ */ new Map();
|
|
74
|
+
for (const user of data) {
|
|
75
|
+
emailMap.set(user.email.toLowerCase(), user);
|
|
76
|
+
}
|
|
77
|
+
const childrenMap = /* @__PURE__ */ new Map();
|
|
78
|
+
const roots = [];
|
|
79
|
+
for (const user of data) {
|
|
80
|
+
const managerEmail = user.manager_email?.toLowerCase();
|
|
81
|
+
if (managerEmail && emailMap.has(managerEmail)) {
|
|
82
|
+
const children = childrenMap.get(managerEmail) ?? [];
|
|
83
|
+
children.push(user);
|
|
84
|
+
childrenMap.set(managerEmail, children);
|
|
85
|
+
} else {
|
|
86
|
+
roots.push(user);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function buildNode(user) {
|
|
90
|
+
const children = childrenMap.get(user.email.toLowerCase()) ?? [];
|
|
91
|
+
return {
|
|
92
|
+
id: user.id,
|
|
93
|
+
email: user.email,
|
|
94
|
+
name: user.full_name ?? user.email,
|
|
95
|
+
title: user.job_title,
|
|
96
|
+
department: user.department,
|
|
97
|
+
reports: children.map(buildNode)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return roots.map(buildNode);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/hooks.ts
|
|
104
|
+
import { useState, useEffect } from "react";
|
|
105
|
+
function useUsers(params) {
|
|
106
|
+
const [users, setUsers] = useState([]);
|
|
107
|
+
const [total, setTotal] = useState(0);
|
|
108
|
+
const [loading, setLoading] = useState(true);
|
|
109
|
+
const [error, setError] = useState(null);
|
|
110
|
+
const search = params?.search;
|
|
111
|
+
const department = params?.department;
|
|
112
|
+
const page = params?.page;
|
|
113
|
+
const limit = params?.limit;
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
let cancelled = false;
|
|
116
|
+
setLoading(true);
|
|
117
|
+
setError(null);
|
|
118
|
+
getUsers({ search, department, page, limit }).then((result) => {
|
|
119
|
+
if (!cancelled) {
|
|
120
|
+
setUsers(result.data);
|
|
121
|
+
setTotal(result.total);
|
|
122
|
+
}
|
|
123
|
+
}).catch((err) => {
|
|
124
|
+
if (!cancelled) setError(err);
|
|
125
|
+
}).finally(() => {
|
|
126
|
+
if (!cancelled) setLoading(false);
|
|
127
|
+
});
|
|
128
|
+
return () => {
|
|
129
|
+
cancelled = true;
|
|
130
|
+
};
|
|
131
|
+
}, [search, department, page, limit]);
|
|
132
|
+
return { users, total, loading, error };
|
|
133
|
+
}
|
|
134
|
+
function useUser(emailOrId) {
|
|
135
|
+
const [user, setUser] = useState(null);
|
|
136
|
+
const [loading, setLoading] = useState(true);
|
|
137
|
+
const [error, setError] = useState(null);
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (!emailOrId) {
|
|
140
|
+
setUser(null);
|
|
141
|
+
setLoading(false);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
let cancelled = false;
|
|
145
|
+
setLoading(true);
|
|
146
|
+
setError(null);
|
|
147
|
+
getUser(emailOrId).then((result) => {
|
|
148
|
+
if (!cancelled) setUser(result);
|
|
149
|
+
}).catch((err) => {
|
|
150
|
+
if (!cancelled) setError(err);
|
|
151
|
+
}).finally(() => {
|
|
152
|
+
if (!cancelled) setLoading(false);
|
|
153
|
+
});
|
|
154
|
+
return () => {
|
|
155
|
+
cancelled = true;
|
|
156
|
+
};
|
|
157
|
+
}, [emailOrId]);
|
|
158
|
+
return { user, loading, error };
|
|
159
|
+
}
|
|
160
|
+
function useDepartments() {
|
|
161
|
+
const [departments, setDepartments] = useState([]);
|
|
162
|
+
const [loading, setLoading] = useState(true);
|
|
163
|
+
const [error, setError] = useState(null);
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
let cancelled = false;
|
|
166
|
+
setLoading(true);
|
|
167
|
+
setError(null);
|
|
168
|
+
getDepartments().then((result) => {
|
|
169
|
+
if (!cancelled) setDepartments(result);
|
|
170
|
+
}).catch((err) => {
|
|
171
|
+
if (!cancelled) setError(err);
|
|
172
|
+
}).finally(() => {
|
|
173
|
+
if (!cancelled) setLoading(false);
|
|
174
|
+
});
|
|
175
|
+
return () => {
|
|
176
|
+
cancelled = true;
|
|
177
|
+
};
|
|
178
|
+
}, []);
|
|
179
|
+
return { departments, loading, error };
|
|
180
|
+
}
|
|
181
|
+
function useOrgChart() {
|
|
182
|
+
const [tree, setTree] = useState([]);
|
|
183
|
+
const [loading, setLoading] = useState(true);
|
|
184
|
+
const [error, setError] = useState(null);
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
let cancelled = false;
|
|
187
|
+
setLoading(true);
|
|
188
|
+
setError(null);
|
|
189
|
+
getOrgChart().then((result) => {
|
|
190
|
+
if (!cancelled) setTree(result);
|
|
191
|
+
}).catch((err) => {
|
|
192
|
+
if (!cancelled) setError(err);
|
|
193
|
+
}).finally(() => {
|
|
194
|
+
if (!cancelled) setLoading(false);
|
|
195
|
+
});
|
|
196
|
+
return () => {
|
|
197
|
+
cancelled = true;
|
|
198
|
+
};
|
|
199
|
+
}, []);
|
|
200
|
+
return { tree, loading, error };
|
|
201
|
+
}
|
|
202
|
+
export {
|
|
203
|
+
getDepartments,
|
|
204
|
+
getOrgChart,
|
|
205
|
+
getUser,
|
|
206
|
+
getUsers,
|
|
207
|
+
useDepartments,
|
|
208
|
+
useOrgChart,
|
|
209
|
+
useUser,
|
|
210
|
+
useUsers
|
|
211
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@danainnovations/directory",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Sonance employee directory — zero-config React hooks and async functions",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup",
|
|
21
|
+
"prepublishOnly": "npm run build"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
25
|
+
},
|
|
26
|
+
"peerDependenciesMeta": {
|
|
27
|
+
"react": {
|
|
28
|
+
"optional": true
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@supabase/supabase-js": "^2.95.3"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/react": "^19.0.0",
|
|
36
|
+
"react": "^19.0.0",
|
|
37
|
+
"tsup": "^8.0.0",
|
|
38
|
+
"typescript": "^5.0.0"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"sonance",
|
|
42
|
+
"directory",
|
|
43
|
+
"employee",
|
|
44
|
+
"okta",
|
|
45
|
+
"scim"
|
|
46
|
+
],
|
|
47
|
+
"license": "UNLICENSED"
|
|
48
|
+
}
|