@aglyn/tenant-feature-instance 1.0.0-beta.172 → 1.0.0-beta.174
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aglyn/tenant-feature-instance",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.174",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"homepage": "https://aglyn.com",
|
|
6
6
|
"repository": {
|
|
@@ -25,11 +25,11 @@
|
|
|
25
25
|
"./package.json": "./package.json"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@aglyn/aglyn": "1.0.0-beta.
|
|
29
|
-
"@aglyn/shared-data-enums": "1.0.0-beta.
|
|
30
|
-
"@aglyn/shared-ui-jsx": "1.0.0-beta.
|
|
31
|
-
"@aglyn/shared-util-http": "1.0.0-beta.
|
|
32
|
-
"@aglyn/shared-util-timestamp": "1.0.0-beta.
|
|
28
|
+
"@aglyn/aglyn": "1.0.0-beta.174",
|
|
29
|
+
"@aglyn/shared-data-enums": "1.0.0-beta.174",
|
|
30
|
+
"@aglyn/shared-ui-jsx": "1.0.0-beta.174",
|
|
31
|
+
"@aglyn/shared-util-http": "1.0.0-beta.174",
|
|
32
|
+
"@aglyn/shared-util-timestamp": "1.0.0-beta.174",
|
|
33
33
|
"@swc/helpers": "0.5.23",
|
|
34
34
|
"lodash-es": "^4.18.1"
|
|
35
35
|
},
|
|
@@ -14,14 +14,26 @@ export interface HostCampaignOption {
|
|
|
14
14
|
/** The campaign window, for a caller that wants to say when it ran. */
|
|
15
15
|
startAtMs?: number | null;
|
|
16
16
|
endAtMs?: number | null;
|
|
17
|
+
/**
|
|
18
|
+
* The sites the campaign is placed on, `null` when it is on every site.
|
|
19
|
+
* Read from its `visibleTo`; an unscoped campaign is on no site (`[]`).
|
|
20
|
+
*/
|
|
21
|
+
siteIds?: string[] | null;
|
|
17
22
|
}
|
|
18
23
|
export interface HostCampaigns {
|
|
19
24
|
options: HostCampaignOption[];
|
|
20
|
-
/** The
|
|
25
|
+
/** The org holds more matching campaigns than the ceiling offers. */
|
|
21
26
|
truncated: boolean;
|
|
22
27
|
/** The read has answered — false while it is still settling or disabled. */
|
|
23
28
|
ready: boolean;
|
|
24
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* The picker's shape, from a ceilinged read of campaign containers.
|
|
32
|
+
*
|
|
33
|
+
* One mapping for both hooks below, so a campaign reads the same on a site's
|
|
34
|
+
* picker and on the organization's.
|
|
35
|
+
*/
|
|
36
|
+
export declare function hostCampaignOptions(data: ReadonlyArray<Record<string, unknown>> | null | undefined, ready: boolean): HostCampaigns;
|
|
25
37
|
/**
|
|
26
38
|
* THE SITE'S CAMPAIGNS, for a picker on some other record's page.
|
|
27
39
|
*
|
|
@@ -30,6 +42,19 @@ export interface HostCampaigns {
|
|
|
30
42
|
* the form and contact surfaces that need the same list are two other
|
|
31
43
|
* libraries again. One read, one shape, three callers.
|
|
32
44
|
*
|
|
45
|
+
* ## The org's campaigns, as this site sees them
|
|
46
|
+
*
|
|
47
|
+
* Campaign containers belong to the organization
|
|
48
|
+
* (`orgs/{orgId}/emailCampaigns`), and each is placed on some of its sites
|
|
49
|
+
* by its `visibleTo`. A site's picker offers the ones placed on it: `'org'`
|
|
50
|
+
* (every site) or `host:{hostId}`, which is `scopeTokensForHost`. The clause
|
|
51
|
+
* is also what makes the read provable for a collaborator scoped to this
|
|
52
|
+
* site — the rules evaluate `visibleTo` per document on a list, and an
|
|
53
|
+
* unfiltered query is refused whole rather than narrowed.
|
|
54
|
+
*
|
|
55
|
+
* The org comes from the `hostIndex` lookup. Nothing is read until it
|
|
56
|
+
* answers, and a host with no org settles as ready with no campaigns.
|
|
57
|
+
*
|
|
33
58
|
* ## It is OFF unless a caller asks
|
|
34
59
|
*
|
|
35
60
|
* `enabled` defaults to false. The picker this feeds sits on a record's own
|
|
@@ -45,9 +70,29 @@ export interface HostCampaigns {
|
|
|
45
70
|
* ordering on a date would not mis-sort the picker, it would DROP the
|
|
46
71
|
* campaigns that have no dates. The labels are sorted by name here, over the
|
|
47
72
|
* whole ceiling rather than a slice of it, which is the one case sorting a
|
|
48
|
-
* window is honest.
|
|
73
|
+
* window is honest. It is also the one ordering the automatic single-field
|
|
74
|
+
* index on `visibleTo` serves beside `array-contains-any`, so the query
|
|
75
|
+
* needs no composite index.
|
|
49
76
|
*/
|
|
50
77
|
export declare function useHostCampaigns(hostId: string | undefined, options?: {
|
|
51
78
|
enabled?: boolean;
|
|
52
79
|
}): HostCampaigns;
|
|
80
|
+
/**
|
|
81
|
+
* THE ORGANIZATION'S CAMPAIGNS, for a picker on an org-level record — a
|
|
82
|
+
* lead on the org's Leads page, an outbound sequence.
|
|
83
|
+
*
|
|
84
|
+
* Every live container, whichever sites it is placed on: the records these
|
|
85
|
+
* pickers file belong to the organization, and so does the campaign. The
|
|
86
|
+
* read is unfiltered because the surfaces that call it are the org-level
|
|
87
|
+
* hubs, whose readers are org-wide members — the rules admit them to every
|
|
88
|
+
* container, and a `visibleTo` clause would only need the org's whole site
|
|
89
|
+
* list and still miss a site it did not carry. Each option carries its
|
|
90
|
+
* `siteIds`, for a caller that wants to say where a campaign runs.
|
|
91
|
+
*
|
|
92
|
+
* Off unless a caller asks, and ceilinged and ordered, for the reasons
|
|
93
|
+
* {@link useHostCampaigns} gives.
|
|
94
|
+
*/
|
|
95
|
+
export declare function useOrgCampaigns(orgId: string | null | undefined, options?: {
|
|
96
|
+
enabled?: boolean;
|
|
97
|
+
}): HostCampaigns;
|
|
53
98
|
export default useHostCampaigns;
|
|
@@ -14,11 +14,13 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/ 'use client';
|
|
17
|
-
import {
|
|
17
|
+
import { hostIdsFromScope, isOrgWideScope, scopeTokensForHost } from "@aglyn/aglyn";
|
|
18
|
+
import { collection, query, where } from "firebase/firestore";
|
|
18
19
|
import { useMemo } from "react";
|
|
19
20
|
import { collectionCeiling } from "./host-collection-queries.js";
|
|
20
21
|
import { useFirestore } from "./firebase/firebase-services.js";
|
|
21
22
|
import { useFirestoreCollection } from "./use-firestore-collection.js";
|
|
23
|
+
import { useHostOrgIdState } from "./use-host-org-id.js";
|
|
22
24
|
/**
|
|
23
25
|
* How many campaigns a picker offers.
|
|
24
26
|
*
|
|
@@ -27,6 +29,35 @@ import { useFirestoreCollection } from "./use-firestore-collection.js";
|
|
|
27
29
|
* larger window would offer campaigns the list they came from does not show;
|
|
28
30
|
* a smaller one would hide campaigns that are plainly there.
|
|
29
31
|
*/ export const HOST_CAMPAIGN_CEILING = 50;
|
|
32
|
+
const NO_ROWS = Object.freeze([]);
|
|
33
|
+
/**
|
|
34
|
+
* The picker's shape, from a ceilinged read of campaign containers.
|
|
35
|
+
*
|
|
36
|
+
* One mapping for both hooks below, so a campaign reads the same on a site's
|
|
37
|
+
* picker and on the organization's.
|
|
38
|
+
*/ export function hostCampaignOptions(data, ready) {
|
|
39
|
+
const rows = data != null ? data : [];
|
|
40
|
+
const truncated = rows.length > HOST_CAMPAIGN_CEILING;
|
|
41
|
+
const live = rows.slice(0, HOST_CAMPAIGN_CEILING)// A campaign the console soft-deleted is not a campaign a record may be
|
|
42
|
+
// filed under. The campaigns table filters the same field for the same
|
|
43
|
+
// reason.
|
|
44
|
+
.filter((row)=>!row['deletedAt']);
|
|
45
|
+
return {
|
|
46
|
+
options: live.map((row)=>{
|
|
47
|
+
var _row_name, _row_startAtMs, _row_endAtMs;
|
|
48
|
+
const visibleTo = Array.isArray(row['visibleTo']) ? row['visibleTo'] : [];
|
|
49
|
+
return {
|
|
50
|
+
value: String(row['$id']),
|
|
51
|
+
label: String((_row_name = row['name']) != null ? _row_name : row['$id']),
|
|
52
|
+
startAtMs: (_row_startAtMs = row['startAtMs']) != null ? _row_startAtMs : null,
|
|
53
|
+
endAtMs: (_row_endAtMs = row['endAtMs']) != null ? _row_endAtMs : null,
|
|
54
|
+
siteIds: isOrgWideScope(visibleTo) ? null : hostIdsFromScope(visibleTo)
|
|
55
|
+
};
|
|
56
|
+
}).sort((a, b)=>a.label.localeCompare(b.label)),
|
|
57
|
+
truncated,
|
|
58
|
+
ready
|
|
59
|
+
};
|
|
60
|
+
}
|
|
30
61
|
/**
|
|
31
62
|
* THE SITE'S CAMPAIGNS, for a picker on some other record's page.
|
|
32
63
|
*
|
|
@@ -35,6 +66,19 @@ import { useFirestoreCollection } from "./use-firestore-collection.js";
|
|
|
35
66
|
* the form and contact surfaces that need the same list are two other
|
|
36
67
|
* libraries again. One read, one shape, three callers.
|
|
37
68
|
*
|
|
69
|
+
* ## The org's campaigns, as this site sees them
|
|
70
|
+
*
|
|
71
|
+
* Campaign containers belong to the organization
|
|
72
|
+
* (`orgs/{orgId}/emailCampaigns`), and each is placed on some of its sites
|
|
73
|
+
* by its `visibleTo`. A site's picker offers the ones placed on it: `'org'`
|
|
74
|
+
* (every site) or `host:{hostId}`, which is `scopeTokensForHost`. The clause
|
|
75
|
+
* is also what makes the read provable for a collaborator scoped to this
|
|
76
|
+
* site — the rules evaluate `visibleTo` per document on a list, and an
|
|
77
|
+
* unfiltered query is refused whole rather than narrowed.
|
|
78
|
+
*
|
|
79
|
+
* The org comes from the `hostIndex` lookup. Nothing is read until it
|
|
80
|
+
* answers, and a host with no org settles as ready with no campaigns.
|
|
81
|
+
*
|
|
38
82
|
* ## It is OFF unless a caller asks
|
|
39
83
|
*
|
|
40
84
|
* `enabled` defaults to false. The picker this feeds sits on a record's own
|
|
@@ -50,42 +94,65 @@ import { useFirestoreCollection } from "./use-firestore-collection.js";
|
|
|
50
94
|
* ordering on a date would not mis-sort the picker, it would DROP the
|
|
51
95
|
* campaigns that have no dates. The labels are sorted by name here, over the
|
|
52
96
|
* whole ceiling rather than a slice of it, which is the one case sorting a
|
|
53
|
-
* window is honest.
|
|
97
|
+
* window is honest. It is also the one ordering the automatic single-field
|
|
98
|
+
* index on `visibleTo` serves beside `array-contains-any`, so the query
|
|
99
|
+
* needs no composite index.
|
|
54
100
|
*/ export function useHostCampaigns(hostId, options) {
|
|
55
101
|
var _ref;
|
|
56
102
|
const enabled = (_ref = options == null ? void 0 : options.enabled) != null ? _ref : false;
|
|
57
103
|
const firestore = useFirestore();
|
|
58
|
-
const
|
|
104
|
+
const org = useHostOrgIdState(enabled ? hostId : undefined);
|
|
105
|
+
const orgId = org.orgId;
|
|
106
|
+
// Memoised: a listener dependency, and a fresh array each render would
|
|
107
|
+
// reopen the subscription every time.
|
|
108
|
+
const tokens = useMemo(()=>hostId ? scopeTokensForHost(hostId) : null, [
|
|
109
|
+
hostId
|
|
110
|
+
]);
|
|
111
|
+
const { data, status } = useFirestoreCollection(()=>enabled && hostId && orgId && tokens ? collectionCeiling(query(collection(firestore, 'orgs', orgId, 'emailCampaigns'), where('visibleTo', 'array-contains-any', tokens)), HOST_CAMPAIGN_CEILING) : null, [
|
|
59
112
|
firestore,
|
|
60
113
|
hostId,
|
|
114
|
+
orgId,
|
|
115
|
+
tokens,
|
|
116
|
+
enabled
|
|
117
|
+
], {
|
|
118
|
+
idField: '$id'
|
|
119
|
+
});
|
|
120
|
+
const ready = enabled && Boolean(hostId) && org.loaded && (orgId ? status !== 'loading' : true);
|
|
121
|
+
return useMemo(()=>hostCampaignOptions(orgId ? data : NO_ROWS, ready), [
|
|
122
|
+
data,
|
|
123
|
+
orgId,
|
|
124
|
+
ready
|
|
125
|
+
]);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* THE ORGANIZATION'S CAMPAIGNS, for a picker on an org-level record — a
|
|
129
|
+
* lead on the org's Leads page, an outbound sequence.
|
|
130
|
+
*
|
|
131
|
+
* Every live container, whichever sites it is placed on: the records these
|
|
132
|
+
* pickers file belong to the organization, and so does the campaign. The
|
|
133
|
+
* read is unfiltered because the surfaces that call it are the org-level
|
|
134
|
+
* hubs, whose readers are org-wide members — the rules admit them to every
|
|
135
|
+
* container, and a `visibleTo` clause would only need the org's whole site
|
|
136
|
+
* list and still miss a site it did not carry. Each option carries its
|
|
137
|
+
* `siteIds`, for a caller that wants to say where a campaign runs.
|
|
138
|
+
*
|
|
139
|
+
* Off unless a caller asks, and ceilinged and ordered, for the reasons
|
|
140
|
+
* {@link useHostCampaigns} gives.
|
|
141
|
+
*/ export function useOrgCampaigns(orgId, options) {
|
|
142
|
+
var _ref;
|
|
143
|
+
const enabled = (_ref = options == null ? void 0 : options.enabled) != null ? _ref : false;
|
|
144
|
+
const firestore = useFirestore();
|
|
145
|
+
const { data, status } = useFirestoreCollection(()=>enabled && orgId ? collectionCeiling(collection(firestore, 'orgs', orgId, 'emailCampaigns'), HOST_CAMPAIGN_CEILING) : null, [
|
|
146
|
+
firestore,
|
|
147
|
+
orgId,
|
|
61
148
|
enabled
|
|
62
149
|
], {
|
|
63
150
|
idField: '$id'
|
|
64
151
|
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const truncated = rows.length > HOST_CAMPAIGN_CEILING;
|
|
68
|
-
const live = rows.slice(0, HOST_CAMPAIGN_CEILING)// A campaign the console soft-deleted is not a campaign a record may be
|
|
69
|
-
// filed under. The campaigns table filters the same field for the same
|
|
70
|
-
// reason.
|
|
71
|
-
.filter((row)=>!row['deletedAt']);
|
|
72
|
-
return {
|
|
73
|
-
options: live.map((row)=>{
|
|
74
|
-
var _row_name, _row_startAtMs, _row_endAtMs;
|
|
75
|
-
return {
|
|
76
|
-
value: String(row['$id']),
|
|
77
|
-
label: String((_row_name = row['name']) != null ? _row_name : row['$id']),
|
|
78
|
-
startAtMs: (_row_startAtMs = row['startAtMs']) != null ? _row_startAtMs : null,
|
|
79
|
-
endAtMs: (_row_endAtMs = row['endAtMs']) != null ? _row_endAtMs : null
|
|
80
|
-
};
|
|
81
|
-
}).sort((a, b)=>a.label.localeCompare(b.label)),
|
|
82
|
-
truncated,
|
|
83
|
-
ready: enabled && status !== 'loading'
|
|
84
|
-
};
|
|
85
|
-
}, [
|
|
152
|
+
const ready = enabled && Boolean(orgId) && status !== 'loading';
|
|
153
|
+
return useMemo(()=>hostCampaignOptions(data, ready), [
|
|
86
154
|
data,
|
|
87
|
-
|
|
88
|
-
status
|
|
155
|
+
ready
|
|
89
156
|
]);
|
|
90
157
|
}
|
|
91
158
|
export default useHostCampaigns;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../libs/tenant/feature/instance/src/lib/hooks/use-host-campaigns.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n'use client'\n\nimport { collection } from 'firebase/firestore'\nimport { useMemo } from 'react'\nimport { collectionCeiling } from './host-collection-queries'\nimport { useFirestore } from './firebase/firebase-services'\nimport { useFirestoreCollection } from './use-firestore-collection'\n\n/**\n * How many campaigns a picker offers.\n *\n * The same fifty the campaigns table draws, so a campaign a merchant can see\n * in the list is a campaign they can file a record under. A picker with a\n * larger window would offer campaigns the list they came from does not show;\n * a smaller one would hide campaigns that are plainly there.\n */\nexport const HOST_CAMPAIGN_CEILING = 50\n\n/** One campaign, as much of it as a picker needs. */\nexport interface HostCampaignOption {\n value: string\n label: string\n /** The campaign window, for a caller that wants to say when it ran. */\n startAtMs?: number | null\n endAtMs?: number | null\n}\n\nexport interface HostCampaigns {\n options: HostCampaignOption[]\n /** The site holds more campaigns than the ceiling offers. */\n truncated: boolean\n /** The read has answered — false while it is still settling or disabled. */\n ready: boolean\n}\n\n/**\n * THE SITE'S CAMPAIGNS, for a picker on some other record's page.\n *\n * Lives here rather than in the marketing plugin because the console app may\n * not import a feature plugin — a screen's detail page is an app route — and\n * the form and contact surfaces that need the same list are two other\n * libraries again. One read, one shape, three callers.\n *\n * ## It is OFF unless a caller asks\n *\n * `enabled` defaults to false. The picker this feeds sits on a record's own\n * page beside fields a reader came for; charging every open of a form, a\n * screen or a contact for a hundred campaign documents they may not touch is\n * the read-on-mount this console refuses. The callers turn it on when their\n * editing surface opens.\n *\n * ## Ordered on the document name\n *\n * `collectionCeiling`, for the reason it exists: `startAtMs` and `endAtMs`\n * are both optional on a campaign — an open-ended one carries neither — so\n * ordering on a date would not mis-sort the picker, it would DROP the\n * campaigns that have no dates. The labels are sorted by name here, over the\n * whole ceiling rather than a slice of it, which is the one case sorting a\n * window is honest.\n */\nexport function useHostCampaigns(\n hostId: string | undefined,\n options?: { enabled?: boolean },\n): HostCampaigns {\n const enabled = options?.enabled ?? false\n const firestore = useFirestore()\n const { data, status } = useFirestoreCollection<Record<string, unknown>>(\n () =>\n enabled && hostId\n ? collectionCeiling(\n collection(firestore, 'hosts', hostId, 'emailCampaigns'),\n HOST_CAMPAIGN_CEILING,\n )\n : null,\n [firestore, hostId, enabled],\n { idField: '$id' },\n )\n\n return useMemo(() => {\n const rows = data ?? []\n const truncated = rows.length > HOST_CAMPAIGN_CEILING\n const live = rows\n .slice(0, HOST_CAMPAIGN_CEILING)\n // A campaign the console soft-deleted is not a campaign a record may be\n // filed under. The campaigns table filters the same field for the same\n // reason.\n .filter((row) => !row['deletedAt'])\n return {\n options: live\n .map((row) => ({\n value: String(row['$id']),\n label: String(row['name'] ?? row['$id']),\n startAtMs: (row['startAtMs'] as number | null | undefined) ?? null,\n endAtMs: (row['endAtMs'] as number | null | undefined) ?? null,\n }))\n .sort((a, b) => a.label.localeCompare(b.label)),\n truncated,\n ready: enabled && status !== 'loading',\n }\n }, [data, enabled, status])\n}\n\nexport default useHostCampaigns\n"],"names":["collection","useMemo","collectionCeiling","useFirestore","useFirestoreCollection","HOST_CAMPAIGN_CEILING","useHostCampaigns","hostId","options","enabled","firestore","data","status","idField","rows","truncated","length","live","slice","filter","row","map","value","String","label","startAtMs","endAtMs","sort","a","b","localeCompare","ready"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GACD;AAEA,SAASA,UAAU,QAAQ,qBAAoB;AAC/C,SAASC,OAAO,QAAQ,QAAO;AAC/B,SAASC,iBAAiB,QAAQ,+BAA2B;AAC7D,SAASC,YAAY,QAAQ,kCAA8B;AAC3D,SAASC,sBAAsB,QAAQ,gCAA4B;AAEnE;;;;;;;CAOC,GACD,OAAO,MAAMC,wBAAwB,GAAE;AAmBvC;;;;;;;;;;;;;;;;;;;;;;;;CAwBC,GACD,OAAO,SAASC,iBACdC,MAA0B,EAC1BC,OAA+B;;IAE/B,MAAMC,kBAAUD,2BAAAA,QAASC,OAAO,mBAAI;IACpC,MAAMC,YAAYP;IAClB,MAAM,EAAEQ,IAAI,EAAEC,MAAM,EAAE,GAAGR,uBACvB,IACEK,WAAWF,SACPL,kBACEF,WAAWU,WAAW,SAASH,QAAQ,mBACvCF,yBAEF,MACN;QAACK;QAAWH;QAAQE;KAAQ,EAC5B;QAAEI,SAAS;IAAM;IAGnB,OAAOZ,QAAQ;QACb,MAAMa,OAAOH,eAAAA,OAAQ,EAAE;QACvB,MAAMI,YAAYD,KAAKE,MAAM,GAAGX;QAChC,MAAMY,OAAOH,KACVI,KAAK,CAAC,GAAGb,sBACV,wEAAwE;QACxE,uEAAuE;QACvE,UAAU;SACTc,MAAM,CAAC,CAACC,MAAQ,CAACA,GAAG,CAAC,YAAY;QACpC,OAAO;YACLZ,SAASS,KACNI,GAAG,CAAC,CAACD;oBAEUA,WACFA,gBACFA;uBAJG;oBACbE,OAAOC,OAAOH,GAAG,CAAC,MAAM;oBACxBI,OAAOD,QAAOH,YAAAA,GAAG,CAAC,OAAO,YAAXA,YAAeA,GAAG,CAAC,MAAM;oBACvCK,SAAS,GAAGL,iBAAAA,GAAG,CAAC,YAAY,YAAhBA,iBAAkD;oBAC9DM,OAAO,GAAGN,eAAAA,GAAG,CAAC,UAAU,YAAdA,eAAgD;gBAC5D;eACCO,IAAI,CAAC,CAACC,GAAGC,IAAMD,EAAEJ,KAAK,CAACM,aAAa,CAACD,EAAEL,KAAK;YAC/CT;YACAgB,OAAOtB,WAAWG,WAAW;QAC/B;IACF,GAAG;QAACD;QAAMF;QAASG;KAAO;AAC5B;AAEA,eAAeN,iBAAgB"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../libs/tenant/feature/instance/src/lib/hooks/use-host-campaigns.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n'use client'\n\nimport {\n hostIdsFromScope,\n isOrgWideScope,\n scopeTokensForHost,\n} from '@aglyn/aglyn'\nimport { collection, query, where } from 'firebase/firestore'\nimport { useMemo } from 'react'\nimport { collectionCeiling } from './host-collection-queries'\nimport { useFirestore } from './firebase/firebase-services'\nimport { useFirestoreCollection } from './use-firestore-collection'\nimport { useHostOrgIdState } from './use-host-org-id'\n\n/**\n * How many campaigns a picker offers.\n *\n * The same fifty the campaigns table draws, so a campaign a merchant can see\n * in the list is a campaign they can file a record under. A picker with a\n * larger window would offer campaigns the list they came from does not show;\n * a smaller one would hide campaigns that are plainly there.\n */\nexport const HOST_CAMPAIGN_CEILING = 50\n\nconst NO_ROWS: ReadonlyArray<Record<string, unknown>> = Object.freeze([])\n\n/** One campaign, as much of it as a picker needs. */\nexport interface HostCampaignOption {\n value: string\n label: string\n /** The campaign window, for a caller that wants to say when it ran. */\n startAtMs?: number | null\n endAtMs?: number | null\n /**\n * The sites the campaign is placed on, `null` when it is on every site.\n * Read from its `visibleTo`; an unscoped campaign is on no site (`[]`).\n */\n siteIds?: string[] | null\n}\n\nexport interface HostCampaigns {\n options: HostCampaignOption[]\n /** The org holds more matching campaigns than the ceiling offers. */\n truncated: boolean\n /** The read has answered — false while it is still settling or disabled. */\n ready: boolean\n}\n\n/**\n * The picker's shape, from a ceilinged read of campaign containers.\n *\n * One mapping for both hooks below, so a campaign reads the same on a site's\n * picker and on the organization's.\n */\nexport function hostCampaignOptions(\n data: ReadonlyArray<Record<string, unknown>> | null | undefined,\n ready: boolean,\n): HostCampaigns {\n const rows = data ?? []\n const truncated = rows.length > HOST_CAMPAIGN_CEILING\n const live = rows\n .slice(0, HOST_CAMPAIGN_CEILING)\n // A campaign the console soft-deleted is not a campaign a record may be\n // filed under. The campaigns table filters the same field for the same\n // reason.\n .filter((row) => !row['deletedAt'])\n return {\n options: live\n .map((row) => {\n const visibleTo = Array.isArray(row['visibleTo'])\n ? (row['visibleTo'] as string[])\n : []\n return {\n value: String(row['$id']),\n label: String(row['name'] ?? row['$id']),\n startAtMs: (row['startAtMs'] as number | null | undefined) ?? null,\n endAtMs: (row['endAtMs'] as number | null | undefined) ?? null,\n siteIds: isOrgWideScope(visibleTo)\n ? null\n : hostIdsFromScope(visibleTo),\n }\n })\n .sort((a, b) => a.label.localeCompare(b.label)),\n truncated,\n ready,\n }\n}\n\n/**\n * THE SITE'S CAMPAIGNS, for a picker on some other record's page.\n *\n * Lives here rather than in the marketing plugin because the console app may\n * not import a feature plugin — a screen's detail page is an app route — and\n * the form and contact surfaces that need the same list are two other\n * libraries again. One read, one shape, three callers.\n *\n * ## The org's campaigns, as this site sees them\n *\n * Campaign containers belong to the organization\n * (`orgs/{orgId}/emailCampaigns`), and each is placed on some of its sites\n * by its `visibleTo`. A site's picker offers the ones placed on it: `'org'`\n * (every site) or `host:{hostId}`, which is `scopeTokensForHost`. The clause\n * is also what makes the read provable for a collaborator scoped to this\n * site — the rules evaluate `visibleTo` per document on a list, and an\n * unfiltered query is refused whole rather than narrowed.\n *\n * The org comes from the `hostIndex` lookup. Nothing is read until it\n * answers, and a host with no org settles as ready with no campaigns.\n *\n * ## It is OFF unless a caller asks\n *\n * `enabled` defaults to false. The picker this feeds sits on a record's own\n * page beside fields a reader came for; charging every open of a form, a\n * screen or a contact for a hundred campaign documents they may not touch is\n * the read-on-mount this console refuses. The callers turn it on when their\n * editing surface opens.\n *\n * ## Ordered on the document name\n *\n * `collectionCeiling`, for the reason it exists: `startAtMs` and `endAtMs`\n * are both optional on a campaign — an open-ended one carries neither — so\n * ordering on a date would not mis-sort the picker, it would DROP the\n * campaigns that have no dates. The labels are sorted by name here, over the\n * whole ceiling rather than a slice of it, which is the one case sorting a\n * window is honest. It is also the one ordering the automatic single-field\n * index on `visibleTo` serves beside `array-contains-any`, so the query\n * needs no composite index.\n */\nexport function useHostCampaigns(\n hostId: string | undefined,\n options?: { enabled?: boolean },\n): HostCampaigns {\n const enabled = options?.enabled ?? false\n const firestore = useFirestore()\n const org = useHostOrgIdState(enabled ? hostId : undefined)\n const orgId = org.orgId\n // Memoised: a listener dependency, and a fresh array each render would\n // reopen the subscription every time.\n const tokens = useMemo(\n () => (hostId ? scopeTokensForHost(hostId) : null),\n [hostId],\n )\n const { data, status } = useFirestoreCollection<Record<string, unknown>>(\n () =>\n enabled && hostId && orgId && tokens\n ? collectionCeiling(\n query(\n collection(firestore, 'orgs', orgId, 'emailCampaigns'),\n where('visibleTo', 'array-contains-any', tokens),\n ),\n HOST_CAMPAIGN_CEILING,\n )\n : null,\n [firestore, hostId, orgId, tokens, enabled],\n { idField: '$id' },\n )\n\n const ready =\n enabled &&\n Boolean(hostId) &&\n org.loaded &&\n (orgId ? status !== 'loading' : true)\n return useMemo(\n () => hostCampaignOptions(orgId ? data : NO_ROWS, ready),\n [data, orgId, ready],\n )\n}\n\n/**\n * THE ORGANIZATION'S CAMPAIGNS, for a picker on an org-level record — a\n * lead on the org's Leads page, an outbound sequence.\n *\n * Every live container, whichever sites it is placed on: the records these\n * pickers file belong to the organization, and so does the campaign. The\n * read is unfiltered because the surfaces that call it are the org-level\n * hubs, whose readers are org-wide members — the rules admit them to every\n * container, and a `visibleTo` clause would only need the org's whole site\n * list and still miss a site it did not carry. Each option carries its\n * `siteIds`, for a caller that wants to say where a campaign runs.\n *\n * Off unless a caller asks, and ceilinged and ordered, for the reasons\n * {@link useHostCampaigns} gives.\n */\nexport function useOrgCampaigns(\n orgId: string | null | undefined,\n options?: { enabled?: boolean },\n): HostCampaigns {\n const enabled = options?.enabled ?? false\n const firestore = useFirestore()\n const { data, status } = useFirestoreCollection<Record<string, unknown>>(\n () =>\n enabled && orgId\n ? collectionCeiling(\n collection(firestore, 'orgs', orgId, 'emailCampaigns'),\n HOST_CAMPAIGN_CEILING,\n )\n : null,\n [firestore, orgId, enabled],\n { idField: '$id' },\n )\n\n const ready = enabled && Boolean(orgId) && status !== 'loading'\n return useMemo(() => hostCampaignOptions(data, ready), [data, ready])\n}\n\nexport default useHostCampaigns\n"],"names":["hostIdsFromScope","isOrgWideScope","scopeTokensForHost","collection","query","where","useMemo","collectionCeiling","useFirestore","useFirestoreCollection","useHostOrgIdState","HOST_CAMPAIGN_CEILING","NO_ROWS","Object","freeze","hostCampaignOptions","data","ready","rows","truncated","length","live","slice","filter","row","options","map","visibleTo","Array","isArray","value","String","label","startAtMs","endAtMs","siteIds","sort","a","b","localeCompare","useHostCampaigns","hostId","enabled","firestore","org","undefined","orgId","tokens","status","idField","Boolean","loaded","useOrgCampaigns"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GACD;AAEA,SACEA,gBAAgB,EAChBC,cAAc,EACdC,kBAAkB,QACb,eAAc;AACrB,SAASC,UAAU,EAAEC,KAAK,EAAEC,KAAK,QAAQ,qBAAoB;AAC7D,SAASC,OAAO,QAAQ,QAAO;AAC/B,SAASC,iBAAiB,QAAQ,+BAA2B;AAC7D,SAASC,YAAY,QAAQ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