File size: 6,344 Bytes
b2a00c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | import { Interval } from '../stats/graph/intervals'
import { Role, UserContextValue } from '../user-context'
import {
formatDayShort,
formatTime,
is12HourClock,
parseUTCDate
} from '../util/date'
export enum AnnotationType {
personal = 'personal',
site = 'site'
}
/** This type signifies that the owner can't be shown. */
// type AnnotationOwnershipHidden = { owner_id: null; owner_name: null }
/** This type signifies that the original owner has been removed from the site. */
type AnnotationOwnershipDangling = { owner_id: null; owner_name: null }
type AnnotationOwnership =
| AnnotationOwnershipDangling
| { owner_id: number; owner_name: string }
export enum AnnotationGranularity {
date = 'date',
minute = 'minute'
}
// the DB side limit is 255, but this looks better in the UI
export const NOTE_MAX_LENGTH = 250
export type Annotation = {
datetime: string
granularity: AnnotationGranularity
type: AnnotationType
note: string
id: number
/** datetime in site timezone, example 2025-02-26 10:00:00 */
inserted_at: string
/** datetime in site timezone, example 2025-02-26 10:00:00 */
updated_at: string
} & AnnotationOwnership
export type AnnotationPayload = Pick<
Annotation,
'note' | 'datetime' | 'granularity' | 'type'
>
export const ANNOTATION_TYPE_LABELS = {
[AnnotationType.personal]: 'Personal note',
[AnnotationType.site]: 'Site note'
}
export const getAnnotationAuthorship = (
annotation: Pick<Annotation, 'type' | 'owner_name'>
): string => {
if (annotation.type === AnnotationType.site && annotation.owner_name) {
return annotation.owner_name
}
return ANNOTATION_TYPE_LABELS[annotation.type]
}
export const getAttributionDateLabel = (
annotation: Pick<Annotation, 'datetime' | 'granularity'>
): string => {
const date = parseUTCDate(annotation.datetime)
const dayLabel = formatDayShort(date)
if (annotation.granularity === AnnotationGranularity.minute) {
const time = formatTime(date, {
use12HourClock: is12HourClock(),
includeMinutes: true
})
return `${dayLabel} ${time}`
}
return dayLabel
}
/** keep in sync with Plausible.Annotations */
const ROLES_WITH_MAYBE_SITE_ANNOTATIONS = [Role.admin, Role.editor, Role.owner]
const ROLES_WITH_PERSONAL_ANNOTATIONS = [
Role.billing,
Role.viewer,
Role.admin,
Role.editor,
Role.owner
]
export function canEditAnnotation({
type,
user
}: {
type: AnnotationType
user: UserContextValue
}) {
if (!user.loggedIn) {
return false
}
switch (type) {
case AnnotationType.site:
// we don't check whether site annotations are available here:
// even when site annotations aren't available,
// the user should be able to downgrade them to personal, or delete them
return ROLES_WITH_MAYBE_SITE_ANNOTATIONS.includes(user.role)
case AnnotationType.personal:
return ROLES_WITH_PERSONAL_ANNOTATIONS.includes(user.role)
default:
return false
}
}
export function canShowAddAnnotationButton(props: {
user: UserContextValue
siteAnnotationsAvailable: boolean
}) {
return (
canAddAnnotation({ type: AnnotationType.personal, ...props }) ||
canAddAnnotation({ type: AnnotationType.site, ...props })
)
}
export function canAddAnnotation({
type,
user,
siteAnnotationsAvailable
}: {
type: AnnotationType
user: UserContextValue
siteAnnotationsAvailable: boolean
}) {
switch (type) {
case AnnotationType.site:
return siteAnnotationsAvailable && canEditAnnotation({ type, user })
case AnnotationType.personal:
return canEditAnnotation({ type, user })
default:
return false
}
}
export const getAnnotationTimeLabel = (
annotation: Pick<Annotation, 'datetime' | 'granularity'>,
interval: Interval
): string => {
const dateString = annotation.datetime.substring(0, 'YYYY-MM-DD'.length)
switch (annotation.granularity) {
case AnnotationGranularity.date: {
switch (interval) {
case Interval.month:
// floors to closest start of month for the date
return parseUTCDate(dateString).startOf('month').format('YYYY-MM-DD')
case Interval.week:
// floors to closest start of week for the date
return parseUTCDate(dateString).startOf('week').format('YYYY-MM-DD')
case Interval.day:
case Interval.hour:
case Interval.minute:
// floors to date
return dateString
}
break
}
case AnnotationGranularity.minute: {
switch (interval) {
case Interval.month:
// floors to closest start of month for the date
return parseUTCDate(dateString).startOf('month').format('YYYY-MM-DD')
case Interval.week:
// floors to closest start of week for the date
return parseUTCDate(dateString).startOf('week').format('YYYY-MM-DD')
case Interval.day:
// floors to date
return dateString
case Interval.hour: {
const [dateYYYYMMDD, timeHHMMSS] = annotation.datetime.split('T')
// floors time to hour
return `${dateYYYYMMDD} ${timeHHMMSS.substring(0, 'HH'.length)}:00:00`
}
case Interval.minute:
return annotation.datetime.split('T').join(' ')
}
}
}
}
export const groupAnnotationsByTimeLabel = <
T extends Pick<Annotation, 'datetime' | 'granularity'>
>(
annotations: T[],
interval: Interval
): Record<string, T[] | undefined> => {
return annotations.reduce<Record<string, T[]>>((acc, annotation) => {
const timeLabel = getAnnotationTimeLabel(annotation, interval)
return { ...acc, [timeLabel]: [...(acc[timeLabel] ?? []), annotation] }
}, {})
}
export const getAnnotationGranularity = (
interval: Interval
): AnnotationGranularity => {
switch (interval) {
case Interval.minute:
case Interval.hour:
return AnnotationGranularity.minute
case Interval.day:
case Interval.week:
case Interval.month:
return AnnotationGranularity.date
}
}
export const getApiFormattedPayload = ({
granularity,
datetime,
...payload
}: AnnotationPayload) => {
switch (granularity) {
case AnnotationGranularity.date:
return { date: datetime, granularity, ...payload }
case AnnotationGranularity.minute:
return { datetime, granularity, ...payload }
}
}
|