openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 2.56 kB
JavaScript
import{n as e}from"./rolldown-runtime-8BhlS34s.js";import{C as t,D as n,E as r,P as i}from"./ui-DuRgK6kL.js";import{n as a,t as o}from"./theme-palette-BXs1N4qF.js";function s(e,t){let o={themes:[],error:null},s=e.connection.gatewayUrl,c=e.snapshot.client,l=e.snapshot.selfUser?.id,u=e.snapshot.phase===`connected`,d=0,f=!1,p=new Map,m=new Map,h=new Set,g=t=>!f&&t===d&&e.snapshot.phase===`connected`&&e.snapshot.client===c&&e.snapshot.selfUser?.id===l,_=e=>{let t,i=e.theme.source===`plugin`?e.theme.artwork:void 0;try{t=r(e.definition,{hatIds:Object.keys(i?.hats??{}),critterIds:Object.keys(i?.critters??{})})}catch{return}let o=t?.light??t?.dark,s=t?.dark??t?.light;t&&o&&s&&(m.delete(e.theme.id),p.set(e.theme.id,{generation:d,theme:{branding:{...n(t),...i?{artwork:i}:{}},mode:t.light?t.dark?void 0:`light`:`dark`,palette:{light:a(`light`,o,void 0),dark:a(`dark`,s,void 0)}}}))},v=async()=>{let n=e.snapshot.client;if(f||e.snapshot.phase!==`connected`||!n)return;c=n,l=e.snapshot.selfUser?.id;let r=++d;h.clear(),m.clear();try{let e=await n.request(`themes.list`,{});if(!g(r))return;let i=new Set(e.themes.map(e=>e.id));for(let e of p.keys())i.has(e)||p.delete(e);for(let e of m.keys())i.has(e)||m.delete(e);_(e),o={themes:e.themes,error:null,unavailableId:e.current.requestedId},t()}catch(e){g(r)&&(o={...o,error:e instanceof Error?e.message:String(e)},t())}},y=n=>{let r=e.snapshot.client;if(n===`custom`||i(n)||p.get(n)?.generation===d||h.has(n)||!o.themes.some(e=>e.id===n)||e.snapshot.phase!==`connected`||!r)return;h.add(n);let a=d,s=()=>g(a)&&o.themes.some(e=>e.id===n);r.request(`themes.get`,{id:n}).then(e=>{s()&&(_(e),t())},e=>{s()&&(m.set(n,e instanceof Error?e.message:String(e)),t())})},b=()=>{p.clear(),m.clear(),h.clear(),o={themes:[],error:null},t()},x=e.subscribe(()=>{let t=e.snapshot.phase===`connected`,n=e.connection.gatewayUrl!==s;if(!t){(u||n)&&(d+=1),u=!1,n&&(s=e.connection.gatewayUrl,l=void 0,c=null,b());return}if(!u||n||e.snapshot.client!==c||e.snapshot.selfUser?.id!==l){d+=1;let t=e.snapshot.selfUser?.id!==l;u=!0,s=e.connection.gatewayUrl,c=e.snapshot.client,l=e.snapshot.selfUser?.id,(n||t)&&b(),v()}}),S=e.subscribeEvents(t=>{(t.event===`plugins.changed`||t.event===`users.prefs.changed`&&e.snapshot.selfUser?.id)&&v()});return u&&queueMicrotask(()=>{f||v()}),{snapshot(e){let t=o.error??m.get(e)??null;return t===o.error?o:{...o,error:t}},theme(e){return y(e),p.get(e)?.theme},refresh:v,dispose(){f=!0,d+=1,x(),S(),p.clear(),m.clear()}}}function c(){return(c=e((()=>{t(),o()})))()}c();export{s as createThemeCatalog};