Emil Ajdyna
11/21/2022, 2:29 PMsass
, which can help you split or abstract css even further.
Here's how it'd would look like for a simple Card
component.
/* Card.tsx */
import React from 'react'
import styles from './index.module.scss'
interface Props {
children: React.ReactNode;
}
export default function Card ({ children }: Props) {
return <div className={styles.card}>{children}</div>
}
dist/index.js
and dist/index.css
.
You'll need to include the css file manually inside of your app.
Configuration wise, we'll need to setup a build process that creates a bundle for us.
Here's an example rollup configuration file.
/* rollup.config.js */
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('@rollup/plugin-typescript');
const postcss = require('rollup-plugin-postcss');
const external = require('rollup-plugin-peer-deps-external');
module.exports = {
input: 'src/index.tsx',
output: {
file: 'dist/index.js',
format: 'cjs',
},
plugins: [
resolve(),
postcss({
extract: true,
modules: true,
use: ['sass'],
}),
commonjs(),
typescript({ tsconfig: './tsconfig.json' }),
external(),
],
};
It's using few dependencies:
npm install --save-dev rollup
npm install --save-dev @rollup/plugin-node-resolve
npm install --save-dev @rollup/plugin-commonjs
npm install --save-dev @rollup/plugin-typescript
npm install --save-dev rollup-plugin-postcss
npm install --save-dev rollup-plugin-peer-deps-external
npm install --save-dev sass
You should also install react.
npm install react
At this point, running npx rollup --config rollup.config.js
would create a bundle that includes react
.
We shouldn't include react there, e.g. because if would significantly increase your app size if you're using a different react version or cause other unintended side effects.
The solution for this is to move react
to peerDependencies
inside of package.json
.
npm
does not offer a shortcut for installing peer dependencies yet (2022), so you need to do it manually.
jest
test setup.
https://github.com/Linen-dev/linen.dev/tree/main/packages/ui
I hope this short article helps even a little :). In case of any questions, just ask them in the thread.Kam
11/28/2022, 7:36 PMhttps://user-images.githubusercontent.com/4218509/200422983-21079c4a-bcb6-4f48-bf82-fdf9d01f1d3f.pngâ–ľ
jsx
function useWebSockets({ room, token, permissions, onNewMessage }: Props) {
const [channel, setChannel] = useState<Channel>();
const [connected, setConnected] = useState<boolean>(false);
useEffect(() => {
if (permissions.chat && token && room) {
//Set url instead of hard coding
const socket = new Socket(
`${process.env.NEXT_PUBLIC_PUSH_SERVICE_URL}/socket`,
{ params: { token } }
);
socket.connect();
const channel = socket.channel(room);
setChannel(channel);
channel
.join()
.receive('ok', () => {
setConnected(true);
})
.receive('error', () => {
setConnected(false);
});
channel.on('new_msg', onNewMessage);
return () => {
setConnected(false);
socket.disconnect();
};
}
return () => {};
}, []);
useEffect(() => {
channel?.off('new_msg');
channel?.on('new_msg', onNewMessage);
}, [onNewMessage]);
return { connected, channel };
}
jsx
return fetch(`/api/messages/channel`, {
method: 'POST',
body: JSON.stringify({
communityId,
body: message,
files,
channelId,
imitationId,
}),
});
},
jsx
const thread = await prisma.threads.create({
data: {
channel: { connect: { id: channelId } },
sentAt: sentAt.getTime(),
lastReplyAt: sentAt.getTime(),
messageCount: 1,
messages,
} as Prisma.threadsCreateInput
...
});
jsx
export const push = ({
channelId,
threadId,
messageId,
isThread,
isReply,
message,
thread,
}: PushType) => {
return request.post(`${pushURL}/api/message`).send({
channel_id: channelId,
thread_id: threadId,
message_id: messageId,
is_thread: isThread,
is_reply: isReply,
message,
thread,
token,
});
};
elixir
def create(conn, params) do
%{
"channel_id" => channel_id,
"token" => token
} = params
PushServiceWeb.Endpoint.broadcast!("room:lobby:" <> channel_id, "new_msg", params)
end
We’re using Phoenix channels which handles the broadcast automatically see: https://hexdocs.pm/phoenix/channels.html for more information
elixir
def join("room:" <> community_id, _params, socket) do
current_user = socket.assigns[:current_user]
{:ok, assign(socket, :community_id, community_id)}
end
end
Kam
01/23/2023, 5:15 PMKam
04/26/2023, 6:58 PMhttps://static.main.linendev.com/attachments/5b12e431-c19b-426d-a9a3-0df889e1ac22/Screen_Shot_2023-04-26_at_12.02.57_PM.jpgâ–ľ
Kam
05/08/2023, 5:57 PM