-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow testing uuid for validity and parse uuid7 timestamp
- Loading branch information
Showing
2 changed files
with
156 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { InputAdornment, TextField } from '@mui/material'; | ||
import { isDefined } from 'app/util/util'; | ||
import moment from 'moment'; | ||
import React from 'react'; | ||
import * as uuid from 'uuid'; | ||
|
||
export function UuidCheck({ input: inputFromProps }: { input?: string }) { | ||
const [input, setInput] = React.useState(''); | ||
React.useEffect(() => { | ||
if (inputFromProps) { | ||
setInput(inputFromProps); | ||
} | ||
}, [inputFromProps]); | ||
|
||
const valid = input ? uuid.validate(input) : undefined; | ||
const version = valid ? uuid.version(input) : undefined; | ||
|
||
return ( | ||
<> | ||
<TextField | ||
fullWidth | ||
label="Tarkista UUID" | ||
variant="filled" | ||
value={input} | ||
onChange={e => setInput(e.target.value)} | ||
slotProps={{ | ||
input: { | ||
endAdornment: isDefined(valid) ? ( | ||
<InputAdornment position="end"> | ||
{valid ? '✅' : '❌'} | ||
{valid && version ? ` v${version}` : null} | ||
</InputAdornment> | ||
) : null, | ||
}, | ||
}} | ||
/> | ||
{valid && version === 7 ? <Uuid7Info input={input} /> : null} | ||
</> | ||
); | ||
} | ||
function Uuid7Info({ input }: { input: string }) { | ||
const stampStr = input.slice(0, 8) + input.slice(9, 13); | ||
const epochMillis = parseInt(stampStr, 16); | ||
const time = moment(epochMillis); | ||
return ( | ||
<> | ||
<TextField | ||
fullWidth | ||
variant="filled" | ||
label="Aikaleima (ms)" | ||
value={epochMillis} | ||
slotProps={{ | ||
input: { | ||
readOnly: true, | ||
}, | ||
}} | ||
/> | ||
<TextField | ||
fullWidth | ||
variant="filled" | ||
label="Aika" | ||
value={time.format('YYYY-MM-DD HH:mm:ss.SSSSSS Z')} | ||
slotProps={{ | ||
input: { | ||
readOnly: true, | ||
}, | ||
}} | ||
/> | ||
</> | ||
); | ||
} |