Skip to content

Instantly share code, notes, and snippets.

@covelitein
Created August 17, 2023 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save covelitein/512b9a010b44ac572bd5ba4256cd0397 to your computer and use it in GitHub Desktop.
Save covelitein/512b9a010b44ac572bd5ba4256cd0397 to your computer and use it in GitHub Desktop.
p2e
import { createGlobalState } from 'react-hooks-global-state'
const { setGlobalState, useGlobalState, getGlobalState } = createGlobalState({
connectedAccount: '',
currentUser: null,
resultModal: 'scale-0',
createModal: 'scale-0',
chatModal: 'scale-0',
inviteModal: 'scale-0',
games: [],
game: null,
group: null,
messages: [],
invitations: [],
scores: [],
myGames: [],
})
const truncate = (text, startChars, endChars, maxLength) => {
if (text.length > maxLength) {
let start = text.substring(0, startChars)
let end = text.substring(text.length - endChars, text.length)
while (start.length + end.length < maxLength) {
start = start + '.'
}
return start + end
}
return text
}
const formatDate = (timestamp) => {
const date = new Date(timestamp)
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
}
return date.toLocaleDateString(undefined, options)
}
const timestampToDate = (timestamp) => {
const date = new Date(timestamp)
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
}
return date.toLocaleDateString('en-US', options)
}
export {
setGlobalState,
useGlobalState,
getGlobalState,
truncate,
formatDate,
timestampToDate,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment