@uniformdev/context-ui 13.0.1-alpha.132
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/dist/index.d.ts +96 -0
- package/dist/index.esm.js +57 -0
- package/dist/index.js +57 -0
- package/dist/index.mjs +57 -0
- package/package.json +80 -0
- package/readme.md +34 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import * as _emotion_react_types_jsx_namespace from '@emotion/react/types/jsx-namespace';
|
|
2
|
+
import React, { ReactElement, ComponentType } from 'react';
|
|
3
|
+
import { DimensionDefinition, ManifestGetResponse } from '@uniformdev/context/api';
|
|
4
|
+
import { EnrichmentData, DimensionMatch, VariantMatchCriteria } from '@uniformdev/context';
|
|
5
|
+
export * from '@uniformdev/design-system';
|
|
6
|
+
|
|
7
|
+
declare type EditLinkProps = {
|
|
8
|
+
linkTo: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
linkText?: string;
|
|
11
|
+
};
|
|
12
|
+
declare const EditLink: ({ linkTo, name, linkText }: EditLinkProps) => _emotion_react_types_jsx_namespace.EmotionJSX.Element;
|
|
13
|
+
|
|
14
|
+
interface ContextConfig {
|
|
15
|
+
projectId?: string;
|
|
16
|
+
apiHost: string;
|
|
17
|
+
apiKey: string;
|
|
18
|
+
}
|
|
19
|
+
declare type DimensionsData = {
|
|
20
|
+
dimensions: ResolvedDimensionDefinition[];
|
|
21
|
+
dimIndex: Record<string, ResolvedDimensionDefinition>;
|
|
22
|
+
};
|
|
23
|
+
declare type ResolvedDimensionDefinition = DimensionDefinition & {
|
|
24
|
+
displayName: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
interface EnrichmentTagProps {
|
|
28
|
+
value: EnrichmentData[] | null | undefined;
|
|
29
|
+
setValue: (value: EnrichmentData[] | null) => void;
|
|
30
|
+
contextConfig: ContextConfig;
|
|
31
|
+
}
|
|
32
|
+
declare const EnrichmentTag: React.FC<EnrichmentTagProps>;
|
|
33
|
+
|
|
34
|
+
declare const equality: Array<{
|
|
35
|
+
name: string;
|
|
36
|
+
value: DimensionMatch['op'];
|
|
37
|
+
}>;
|
|
38
|
+
interface PersonalizationCriteriaProps {
|
|
39
|
+
value: VariantMatchCriteria | null | undefined;
|
|
40
|
+
setValue: (value: VariantMatchCriteria | null) => void;
|
|
41
|
+
contextConfig: ContextConfig;
|
|
42
|
+
}
|
|
43
|
+
declare const PersonalizationCriteria: React.FC<PersonalizationCriteriaProps>;
|
|
44
|
+
|
|
45
|
+
declare type UIVersionProps = {
|
|
46
|
+
versionMap: Record<number, React.ComponentType>;
|
|
47
|
+
children?: React.ReactNode;
|
|
48
|
+
contextConfig: ContextConfig;
|
|
49
|
+
};
|
|
50
|
+
declare function ProjectUIVersion({ children, versionMap, contextConfig }: UIVersionProps): ReactElement;
|
|
51
|
+
|
|
52
|
+
interface UseContextDataResult {
|
|
53
|
+
result: ManifestGetResponse | null;
|
|
54
|
+
error: string | null;
|
|
55
|
+
loading: boolean;
|
|
56
|
+
notConfigured: boolean;
|
|
57
|
+
}
|
|
58
|
+
declare function useContextData({ apiHost, apiKey, projectId }: ContextConfig): UseContextDataResult;
|
|
59
|
+
|
|
60
|
+
interface UseDimensionsResult {
|
|
61
|
+
result: DimensionsData | null;
|
|
62
|
+
error: string | null;
|
|
63
|
+
loading: boolean;
|
|
64
|
+
notConfigured: boolean;
|
|
65
|
+
}
|
|
66
|
+
declare function useDimensions({ apiHost, apiKey, projectId }: ContextConfig): UseDimensionsResult;
|
|
67
|
+
|
|
68
|
+
interface DataContextErrorProps {
|
|
69
|
+
contextConfig: ContextConfig;
|
|
70
|
+
result: UseContextDataResult | UseDimensionsResult;
|
|
71
|
+
}
|
|
72
|
+
interface ContextDataProps {
|
|
73
|
+
loadingComponent?: ComponentType;
|
|
74
|
+
errorComponent?: ComponentType<DataContextErrorProps>;
|
|
75
|
+
noIntentsComponent?: ComponentType<DataContextErrorProps>;
|
|
76
|
+
contextConfig: ContextConfig;
|
|
77
|
+
}
|
|
78
|
+
declare const ContextData: React.FC<ContextDataProps>;
|
|
79
|
+
declare function useContextConfig(): ContextConfig;
|
|
80
|
+
declare function useManifest(): ManifestGetResponse;
|
|
81
|
+
declare function useDimensionsDataContext(): DimensionsData;
|
|
82
|
+
|
|
83
|
+
declare const validateContextConfig: (contextConfig?: ContextConfig | undefined) => Promise<{
|
|
84
|
+
valid: boolean;
|
|
85
|
+
error?: Error;
|
|
86
|
+
result?: ManifestGetResponse;
|
|
87
|
+
}>;
|
|
88
|
+
|
|
89
|
+
interface UseValidateContextConfigResult {
|
|
90
|
+
validating: boolean;
|
|
91
|
+
error?: Error;
|
|
92
|
+
result?: ManifestGetResponse;
|
|
93
|
+
}
|
|
94
|
+
declare const useValidateContextConfig: (contextConfig?: ContextConfig | undefined) => UseValidateContextConfigResult;
|
|
95
|
+
|
|
96
|
+
export { ContextConfig, ContextData, ContextDataProps, DataContextErrorProps, DimensionsData, EditLink, EditLinkProps, EnrichmentTag, EnrichmentTagProps, PersonalizationCriteria, PersonalizationCriteriaProps, ProjectUIVersion, ResolvedDimensionDefinition, UseContextDataResult, UseDimensionsResult, UseValidateContextConfigResult, equality, useContextConfig, useContextData, useDimensions, useDimensionsDataContext, useManifest, useValidateContextConfig, validateContextConfig };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import{jsx as e}from"@emotion/react";import*as h from"react";import{Icon as Y}from"@uniformdev/design-system";import{CgChevronRight as J}from"react-icons/cg";import{css as K}from"@emotion/react";var T=K`
|
|
2
|
+
display: flex;
|
|
3
|
+
align-items: center;
|
|
4
|
+
font-weight: var(--fw-bold);
|
|
5
|
+
color: var(--brand-secondary-3);
|
|
6
|
+
&:hover,
|
|
7
|
+
&:focus {
|
|
8
|
+
text-decoration-line: underline;
|
|
9
|
+
}
|
|
10
|
+
`;var M=({linkTo:t,name:n,linkText:i=`Edit ${n} Component`})=>e("a",{css:T,title:`Edit ${n} component definition`,rel:"noopener noreferrer",target:"_blank",href:t},i,e(Y,{icon:J,iconColorClass:"currentColor",size:"1.5rem"}));import $,{useMemo as P,useState as V}from"react";import{useEffect as Q,useState as Z}from"react";import{DimensionClient as j,computeDimensionDisplayName as ee,ApiClientError as te}from"@uniformdev/context/api";function S(t,n,i){return t.reduce((a,r)=>{let o=n(r);if(typeof o=="undefined"||o===null)throw new Error("Objectify key selector returned undefined or null.");return a[n(r)]=i?i(r):r,a},{})}function y({apiHost:t,apiKey:n,projectId:i}){let[a,r]=Z({loading:!1,notConfigured:!1,error:null,result:null});return Q(()=>{if(!i||!n||!t){r({notConfigured:!0,loading:!1,error:null,result:null});return}(async()=>{r({notConfigured:!1,loading:!0,error:null,result:null});try{let s=(await new j({projectId:i,apiKey:n,apiHost:t}).get()).dimensions.map(u=>({...u,displayName:ee(u)})),f={dimensions:s,dimIndex:S(s,u=>u.dim,u=>u)};r({notConfigured:!1,loading:!1,error:null,result:f})}catch(c){let s;c instanceof te?s=c.message:s=c.toString(),r({notConfigured:!1,loading:!1,error:s,result:null});return}})()},[t,n,i]),{result:a.result,error:a.error,loading:a.loading,notConfigured:a.notConfigured}}import{getEnrichmentVectorKey as z}from"@uniformdev/context";import{Input as U,Button as ne,InputSelect as oe,Callout as re,Icon as w,LoadingIndicator as ie}from"@uniformdev/design-system";import{CgMathPlus as ae,CgMathMinus as se,CgCloseO as le}from"react-icons/cg";import L from"immer";import{css as N}from"@emotion/react";var et=({value:t,setValue:n,contextConfig:i})=>{let{loading:a,result:r}=y(i),o=P(()=>{if(r)return r.dimensions.filter(m=>m.category==="ENR")},[r]),c=P(()=>{if(!t)return o;if(o)return o.filter(m=>!t.some(b=>z(b.cat,b.key)===m.dim))},[o,t]),[s,f]=V(""),[u,l]=V(50),d=()=>{let[m,b]=s.split("_");v([...t!=null?t:[],{cat:m,key:b,str:u}]),f(""),l(50)},v=m=>{let b=m.length===0?null:m;n(b)};return a||r===null?e(ie,null):e("fieldset",{css:{marginBlock:"var(--spacing-base)"}},e("div",{css:{display:"flex",justifyContent:"space-between",marginBottom:"var(--spacing-base)"}},e("legend",{css:{fontSize:"var(--fs-md)",fontWeight:"var(--fw-bold)"}},"Enrichment Tags"),e(M,{name:"Enrichments",linkText:"Edit Enrichments",linkTo:`${i.apiHost}/projects/${encodeURIComponent(i.projectId)}/personalization/enrichments`})),o!=null&&o.length?e($.Fragment,null,r&&e(de,{list:t!=null?t:[],setList:v,dimIndex:r.dimIndex}),c&&c.length>0?e("div",{css:{display:"flex",flexWrap:"wrap",gap:"var(--spacing-lg)",alignItems:"flex-end"}},e("div",{css:{flexGrow:1}},e(oe,{name:"enrichment-type",label:"Add Tag",showLabel:!0,value:s,options:[{label:"Select",value:""},...c.map(m=>({label:m.displayName,value:m.dim}))],onChange:m=>f(m.currentTarget.value)})),e(ce,{score:u,setValue:l,css:{flexBasis:"9rem"}}),e(ne,{buttonType:"secondary",size:"xl",css:{marginBottom:"var(--spacing-xs)"},onClick:d,disabled:!s},"Add")):null):e(re,{title:"Enrichments",type:"info",css:{marginBlock:"var(--spacing-base)"}},e("p",null,"You do not have any Enrichments configured. Create your first"," ",e("a",{href:`${i.apiHost}/projects/${encodeURIComponent(i.projectId)}/personalization/enrichments`,target:"_blank",rel:"noopener noreferrer",css:{":hover":{textDecorationLine:"underline"}}},"Enrichment"))))},B=N`
|
|
11
|
+
position: absolute;
|
|
12
|
+
bottom: 0.875rem;
|
|
13
|
+
left: var(--spacing-sm);
|
|
14
|
+
display: flex;
|
|
15
|
+
align-items: center;
|
|
16
|
+
justify-content: center;
|
|
17
|
+
height: 1.75rem;
|
|
18
|
+
width: 1.75rem;
|
|
19
|
+
background-color: var(--gray-100);
|
|
20
|
+
border: 1px solid var(--gray-300);
|
|
21
|
+
border-radius: var(--rounded-full);
|
|
22
|
+
`,ue=N`
|
|
23
|
+
${B}
|
|
24
|
+
left: auto;
|
|
25
|
+
right: var(--spacing-sm);
|
|
26
|
+
`,ce=({score:t,setValue:n,...i})=>{let a=r=>{t!==0&&n(r==="increment"?t+10:t-10)};return e("div",{css:{position:"relative"},...i},e(U,{label:"Strength",id:"enrichment-score",type:"text",min:0,value:t||"",onChange:r=>n(Number(r.currentTarget.value)),css:{textAlign:"center"}}),e("button",{type:"button",title:"Reduce enrichment count",onClick:()=>a("decrement"),disabled:t===0,css:B},e(w,{icon:se,iconColorClass:"gray",size:"1.5rem"})),e("button",{type:"button",title:"Reduce enrichment count",onClick:()=>a("increment"),css:ue},e(w,{icon:ae,iconColorClass:"gray",size:"1.5rem"})))},de=({list:t,setList:n,dimIndex:i})=>{let a=o=>{n(L(t,c=>{c.splice(o,1)}))},r=(o,c)=>{n(L(t,s=>{s[o].str=Number(c)}))};return e($.Fragment,null,t.map((o,c)=>{let s=i[z(o.cat,o.key)];return e("div",{css:{display:"flex",alignItems:"center",gap:"var(--spacing-base)",backgroundColor:"var(--brand-secondary-2)",boxShadow:"var(--shadow-base)",borderRadius:"var(--rounded-base)",paddingInline:"var(--spacing-base)",marginBlock:"var(--spacing-base)"},key:`${o.cat}-${o.key}`},e("span",{css:{fontWeight:"var(--fw-bold)",color:s?void 0:"var(--brand-secondary-5)"}},s?s.displayName:`Enrichment '${o.cat}_${o.key}' is unknown`),e("div",{css:{marginLeft:"auto",display:"flex",alignItems:"center",border:"0 solid var(--gray-400)",borderLeftWidth:"1px",borderRightWidth:"1px",padding:"var(--spacing-sm) var(--spacing-base)",flexBasis:"7rem"}},e(U,{type:"text",min:0,title:"score",value:o.str||50,css:{textAlign:"center"},onChange:f=>r(c,f.currentTarget.value)})),e("button",{type:"button",title:"Delete enrichment",onClick:()=>a(c)},e(w,{icon:le,iconColorClass:"red",size:"1.5rem"})))}))};import{useState as fe}from"react";import{LoadingIndicator as pe,Callout as ge,InputSelect as H,InputComboBox as Ce,InputInlineSelect as ve,Icon as W}from"@uniformdev/design-system";import _ from"immer";import{CgCloseO as be,CgMathPlus as he}from"react-icons/cg";import{css as A}from"@emotion/react";var I="6rem",G=A`
|
|
27
|
+
position: relative;
|
|
28
|
+
padding: var(--spacing-base);
|
|
29
|
+
border: 1px solid var(--gray-300);
|
|
30
|
+
box-shadow: var(--shadow-base);
|
|
31
|
+
background-color: white;
|
|
32
|
+
border-radius: var(--rounded-base);
|
|
33
|
+
margin-top: ${I};
|
|
34
|
+
display: flex;
|
|
35
|
+
&:before {
|
|
36
|
+
content: '';
|
|
37
|
+
display: block;
|
|
38
|
+
width: 1px;
|
|
39
|
+
height: ${I};
|
|
40
|
+
background-color: var(--gray-300);
|
|
41
|
+
position: absolute;
|
|
42
|
+
top: -${I};
|
|
43
|
+
left: var(--spacing-lg);
|
|
44
|
+
}
|
|
45
|
+
&:first-of-type {
|
|
46
|
+
margin-top: var(--spacing-md);
|
|
47
|
+
&:before {
|
|
48
|
+
display: none;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
`,O=A`
|
|
52
|
+
display: grid;
|
|
53
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
54
|
+
gap: var(--spacing-base);
|
|
55
|
+
flex-grow: 1;
|
|
56
|
+
margin-right: var(--spacing-base);
|
|
57
|
+
`;var ye=[{name:"=",value:"="},{name:"!=",value:"!="},{name:">",value:">"},{name:">=",value:">="},{name:"<",value:"<"},{name:"<=",value:"<="},{name:"is strongest",value:"+"},{name:"is weakest",value:"-"}],ut=({value:t,setValue:n,contextConfig:i})=>{let{loading:a,result:r}=y(i),o=t||{crit:[]},c=l=>{n({...o,op:l==="&"?void 0:l})},s=()=>{n({...o,crit:[...o.crit,{l:"",op:">",r:50}]})},f=(l,d)=>{n(_(o,v=>{v.crit[d]=l}))},u=l=>{let d=_(o,m=>{m.crit.splice(l,1)}),v=d.crit.length===0?null:d;n(v)};return a||r===null?e(pe,null):e("fieldset",{css:{marginBlock:"var(--spacing-base)"}},e("legend",{css:{fontSize:"var(--fs-md)",fontWeight:"var(--fw-bold)"}},"Personalize This"),o.crit.length?o.crit.map((l,d)=>{var m,b;let v=((m=l.l)==null?void 0:m.length)>0;return e("div",{css:G,key:d},e("div",{css:O},e(H,{name:`lhs-${d}`,label:"Match Dimension",showLabel:!1,value:l.l,options:[{label:"Select",value:""},...r.dimensions.map(p=>({label:p.displayName,value:p.dim}))],onChange:p=>{f({...l,l:p.target.value},d)}}),e("div",{css:{display:"flex",alignItems:"center",gap:"var(--spacing-base)"}},e("span",{css:{color:"var(--gray-700)"}},"score"),e(H,{css:{width:"100%"},name:`op-${d}`,label:"Operator",showLabel:!1,value:l.op,options:ye.map(p=>({label:p.name,value:p.value})),onChange:p=>{let R=p.target.value;f(R==="+"||R==="-"?{...l,op:R,r:void 0,rDim:void 0}:{...l,op:p.target.value},d)},disabled:!v})),l.op==="+"||l.op==="-"?null:e(De,{critHasLhs:v,currentCriteria:l,dimensions:r,update:f,index:d})),e("button",{type:"button",onClick:()=>u(d),title:"Delete Personalization"},e(W,{icon:be,iconColorClass:"red",size:"1.5rem"})),d>0?e("div",{css:{position:"absolute",top:"-4rem",transform:"translateX(calc(1.5rem - 50%))"}},e(ve,{disabled:d>1,value:(b=o.op)!=null?b:"&",options:[{label:"AND",value:"&"},{label:"OR",value:"|"}],onChange:p=>{c(p.value)}})):null)}):e(ge,{title:"Default variant",type:"info",css:{marginBlock:"var(--spacing-base)"}},e("p",null,'This personalized variant has no match criteria and will be shown to any visitor that does not match any preceding variants. Ensure that default variants come last in the variant list. Personalize this variant by clicking "Add Criteria" to get started.')),e("button",{type:"button",onClick:s,css:{color:"var(--brand-secondary-3)",display:"flex",gap:"var(--spacing-sm)",fontWeight:"var(--fw-bold)",marginTop:"var(--spacing-md)",alignItems:"center"}},e("div",{css:{color:"white",backgroundColor:"var(--brand-secondary-3)",borderRadius:"var(--rounded-full)",padding:"var(--spacing-xs)"}},e(W,{icon:he,iconColorClass:"currentColor",size:"1.25rem"})),"Add Criteria"))};function De({update:t,currentCriteria:n,index:i,dimensions:a,critHasLhs:r}){var s,f;let[o,c]=fe(n.r&&D(n.r)!==null?n.r.toString(10):"");return e(Ce,{name:`rhs-${i}`,inputValue:o,value:{label:n.rDim?(f=(s=a.dimIndex[n.rDim])==null?void 0:s.displayName)!=null?f:n.r:"",value:n.r,isDisabled:!1},styles:{option:(u,{isDisabled:l})=>({...u,background:l?"transparent":void 0,fontSize:l?"0.8rem":void 0})},options:[{label:"Enter numeric score or choose another dimension score",value:"",isDisabled:!0},...a.dimensions.map(u=>({label:u.displayName,value:u.dim,isDisabled:!1}))],onChange:u=>{var l;u&&t({...n,rDim:(l=u.value)==null?void 0:l.toString(),r:void 0},i)},onInputChange:(u,l)=>{let d=D(u);l.action==="input-change"||l.action==="set-value"?(c(u),(d||u==="")&&t({...n,r:u,rDim:void 0},i)):!d&&!D(l.prevInputValue)&&c(u)},isDisabled:!r,noOptionsMessage:({inputValue:u})=>D(u)?`${u}`:"No options match"})}function D(t){return/^\d+$/.test(t.toString(10))}import Ie from"react";import{useEffect as Ee,useState as xe}from"react";import{ManifestClient as Re,ApiClientError as we}from"@uniformdev/context/api";function E({apiHost:t,apiKey:n,projectId:i}){let[a,r]=xe({loading:!1,notConfigured:!1,error:null,result:null});return Ee(()=>{if(!i||!n||!t){r({notConfigured:!0,loading:!1,error:null,result:null});return}(async()=>{r({notConfigured:!1,loading:!0,error:null,result:null});try{let s=await new Re({projectId:i,apiKey:n,apiHost:t}).get({preview:!0});r({notConfigured:!1,loading:!1,error:null,result:s})}catch(c){let s;c instanceof we?(c.statusCode===403&&(s=`The API key ${n} did not have permissions to fetch the manifest. Ensure Context > Read Drafts permissions are granted.`),s=c.message):s=c.toString(),r({notConfigured:!1,loading:!1,error:s,result:null});return}})()},[t,n,i]),{result:a.result,error:a.error,loading:a.loading,notConfigured:a.notConfigured}}import{LoadingIndicator as ke}from"@uniformdev/design-system";function ht({children:t,versionMap:n,contextConfig:i}){let{loading:a,result:r}=E(i);if(a)return e(ke,null);if(r){let o=n[r.project.ui_version];if(o)return e(o,null)}return e(Ie.Fragment,null,t)}import q,{createContext as Te,useContext as k}from"react";import{LoadingIndicator as Me}from"@uniformdev/design-system";var x=Te(null),Tt=({loadingComponent:t,errorComponent:n,contextConfig:i,children:a})=>{let r=E(i),o=y(i);return r.error||r.notConfigured?n?e(n,{contextConfig:i,result:r}):e(q.Fragment,null,"ErrorComponent is not configured"):o.error||o.notConfigured?n?e(n,{contextConfig:i,result:o}):e(q.Fragment,null,"ErrorComponent is not configured"):r.loading||o.loading?t?e(t,null):e(Me,null):e(x.Provider,{value:{manifest:r.result,dimensions:o.result,contextConfig:i}},a)};function Mt(){let t=k(x);if(!(t!=null&&t.contextConfig))throw new Error("Not within DataContext! Configuration data is not exist.");return t.contextConfig}function St(){let t=k(x);if(!(t!=null&&t.manifest))throw new Error("Not within DataContext! Manifest data is not exist.");return t.manifest}function Pt(){let t=k(x);if(!(t!=null&&t.dimensions))throw new Error("Not within DataContext! Dimensions data is not exist.");return t.dimensions}import{validate as Se}from"uuid";import{ManifestClient as Pe}from"@uniformdev/context/api";var F=async t=>{if(!t)return{valid:!1,error:new Error("contextConfig was not defined.")};if(!t.apiHost)return{valid:!1,error:new Error("apiHost was not defined.")};if(!t.apiKey)return{valid:!1,error:new Error("apiKey was not defined.")};if(!Se(t.apiKey)&&!t.projectId)return{valid:!1,error:new Error("projectId is required when using a modern API key.")};let n=new Pe({projectId:t.projectId,apiKey:t.apiKey,apiHost:t.apiHost});try{let i=await n.get({preview:!0});return{valid:!0,result:i}}catch(i){return{valid:!1,error:i}}};import{useEffect as Ve,useState as Le}from"react";var At=t=>{let[n,i]=Le({validating:!1,error:void 0}),{apiKey:a,apiHost:r,projectId:o}=t||{};return Ve(()=>{if(!a||!r)return;(async()=>{i({validating:!0,error:void 0});let{error:s,result:f}=await F({apiHost:r,apiKey:a,projectId:o});i(s?{error:s,validating:!1}:{error:void 0,validating:!1,result:f})})()},[r,a,o]),{validating:n.validating,error:n.error,result:n.result}};export*from"@uniformdev/design-system";export{Tt as ContextData,M as EditLink,et as EnrichmentTag,ut as PersonalizationCriteria,ht as ProjectUIVersion,ye as equality,Mt as useContextConfig,E as useContextData,y as useDimensions,Pt as useDimensionsDataContext,St as useManifest,At as useValidateContextConfig,F as validateContextConfig};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
var ue=Object.create;var S=Object.defineProperty;var ce=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var me=Object.getPrototypeOf,fe=Object.prototype.hasOwnProperty;var K=e=>S(e,"__esModule",{value:!0});var pe=(e,n)=>{for(var o in n)S(e,o,{get:n[o],enumerable:!0})},C=(e,n,o,a)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of de(n))!fe.call(e,r)&&(o||r!=="default")&&S(e,r,{get:()=>n[r],enumerable:!(a=ce(n,r))||a.enumerable});return e},I=(e,n)=>C(K(S(e!=null?ue(me(e)):{},"default",!n&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e),ge=(e=>(n,o)=>e&&e.get(n)||(o=C(K({}),n,1),e&&e.set(n,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var h={};pe(h,{ContextData:()=>we,EditLink:()=>A,EnrichmentTag:()=>ve,PersonalizationCriteria:()=>Ee,ProjectUIVersion:()=>Re,equality:()=>oe,useContextConfig:()=>Ie,useContextData:()=>M,useDimensions:()=>w,useDimensionsDataContext:()=>Te,useManifest:()=>ke,useValidateContextConfig:()=>Me,validateContextConfig:()=>F});var t=require("@emotion/react"),R=I(require("react"));var J=require("@uniformdev/design-system"),Q=require("react-icons/cg");var X=require("@emotion/react"),Y=X.css`
|
|
2
|
+
display: flex;
|
|
3
|
+
align-items: center;
|
|
4
|
+
font-weight: var(--fw-bold);
|
|
5
|
+
color: var(--brand-secondary-3);
|
|
6
|
+
&:hover,
|
|
7
|
+
&:focus {
|
|
8
|
+
text-decoration-line: underline;
|
|
9
|
+
}
|
|
10
|
+
`;var A=({linkTo:e,name:n,linkText:o=`Edit ${n} Component`})=>(0,t.jsx)("a",{css:Y,title:`Edit ${n} component definition`,rel:"noopener noreferrer",target:"_blank",href:e},o,(0,t.jsx)(J.Icon,{icon:Q.CgChevronRight,iconColorClass:"currentColor",size:"1.5rem"}));var E=I(require("react"));var P=require("react"),k=require("@uniformdev/context/api");function Z(e,n,o){return e.reduce((a,r)=>{let i=n(r);if(typeof i=="undefined"||i===null)throw new Error("Objectify key selector returned undefined or null.");return a[n(r)]=o?o(r):r,a},{})}function w({apiHost:e,apiKey:n,projectId:o}){let[a,r]=(0,P.useState)({loading:!1,notConfigured:!1,error:null,result:null});return(0,P.useEffect)(()=>{if(!o||!n||!e){r({notConfigured:!0,loading:!1,error:null,result:null});return}(async()=>{r({notConfigured:!1,loading:!0,error:null,result:null});try{let s=(await new k.DimensionClient({projectId:o,apiKey:n,apiHost:e}).get()).dimensions.map(u=>({...u,displayName:(0,k.computeDimensionDisplayName)(u)})),f={dimensions:s,dimIndex:Z(s,u=>u.dim,u=>u)};r({notConfigured:!1,loading:!1,error:null,result:f})}catch(c){let s;c instanceof k.ApiClientError?s=c.message:s=c.toString(),r({notConfigured:!1,loading:!1,error:s,result:null});return}})()},[e,n,o]),{result:a.result,error:a.error,loading:a.loading,notConfigured:a.notConfigured}}var O=require("@uniformdev/context"),p=require("@uniformdev/design-system"),T=require("react-icons/cg"),G=I(require("immer")),H=require("@emotion/react"),ve=({value:e,setValue:n,contextConfig:o})=>{let{loading:a,result:r}=w(o),i=(0,E.useMemo)(()=>{if(r)return r.dimensions.filter(m=>m.category==="ENR")},[r]),c=(0,E.useMemo)(()=>{if(!e)return i;if(i)return i.filter(m=>!e.some(D=>(0,O.getEnrichmentVectorKey)(D.cat,D.key)===m.dim))},[i,e]),[s,f]=(0,E.useState)(""),[u,l]=(0,E.useState)(50),d=()=>{let[m,D]=s.split("_");y([...e!=null?e:[],{cat:m,key:D,str:u}]),f(""),l(50)},y=m=>{let D=m.length===0?null:m;n(D)};return a||r===null?(0,t.jsx)(p.LoadingIndicator,null):(0,t.jsx)("fieldset",{css:{marginBlock:"var(--spacing-base)"}},(0,t.jsx)("div",{css:{display:"flex",justifyContent:"space-between",marginBottom:"var(--spacing-base)"}},(0,t.jsx)("legend",{css:{fontSize:"var(--fs-md)",fontWeight:"var(--fw-bold)"}},"Enrichment Tags"),(0,t.jsx)(A,{name:"Enrichments",linkText:"Edit Enrichments",linkTo:`${o.apiHost}/projects/${encodeURIComponent(o.projectId)}/personalization/enrichments`})),i!=null&&i.length?(0,t.jsx)(E.default.Fragment,null,r&&(0,t.jsx)(ye,{list:e!=null?e:[],setList:y,dimIndex:r.dimIndex}),c&&c.length>0?(0,t.jsx)("div",{css:{display:"flex",flexWrap:"wrap",gap:"var(--spacing-lg)",alignItems:"flex-end"}},(0,t.jsx)("div",{css:{flexGrow:1}},(0,t.jsx)(p.InputSelect,{name:"enrichment-type",label:"Add Tag",showLabel:!0,value:s,options:[{label:"Select",value:""},...c.map(m=>({label:m.displayName,value:m.dim}))],onChange:m=>f(m.currentTarget.value)})),(0,t.jsx)(he,{score:u,setValue:l,css:{flexBasis:"9rem"}}),(0,t.jsx)(p.Button,{buttonType:"secondary",size:"xl",css:{marginBottom:"var(--spacing-xs)"},onClick:d,disabled:!s},"Add")):null):(0,t.jsx)(p.Callout,{title:"Enrichments",type:"info",css:{marginBlock:"var(--spacing-base)"}},(0,t.jsx)("p",null,"You do not have any Enrichments configured. Create your first"," ",(0,t.jsx)("a",{href:`${o.apiHost}/projects/${encodeURIComponent(o.projectId)}/personalization/enrichments`,target:"_blank",rel:"noopener noreferrer",css:{":hover":{textDecorationLine:"underline"}}},"Enrichment"))))},j=H.css`
|
|
11
|
+
position: absolute;
|
|
12
|
+
bottom: 0.875rem;
|
|
13
|
+
left: var(--spacing-sm);
|
|
14
|
+
display: flex;
|
|
15
|
+
align-items: center;
|
|
16
|
+
justify-content: center;
|
|
17
|
+
height: 1.75rem;
|
|
18
|
+
width: 1.75rem;
|
|
19
|
+
background-color: var(--gray-100);
|
|
20
|
+
border: 1px solid var(--gray-300);
|
|
21
|
+
border-radius: var(--rounded-full);
|
|
22
|
+
`,be=H.css`
|
|
23
|
+
${j}
|
|
24
|
+
left: auto;
|
|
25
|
+
right: var(--spacing-sm);
|
|
26
|
+
`,he=({score:e,setValue:n,...o})=>{let a=r=>{e!==0&&n(r==="increment"?e+10:e-10)};return(0,t.jsx)("div",{css:{position:"relative"},...o},(0,t.jsx)(p.Input,{label:"Strength",id:"enrichment-score",type:"text",min:0,value:e||"",onChange:r=>n(Number(r.currentTarget.value)),css:{textAlign:"center"}}),(0,t.jsx)("button",{type:"button",title:"Reduce enrichment count",onClick:()=>a("decrement"),disabled:e===0,css:j},(0,t.jsx)(p.Icon,{icon:T.CgMathMinus,iconColorClass:"gray",size:"1.5rem"})),(0,t.jsx)("button",{type:"button",title:"Reduce enrichment count",onClick:()=>a("increment"),css:be},(0,t.jsx)(p.Icon,{icon:T.CgMathPlus,iconColorClass:"gray",size:"1.5rem"})))},ye=({list:e,setList:n,dimIndex:o})=>{let a=i=>{n((0,G.default)(e,c=>{c.splice(i,1)}))},r=(i,c)=>{n((0,G.default)(e,s=>{s[i].str=Number(c)}))};return(0,t.jsx)(E.default.Fragment,null,e.map((i,c)=>{let s=o[(0,O.getEnrichmentVectorKey)(i.cat,i.key)];return(0,t.jsx)("div",{css:{display:"flex",alignItems:"center",gap:"var(--spacing-base)",backgroundColor:"var(--brand-secondary-2)",boxShadow:"var(--shadow-base)",borderRadius:"var(--rounded-base)",paddingInline:"var(--spacing-base)",marginBlock:"var(--spacing-base)"},key:`${i.cat}-${i.key}`},(0,t.jsx)("span",{css:{fontWeight:"var(--fw-bold)",color:s?void 0:"var(--brand-secondary-5)"}},s?s.displayName:`Enrichment '${i.cat}_${i.key}' is unknown`),(0,t.jsx)("div",{css:{marginLeft:"auto",display:"flex",alignItems:"center",border:"0 solid var(--gray-400)",borderLeftWidth:"1px",borderRightWidth:"1px",padding:"var(--spacing-sm) var(--spacing-base)",flexBasis:"7rem"}},(0,t.jsx)(p.Input,{type:"text",min:0,title:"score",value:i.str||50,css:{textAlign:"center"},onChange:f=>r(c,f.currentTarget.value)})),(0,t.jsx)("button",{type:"button",title:"Delete enrichment",onClick:()=>a(c)},(0,t.jsx)(p.Icon,{icon:T.CgCloseO,iconColorClass:"red",size:"1.5rem"})))}))};var ne=require("react");var g=require("@uniformdev/design-system"),q=I(require("immer")),L=require("react-icons/cg");var _=require("@emotion/react"),W="6rem",ee=_.css`
|
|
27
|
+
position: relative;
|
|
28
|
+
padding: var(--spacing-base);
|
|
29
|
+
border: 1px solid var(--gray-300);
|
|
30
|
+
box-shadow: var(--shadow-base);
|
|
31
|
+
background-color: white;
|
|
32
|
+
border-radius: var(--rounded-base);
|
|
33
|
+
margin-top: ${W};
|
|
34
|
+
display: flex;
|
|
35
|
+
&:before {
|
|
36
|
+
content: '';
|
|
37
|
+
display: block;
|
|
38
|
+
width: 1px;
|
|
39
|
+
height: ${W};
|
|
40
|
+
background-color: var(--gray-300);
|
|
41
|
+
position: absolute;
|
|
42
|
+
top: -${W};
|
|
43
|
+
left: var(--spacing-lg);
|
|
44
|
+
}
|
|
45
|
+
&:first-of-type {
|
|
46
|
+
margin-top: var(--spacing-md);
|
|
47
|
+
&:before {
|
|
48
|
+
display: none;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
`,te=_.css`
|
|
52
|
+
display: grid;
|
|
53
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
54
|
+
gap: var(--spacing-base);
|
|
55
|
+
flex-grow: 1;
|
|
56
|
+
margin-right: var(--spacing-base);
|
|
57
|
+
`;var oe=[{name:"=",value:"="},{name:"!=",value:"!="},{name:">",value:">"},{name:">=",value:">="},{name:"<",value:"<"},{name:"<=",value:"<="},{name:"is strongest",value:"+"},{name:"is weakest",value:"-"}],Ee=({value:e,setValue:n,contextConfig:o})=>{let{loading:a,result:r}=w(o),i=e||{crit:[]},c=l=>{n({...i,op:l==="&"?void 0:l})},s=()=>{n({...i,crit:[...i.crit,{l:"",op:">",r:50}]})},f=(l,d)=>{n((0,q.default)(i,y=>{y.crit[d]=l}))},u=l=>{let d=(0,q.default)(i,m=>{m.crit.splice(l,1)}),y=d.crit.length===0?null:d;n(y)};return a||r===null?(0,t.jsx)(g.LoadingIndicator,null):(0,t.jsx)("fieldset",{css:{marginBlock:"var(--spacing-base)"}},(0,t.jsx)("legend",{css:{fontSize:"var(--fs-md)",fontWeight:"var(--fw-bold)"}},"Personalize This"),i.crit.length?i.crit.map((l,d)=>{var m,D;let y=((m=l.l)==null?void 0:m.length)>0;return(0,t.jsx)("div",{css:ee,key:d},(0,t.jsx)("div",{css:te},(0,t.jsx)(g.InputSelect,{name:`lhs-${d}`,label:"Match Dimension",showLabel:!1,value:l.l,options:[{label:"Select",value:""},...r.dimensions.map(v=>({label:v.displayName,value:v.dim}))],onChange:v=>{f({...l,l:v.target.value},d)}}),(0,t.jsx)("div",{css:{display:"flex",alignItems:"center",gap:"var(--spacing-base)"}},(0,t.jsx)("span",{css:{color:"var(--gray-700)"}},"score"),(0,t.jsx)(g.InputSelect,{css:{width:"100%"},name:`op-${d}`,label:"Operator",showLabel:!1,value:l.op,options:oe.map(v=>({label:v.name,value:v.value})),onChange:v=>{let B=v.target.value;f(B==="+"||B==="-"?{...l,op:B,r:void 0,rDim:void 0}:{...l,op:v.target.value},d)},disabled:!y})),l.op==="+"||l.op==="-"?null:(0,t.jsx)(xe,{critHasLhs:y,currentCriteria:l,dimensions:r,update:f,index:d})),(0,t.jsx)("button",{type:"button",onClick:()=>u(d),title:"Delete Personalization"},(0,t.jsx)(g.Icon,{icon:L.CgCloseO,iconColorClass:"red",size:"1.5rem"})),d>0?(0,t.jsx)("div",{css:{position:"absolute",top:"-4rem",transform:"translateX(calc(1.5rem - 50%))"}},(0,t.jsx)(g.InputInlineSelect,{disabled:d>1,value:(D=i.op)!=null?D:"&",options:[{label:"AND",value:"&"},{label:"OR",value:"|"}],onChange:v=>{c(v.value)}})):null)}):(0,t.jsx)(g.Callout,{title:"Default variant",type:"info",css:{marginBlock:"var(--spacing-base)"}},(0,t.jsx)("p",null,'This personalized variant has no match criteria and will be shown to any visitor that does not match any preceding variants. Ensure that default variants come last in the variant list. Personalize this variant by clicking "Add Criteria" to get started.')),(0,t.jsx)("button",{type:"button",onClick:s,css:{color:"var(--brand-secondary-3)",display:"flex",gap:"var(--spacing-sm)",fontWeight:"var(--fw-bold)",marginTop:"var(--spacing-md)",alignItems:"center"}},(0,t.jsx)("div",{css:{color:"white",backgroundColor:"var(--brand-secondary-3)",borderRadius:"var(--rounded-full)",padding:"var(--spacing-xs)"}},(0,t.jsx)(g.Icon,{icon:L.CgMathPlus,iconColorClass:"currentColor",size:"1.25rem"})),"Add Criteria"))};function xe({update:e,currentCriteria:n,index:o,dimensions:a,critHasLhs:r}){var s,f;let[i,c]=(0,ne.useState)(n.r&&V(n.r)!==null?n.r.toString(10):"");return(0,t.jsx)(g.InputComboBox,{name:`rhs-${o}`,inputValue:i,value:{label:n.rDim?(f=(s=a.dimIndex[n.rDim])==null?void 0:s.displayName)!=null?f:n.r:"",value:n.r,isDisabled:!1},styles:{option:(u,{isDisabled:l})=>({...u,background:l?"transparent":void 0,fontSize:l?"0.8rem":void 0})},options:[{label:"Enter numeric score or choose another dimension score",value:"",isDisabled:!0},...a.dimensions.map(u=>({label:u.displayName,value:u.dim,isDisabled:!1}))],onChange:u=>{var l;u&&e({...n,rDim:(l=u.value)==null?void 0:l.toString(),r:void 0},o)},onInputChange:(u,l)=>{let d=V(u);l.action==="input-change"||l.action==="set-value"?(c(u),(d||u==="")&&e({...n,r:u,rDim:void 0},o)):!d&&!V(l.prevInputValue)&&c(u)},isDisabled:!r,noOptionsMessage:({inputValue:u})=>V(u)?`${u}`:"No options match"})}function V(e){return/^\d+$/.test(e.toString(10))}var re=I(require("react"));var $=require("react"),z=require("@uniformdev/context/api");function M({apiHost:e,apiKey:n,projectId:o}){let[a,r]=(0,$.useState)({loading:!1,notConfigured:!1,error:null,result:null});return(0,$.useEffect)(()=>{if(!o||!n||!e){r({notConfigured:!0,loading:!1,error:null,result:null});return}(async()=>{r({notConfigured:!1,loading:!0,error:null,result:null});try{let s=await new z.ManifestClient({projectId:o,apiKey:n,apiHost:e}).get({preview:!0});r({notConfigured:!1,loading:!1,error:null,result:s})}catch(c){let s;c instanceof z.ApiClientError?(c.statusCode===403&&(s=`The API key ${n} did not have permissions to fetch the manifest. Ensure Context > Read Drafts permissions are granted.`),s=c.message):s=c.toString(),r({notConfigured:!1,loading:!1,error:s,result:null});return}})()},[e,n,o]),{result:a.result,error:a.error,loading:a.loading,notConfigured:a.notConfigured}}var ie=require("@uniformdev/design-system");function Re({children:e,versionMap:n,contextConfig:o}){let{loading:a,result:r}=M(o);if(a)return(0,t.jsx)(ie.LoadingIndicator,null);if(r){let i=n[r.project.ui_version];if(i)return(0,t.jsx)(i,null)}return(0,t.jsx)(re.default.Fragment,null,e)}var x=I(require("react"));var ae=require("@uniformdev/design-system"),U=(0,x.createContext)(null),we=({loadingComponent:e,errorComponent:n,contextConfig:o,children:a})=>{let r=M(o),i=w(o);return r.error||r.notConfigured?n?(0,t.jsx)(n,{contextConfig:o,result:r}):(0,t.jsx)(x.default.Fragment,null,"ErrorComponent is not configured"):i.error||i.notConfigured?n?(0,t.jsx)(n,{contextConfig:o,result:i}):(0,t.jsx)(x.default.Fragment,null,"ErrorComponent is not configured"):r.loading||i.loading?e?(0,t.jsx)(e,null):(0,t.jsx)(ae.LoadingIndicator,null):(0,t.jsx)(U.Provider,{value:{manifest:r.result,dimensions:i.result,contextConfig:o}},a)};function Ie(){let e=(0,x.useContext)(U);if(!(e!=null&&e.contextConfig))throw new Error("Not within DataContext! Configuration data is not exist.");return e.contextConfig}function ke(){let e=(0,x.useContext)(U);if(!(e!=null&&e.manifest))throw new Error("Not within DataContext! Manifest data is not exist.");return e.manifest}function Te(){let e=(0,x.useContext)(U);if(!(e!=null&&e.dimensions))throw new Error("Not within DataContext! Dimensions data is not exist.");return e.dimensions}var se=require("uuid"),le=require("@uniformdev/context/api"),F=async e=>{if(!e)return{valid:!1,error:new Error("contextConfig was not defined.")};if(!e.apiHost)return{valid:!1,error:new Error("apiHost was not defined.")};if(!e.apiKey)return{valid:!1,error:new Error("apiKey was not defined.")};if(!(0,se.validate)(e.apiKey)&&!e.projectId)return{valid:!1,error:new Error("projectId is required when using a modern API key.")};let n=new le.ManifestClient({projectId:e.projectId,apiKey:e.apiKey,apiHost:e.apiHost});try{let o=await n.get({preview:!0});return{valid:!0,result:o}}catch(o){return{valid:!1,error:o}}};var N=require("react");var Me=e=>{let[n,o]=(0,N.useState)({validating:!1,error:void 0}),{apiKey:a,apiHost:r,projectId:i}=e||{};return(0,N.useEffect)(()=>{if(!a||!r)return;(async()=>{o({validating:!0,error:void 0});let{error:s,result:f}=await F({apiHost:r,apiKey:a,projectId:i});o(s?{error:s,validating:!1}:{error:void 0,validating:!1,result:f})})()},[r,a,i]),{validating:n.validating,error:n.error,result:n.result}};C(h,require("@uniformdev/design-system"));module.exports=ge(h);0&&(module.exports={ContextData,EditLink,EnrichmentTag,PersonalizationCriteria,ProjectUIVersion,equality,useContextConfig,useContextData,useDimensions,useDimensionsDataContext,useManifest,useValidateContextConfig,validateContextConfig});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import{jsx as e}from"@emotion/react";import*as h from"react";import{Icon as Y}from"@uniformdev/design-system";import{CgChevronRight as J}from"react-icons/cg";import{css as K}from"@emotion/react";var T=K`
|
|
2
|
+
display: flex;
|
|
3
|
+
align-items: center;
|
|
4
|
+
font-weight: var(--fw-bold);
|
|
5
|
+
color: var(--brand-secondary-3);
|
|
6
|
+
&:hover,
|
|
7
|
+
&:focus {
|
|
8
|
+
text-decoration-line: underline;
|
|
9
|
+
}
|
|
10
|
+
`;var M=({linkTo:t,name:n,linkText:i=`Edit ${n} Component`})=>e("a",{css:T,title:`Edit ${n} component definition`,rel:"noopener noreferrer",target:"_blank",href:t},i,e(Y,{icon:J,iconColorClass:"currentColor",size:"1.5rem"}));import $,{useMemo as P,useState as V}from"react";import{useEffect as Q,useState as Z}from"react";import{DimensionClient as j,computeDimensionDisplayName as ee,ApiClientError as te}from"@uniformdev/context/api";function S(t,n,i){return t.reduce((a,r)=>{let o=n(r);if(typeof o=="undefined"||o===null)throw new Error("Objectify key selector returned undefined or null.");return a[n(r)]=i?i(r):r,a},{})}function y({apiHost:t,apiKey:n,projectId:i}){let[a,r]=Z({loading:!1,notConfigured:!1,error:null,result:null});return Q(()=>{if(!i||!n||!t){r({notConfigured:!0,loading:!1,error:null,result:null});return}(async()=>{r({notConfigured:!1,loading:!0,error:null,result:null});try{let s=(await new j({projectId:i,apiKey:n,apiHost:t}).get()).dimensions.map(u=>({...u,displayName:ee(u)})),f={dimensions:s,dimIndex:S(s,u=>u.dim,u=>u)};r({notConfigured:!1,loading:!1,error:null,result:f})}catch(c){let s;c instanceof te?s=c.message:s=c.toString(),r({notConfigured:!1,loading:!1,error:s,result:null});return}})()},[t,n,i]),{result:a.result,error:a.error,loading:a.loading,notConfigured:a.notConfigured}}import{getEnrichmentVectorKey as z}from"@uniformdev/context";import{Input as U,Button as ne,InputSelect as oe,Callout as re,Icon as w,LoadingIndicator as ie}from"@uniformdev/design-system";import{CgMathPlus as ae,CgMathMinus as se,CgCloseO as le}from"react-icons/cg";import L from"immer";import{css as N}from"@emotion/react";var et=({value:t,setValue:n,contextConfig:i})=>{let{loading:a,result:r}=y(i),o=P(()=>{if(r)return r.dimensions.filter(m=>m.category==="ENR")},[r]),c=P(()=>{if(!t)return o;if(o)return o.filter(m=>!t.some(b=>z(b.cat,b.key)===m.dim))},[o,t]),[s,f]=V(""),[u,l]=V(50),d=()=>{let[m,b]=s.split("_");v([...t!=null?t:[],{cat:m,key:b,str:u}]),f(""),l(50)},v=m=>{let b=m.length===0?null:m;n(b)};return a||r===null?e(ie,null):e("fieldset",{css:{marginBlock:"var(--spacing-base)"}},e("div",{css:{display:"flex",justifyContent:"space-between",marginBottom:"var(--spacing-base)"}},e("legend",{css:{fontSize:"var(--fs-md)",fontWeight:"var(--fw-bold)"}},"Enrichment Tags"),e(M,{name:"Enrichments",linkText:"Edit Enrichments",linkTo:`${i.apiHost}/projects/${encodeURIComponent(i.projectId)}/personalization/enrichments`})),o!=null&&o.length?e($.Fragment,null,r&&e(de,{list:t!=null?t:[],setList:v,dimIndex:r.dimIndex}),c&&c.length>0?e("div",{css:{display:"flex",flexWrap:"wrap",gap:"var(--spacing-lg)",alignItems:"flex-end"}},e("div",{css:{flexGrow:1}},e(oe,{name:"enrichment-type",label:"Add Tag",showLabel:!0,value:s,options:[{label:"Select",value:""},...c.map(m=>({label:m.displayName,value:m.dim}))],onChange:m=>f(m.currentTarget.value)})),e(ce,{score:u,setValue:l,css:{flexBasis:"9rem"}}),e(ne,{buttonType:"secondary",size:"xl",css:{marginBottom:"var(--spacing-xs)"},onClick:d,disabled:!s},"Add")):null):e(re,{title:"Enrichments",type:"info",css:{marginBlock:"var(--spacing-base)"}},e("p",null,"You do not have any Enrichments configured. Create your first"," ",e("a",{href:`${i.apiHost}/projects/${encodeURIComponent(i.projectId)}/personalization/enrichments`,target:"_blank",rel:"noopener noreferrer",css:{":hover":{textDecorationLine:"underline"}}},"Enrichment"))))},B=N`
|
|
11
|
+
position: absolute;
|
|
12
|
+
bottom: 0.875rem;
|
|
13
|
+
left: var(--spacing-sm);
|
|
14
|
+
display: flex;
|
|
15
|
+
align-items: center;
|
|
16
|
+
justify-content: center;
|
|
17
|
+
height: 1.75rem;
|
|
18
|
+
width: 1.75rem;
|
|
19
|
+
background-color: var(--gray-100);
|
|
20
|
+
border: 1px solid var(--gray-300);
|
|
21
|
+
border-radius: var(--rounded-full);
|
|
22
|
+
`,ue=N`
|
|
23
|
+
${B}
|
|
24
|
+
left: auto;
|
|
25
|
+
right: var(--spacing-sm);
|
|
26
|
+
`,ce=({score:t,setValue:n,...i})=>{let a=r=>{t!==0&&n(r==="increment"?t+10:t-10)};return e("div",{css:{position:"relative"},...i},e(U,{label:"Strength",id:"enrichment-score",type:"text",min:0,value:t||"",onChange:r=>n(Number(r.currentTarget.value)),css:{textAlign:"center"}}),e("button",{type:"button",title:"Reduce enrichment count",onClick:()=>a("decrement"),disabled:t===0,css:B},e(w,{icon:se,iconColorClass:"gray",size:"1.5rem"})),e("button",{type:"button",title:"Reduce enrichment count",onClick:()=>a("increment"),css:ue},e(w,{icon:ae,iconColorClass:"gray",size:"1.5rem"})))},de=({list:t,setList:n,dimIndex:i})=>{let a=o=>{n(L(t,c=>{c.splice(o,1)}))},r=(o,c)=>{n(L(t,s=>{s[o].str=Number(c)}))};return e($.Fragment,null,t.map((o,c)=>{let s=i[z(o.cat,o.key)];return e("div",{css:{display:"flex",alignItems:"center",gap:"var(--spacing-base)",backgroundColor:"var(--brand-secondary-2)",boxShadow:"var(--shadow-base)",borderRadius:"var(--rounded-base)",paddingInline:"var(--spacing-base)",marginBlock:"var(--spacing-base)"},key:`${o.cat}-${o.key}`},e("span",{css:{fontWeight:"var(--fw-bold)",color:s?void 0:"var(--brand-secondary-5)"}},s?s.displayName:`Enrichment '${o.cat}_${o.key}' is unknown`),e("div",{css:{marginLeft:"auto",display:"flex",alignItems:"center",border:"0 solid var(--gray-400)",borderLeftWidth:"1px",borderRightWidth:"1px",padding:"var(--spacing-sm) var(--spacing-base)",flexBasis:"7rem"}},e(U,{type:"text",min:0,title:"score",value:o.str||50,css:{textAlign:"center"},onChange:f=>r(c,f.currentTarget.value)})),e("button",{type:"button",title:"Delete enrichment",onClick:()=>a(c)},e(w,{icon:le,iconColorClass:"red",size:"1.5rem"})))}))};import{useState as fe}from"react";import{LoadingIndicator as pe,Callout as ge,InputSelect as H,InputComboBox as Ce,InputInlineSelect as ve,Icon as W}from"@uniformdev/design-system";import _ from"immer";import{CgCloseO as be,CgMathPlus as he}from"react-icons/cg";import{css as A}from"@emotion/react";var I="6rem",G=A`
|
|
27
|
+
position: relative;
|
|
28
|
+
padding: var(--spacing-base);
|
|
29
|
+
border: 1px solid var(--gray-300);
|
|
30
|
+
box-shadow: var(--shadow-base);
|
|
31
|
+
background-color: white;
|
|
32
|
+
border-radius: var(--rounded-base);
|
|
33
|
+
margin-top: ${I};
|
|
34
|
+
display: flex;
|
|
35
|
+
&:before {
|
|
36
|
+
content: '';
|
|
37
|
+
display: block;
|
|
38
|
+
width: 1px;
|
|
39
|
+
height: ${I};
|
|
40
|
+
background-color: var(--gray-300);
|
|
41
|
+
position: absolute;
|
|
42
|
+
top: -${I};
|
|
43
|
+
left: var(--spacing-lg);
|
|
44
|
+
}
|
|
45
|
+
&:first-of-type {
|
|
46
|
+
margin-top: var(--spacing-md);
|
|
47
|
+
&:before {
|
|
48
|
+
display: none;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
`,O=A`
|
|
52
|
+
display: grid;
|
|
53
|
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
|
54
|
+
gap: var(--spacing-base);
|
|
55
|
+
flex-grow: 1;
|
|
56
|
+
margin-right: var(--spacing-base);
|
|
57
|
+
`;var ye=[{name:"=",value:"="},{name:"!=",value:"!="},{name:">",value:">"},{name:">=",value:">="},{name:"<",value:"<"},{name:"<=",value:"<="},{name:"is strongest",value:"+"},{name:"is weakest",value:"-"}],ut=({value:t,setValue:n,contextConfig:i})=>{let{loading:a,result:r}=y(i),o=t||{crit:[]},c=l=>{n({...o,op:l==="&"?void 0:l})},s=()=>{n({...o,crit:[...o.crit,{l:"",op:">",r:50}]})},f=(l,d)=>{n(_(o,v=>{v.crit[d]=l}))},u=l=>{let d=_(o,m=>{m.crit.splice(l,1)}),v=d.crit.length===0?null:d;n(v)};return a||r===null?e(pe,null):e("fieldset",{css:{marginBlock:"var(--spacing-base)"}},e("legend",{css:{fontSize:"var(--fs-md)",fontWeight:"var(--fw-bold)"}},"Personalize This"),o.crit.length?o.crit.map((l,d)=>{var m,b;let v=((m=l.l)==null?void 0:m.length)>0;return e("div",{css:G,key:d},e("div",{css:O},e(H,{name:`lhs-${d}`,label:"Match Dimension",showLabel:!1,value:l.l,options:[{label:"Select",value:""},...r.dimensions.map(p=>({label:p.displayName,value:p.dim}))],onChange:p=>{f({...l,l:p.target.value},d)}}),e("div",{css:{display:"flex",alignItems:"center",gap:"var(--spacing-base)"}},e("span",{css:{color:"var(--gray-700)"}},"score"),e(H,{css:{width:"100%"},name:`op-${d}`,label:"Operator",showLabel:!1,value:l.op,options:ye.map(p=>({label:p.name,value:p.value})),onChange:p=>{let R=p.target.value;f(R==="+"||R==="-"?{...l,op:R,r:void 0,rDim:void 0}:{...l,op:p.target.value},d)},disabled:!v})),l.op==="+"||l.op==="-"?null:e(De,{critHasLhs:v,currentCriteria:l,dimensions:r,update:f,index:d})),e("button",{type:"button",onClick:()=>u(d),title:"Delete Personalization"},e(W,{icon:be,iconColorClass:"red",size:"1.5rem"})),d>0?e("div",{css:{position:"absolute",top:"-4rem",transform:"translateX(calc(1.5rem - 50%))"}},e(ve,{disabled:d>1,value:(b=o.op)!=null?b:"&",options:[{label:"AND",value:"&"},{label:"OR",value:"|"}],onChange:p=>{c(p.value)}})):null)}):e(ge,{title:"Default variant",type:"info",css:{marginBlock:"var(--spacing-base)"}},e("p",null,'This personalized variant has no match criteria and will be shown to any visitor that does not match any preceding variants. Ensure that default variants come last in the variant list. Personalize this variant by clicking "Add Criteria" to get started.')),e("button",{type:"button",onClick:s,css:{color:"var(--brand-secondary-3)",display:"flex",gap:"var(--spacing-sm)",fontWeight:"var(--fw-bold)",marginTop:"var(--spacing-md)",alignItems:"center"}},e("div",{css:{color:"white",backgroundColor:"var(--brand-secondary-3)",borderRadius:"var(--rounded-full)",padding:"var(--spacing-xs)"}},e(W,{icon:he,iconColorClass:"currentColor",size:"1.25rem"})),"Add Criteria"))};function De({update:t,currentCriteria:n,index:i,dimensions:a,critHasLhs:r}){var s,f;let[o,c]=fe(n.r&&D(n.r)!==null?n.r.toString(10):"");return e(Ce,{name:`rhs-${i}`,inputValue:o,value:{label:n.rDim?(f=(s=a.dimIndex[n.rDim])==null?void 0:s.displayName)!=null?f:n.r:"",value:n.r,isDisabled:!1},styles:{option:(u,{isDisabled:l})=>({...u,background:l?"transparent":void 0,fontSize:l?"0.8rem":void 0})},options:[{label:"Enter numeric score or choose another dimension score",value:"",isDisabled:!0},...a.dimensions.map(u=>({label:u.displayName,value:u.dim,isDisabled:!1}))],onChange:u=>{var l;u&&t({...n,rDim:(l=u.value)==null?void 0:l.toString(),r:void 0},i)},onInputChange:(u,l)=>{let d=D(u);l.action==="input-change"||l.action==="set-value"?(c(u),(d||u==="")&&t({...n,r:u,rDim:void 0},i)):!d&&!D(l.prevInputValue)&&c(u)},isDisabled:!r,noOptionsMessage:({inputValue:u})=>D(u)?`${u}`:"No options match"})}function D(t){return/^\d+$/.test(t.toString(10))}import Ie from"react";import{useEffect as Ee,useState as xe}from"react";import{ManifestClient as Re,ApiClientError as we}from"@uniformdev/context/api";function E({apiHost:t,apiKey:n,projectId:i}){let[a,r]=xe({loading:!1,notConfigured:!1,error:null,result:null});return Ee(()=>{if(!i||!n||!t){r({notConfigured:!0,loading:!1,error:null,result:null});return}(async()=>{r({notConfigured:!1,loading:!0,error:null,result:null});try{let s=await new Re({projectId:i,apiKey:n,apiHost:t}).get({preview:!0});r({notConfigured:!1,loading:!1,error:null,result:s})}catch(c){let s;c instanceof we?(c.statusCode===403&&(s=`The API key ${n} did not have permissions to fetch the manifest. Ensure Context > Read Drafts permissions are granted.`),s=c.message):s=c.toString(),r({notConfigured:!1,loading:!1,error:s,result:null});return}})()},[t,n,i]),{result:a.result,error:a.error,loading:a.loading,notConfigured:a.notConfigured}}import{LoadingIndicator as ke}from"@uniformdev/design-system";function ht({children:t,versionMap:n,contextConfig:i}){let{loading:a,result:r}=E(i);if(a)return e(ke,null);if(r){let o=n[r.project.ui_version];if(o)return e(o,null)}return e(Ie.Fragment,null,t)}import q,{createContext as Te,useContext as k}from"react";import{LoadingIndicator as Me}from"@uniformdev/design-system";var x=Te(null),Tt=({loadingComponent:t,errorComponent:n,contextConfig:i,children:a})=>{let r=E(i),o=y(i);return r.error||r.notConfigured?n?e(n,{contextConfig:i,result:r}):e(q.Fragment,null,"ErrorComponent is not configured"):o.error||o.notConfigured?n?e(n,{contextConfig:i,result:o}):e(q.Fragment,null,"ErrorComponent is not configured"):r.loading||o.loading?t?e(t,null):e(Me,null):e(x.Provider,{value:{manifest:r.result,dimensions:o.result,contextConfig:i}},a)};function Mt(){let t=k(x);if(!(t!=null&&t.contextConfig))throw new Error("Not within DataContext! Configuration data is not exist.");return t.contextConfig}function St(){let t=k(x);if(!(t!=null&&t.manifest))throw new Error("Not within DataContext! Manifest data is not exist.");return t.manifest}function Pt(){let t=k(x);if(!(t!=null&&t.dimensions))throw new Error("Not within DataContext! Dimensions data is not exist.");return t.dimensions}import{validate as Se}from"uuid";import{ManifestClient as Pe}from"@uniformdev/context/api";var F=async t=>{if(!t)return{valid:!1,error:new Error("contextConfig was not defined.")};if(!t.apiHost)return{valid:!1,error:new Error("apiHost was not defined.")};if(!t.apiKey)return{valid:!1,error:new Error("apiKey was not defined.")};if(!Se(t.apiKey)&&!t.projectId)return{valid:!1,error:new Error("projectId is required when using a modern API key.")};let n=new Pe({projectId:t.projectId,apiKey:t.apiKey,apiHost:t.apiHost});try{let i=await n.get({preview:!0});return{valid:!0,result:i}}catch(i){return{valid:!1,error:i}}};import{useEffect as Ve,useState as Le}from"react";var At=t=>{let[n,i]=Le({validating:!1,error:void 0}),{apiKey:a,apiHost:r,projectId:o}=t||{};return Ve(()=>{if(!a||!r)return;(async()=>{i({validating:!0,error:void 0});let{error:s,result:f}=await F({apiHost:r,apiKey:a,projectId:o});i(s?{error:s,validating:!1}:{error:void 0,validating:!1,result:f})})()},[r,a,o]),{validating:n.validating,error:n.error,result:n.result}};export*from"@uniformdev/design-system";export{Tt as ContextData,M as EditLink,et as EnrichmentTag,ut as PersonalizationCriteria,ht as ProjectUIVersion,ye as equality,Mt as useContextConfig,E as useContextData,y as useDimensions,Pt as useDimensionsDataContext,St as useManifest,At as useValidateContextConfig,F as validateContextConfig};
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@uniformdev/context-ui",
|
|
3
|
+
"version": "13.0.1-alpha.132+8cada5baa",
|
|
4
|
+
"description": "React-based functionality and components for Uniform Context",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.esm.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"node": "./dist/index.mjs",
|
|
12
|
+
"default": "./dist/index.esm.js"
|
|
13
|
+
},
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"typesVersions": {
|
|
18
|
+
"*": {
|
|
19
|
+
"*": [
|
|
20
|
+
"./dist/index.d.ts"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "pnpm build:esm",
|
|
27
|
+
"build:esm": "tsup --minify",
|
|
28
|
+
"dev": "tsup --watch",
|
|
29
|
+
"clean": "rimraf dist",
|
|
30
|
+
"test": "jest --maxWorkers=1 --passWithNoTests",
|
|
31
|
+
"lint": "eslint \"src/**/*.{js,ts,tsx}\"",
|
|
32
|
+
"format": "prettier --write \"src/**/*.{js,ts,tsx}\"",
|
|
33
|
+
"storybook": "pnpm build && start-storybook -p 9011 -c ./.storybook",
|
|
34
|
+
"storybook:publish": "pnpm build && pnpm build-storybook -o ./out"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@emotion/react": "^11.9.0",
|
|
38
|
+
"@uniformdev/context": "^13.0.1-alpha.132+8cada5baa",
|
|
39
|
+
"@uniformdev/design-system": "^13.0.1-alpha.132+8cada5baa",
|
|
40
|
+
"immer": "9.0.12",
|
|
41
|
+
"react-beautiful-dnd": "13.1.0",
|
|
42
|
+
"react-icons": "^4.3.1",
|
|
43
|
+
"react-select": "5.2.2",
|
|
44
|
+
"react-use": "17.3.2",
|
|
45
|
+
"reakit": "1.3.11",
|
|
46
|
+
"timeago.js": "4.0.2",
|
|
47
|
+
"uuid": "8.3.2"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"react": ">=16.8"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@babel/core": "7.17.8",
|
|
54
|
+
"@babel/preset-env": "7.16.11",
|
|
55
|
+
"@babel/preset-react": "7.16.7",
|
|
56
|
+
"@emotion/babel-preset-css-prop": "11.2.0",
|
|
57
|
+
"@storybook/addon-controls": "6.4.19",
|
|
58
|
+
"@storybook/addon-essentials": "6.4.19",
|
|
59
|
+
"@storybook/addon-postcss": "2.0.0",
|
|
60
|
+
"@storybook/builder-webpack5": "6.5.0-alpha.50",
|
|
61
|
+
"@storybook/manager-webpack5": "6.5.0-alpha.50",
|
|
62
|
+
"@storybook/react": "6.4.19",
|
|
63
|
+
"@types/react": "17.0.44",
|
|
64
|
+
"@types/react-beautiful-dnd": "13.1.2",
|
|
65
|
+
"@types/uuid": "8.3.4",
|
|
66
|
+
"autoprefixer": "10.4.4",
|
|
67
|
+
"postcss": "8.4.8",
|
|
68
|
+
"postcss-import": "14.0.2",
|
|
69
|
+
"react": "17.0.2",
|
|
70
|
+
"react-dom": "17.0.2",
|
|
71
|
+
"webpack": "5.70.0"
|
|
72
|
+
},
|
|
73
|
+
"files": [
|
|
74
|
+
"/dist"
|
|
75
|
+
],
|
|
76
|
+
"publishConfig": {
|
|
77
|
+
"access": "public"
|
|
78
|
+
},
|
|
79
|
+
"gitHead": "8cada5baa3a3cb2a601eeca9c7587ac358d476b3"
|
|
80
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Development notes
|
|
2
|
+
|
|
3
|
+
## Developing
|
|
4
|
+
* Copy/past `.env.example` and rename to `.env`, set up your project id, host, and API key.
|
|
5
|
+
* `pnpm storybook` - run storybook app on http://localhost:9011/, is used for creating and editing components.
|
|
6
|
+
|
|
7
|
+
## Icon component
|
|
8
|
+
Icon component can be is used only as a child of IconsProvider component.
|
|
9
|
+
|
|
10
|
+
## Build
|
|
11
|
+
* `pnpm build` - builds icons, JS modules, and CSS.
|
|
12
|
+
|
|
13
|
+
## CSS
|
|
14
|
+
This package uses Tailwind CSS. Run `pnpm build:css` to compile the CSS file, which will run PostCSS with the Tailwind plugin against all `.css` files in the `assets` folder. The compiled CSS will be output to `dist/assets/[filename].css`.
|
|
15
|
+
|
|
16
|
+
When Sanity Studio imports the JS modules exported by our plugin, the Sanity Studio bundler will also automatically import any CSS files that our code imports, e.g. `import '../dist/assets/optimize.css';`
|
|
17
|
+
|
|
18
|
+
> NOTE: be sure to set NODE_ENV=production if you want PostCSS to purge unused CSS.
|
|
19
|
+
|
|
20
|
+
## Icons
|
|
21
|
+
If you want to import SVG icons as components in your code, do the following:
|
|
22
|
+
|
|
23
|
+
* Add the the SVG icon to the `src/assets` folder
|
|
24
|
+
* Run `pnpm build:icons`
|
|
25
|
+
|
|
26
|
+
This will generate React components for every SVG icon in the `src/assets` folder. You can then import the icons in other React components, e.g. `import MyIcon from '../assets/MyIcon';`.
|
|
27
|
+
|
|
28
|
+
Ideally, we could have `import MyIcon from '../assets/my-icon.svg` in our code and the SVG icon would automatically be converted to a React component at build time and our source code would be transformed to import the component. However, we're not using a bundler so it becomes challenging because we can't "hook" into the TypeScript build/transpile process. We could introduce a build tool like `ESBuild` or `@babel/typescript` and stop using `tsc`, but those options more tooling/overhead. We could also potentially use something like `ttypescript` (https://github.com/cevek/ttypescript) to create babel-esque plugins for TypeScript, but who knows how reliable that is and if we'd run into any issues in CI/testing.
|
|
29
|
+
|
|
30
|
+
So, for now, icons are somewhat of a manual process.
|
|
31
|
+
|
|
32
|
+
## CommonJS
|
|
33
|
+
|
|
34
|
+
No CommonJS modules are exported from this package as the package is intended to be consumed by Sanity Studio and not used during SSR.
|