import { useNow } from './useNow'
function MyApp() {
const now = useNow({ interval: 1000 })
return (
<div>The current date is {now}</div>
)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { format } from 'date-fns' | |
| /** | |
| * Gives you a mysql standard formatted datetime | |
| * e.g., 2018-08-08 23:00:00 | |
| */ | |
| function getStandardFormattedDateTime(date: Date = new Date()) { | |
| return format(date, 'YYYY-MM-DD HH-mm-ss') | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface AppPartyLog { | |
| id: number | |
| party_id: number | |
| type: 'activity' | 'message' | |
| activity?: { | |
| id: string | |
| text: string | |
| user: AppUser | |
| } | |
| message?: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Little trick to force our component to remount entirely if the user | |
| * creates a tracker all while viewing another tracker. | |
| */ | |
| function WrappedDashboardTracker(props: Props) { | |
| return ( | |
| <DashboardTracker key={props.match.params.trackerId} {...props} /> | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as React from 'react' | |
| interface Props { | |
| interval: number | |
| children: (now: Date) => React.ReactNode | |
| } | |
| interface State { | |
| now: Date | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function composeRefs<T>(refs: React.Ref<T>[]): React.Ref<T> { | |
| return function(component: T) { | |
| refs.forEach((ref) => { | |
| if (typeof ref === 'function') { | |
| ref(component) | |
| } else if (ref && 'current' in ref) { | |
| ref.current = component | |
| } | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface RemainingTimeValue { | |
| hours: number | |
| minutes: number | |
| seconds: number | |
| } | |
| /** | |
| * Get remaining time from seconds | |
| * | |
| * Used by `toReadableTime` and `distanceInWordsAbbreivated` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import get from 'lodash.get' | |
| interface SearchMap { | |
| [key: string]: number | |
| } | |
| /** | |
| * Performantly get the original index of an item. | |
| * Use-case: Get index of item in array A in array B | |
| * e.g., Check if user (inside users array) is inside the invitations array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface SearchMap { | |
| [key: string]: boolean | |
| } | |
| /** | |
| * Make it easy to search for an item inside an array | |
| * | |
| * @input | |
| * [{ id: 5 }, { id: 6 }, { id: 7 }, { id: 32 }] | |
| * @output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Converts seconds to readable time (e.g., 03:45:24, 45:34, 34) | |
| */ | |
| export default function toReadableTime(seconds: number): string { | |
| const hh = Math.floor(seconds / 3600) | |
| const mm = Math.floor((seconds % 3600 / 60)) | |
| const ss = Math.floor(seconds % 60) | |
| if (hh >= 1) { | |
| return [hh, mm, ss].map(t => String(t).padStart(2, '0')).join(':') |