Skip to content

Instantly share code, notes, and snippets.

@LiorB-D
Created June 21, 2022 10:40
Show Gist options
  • Save LiorB-D/73fd0c8eda10fab83cf3c87d82161d67 to your computer and use it in GitHub Desktop.
Save LiorB-D/73fd0c8eda10fab83cf3c87d82161d67 to your computer and use it in GitHub Desktop.
async function loadSnippyly() {
await Snippyly.init("<Your API Key Goes Here>");
const presenceElement = Snippyly.getPresenceElement();
presenceElement.getOnlineUsersOnCurrentDocument().subscribe((data) => {
console.log('getOnlineUsersOnCurrentDocument in html', data);
});
// To enable text comment feature
const commentElement = Snippyly.getCommentElement();
commentElement.enableTextComments(true);
if (localStorage.getItem('user')) {
try {
const user = JSON.parse(localStorage.getItem('user'));
if (user) {
signIn(user.userId);
}
} catch (err) {
localStorage.removeItem('user');
window.location.reload();
}
} else {
const userDetailsContainer = document.getElementById('userDetailsContainer');
userDetailsContainer.style.display = 'none';
const loginContainer = document.getElementById('loginContainer');
loginContainer.style.display = 'block';
}
}
const users = [
{
userId: '1',
name: 'James Smith',
photoUrl: '',
email: 'james.smith@gmail.com',
plan: 'free',
groupId: '',
contacts: [
{
userId: '2',
name: 'Maria Garcia',
email: 'maria.garcia@gmail.com',
},
{
userId: '3',
name: 'Sarah Wilson',
email: 'sarah.wilson@gmail.com',
}
]
},
{
userId: '2',
name: 'Maria Garcia',
photoUrl: '',
email: 'maria.garcia@gmail.com',
plan: 'paid',
groupId: '',
contacts: [
{
userId: '1',
name: 'James Smith',
email: 'james.smith@gmail.com',
},
{
userId: '3',
name: 'Sarah Wilson',
email: 'sarah.wilson@gmail.com',
}
]
},
{
userId: '3',
name: 'Sarah Wilson',
photoUrl: '',
email: 'sarah.wilson@gmail.com',
plan: 'trial',
groupId: '',
contacts: [
{
userId: '1',
name: 'James Smith',
email: 'james.smith@gmail.com',
},
{
userId: '2',
name: 'Maria Garcia',
email: 'maria.garcia@gmail.com',
}
]
}
];
async function signIn(userId) {
if (Snippyly) {
const user = users.find((user) => user.userId === userId);
if (user) {
const userDetails = document.getElementById('userDetails');
userDetails.innerText = `Hi ${user.name}`;
const userDetailsContainer = document.getElementById('userDetailsContainer');
userDetailsContainer.style.display = 'block';
const loginContainer = document.getElementById('loginContainer');
loginContainer.style.display = 'none';
localStorage.setItem('user', JSON.stringify(user));
await Snippyly.identify(user);
}
}
}
function signOut() {
localStorage.removeItem('user');
window.location.reload();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment