useUpdateNotification.ts
hooks/useUpdateNotification.ts
No strong subsystem tag
35
Lines
982
Bytes
3
Exports
2
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with general runtime concerns. It contains 35 lines, 2 detected imports, and 3 detected exports.
Important relationships
Detected exports
getSemverPartshouldShowUpdateNotificationuseUpdateNotification
Keywords
updatedsemverversionupdatedversiongetsemverpartlastnotifiedsemverlooseusestatemajorminorpatch
Detected imports
reactsemver
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { useState } from 'react'
import { major, minor, patch } from 'semver'
export function getSemverPart(version: string): string {
return `${major(version, { loose: true })}.${minor(version, { loose: true })}.${patch(version, { loose: true })}`
}
export function shouldShowUpdateNotification(
updatedVersion: string,
lastNotifiedSemver: string | null,
): boolean {
const updatedSemver = getSemverPart(updatedVersion)
return updatedSemver !== lastNotifiedSemver
}
export function useUpdateNotification(
updatedVersion: string | null | undefined,
initialVersion: string = MACRO.VERSION,
): string | null {
const [lastNotifiedSemver, setLastNotifiedSemver] = useState<string | null>(
() => getSemverPart(initialVersion),
)
if (!updatedVersion) {
return null
}
const updatedSemver = getSemverPart(updatedVersion)
if (updatedSemver !== lastNotifiedSemver) {
setLastNotifiedSemver(updatedSemver)
return updatedSemver
}
return null
}