Skip to content

Commit

Permalink
feat: add derived channel usage to 'sample-app'
Browse files Browse the repository at this point in the history
  • Loading branch information
rustworthy committed Nov 13, 2023
1 parent 9bd2e77 commit 8f998d0
Showing 1 changed file with 95 additions and 2 deletions.
97 changes: 95 additions & 2 deletions src/platform/react-hooks/sample-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,36 @@ import './App.css';

function App() {
const [messages, updateMessages] = useState<Types.Message[]>([]);
const [derivedChannelMessages, updateDerivedChannelMessages] = useState<Types.Message[]>([]);
const [frontOficeOnlyMessages, updateFrontOfficeOnlyMessages] = useState<Types.Message[]>([]);

const [skip, setSkip] = useState(false);
const { channel, ably } = useChannel({ channelName: 'your-channel-name', skip }, (message) => {
updateMessages((prev) => [...prev, message]);
});

useChannel(
{
channelName: 'your-derived-channel-name',
deriveOptions: { filter: 'headers.email == `"[email protected]"` || headers.company == `"domain"`' },
},
(message) => {
updateDerivedChannelMessages((prev) => [...prev, message]);
}
);

useChannel(
{
channelName: 'your-derived-channel-name',
deriveOptions: { filter: 'headers.role == `"front-office"` || headers.company == `"domain"`' },
},
(message) => {
updateFrontOfficeOnlyMessages((prev) => [...prev, message]);
}
);

const { channel: anotherChannelPublisher } = useChannel({ channelName: 'your-derived-channel-name' });

const { presenceData, updateStatus } = usePresence(
{ channelName: 'your-channel-name', skip },
{ foo: 'bar' },
Expand All @@ -42,7 +67,14 @@ function App() {
setChannelStateReason(stateChange.reason ?? undefined);
});

const messagePreviews = messages.map((msg, index) => <li key={index}>{msg.data.text}</li>);
const messagePreviews = messages.map((message, idx) => <MessagePreview key={idx} message={message} />);
const derivedChannelMessagePreviews = derivedChannelMessages.map((message, idx) => (
<MessagePreview key={idx} message={message} />
));
const frontOfficeMessagePreviews = frontOficeOnlyMessages.map((message, idx) => (
<MessagePreview key={idx} message={message} />
));

const presentClients = presenceData.map((msg, index) => (
<li key={index}>
{msg.clientId}: {JSON.stringify(msg.data)}
Expand All @@ -69,6 +101,57 @@ function App() {
>
Update status to hello
</button>
<button
onClick={() => {
anotherChannelPublisher.publish({
name: 'test-message',
data: {
text: 'This is a message for Rob',
},
extras: {
headers: {
email: '[email protected]',
},
},
});
}}
>
Send Message to Rob Only
</button>
<button
onClick={() => {
anotherChannelPublisher.publish({
name: 'test-message',
data: {
text: 'This is a company-wide message',
},
extras: {
headers: {
company: 'domain',
},
},
});
}}
>
Send Company-wide message
</button>
<button
onClick={() => {
anotherChannelPublisher.publish({
name: 'test-message',
data: {
text: 'This is a message for front office employees only',
},
extras: {
headers: {
role: 'front-office',
},
},
});
}}
>
Send message to Front Office
</button>
</div>

<div style={{ position: 'fixed', width: '250px' }}>
Expand All @@ -94,7 +177,13 @@ function App() {
<div>{ablyErr}</div>

<h2>Messages</h2>
<ul>{messagePreviews}</ul>
{<ul>{messagePreviews}</ul>}

<h2>Derived Channel Messages</h2>
<ul>{derivedChannelMessagePreviews}</ul>

<h2>Front Office Messages</h2>
<ul>{frontOfficeMessagePreviews}</ul>

<h2>Present Clients</h2>
<ul>{presentClients}</ul>
Expand All @@ -103,6 +192,10 @@ function App() {
);
}

function MessagePreview({ message }: { message: Types.Message }) {
return <li>{message.data.text}</li>;
}

function ConnectionState() {
const ably = useAbly();
const [connectionState, setConnectionState] = useState(ably.connection.state);
Expand Down

0 comments on commit 8f998d0

Please sign in to comment.