Skip to content

Commit

Permalink
refactor: remove a lot of unused code, fix bugs and add correct font
Browse files Browse the repository at this point in the history
  • Loading branch information
ph1p committed Nov 4, 2021
1 parent b629bfe commit 81751f4
Show file tree
Hide file tree
Showing 26 changed files with 196 additions and 482 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
26 changes: 4 additions & 22 deletions packages/plugin/src/Ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,22 @@ const App = observer(() => {
nodes: [],
},
currentUser,
instanceId = '',
} = rootData;

store.setCurrentUser(currentUser);
store.setSecret(dataSecret);
store.setRoomName(dataRoomName);
store.setMessages(messages);
store.setInstanceId(instanceId);
store.setSelection(selection);

// console.log(settings);

// store.persistSettings({ ...settings, ...currentUser }, socket, true);

// socket listener
socket.io.on('error', () => store.setStatus(ConnectionEnum.ERROR));

socket.io.on('reconnect_error', () =>
store.setStatus(ConnectionEnum.ERROR)
);

socket.on('chat message', (data) => {
store.addMessage(data);
});
socket.on('chat message', (data) => store.addMessage(data));

socket.on('join leave message', (data) => {
const username = data.user.name || 'Anon';
Expand All @@ -134,16 +126,9 @@ const App = observer(() => {

store.setStatus(ConnectionEnum.CONNECTED);

const settings = {
...toJS(store.settings),
figmaId: currentUser.id,
name: currentUser.name,
photoUrl: currentUser.photoUrl,
};

socket.emit('set user', settings);
socket.emit('set user', toJS(store.currentUser));
socket.emit('join room', {
settings,
settings: toJS(store.currentUser),
room: dataRoomName,
});

Expand Down Expand Up @@ -197,10 +182,7 @@ const App = observer(() => {
<Route
path="/user-list"
element={
<UserListView
users={store.online}
socketId={socket?.id || ''}
/>
<UserListView users={store.online} user={store.currentUser} />
}
/>
<Route path="/settings" element={<SettingsView />} />
Expand Down
24 changes: 9 additions & 15 deletions packages/plugin/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ let triggerSelectionEvent = true;

const isRelaunch = figma.command === 'relaunch';

const currentUser = {
...figma.currentUser,
};

delete currentUser.sessionId;

figma.showUI(__html__, {
width: 333,
height: 490,
Expand All @@ -24,7 +30,6 @@ const main = async () => {
const timestamp = +new Date();

// random user id for current user
let instanceId = await figma.clientStorage.getAsync('id');
let history = figma.root.getPluginData('history');
let roomName = figma.root.getPluginData('roomName');
let secret = figma.root.getPluginData('secret');
Expand All @@ -36,15 +41,6 @@ const main = async () => {
history = '';
}

if (!instanceId) {
instanceId = 'user-' + timestamp + '-' + generateString(15);
await figma.clientStorage.setAsync('id', instanceId);
}

if (!roomName && !secret) {
figma.root.setPluginData('ownerId', instanceId);
}

if (!roomName) {
const randomRoomName = timestamp + '-' + generateString(15);
figma.root.setPluginData('roomName', randomRoomName);
Expand All @@ -70,7 +66,6 @@ const main = async () => {
roomName,
secret,
history,
instanceId,
};
};

Expand Down Expand Up @@ -147,17 +142,16 @@ EventEmitter.on('notification', (payload) => {
}
});

EventEmitter.answer('current-user', async () => figma.currentUser);
EventEmitter.answer('current-user', async () => currentUser);

EventEmitter.answer('root-data', async () => {
const { roomName, secret, history, instanceId } = await main();
const { roomName, secret, history } = await main();

return {
roomName,
secret,
history,
instanceId,
currentUser: figma.currentUser,
currentUser,
selection: getSelectionIds(),
};
});
Expand Down
38 changes: 15 additions & 23 deletions packages/plugin/src/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ export class RootStore {

@ignore
status = ConnectionEnum.NONE;

@ignore
online = [];

@ignore
messages = [];

@ignore
messagesRef = createRef<HTMLDivElement>();

@ignore
secret = '';

@ignore
roomName = '';
@ignore
instanceId = '';

@ignore
autoScrollDisabled = false;
Expand All @@ -52,8 +54,9 @@ export class RootStore {
id: '',
name: '',
sessionId: '',
color: '',
avatar: '',
photoUrl: '',
color: '#4F4F4F',
};

setStatus(status) {
Expand All @@ -70,9 +73,6 @@ export class RootStore {
setRoomName(roomName) {
this.roomName = roomName;
}
setInstanceId(instanceId) {
this.instanceId = instanceId;
}
setOnline(online) {
this.online = online;
}
Expand Down Expand Up @@ -127,8 +127,6 @@ export class RootStore {

@observable.deep
settings: Omit<StoreSettings, 'name'> = {
avatar: '',
color: '#4F4F4F',
url: DEFAULT_SERVER_URL,
enableNotificationTooltip: true,
enableNotificationSound: true,
Expand Down Expand Up @@ -191,28 +189,26 @@ export class RootStore {
}
}

persistSettings(settings, socket?, isInit = false) {
persistSettings(settings, isInit = false) {
const oldUrl = this.settings.url;

this.settings = {
...this.settings,
...settings,
figmaId: this.currentUser.id,
name: this.currentUser.name,
photoUrl: this.currentUser.photoUrl,
};

// save user settings in main
// EventEmitter.emit('save-user-settings', { ...toJS(this.settings) });

// set server URL
if (!isInit && settings.url && settings.url !== oldUrl) {
this.addNotification('Updated server-URL');
}
}

persistCurrentUser(currentUser, socket?) {
this.setCurrentUser(currentUser);

if (socket && socket.connected) {
// set user data on server
socket.emit('set user', this.settings);
socket.emit('set user', toJS(this.currentUser));
}
}

Expand All @@ -226,6 +222,7 @@ export class RootStore {
// silent on error
try {
const data = JSON.parse(decryptedMessage);

let newMessage: Record<string, unknown> = {
message: {
...data,
Expand All @@ -235,12 +232,7 @@ export class RootStore {
// is local sender
if (isLocal) {
newMessage = {
id: this.instanceId,
user: {
...this.currentUser,
avatar: this.settings.avatar,
color: this.settings.color,
},
user: toJS(this.currentUser),
message: {
...data,
},
Expand All @@ -249,7 +241,6 @@ export class RootStore {
EventEmitter.emit('add-message-to-history', newMessage);
} else {
newMessage = {
id: messageData.id,
user: messageData.user,
message: {
...data,
Expand All @@ -264,6 +255,7 @@ export class RootStore {
const audio = new Audio(MessageSound);
audio.play();
}

if (this.settings.enableNotificationTooltip) {
let text = '';
if (data.text) {
Expand Down
35 changes: 5 additions & 30 deletions packages/plugin/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,15 @@

body {
overflow: hidden;
font-family: Inter, sans-serif;
font-family: 'Inter', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-size: 12px;
margin: 0;
}

/* Typography */
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 400;
src: url('https://rsms.me/inter/font-files/Inter-Regular.woff2?v=3.7')
format('woff2'),
url('https://rsms.me/inter/font-files/Inter-Regular.woff?v=3.7')
format('woff');
}

@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 500;
src: url('https://rsms.me/inter/font-files/Inter-Medium.woff2?v=3.7')
format('woff2'),
url('https://rsms.me/inter/font-files/Inter-Medium.woff2?v=3.7')
format('woff');
}

@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 600;
src: url('https://rsms.me/inter/font-files/Inter-SemiBold.woff2?v=3.7')
format('woff2'),
url('https://rsms.me/inter/font-files/Inter-SemiBold.woff2?v=3.7')
format('woff');
input {
font-family: 'Inter', sans-serif;
}

.main {
Expand Down
11 changes: 4 additions & 7 deletions packages/plugin/src/views/Chat/components/Chatbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ const ChatBar: FunctionComponent<ChatProps> = (props) => {
</RefTooltip>
<SettingsAndUsers>
<CustomLink to="/settings">
<div
className={`gear ${store.settings.isDarkTheme ? 'dark' : ''}`}
>
<div className="gear">
<GearIcon />
</div>
</CustomLink>
Expand Down Expand Up @@ -272,10 +270,9 @@ const SettingsAndUsers = styled.div`
align-items: center;
background-color: ${(p) => p.theme.chatbarSecondaryBackground};
border-radius: 100%;
&.dark {
svg path {
fill: ${({ theme }) => theme.thirdFontColor};
}
svg path {
fill: ${({ theme }) => theme.thirdFontColor};
}
}
`;
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin/src/views/Chat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toJS } from 'mobx';
import { observer, useLocalObservable } from 'mobx-react-lite';
import React, { useEffect, FunctionComponent } from 'react';
import styled from 'styled-components';
Expand Down Expand Up @@ -104,7 +105,9 @@ const Chat: FunctionComponent = observer(() => {
};
}, []);

useEffect(() => store.scrollToBottom(), [store.messages]);
useEffect(() => {
store.scrollToBottom();
}, [store.messages]);

const onClickSelection = (selection) => {
let selectionData = null;
Expand Down
Loading

0 comments on commit 81751f4

Please sign in to comment.