@yomo/react-cursor-chat
Advanced tools
Comparing version 0.0.12 to 0.0.13
import React from 'react'; | ||
import './styles/cursor-chat.less'; | ||
declare const _default: React.MemoExoticComponent<({ presenceURL, presenceAuth, room, name, avatar, theme, }: { | ||
declare const _default: React.MemoExoticComponent<({ presenceURL, presenceAuth, room, showLatency, name, avatar, theme, }: { | ||
presenceURL: string; | ||
@@ -11,2 +11,3 @@ presenceAuth: { | ||
room?: string | undefined; | ||
showLatency?: boolean | undefined; | ||
name?: string | undefined; | ||
@@ -13,0 +14,0 @@ avatar?: string | undefined; |
import Others from '../cursor/others'; | ||
import Me from '../cursor/me'; | ||
declare const useLatency: (cursor: Me | Others) => { | ||
declare const useLatency: (cursor: Me | Others, showLatency: boolean) => { | ||
meshId: string; | ||
@@ -5,0 +5,0 @@ latency: number; |
@@ -1298,3 +1298,3 @@ 'use strict'; | ||
var useLatency = function useLatency(cursor) { | ||
var useLatency = function useLatency(cursor, showLatency) { | ||
var _useState = React.useState({ | ||
@@ -1309,22 +1309,32 @@ meshId: '', | ||
React.useEffect(function () { | ||
cursor.onGetLatency = function (data) { | ||
if (data.latency) { | ||
var backgroundColor = 'green'; | ||
if (showLatency) { | ||
cursor.onGetLatency = function (data) { | ||
if (data.latency) { | ||
var backgroundColor = 'green'; | ||
if (data.latency >= 200 && data.latency < 300) { | ||
backgroundColor = '#FFB02A'; | ||
} | ||
if (data.latency >= 200 && data.latency < 300) { | ||
backgroundColor = '#FFB02A'; | ||
} | ||
if (data.latency >= 300) { | ||
backgroundColor = 'red'; | ||
if (data.latency >= 300) { | ||
backgroundColor = 'red'; | ||
} | ||
setLatencyData({ | ||
backgroundColor: backgroundColor, | ||
meshId: data.meshId, | ||
latency: data.latency | ||
}); | ||
} | ||
}; | ||
} else { | ||
cursor.onGetLatency = function (_) {}; | ||
setLatencyData({ | ||
backgroundColor: backgroundColor, | ||
meshId: data.meshId, | ||
latency: data.latency | ||
}); | ||
} | ||
}; | ||
}, []); | ||
setLatencyData({ | ||
meshId: '', | ||
latency: 0, | ||
backgroundColor: 'green' | ||
}); | ||
} | ||
}, [showLatency]); | ||
return latencyData; | ||
@@ -1335,2 +1345,3 @@ }; | ||
var cursor = _ref.cursor, | ||
showLatency = _ref.showLatency, | ||
theme = _ref.theme; | ||
@@ -1347,3 +1358,3 @@ var refContainer = useRenderPosition(cursor); | ||
var latencyData = useLatency(cursor); | ||
var latencyData = useLatency(cursor, showLatency); | ||
var onKeydown = React.useCallback(function (e) { | ||
@@ -1405,2 +1416,3 @@ if (e.code === 'Slash') { | ||
var cursor = _ref2.cursor, | ||
showLatency = _ref2.showLatency, | ||
theme = _ref2.theme; | ||
@@ -1413,3 +1425,3 @@ var refContainer = useRenderPosition(cursor); | ||
var latencyData = useLatency(cursor); | ||
var latencyData = useLatency(cursor, showLatency); | ||
React.useEffect(function () { | ||
@@ -1446,2 +1458,4 @@ cursor.onTextMessage = function (msg) { | ||
room = _ref3.room, | ||
_ref3$showLatency = _ref3.showLatency, | ||
showLatency = _ref3$showLatency === void 0 ? false : _ref3$showLatency, | ||
name = _ref3.name, | ||
@@ -1472,2 +1486,3 @@ avatar = _ref3.avatar, | ||
cursor: item, | ||
showLatency: showLatency, | ||
theme: theme | ||
@@ -1477,2 +1492,3 @@ }); | ||
cursor: me, | ||
showLatency: showLatency, | ||
theme: theme | ||
@@ -1479,0 +1495,0 @@ })); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,r=require("rxjs"),o=require("rxjs/operators"),i=require("@yomo/presencejs");function a(e,t,n,r,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,o)}function s(e){return function(){var t=this,n=arguments;return new Promise((function(r,o){var i=e.apply(t,n);function s(e){a(i,r,o,s,c,"next",e)}function c(e){a(i,r,o,s,c,"throw",e)}s(void 0)}))}}function c(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var u,l=(function(e){var t=function(e){var t=Object.prototype,n=t.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},o=r.iterator||"@@iterator",i=r.asyncIterator||"@@asyncIterator",a=r.toStringTag||"@@toStringTag";function s(e,t,n){return Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{s({},"")}catch(e){s=function(e,t,n){return e[t]=n}}function c(e,t,n,r){var o=Object.create((t&&t.prototype instanceof f?t:f).prototype),i=new S(r||[]);return o._invoke=function(e,t,n){var r="suspendedStart";return function(o,i){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===o)throw i;return{value:void 0,done:!0}}for(n.method=o,n.arg=i;;){var a=n.delegate;if(a){var s=x(a,n);if(s){if(s===l)continue;return s}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var c=u(e,t,n);if("normal"===c.type){if(r=n.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(r="completed",n.method="throw",n.arg=c.arg)}}}(e,n,i),o}function u(e,t,n){try{return{type:"normal",arg:e.call(t,n)}}catch(e){return{type:"throw",arg:e}}}e.wrap=c;var l={};function f(){}function h(){}function p(){}var d={};s(d,o,(function(){return this}));var m=Object.getPrototypeOf,v=m&&m(m(L([])));v&&v!==t&&n.call(v,o)&&(d=v);var y=p.prototype=f.prototype=Object.create(d);function b(e){["next","throw","return"].forEach((function(t){s(e,t,(function(e){return this._invoke(t,e)}))}))}function g(e,t){var r;this._invoke=function(o,i){function a(){return new t((function(r,a){!function r(o,i,a,s){var c=u(e[o],e,i);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==typeof f&&n.call(f,"__await")?t.resolve(f.__await).then((function(e){r("next",e,a,s)}),(function(e){r("throw",e,a,s)})):t.resolve(f).then((function(e){l.value=e,a(l)}),(function(e){return r("throw",e,a,s)}))}s(c.arg)}(o,i,r,a)}))}return r=r?r.then(a,a):a()}}function x(e,t){var n=e.iterator[t.method];if(void 0===n){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,x(e,t),"throw"===t.method))return l;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=u(n,e.iterator,t.arg);if("throw"===r.type)return t.method="throw",t.arg=r.arg,t.delegate=null,l;var o=r.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,l):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,l)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function S(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,i=function t(){for(;++r<e.length;)if(n.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:M}}function M(){return{value:void 0,done:!0}}return h.prototype=p,s(y,"constructor",p),s(p,"constructor",h),h.displayName=s(p,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===h||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,p):(e.__proto__=p,s(e,a,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},b(g.prototype),s(g.prototype,i,(function(){return this})),e.AsyncIterator=g,e.async=function(t,n,r,o,i){void 0===i&&(i=Promise);var a=new g(c(t,n,r,o),i);return e.isGeneratorFunction(n)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},b(y),s(y,a,"Generator"),s(y,o,(function(){return this})),s(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=[];for(var n in e)t.push(n);return t.reverse(),function n(){for(;t.length;){var r=t.pop();if(r in e)return n.value=r,n.done=!1,n}return n.done=!0,n}},e.values=L,S.prototype={constructor:S,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!e)for(var t in this)"t"===t.charAt(0)&&n.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function r(n,r){return a.type="throw",a.arg=e,t.next=n,r&&(t.method="next",t.arg=void 0),!!r}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return r("end");if(i.tryLoc<=this.prev){var s=n.call(i,"catchLoc"),c=n.call(i,"finallyLoc");if(s&&c){if(this.prev<i.catchLoc)return r(i.catchLoc,!0);if(this.prev<i.finallyLoc)return r(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return r(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return r(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),l},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),E(n),l}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var o=r.arg;E(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:L(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},e}(e.exports);try{regeneratorRuntime=t}catch(e){"object"==typeof globalThis?globalThis.regeneratorRuntime=t:Function("r","regeneratorRuntime = r")(t)}}(u={exports:{}}),u.exports),f=function(){return window.getViewportSize||(window.onresize=function(){window.getViewportSize={width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}},window.onresize()),window.getViewportSize},h=function(){function e(e,t,n,r,o){this.id=e,this.x=t,this.y=n,this.name=r,this.avatar=o,this.color=function(e){for(var t=0,n=0;n<e.length;n++)t=e.charCodeAt(n)+((t<<5)-t);for(var r="#",o=0;o<3;o++)r+=("00"+(t>>8*o&255).toString(16)).slice(-2);return r}(e)}var t=e.prototype;return t.move=function(e,t){this.x=e,this.y=t},t.onMove=function(e){},t.onGetLatency=function(e){},t.subscribeLatency=function(e){var t=this;return e.on$("latency").subscribe((function(e){e.id===t.id&&t.onGetLatency(e)}))},e}(),p=function(e){function t(t){var n,r=t.name,o=t.avatar;return(n=e.call(this,t.id,t.x,t.y,void 0===r?"":r,void 0===o?"":o)||this).mousemoveSubscription=n.subscribeMousemove(),n}c(t,e);var n=t.prototype;return n.goOnline=function(t){this.yomo=t,this.online(t),this.onlineSubscription=this.subscribeOnline(t),this.mousePositionSubscription&&this.mousePositionSubscription.unsubscribe(),this.mousePositionSubscription=this.subscribeMousePosition(t),this.latencySubscription=e.prototype.subscribeLatency.call(this,t)},n.goOffline=function(){var e=s(l.mark((function e(){return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return this.yomo&&this.yomo.send("offline",{id:this.id}),this.mousemoveSubscription&&(this.mousemoveSubscription.unsubscribe(),this.mousemoveSubscription=void 0),this.mousePositionSubscription&&(this.mousePositionSubscription.unsubscribe(),this.mousePositionSubscription=void 0),this.onlineSubscription&&(this.onlineSubscription.unsubscribe(),this.onlineSubscription=void 0),this.latencySubscription&&(this.latencySubscription.unsubscribe(),this.latencySubscription=void 0),e.next=7,new Promise((function(e){setTimeout(e,500)}));case 7:return e.abrupt("return",e.sent);case 8:case"end":return e.stop()}}),e,this)})));return function(){return e.apply(this,arguments)}}(),n.sendMessage=function(e){this.yomo&&this.yomo.send("text",{id:this.id,message:e})},n.online=function(e){e.send("online",{id:this.id,x:0,y:0,name:this.name,avatar:this.avatar})},n.subscribeOnline=function(e){var t=this;return e.on$("online").subscribe((function(){e.send("sync",{id:t.id,x:t.x,y:t.y,name:t.name,avatar:t.avatar})}))},n.subscribeMousemove=function(){var t=this;return r.fromEvent(document,"mousemove").subscribe((function(n){var r=n.clientX,o=n.clientY;e.prototype.move.call(t,r,o),t.onMove({mouseX:r,mouseY:o})}))},n.subscribeMousePosition=function(e){var t=this;return r.fromEvent(document,"mousemove").pipe(o.map((function(e){var n,r,o=(n=e.clientY,{scaleX:e.clientX/(r=f()).width,scaleY:n/r.height});return{id:t.id,x:o.scaleX,y:o.scaleY}}))).subscribe((function(t){e.send("movement",t)}))},t}(h),d=function(e){function t(t){var n=t.name,r=t.avatar;return e.call(this,t.id,t.x,t.y,void 0===n?"":n,void 0===r?"":r)||this}c(t,e);var n=t.prototype;return n.goOnline=function(t){this.movementMessageSubscription=this.subscribeMovement(t),this.textMessageSubscription=this.subscribeTextMessage(t),this.latencySubscription=e.prototype.subscribeLatency.call(this,t)},n.unsubscribe=function(){this.textMessageSubscription&&(this.textMessageSubscription.unsubscribe(),this.textMessageSubscription=void 0),this.movementMessageSubscription&&(this.movementMessageSubscription.unsubscribe(),this.movementMessageSubscription=void 0),this.latencySubscription&&(this.latencySubscription.unsubscribe(),this.latencySubscription=void 0)},n.onTextMessage=function(e){},n.subscribeTextMessage=function(e){var t=this;return e.on$("text").pipe(o.filter((function(e){return e.id===t.id}))).subscribe((function(e){t.onTextMessage(e.message)}))},n.subscribeMovement=function(t){var n=this;return t.on$("movement").pipe(o.filter((function(e){return e.id===n.id})),o.map((function(e){return t=e.y,{mouseX:e.x*(n=f()).width,mouseY:t*n.height};var t,n}))).subscribe((function(t){var r=t.mouseX,o=t.mouseY;e.prototype.move.call(n,r,o),n.onMove({mouseX:r,mouseY:o})}))},t}(h),m=function(e){var n=e.presenceURL,r=e.presenceAuth,a=e.room,c=e.name,u=e.avatar,f=t.useState(null),h=f[0],m=f[1],v=t.useState(new Map),y=v[0],b=v[1];t.useEffect((function(){var e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})),t=new p({id:e,x:0,y:0,name:c||"",avatar:u||""});m(t);var f=new i.Presence(n,{auth:r});f.on("connected",(function(){a&&f.toRoom(a),f.on$("online").pipe(o.filter((function(t){return t.id!==e}))).subscribe((function(e){b((function(t){if(t.has(e.id))return t;var n=new Map(t),r=new d(e);return r.goOnline(f),n.set(r.id,r),n}))})),f.on("offline",(function(e){b((function(t){var n=new Map(t),r=n.get(e.id);return r&&r.unsubscribe(),n.delete(e.id),n}))})),f.on$("sync").pipe(o.filter((function(t){return t.id!==e}))).subscribe((function(e){b((function(t){if(t.has(e.id))return t;var n=new Map(t),r=new d(e);return r.goOnline(f),n.set(r.id,r),n}))})),t.goOnline(f)}));var h=function(){var e=s(l.mark((function e(){return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.goOffline();case 2:f.close();case 3:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}();return window.addEventListener("unload",h),function(){h(),window.removeEventListener("unload",h)}}),[a]);var g=[];return y.forEach((function(e){g.push(e)})),{me:h,others:g}},v=function(e){var n=t.useRef(null);return t.useEffect((function(){var t=function(e){n.current&&n.current.setAttribute("style","transform: translate3d("+e.mouseX+"px, "+e.mouseY+"px, 0);")};t({mouseX:e.x,mouseY:e.y}),e.onMove=function(e){t(e)}}),[e]),n},y=function(e){var n=t.useState({meshId:"",latency:0,backgroundColor:"green"}),r=n[0],o=n[1];return t.useEffect((function(){e.onGetLatency=function(e){if(e.latency){var t="green";e.latency>=200&&e.latency<300&&(t="#FFB02A"),e.latency>=300&&(t="red"),o({backgroundColor:t,meshId:e.meshId,latency:e.latency})}}}),[]),r},b=function(e){var r=e.cursor,o=e.theme,i=v(r),a=t.useState(!1),s=a[0],c=a[1],u=t.useState(""),l=u[0],f=u[1],h=y(r),p=t.useCallback((function(e){"Slash"===e.code&&c(!0),"Escape"===e.code&&(c(!1),f(""),r.sendMessage(""))}),[]),d=t.useCallback((function(e){var t=e.target.value;"/"!==t&&(f(t),r.sendMessage(t))}),[]);return t.useEffect((function(){return document.addEventListener("keydown",p),function(){document.removeEventListener("keydown",p)}}),[]),t.useMemo((function(){return n.createElement("div",{className:"online-cursor-wrapper__cursor",ref:i},n.createElement(x,{color:r.color}),h.latency>0&&n.createElement("div",{className:"online-cursor-wrapper__latency"},"๐ ",h.meshId," ",n.createElement("span",{style:{backgroundColor:h.backgroundColor}},h.latency,"ms")),r.avatar&&n.createElement("img",{className:"online-cursor-wrapper__avatar",src:r.avatar,alt:"avatar"}),s&&n.createElement("div",{className:"online-cursor-wrapper__input-box "+("light"===o?"light":"dark")},n.createElement("span",null,l),n.createElement("input",{autoFocus:!0,placeholder:"Say something",value:l,onChange:d})))}),[s,l,h])},g=function(e){var r=e.cursor,o=e.theme,i=v(r),a=t.useState(r.name),s=a[0],c=a[1],u=y(r);return t.useEffect((function(){r.onTextMessage=function(e){c(e)}}),[]),t.useMemo((function(){return n.createElement("div",{className:"online-cursor-wrapper__cursor",ref:i},n.createElement(x,{color:r.color}),u.latency>0&&n.createElement("div",{className:"online-cursor-wrapper__latency"},"๐ ",u.meshId," ",n.createElement("span",{style:{backgroundColor:u.backgroundColor}},u.latency,"ms")),r.avatar&&n.createElement("img",{className:"online-cursor-wrapper__avatar",src:r.avatar,alt:"avatar"}),s&&n.createElement("div",{className:"online-cursor-wrapper__text "+("light"===o?"light":"dark")},s))}),[s,u])};function x(e){var r=e.color;return t.useMemo((function(){return n.createElement("svg",{shapeRendering:"geometricPrecision",xmlns:"http://www.w3.org/2000/svg",fill:r},n.createElement("path",{fill:"#666",d:"M9.63 6.9a1 1 0 011.27-1.27l11.25 3.75a1 1 0 010 1.9l-4.68 1.56a1 1 0 00-.63.63l-1.56 4.68a1 1 0 01-1.9 0L9.63 6.9z"}),n.createElement("path",{stroke:"#fff",strokeWidth:"1.5",d:"M11.13 4.92a1.75 1.75 0 00-2.2 2.21l3.74 11.26a1.75 1.75 0 003.32 0l1.56-4.68a.25.25 0 01.16-.16L22.4 12a1.75 1.75 0 000-3.32L11.13 4.92z"}))}),[r])}exports.default=t.memo((function(e){var t=e.theme,r=void 0===t?"dark":t,o=m({presenceURL:e.presenceURL,presenceAuth:e.presenceAuth,room:e.room,name:e.name,avatar:e.avatar}),i=o.me;return i?n.createElement("div",{className:"online-cursor-wrapper"},o.others.map((function(e){return n.createElement(g,{key:e.id,cursor:e,theme:r})})),n.createElement(b,{cursor:i,theme:r})):null})),exports.useOnlineCursor=m,exports.useRenderPosition=v; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e,r=require("rxjs"),o=require("rxjs/operators"),i=require("@yomo/presencejs");function a(e,t,n,r,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,o)}function s(e){return function(){var t=this,n=arguments;return new Promise((function(r,o){var i=e.apply(t,n);function s(e){a(i,r,o,s,c,"next",e)}function c(e){a(i,r,o,s,c,"throw",e)}s(void 0)}))}}function c(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var u,l=(function(e){var t=function(e){var t=Object.prototype,n=t.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},o=r.iterator||"@@iterator",i=r.asyncIterator||"@@asyncIterator",a=r.toStringTag||"@@toStringTag";function s(e,t,n){return Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{s({},"")}catch(e){s=function(e,t,n){return e[t]=n}}function c(e,t,n,r){var o=Object.create((t&&t.prototype instanceof h?t:h).prototype),i=new L(r||[]);return o._invoke=function(e,t,n){var r="suspendedStart";return function(o,i){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===o)throw i;return{value:void 0,done:!0}}for(n.method=o,n.arg=i;;){var a=n.delegate;if(a){var s=w(a,n);if(s){if(s===l)continue;return s}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var c=u(e,t,n);if("normal"===c.type){if(r=n.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(r="completed",n.method="throw",n.arg=c.arg)}}}(e,n,i),o}function u(e,t,n){try{return{type:"normal",arg:e.call(t,n)}}catch(e){return{type:"throw",arg:e}}}e.wrap=c;var l={};function h(){}function f(){}function p(){}var d={};s(d,o,(function(){return this}));var m=Object.getPrototypeOf,v=m&&m(m(S([])));v&&v!==t&&n.call(v,o)&&(d=v);var y=p.prototype=h.prototype=Object.create(d);function b(e){["next","throw","return"].forEach((function(t){s(e,t,(function(e){return this._invoke(t,e)}))}))}function g(e,t){var r;this._invoke=function(o,i){function a(){return new t((function(r,a){!function r(o,i,a,s){var c=u(e[o],e,i);if("throw"!==c.type){var l=c.arg,h=l.value;return h&&"object"==typeof h&&n.call(h,"__await")?t.resolve(h.__await).then((function(e){r("next",e,a,s)}),(function(e){r("throw",e,a,s)})):t.resolve(h).then((function(e){l.value=e,a(l)}),(function(e){return r("throw",e,a,s)}))}s(c.arg)}(o,i,r,a)}))}return r=r?r.then(a,a):a()}}function w(e,t){var n=e.iterator[t.method];if(void 0===n){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,w(e,t),"throw"===t.method))return l;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var r=u(n,e.iterator,t.arg);if("throw"===r.type)return t.method="throw",t.arg=r.arg,t.delegate=null,l;var o=r.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,l):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,l)}function x(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function L(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(x,this),this.reset(!0)}function S(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,i=function t(){for(;++r<e.length;)if(n.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:M}}function M(){return{value:void 0,done:!0}}return f.prototype=p,s(y,"constructor",p),s(p,"constructor",f),f.displayName=s(p,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===f||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,p):(e.__proto__=p,s(e,a,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},b(g.prototype),s(g.prototype,i,(function(){return this})),e.AsyncIterator=g,e.async=function(t,n,r,o,i){void 0===i&&(i=Promise);var a=new g(c(t,n,r,o),i);return e.isGeneratorFunction(n)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},b(y),s(y,a,"Generator"),s(y,o,(function(){return this})),s(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=[];for(var n in e)t.push(n);return t.reverse(),function n(){for(;t.length;){var r=t.pop();if(r in e)return n.value=r,n.done=!1,n}return n.done=!0,n}},e.values=S,L.prototype={constructor:L,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!e)for(var t in this)"t"===t.charAt(0)&&n.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function r(n,r){return a.type="throw",a.arg=e,t.next=n,r&&(t.method="next",t.arg=void 0),!!r}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return r("end");if(i.tryLoc<=this.prev){var s=n.call(i,"catchLoc"),c=n.call(i,"finallyLoc");if(s&&c){if(this.prev<i.catchLoc)return r(i.catchLoc,!0);if(this.prev<i.finallyLoc)return r(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return r(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return r(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),l},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),E(n),l}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var o=r.arg;E(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:S(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),l}},e}(e.exports);try{regeneratorRuntime=t}catch(e){"object"==typeof globalThis?globalThis.regeneratorRuntime=t:Function("r","regeneratorRuntime = r")(t)}}(u={exports:{}}),u.exports),h=function(){return window.getViewportSize||(window.onresize=function(){window.getViewportSize={width:window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,height:window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}},window.onresize()),window.getViewportSize},f=function(){function e(e,t,n,r,o){this.id=e,this.x=t,this.y=n,this.name=r,this.avatar=o,this.color=function(e){for(var t=0,n=0;n<e.length;n++)t=e.charCodeAt(n)+((t<<5)-t);for(var r="#",o=0;o<3;o++)r+=("00"+(t>>8*o&255).toString(16)).slice(-2);return r}(e)}var t=e.prototype;return t.move=function(e,t){this.x=e,this.y=t},t.onMove=function(e){},t.onGetLatency=function(e){},t.subscribeLatency=function(e){var t=this;return e.on$("latency").subscribe((function(e){e.id===t.id&&t.onGetLatency(e)}))},e}(),p=function(e){function t(t){var n,r=t.name,o=t.avatar;return(n=e.call(this,t.id,t.x,t.y,void 0===r?"":r,void 0===o?"":o)||this).mousemoveSubscription=n.subscribeMousemove(),n}c(t,e);var n=t.prototype;return n.goOnline=function(t){this.yomo=t,this.online(t),this.onlineSubscription=this.subscribeOnline(t),this.mousePositionSubscription&&this.mousePositionSubscription.unsubscribe(),this.mousePositionSubscription=this.subscribeMousePosition(t),this.latencySubscription=e.prototype.subscribeLatency.call(this,t)},n.goOffline=function(){var e=s(l.mark((function e(){return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return this.yomo&&this.yomo.send("offline",{id:this.id}),this.mousemoveSubscription&&(this.mousemoveSubscription.unsubscribe(),this.mousemoveSubscription=void 0),this.mousePositionSubscription&&(this.mousePositionSubscription.unsubscribe(),this.mousePositionSubscription=void 0),this.onlineSubscription&&(this.onlineSubscription.unsubscribe(),this.onlineSubscription=void 0),this.latencySubscription&&(this.latencySubscription.unsubscribe(),this.latencySubscription=void 0),e.next=7,new Promise((function(e){setTimeout(e,500)}));case 7:return e.abrupt("return",e.sent);case 8:case"end":return e.stop()}}),e,this)})));return function(){return e.apply(this,arguments)}}(),n.sendMessage=function(e){this.yomo&&this.yomo.send("text",{id:this.id,message:e})},n.online=function(e){e.send("online",{id:this.id,x:0,y:0,name:this.name,avatar:this.avatar})},n.subscribeOnline=function(e){var t=this;return e.on$("online").subscribe((function(){e.send("sync",{id:t.id,x:t.x,y:t.y,name:t.name,avatar:t.avatar})}))},n.subscribeMousemove=function(){var t=this;return r.fromEvent(document,"mousemove").subscribe((function(n){var r=n.clientX,o=n.clientY;e.prototype.move.call(t,r,o),t.onMove({mouseX:r,mouseY:o})}))},n.subscribeMousePosition=function(e){var t=this;return r.fromEvent(document,"mousemove").pipe(o.map((function(e){var n,r,o=(n=e.clientY,{scaleX:e.clientX/(r=h()).width,scaleY:n/r.height});return{id:t.id,x:o.scaleX,y:o.scaleY}}))).subscribe((function(t){e.send("movement",t)}))},t}(f),d=function(e){function t(t){var n=t.name,r=t.avatar;return e.call(this,t.id,t.x,t.y,void 0===n?"":n,void 0===r?"":r)||this}c(t,e);var n=t.prototype;return n.goOnline=function(t){this.movementMessageSubscription=this.subscribeMovement(t),this.textMessageSubscription=this.subscribeTextMessage(t),this.latencySubscription=e.prototype.subscribeLatency.call(this,t)},n.unsubscribe=function(){this.textMessageSubscription&&(this.textMessageSubscription.unsubscribe(),this.textMessageSubscription=void 0),this.movementMessageSubscription&&(this.movementMessageSubscription.unsubscribe(),this.movementMessageSubscription=void 0),this.latencySubscription&&(this.latencySubscription.unsubscribe(),this.latencySubscription=void 0)},n.onTextMessage=function(e){},n.subscribeTextMessage=function(e){var t=this;return e.on$("text").pipe(o.filter((function(e){return e.id===t.id}))).subscribe((function(e){t.onTextMessage(e.message)}))},n.subscribeMovement=function(t){var n=this;return t.on$("movement").pipe(o.filter((function(e){return e.id===n.id})),o.map((function(e){return t=e.y,{mouseX:e.x*(n=h()).width,mouseY:t*n.height};var t,n}))).subscribe((function(t){var r=t.mouseX,o=t.mouseY;e.prototype.move.call(n,r,o),n.onMove({mouseX:r,mouseY:o})}))},t}(f),m=function(e){var n=e.presenceURL,r=e.presenceAuth,a=e.room,c=e.name,u=e.avatar,h=t.useState(null),f=h[0],m=h[1],v=t.useState(new Map),y=v[0],b=v[1];t.useEffect((function(){var e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})),t=new p({id:e,x:0,y:0,name:c||"",avatar:u||""});m(t);var h=new i.Presence(n,{auth:r});h.on("connected",(function(){a&&h.toRoom(a),h.on$("online").pipe(o.filter((function(t){return t.id!==e}))).subscribe((function(e){b((function(t){if(t.has(e.id))return t;var n=new Map(t),r=new d(e);return r.goOnline(h),n.set(r.id,r),n}))})),h.on("offline",(function(e){b((function(t){var n=new Map(t),r=n.get(e.id);return r&&r.unsubscribe(),n.delete(e.id),n}))})),h.on$("sync").pipe(o.filter((function(t){return t.id!==e}))).subscribe((function(e){b((function(t){if(t.has(e.id))return t;var n=new Map(t),r=new d(e);return r.goOnline(h),n.set(r.id,r),n}))})),t.goOnline(h)}));var f=function(){var e=s(l.mark((function e(){return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.goOffline();case 2:h.close();case 3:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}();return window.addEventListener("unload",f),function(){f(),window.removeEventListener("unload",f)}}),[a]);var g=[];return y.forEach((function(e){g.push(e)})),{me:f,others:g}},v=function(e){var n=t.useRef(null);return t.useEffect((function(){var t=function(e){n.current&&n.current.setAttribute("style","transform: translate3d("+e.mouseX+"px, "+e.mouseY+"px, 0);")};t({mouseX:e.x,mouseY:e.y}),e.onMove=function(e){t(e)}}),[e]),n},y=function(e,n){var r=t.useState({meshId:"",latency:0,backgroundColor:"green"}),o=r[0],i=r[1];return t.useEffect((function(){n?e.onGetLatency=function(e){if(e.latency){var t="green";e.latency>=200&&e.latency<300&&(t="#FFB02A"),e.latency>=300&&(t="red"),i({backgroundColor:t,meshId:e.meshId,latency:e.latency})}}:(e.onGetLatency=function(e){},i({meshId:"",latency:0,backgroundColor:"green"}))}),[n]),o},b=function(e){var r=e.cursor,o=e.showLatency,i=e.theme,a=v(r),s=t.useState(!1),c=s[0],u=s[1],l=t.useState(""),h=l[0],f=l[1],p=y(r,o),d=t.useCallback((function(e){"Slash"===e.code&&u(!0),"Escape"===e.code&&(u(!1),f(""),r.sendMessage(""))}),[]),m=t.useCallback((function(e){var t=e.target.value;"/"!==t&&(f(t),r.sendMessage(t))}),[]);return t.useEffect((function(){return document.addEventListener("keydown",d),function(){document.removeEventListener("keydown",d)}}),[]),t.useMemo((function(){return n.createElement("div",{className:"online-cursor-wrapper__cursor",ref:a},n.createElement(w,{color:r.color}),p.latency>0&&n.createElement("div",{className:"online-cursor-wrapper__latency"},"๐ ",p.meshId," ",n.createElement("span",{style:{backgroundColor:p.backgroundColor}},p.latency,"ms")),r.avatar&&n.createElement("img",{className:"online-cursor-wrapper__avatar",src:r.avatar,alt:"avatar"}),c&&n.createElement("div",{className:"online-cursor-wrapper__input-box "+("light"===i?"light":"dark")},n.createElement("span",null,h),n.createElement("input",{autoFocus:!0,placeholder:"Say something",value:h,onChange:m})))}),[c,h,p])},g=function(e){var r=e.cursor,o=e.showLatency,i=e.theme,a=v(r),s=t.useState(r.name),c=s[0],u=s[1],l=y(r,o);return t.useEffect((function(){r.onTextMessage=function(e){u(e)}}),[]),t.useMemo((function(){return n.createElement("div",{className:"online-cursor-wrapper__cursor",ref:a},n.createElement(w,{color:r.color}),l.latency>0&&n.createElement("div",{className:"online-cursor-wrapper__latency"},"๐ ",l.meshId," ",n.createElement("span",{style:{backgroundColor:l.backgroundColor}},l.latency,"ms")),r.avatar&&n.createElement("img",{className:"online-cursor-wrapper__avatar",src:r.avatar,alt:"avatar"}),c&&n.createElement("div",{className:"online-cursor-wrapper__text "+("light"===i?"light":"dark")},c))}),[c,l])};function w(e){var r=e.color;return t.useMemo((function(){return n.createElement("svg",{shapeRendering:"geometricPrecision",xmlns:"http://www.w3.org/2000/svg",fill:r},n.createElement("path",{fill:"#666",d:"M9.63 6.9a1 1 0 011.27-1.27l11.25 3.75a1 1 0 010 1.9l-4.68 1.56a1 1 0 00-.63.63l-1.56 4.68a1 1 0 01-1.9 0L9.63 6.9z"}),n.createElement("path",{stroke:"#fff",strokeWidth:"1.5",d:"M11.13 4.92a1.75 1.75 0 00-2.2 2.21l3.74 11.26a1.75 1.75 0 003.32 0l1.56-4.68a.25.25 0 01.16-.16L22.4 12a1.75 1.75 0 000-3.32L11.13 4.92z"}))}),[r])}exports.default=t.memo((function(e){var t=e.showLatency,r=void 0!==t&&t,o=e.theme,i=void 0===o?"dark":o,a=m({presenceURL:e.presenceURL,presenceAuth:e.presenceAuth,room:e.room,name:e.name,avatar:e.avatar}),s=a.me;return s?n.createElement("div",{className:"online-cursor-wrapper"},a.others.map((function(e){return n.createElement(g,{key:e.id,cursor:e,showLatency:r,theme:i})})),n.createElement(b,{cursor:s,showLatency:r,theme:i})):null})),exports.useOnlineCursor=m,exports.useRenderPosition=v; | ||
//# sourceMappingURL=react-cursor-chat.cjs.production.min.js.map |
@@ -1291,3 +1291,3 @@ import React, { useState, useEffect, useRef, memo, useCallback, useMemo } from 'react'; | ||
var useLatency = function useLatency(cursor) { | ||
var useLatency = function useLatency(cursor, showLatency) { | ||
var _useState = useState({ | ||
@@ -1302,22 +1302,32 @@ meshId: '', | ||
useEffect(function () { | ||
cursor.onGetLatency = function (data) { | ||
if (data.latency) { | ||
var backgroundColor = 'green'; | ||
if (showLatency) { | ||
cursor.onGetLatency = function (data) { | ||
if (data.latency) { | ||
var backgroundColor = 'green'; | ||
if (data.latency >= 200 && data.latency < 300) { | ||
backgroundColor = '#FFB02A'; | ||
} | ||
if (data.latency >= 200 && data.latency < 300) { | ||
backgroundColor = '#FFB02A'; | ||
} | ||
if (data.latency >= 300) { | ||
backgroundColor = 'red'; | ||
if (data.latency >= 300) { | ||
backgroundColor = 'red'; | ||
} | ||
setLatencyData({ | ||
backgroundColor: backgroundColor, | ||
meshId: data.meshId, | ||
latency: data.latency | ||
}); | ||
} | ||
}; | ||
} else { | ||
cursor.onGetLatency = function (_) {}; | ||
setLatencyData({ | ||
backgroundColor: backgroundColor, | ||
meshId: data.meshId, | ||
latency: data.latency | ||
}); | ||
} | ||
}; | ||
}, []); | ||
setLatencyData({ | ||
meshId: '', | ||
latency: 0, | ||
backgroundColor: 'green' | ||
}); | ||
} | ||
}, [showLatency]); | ||
return latencyData; | ||
@@ -1328,2 +1338,3 @@ }; | ||
var cursor = _ref.cursor, | ||
showLatency = _ref.showLatency, | ||
theme = _ref.theme; | ||
@@ -1340,3 +1351,3 @@ var refContainer = useRenderPosition(cursor); | ||
var latencyData = useLatency(cursor); | ||
var latencyData = useLatency(cursor, showLatency); | ||
var onKeydown = useCallback(function (e) { | ||
@@ -1398,2 +1409,3 @@ if (e.code === 'Slash') { | ||
var cursor = _ref2.cursor, | ||
showLatency = _ref2.showLatency, | ||
theme = _ref2.theme; | ||
@@ -1406,3 +1418,3 @@ var refContainer = useRenderPosition(cursor); | ||
var latencyData = useLatency(cursor); | ||
var latencyData = useLatency(cursor, showLatency); | ||
useEffect(function () { | ||
@@ -1439,2 +1451,4 @@ cursor.onTextMessage = function (msg) { | ||
room = _ref3.room, | ||
_ref3$showLatency = _ref3.showLatency, | ||
showLatency = _ref3$showLatency === void 0 ? false : _ref3$showLatency, | ||
name = _ref3.name, | ||
@@ -1465,2 +1479,3 @@ avatar = _ref3.avatar, | ||
cursor: item, | ||
showLatency: showLatency, | ||
theme: theme | ||
@@ -1470,2 +1485,3 @@ }); | ||
cursor: me, | ||
showLatency: showLatency, | ||
theme: theme | ||
@@ -1472,0 +1488,0 @@ })); |
{ | ||
"name": "@yomo/react-cursor-chat", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "A react component for cursor chat", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -5,3 +5,3 @@ # @yomo/react-cursor-chat | ||
A react component for cursor chat | ||
A react component helps bring Figma's Cursor Chat to your web applications in less than 3 minutes, making real-time collaboration anywhere. | ||
@@ -35,5 +35,19 @@ - Press `/` to bring up the input box | ||
### Request free dev/test account | ||
Login with your Github account on `https://presence.yomo.run`, will get a free `app_id` and `app_secret` | ||
then, add `.env.local` like: | ||
```bash | ||
APP_ID="abrHlqtooFakeID" | ||
APP_SECRET="nFJqSVzQyhbVgdsBeBojoeJTooFakeSecret" | ||
``` | ||
### Integrate to your project | ||
```javascript | ||
import CursorChat from "@yomo/react-cursor-chat"; | ||
import "@yomo/react-cursor-chat/dist/cursor-chat.min.css"; | ||
const App = () => { | ||
@@ -48,8 +62,8 @@ return ( | ||
<CursorChat | ||
presenceURL="wss://presence.yomo.dev" | ||
presenceURL="wss://prsc.yomo.dev" | ||
presenceAuth={{ | ||
type: 'publickey', | ||
publicKey: 'YOUR_PUBLIC_KEY' | ||
type: 'token', | ||
endpoint: "/api/auth", | ||
}} | ||
avatar='assets/cursor.png' | ||
avatar='https://cursor-chat-example.vercel.app/_next/image?url=%2Flogo.png&w=256&q=75' | ||
theme="light" | ||
@@ -64,9 +78,33 @@ /> | ||
### Start | ||
### add `/api/auth.js` | ||
```javascript | ||
export default async function handler(req, res) { | ||
if (req.method === "GET") { | ||
const response = await fetch("https://prsc.yomo.dev/api/v1/auth", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
app_id: process.env.APP_ID, | ||
app_secret: process.env.APP_SECRET, | ||
}), | ||
}); | ||
const data = await response.json(); | ||
res.status(200).json(data.data); | ||
} else { | ||
// Handle any other HTTP method | ||
} | ||
} | ||
``` | ||
### Start dev | ||
```shell | ||
$ npm run start | ||
$ npm run dev | ||
``` | ||
## ๐ฅท๐ผ Advanced | ||
## ๐ฅท๐ผ for hackers | ||
@@ -97,2 +135,3 @@ ### Importing the CursorChat component | ||
- `room?: string`: to set room. | ||
- `showLatency?: boolean`: to set showLatency. | ||
- `avatar?: string`: to set avatar. | ||
@@ -102,3 +141,3 @@ - `name?: string`: to set name. | ||
### Use hooks and customize the components yourself: | ||
### Use hooks to customize the component: | ||
@@ -166,36 +205,13 @@ ```tsx | ||
### For Production env, How do I get a `presenceAuth` token? | ||
## Deploy to vercel | ||
If you build your application using next.js, then you can use [API Routes](https://nextjs.org/docs/api-routes/introduction) to get the access token. | ||
For example, the following API route `pages/api/auth.js` returns a json response with a status code of 200: | ||
`vc --prod` | ||
```js | ||
export default async function handler(req, res) { | ||
if (req.method === 'GET') { | ||
const response = await fetch('https://presence.yomo.dev/api/v1/auth', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
app_id: process.env.PRESENCE_APP_ID, | ||
app_secret: process.env.PRESENCE_APP_SECRET, | ||
}), | ||
}); | ||
const data = await response.json(); | ||
res.status(200).json(data.data); | ||
} else { | ||
// Handle any other HTTP method | ||
} | ||
} | ||
## Run example | ||
```sh | ||
cd react-cursor-chat/example | ||
npm i | ||
npm run start | ||
``` | ||
Response data: | ||
```json | ||
{ | ||
"token": "eyJhbGciOiJIUzI1..." | ||
} | ||
``` | ||
## LICENSE | ||
@@ -202,0 +218,0 @@ |
@@ -5,3 +5,3 @@ import { useState, useEffect } from 'react'; | ||
const useLatency = (cursor: Me | Others) => { | ||
const useLatency = (cursor: Me | Others, showLatency: boolean) => { | ||
const [latencyData, setLatencyData] = useState({ | ||
@@ -14,19 +14,28 @@ meshId: '', | ||
useEffect(() => { | ||
cursor.onGetLatency = data => { | ||
if (data.latency) { | ||
let backgroundColor = 'green'; | ||
if (data.latency >= 200 && data.latency < 300) { | ||
backgroundColor = '#FFB02A'; | ||
if (showLatency) { | ||
cursor.onGetLatency = data => { | ||
if (data.latency) { | ||
let backgroundColor = 'green'; | ||
if (data.latency >= 200 && data.latency < 300) { | ||
backgroundColor = '#FFB02A'; | ||
} | ||
if (data.latency >= 300) { | ||
backgroundColor = 'red'; | ||
} | ||
setLatencyData({ | ||
backgroundColor, | ||
meshId: data.meshId, | ||
latency: data.latency, | ||
}); | ||
} | ||
if (data.latency >= 300) { | ||
backgroundColor = 'red'; | ||
} | ||
setLatencyData({ | ||
backgroundColor, | ||
meshId: data.meshId, | ||
latency: data.latency, | ||
}); | ||
} | ||
}; | ||
}, []); | ||
}; | ||
} else { | ||
cursor.onGetLatency = _ => {}; | ||
setLatencyData({ | ||
meshId: '', | ||
latency: 0, | ||
backgroundColor: 'green', | ||
}); | ||
} | ||
}, [showLatency]); | ||
@@ -33,0 +42,0 @@ return latencyData; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
383088
3465
215