// Hooks
import { useState, useEffect } from 'react'
import { useTranslation } from 'next-i18next'
import { useBackend } from 'site/hooks/useBackend.mjs'
import { useToast } from 'site/hooks/useToast.mjs'
// Dependencies
import { DateTime } from 'luxon'
import { CopyToClipboard } from 'react-copy-to-clipboard'
// Components
import { BackToAccountButton, Choice } from './shared.mjs'
import { Popout } from 'shared/components/popout.mjs'
import { WebLink } from 'shared/components/web-link.mjs'
import { Bullet } from 'site/components/bullet.mjs'
import { CopyIcon } from 'shared/components/icons.mjs'
export const ns = ['account', 'toast']
const ExpiryPicker = ({ t, expires, setExpires }) => {
const [days, setDays] = useState(true) // False = months
const [val, setVal] = useState(3)
// Run update when component mounts
useEffect(() => update(val), [])
const update = (evt) => {
const value = typeof evt === 'number' ? evt : evt.target.value
setVal(value)
const plus = {}
if (days) plus.days = value
else plus.months = value
setExpires(DateTime.now().plus(plus))
}
return (
<>
{t('keyExpiresDesc')}
{expires.toHTTP()}
>
)
}
const CopyInput = ({ text }) => {
const { t } = useTranslation(['toast'])
const toast = useToast()
const [copied, setCopied] = useState(false)
const showCopied = () => {
setCopied(true)
toast.success({t('copiedToClipboard')} )
window.setTimeout(() => setCopied(false), 3000)
}
return (
)
}
const Row = ({ title, children }) => (
)
const ShowKey = ({ apikey, t, clear }) => (
{apikey.name}
{DateTime.fromISO(apikey.createdAt).toHTTP()}
{DateTime.fromISO(apikey.expiresAt).toHTTP()}
{t('keySecretWarning')}
{t('apikeys')}
)
const NewKey = ({ app, t, setGenerate, backend, toast }) => {
const [name, setName] = useState('')
const [level, setLevel] = useState(1)
const [expires, setExpires] = useState(DateTime.now())
const [apikey, setApikey] = useState(false)
const levels = app.account.role === 'admin' ? [0, 1, 2, 3, 4, 5, 6, 7, 8] : [0, 1, 2, 3, 4]
const createKey = async () => {
app.startLoading()
const result = await backend.createApikey({
name,
level,
expiresIn: Math.floor((expires.valueOf() - DateTime.now().valueOf()) / 1000),
})
console.log({ result })
if (result.key) {
toast.success({t('nailedIt')} )
setApikey(result)
} else toast.for.backendError()
app.stopLoading()
}
const clear = () => {
setApikey(false)
setGenerate(false)
}
return (
{t('newApikey')}
{apikey ? (
<>
>
) : (
<>
{t('keyName')}
{t('keyNameDesc')}
setName(evt.target.value)}
className="input w-full input-bordered flex flex-row"
type="text"
placeholder={'Alicia key'}
/>
{t('keyExpires')}
{t('keyLevel')}
{levels.map((l) => (
{t(`keyLevel${l}`)}
))}
{t('newApikey')}
setGenerate(false)}
>
{t('cancel')}
>
)}
)
}
export const Apikeys = ({ app, title = false, welcome = false }) => {
const backend = useBackend(app)
const { t } = useTranslation(ns)
const toast = useToast()
const [keys, setKeys] = useState([])
const [generate, setGenerate] = useState(false)
//useEffect(() => {
// const getApikeys = () => {
// const allKeys = await backend.getApikeys()
// if (allKeys) setKeys(allKeys)
// }
// getApiKeys()
//}, [ ])
const save = async () => {
app.startLoading()
const result = await backend.updateAccount({ bio })
if (result === true) toast.for.settingsSaved()
else toast.for.backendError()
app.stopLoading()
}
return (
<>
{generate ? (
) : (
<>
{t('apikeys')}
setGenerate(true)}>
{t('newApikey')}
Refer to FreeSewing.dev for details (English only)
This is an advanced feature aimed at developers or anyone who wants to interact with
our backend directly. For details, please refer to{' '}
{' '}
on our site for
developers and contributors.
>
)}
>
)
}