Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
d51de554d6 | |||
a068d31d14 | |||
3d185bf044 | |||
6cfee0f6a3 | |||
66f3e0475f | |||
dee8cc2c41 | |||
91a87199c4 | |||
38e227a9d4 | |||
c4bb7c9b17 | |||
88f1cedcdc | |||
c75dc80f82 | |||
351834633f |
1
.gitignore
vendored
1
.gitignore
vendored
@ -49,3 +49,4 @@ npm-debug.log*
|
||||
.DS_Store
|
||||
.lighthouseci
|
||||
.vercel
|
||||
*.hbs
|
||||
|
@ -1,7 +0,0 @@
|
||||
.next
|
||||
.lighthouseci
|
||||
coverage
|
||||
node_modules
|
||||
**/workbox-*.js
|
||||
**/sw.js
|
||||
*.hbs
|
@ -18,7 +18,7 @@
|
||||
|
||||
Thream's website to stay close with your friends and communities.
|
||||
|
||||
It uses [Thream/api](https://github.com/Thream/api) [v1.1.0](https://github.com/Thream/api/releases/tag/v1.1.0).
|
||||
It uses [Thream/api](https://github.com/Thream/api) [v1.2.0](https://github.com/Thream/api/releases/tag/v1.2.0).
|
||||
|
||||
## ⚙️ Getting Started
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import Image from 'next/image'
|
||||
import { PlusIcon, MenuIcon, UsersIcon, XIcon } from '@heroicons/react/solid'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
import { useMediaQuery } from 'react-responsive'
|
||||
import { useSwipeable } from 'react-swipeable'
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { useRouter } from 'next/router'
|
||||
import { useState } from 'react'
|
||||
import { Form } from 'react-component-form'
|
||||
import { Form, useForm } from 'react-component-form'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
import axios from 'axios'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
import { HandleSubmitCallback, useForm } from '../../../hooks/useForm'
|
||||
import { FormState } from '../../design/FormState'
|
||||
import { useGuildMember } from '../../../contexts/GuildMember'
|
||||
import { Input } from '../../design/Input'
|
||||
@ -17,6 +17,11 @@ import {
|
||||
ChannelWithDefaultChannelId
|
||||
} from '../../../models/Channel'
|
||||
import { ConfirmPopup } from '../ConfirmPopup'
|
||||
import { useFormTranslation } from '../../../hooks/useFormTranslation'
|
||||
|
||||
const schema = {
|
||||
name: channelSchema.name
|
||||
}
|
||||
|
||||
export interface ChannelSettingsProps {
|
||||
channel: Channel
|
||||
@ -41,25 +46,19 @@ export const ChannelSettings: React.FC<ChannelSettingsProps> = (props) => {
|
||||
}
|
||||
|
||||
const {
|
||||
handleUseForm,
|
||||
fetchState,
|
||||
message,
|
||||
errors,
|
||||
getErrorTranslation,
|
||||
handleSubmit,
|
||||
setFetchState,
|
||||
setMessageTranslationKey
|
||||
} = useForm({
|
||||
validateSchema: {
|
||||
name: channelSchema.name
|
||||
},
|
||||
replaceEmptyStringToNull: true,
|
||||
resetOnSuccess: false
|
||||
})
|
||||
setMessage
|
||||
} = useForm(schema)
|
||||
const { getFirstErrorTranslation } = useFormTranslation()
|
||||
|
||||
const onSubmit: HandleSubmitCallback = async (formData) => {
|
||||
const onSubmit: HandleUseFormCallback<typeof schema> = async (formData) => {
|
||||
try {
|
||||
await authentication.api.put(`/channels/${channel.id}`, formData)
|
||||
setInputValues(formData as any)
|
||||
setInputValues(formData)
|
||||
await router.push(`/application/${guild.id}/${channel.id}`)
|
||||
return null
|
||||
} catch (error) {
|
||||
@ -91,9 +90,9 @@ export const ChannelSettings: React.FC<ChannelSettingsProps> = (props) => {
|
||||
} catch (error) {
|
||||
setFetchState('error')
|
||||
if (axios.isAxiosError(error) && error.response?.status === 400) {
|
||||
setMessageTranslationKey('application:delete-channel-only-one')
|
||||
setMessage('application:delete-channel-only-one')
|
||||
} else {
|
||||
setMessageTranslationKey('errors:server-error')
|
||||
setMessage('errors:server-error')
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -101,7 +100,7 @@ export const ChannelSettings: React.FC<ChannelSettingsProps> = (props) => {
|
||||
return (
|
||||
<>
|
||||
<Form
|
||||
onSubmit={handleSubmit(onSubmit)}
|
||||
onSubmit={handleUseForm(onSubmit)}
|
||||
className='my-auto flex flex-col items-center justify-center py-12'
|
||||
>
|
||||
<div className='flex w-full flex-col items-center justify-center sm:w-fit lg:flex-row'>
|
||||
@ -114,7 +113,7 @@ export const ChannelSettings: React.FC<ChannelSettingsProps> = (props) => {
|
||||
className='!mt-0'
|
||||
onChange={onChange}
|
||||
value={inputValues.name}
|
||||
error={getErrorTranslation(errors.name)}
|
||||
error={getFirstErrorTranslation(errors.name)}
|
||||
data-cy='channel-name-input'
|
||||
/>
|
||||
</div>
|
||||
|
@ -1,4 +1,5 @@
|
||||
import classNames from 'classnames'
|
||||
import { memo } from 'react'
|
||||
import classNames from 'clsx'
|
||||
import Link from 'next/link'
|
||||
import { useRouter } from 'next/router'
|
||||
import { CogIcon } from '@heroicons/react/solid'
|
||||
@ -14,7 +15,7 @@ export interface ChannelProps {
|
||||
selected?: boolean
|
||||
}
|
||||
|
||||
export const Channel: React.FC<ChannelProps> = (props) => {
|
||||
const ChannelMemo: React.FC<ChannelProps> = (props) => {
|
||||
const { channel, path, selected = false } = props
|
||||
const router = useRouter()
|
||||
|
||||
@ -53,3 +54,5 @@ export const Channel: React.FC<ChannelProps> = (props) => {
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
|
||||
export const Channel = memo(ChannelMemo)
|
||||
|
@ -1,7 +1,7 @@
|
||||
import Image from 'next/image'
|
||||
import { useState } from 'react'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
import { Loader } from '../../design/Loader'
|
||||
|
||||
|
@ -1,32 +1,32 @@
|
||||
import { useRouter } from 'next/router'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import { Form } from 'react-component-form'
|
||||
import { Form, useForm } from 'react-component-form'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
import { useAuthentication } from '../../../tools/authentication'
|
||||
import { HandleSubmitCallback, useForm } from '../../../hooks/useForm'
|
||||
import { Input } from '../../design/Input'
|
||||
import { Main } from '../../design/Main'
|
||||
import { Button } from '../../design/Button'
|
||||
import { FormState } from '../../design/FormState'
|
||||
import { Channel, channelSchema } from '../../../models/Channel'
|
||||
import { useGuildMember } from '../../../contexts/GuildMember'
|
||||
import { useFormTranslation } from '../../../hooks/useFormTranslation'
|
||||
|
||||
const schema = {
|
||||
name: channelSchema.name
|
||||
}
|
||||
|
||||
export const CreateChannel: React.FC = () => {
|
||||
const { t } = useTranslation()
|
||||
const router = useRouter()
|
||||
const { guild } = useGuildMember()
|
||||
|
||||
const { fetchState, message, errors, getErrorTranslation, handleSubmit } =
|
||||
useForm({
|
||||
validateSchema: {
|
||||
name: channelSchema.name
|
||||
},
|
||||
resetOnSuccess: true
|
||||
})
|
||||
const { handleUseForm, fetchState, message, errors } = useForm(schema)
|
||||
const { getFirstErrorTranslation } = useFormTranslation()
|
||||
|
||||
const { authentication } = useAuthentication()
|
||||
|
||||
const onSubmit: HandleSubmitCallback = async (formData) => {
|
||||
const onSubmit: HandleUseFormCallback<typeof schema> = async (formData) => {
|
||||
try {
|
||||
const { data: channel } = await authentication.api.post<Channel>(
|
||||
`/guilds/${guild.id}/channels`,
|
||||
@ -44,13 +44,13 @@ export const CreateChannel: React.FC = () => {
|
||||
|
||||
return (
|
||||
<Main>
|
||||
<Form className='w-4/6 max-w-xs' onSubmit={handleSubmit(onSubmit)}>
|
||||
<Form className='w-4/6 max-w-xs' onSubmit={handleUseForm(onSubmit)}>
|
||||
<Input
|
||||
type='text'
|
||||
placeholder={t('common:name')}
|
||||
name='name'
|
||||
label={t('common:name')}
|
||||
error={getErrorTranslation(errors.name)}
|
||||
error={getFirstErrorTranslation(errors.name)}
|
||||
data-cy='channel-name-input'
|
||||
/>
|
||||
<Button
|
||||
|
@ -1,33 +1,33 @@
|
||||
import { useRouter } from 'next/router'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import { Form } from 'react-component-form'
|
||||
import { Form, useForm } from 'react-component-form'
|
||||
import { AxiosResponse } from 'axios'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
import { useAuthentication } from '../../../tools/authentication'
|
||||
import { HandleSubmitCallback, useForm } from '../../../hooks/useForm'
|
||||
import { GuildComplete, guildSchema } from '../../../models/Guild'
|
||||
import { Input } from '../../design/Input'
|
||||
import { Main } from '../../design/Main'
|
||||
import { Button } from '../../design/Button'
|
||||
import { FormState } from '../../design/FormState'
|
||||
import { Textarea } from '../../design/Textarea'
|
||||
import { useFormTranslation } from '../../../hooks/useFormTranslation'
|
||||
|
||||
const schema = {
|
||||
name: guildSchema.name,
|
||||
description: guildSchema.description
|
||||
}
|
||||
|
||||
export const CreateGuild: React.FC = () => {
|
||||
const { t } = useTranslation()
|
||||
const router = useRouter()
|
||||
|
||||
const { fetchState, message, errors, getErrorTranslation, handleSubmit } =
|
||||
useForm({
|
||||
validateSchema: {
|
||||
name: guildSchema.name,
|
||||
description: guildSchema.description
|
||||
},
|
||||
resetOnSuccess: true
|
||||
})
|
||||
const { handleUseForm, fetchState, message, errors } = useForm(schema as any)
|
||||
const { getFirstErrorTranslation } = useFormTranslation()
|
||||
|
||||
const { authentication } = useAuthentication()
|
||||
|
||||
const onSubmit: HandleSubmitCallback = async (formData) => {
|
||||
const onSubmit: HandleUseFormCallback<any> = async (formData) => {
|
||||
try {
|
||||
const { data } = await authentication.api.post<
|
||||
any,
|
||||
@ -47,13 +47,13 @@ export const CreateGuild: React.FC = () => {
|
||||
|
||||
return (
|
||||
<Main>
|
||||
<Form className='w-4/6 max-w-xs' onSubmit={handleSubmit(onSubmit)}>
|
||||
<Form className='w-4/6 max-w-xs' onSubmit={handleUseForm(onSubmit)}>
|
||||
<Input
|
||||
type='text'
|
||||
placeholder={t('common:name')}
|
||||
name='name'
|
||||
label={t('common:name')}
|
||||
error={getErrorTranslation(errors.name)}
|
||||
error={getFirstErrorTranslation(errors.name)}
|
||||
/>
|
||||
<Textarea
|
||||
label='Description'
|
||||
@ -64,7 +64,11 @@ export const CreateGuild: React.FC = () => {
|
||||
{t('application:create')}
|
||||
</Button>
|
||||
</Form>
|
||||
<FormState id='message' state={fetchState} message={message} />
|
||||
<FormState
|
||||
id='message'
|
||||
state={fetchState}
|
||||
message={message != null ? t(message) : undefined}
|
||||
/>
|
||||
</Main>
|
||||
)
|
||||
}
|
||||
|
@ -3,11 +3,11 @@ import { useRouter } from 'next/router'
|
||||
import { useState } from 'react'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { PhotographIcon } from '@heroicons/react/solid'
|
||||
import { Form } from 'react-component-form'
|
||||
import { Form, useForm } from 'react-component-form'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
import { HandleSubmitCallback, useForm } from '../../../hooks/useForm'
|
||||
import { guildSchema } from '../../../models/Guild'
|
||||
import { FormState } from '../../design/FormState'
|
||||
import { useGuildMember } from '../../../contexts/GuildMember'
|
||||
@ -16,6 +16,12 @@ import { Input } from '../../design/Input'
|
||||
import { Button } from '../../design/Button'
|
||||
import { useAuthentication } from '../../../tools/authentication'
|
||||
import { ConfirmPopup } from '../ConfirmPopup'
|
||||
import { useFormTranslation } from '../../../hooks/useFormTranslation'
|
||||
|
||||
const schema = {
|
||||
name: guildSchema.name,
|
||||
description: Type.Optional(guildSchema.description)
|
||||
}
|
||||
|
||||
export const GuildSettings: React.FC = () => {
|
||||
const { t } = useTranslation()
|
||||
@ -35,26 +41,19 @@ export const GuildSettings: React.FC = () => {
|
||||
}
|
||||
|
||||
const {
|
||||
handleUseForm,
|
||||
fetchState,
|
||||
message,
|
||||
errors,
|
||||
getErrorTranslation,
|
||||
handleSubmit,
|
||||
setFetchState,
|
||||
setMessageTranslationKey
|
||||
} = useForm({
|
||||
validateSchema: {
|
||||
name: guildSchema.name,
|
||||
description: Type.Optional(guildSchema.description)
|
||||
},
|
||||
replaceEmptyStringToNull: true,
|
||||
resetOnSuccess: false
|
||||
})
|
||||
setMessage
|
||||
} = useForm(schema as any)
|
||||
const { getFirstErrorTranslation } = useFormTranslation()
|
||||
|
||||
const onSubmit: HandleSubmitCallback = async (formData) => {
|
||||
const onSubmit: HandleUseFormCallback<any> = async (formData) => {
|
||||
try {
|
||||
await authentication.api.put(`/guilds/${guild.id}`, formData)
|
||||
setInputValues(formData as any)
|
||||
setInputValues(formData as unknown as any)
|
||||
return {
|
||||
type: 'success',
|
||||
value: 'application:saved-information'
|
||||
@ -92,7 +91,7 @@ export const GuildSettings: React.FC = () => {
|
||||
setFetchState('idle')
|
||||
} catch (error) {
|
||||
setFetchState('error')
|
||||
setMessageTranslationKey('errors:server-error')
|
||||
setMessage('errors:server-error')
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -102,7 +101,7 @@ export const GuildSettings: React.FC = () => {
|
||||
await authentication.api.delete(`/guilds/${guild.id}`)
|
||||
} catch (error) {
|
||||
setFetchState('error')
|
||||
setMessageTranslationKey('errors:server-error')
|
||||
setMessage('errors:server-error')
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,14 +111,14 @@ export const GuildSettings: React.FC = () => {
|
||||
await router.push('/application')
|
||||
} catch (error) {
|
||||
setFetchState('error')
|
||||
setMessageTranslationKey('errors:server-error')
|
||||
setMessage('errors:server-error')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Form
|
||||
onSubmit={handleSubmit(onSubmit)}
|
||||
onSubmit={handleUseForm(onSubmit)}
|
||||
className='my-auto flex flex-col items-center justify-center py-12'
|
||||
>
|
||||
{member.isOwner && (
|
||||
@ -160,7 +159,7 @@ export const GuildSettings: React.FC = () => {
|
||||
className='!mt-0'
|
||||
onChange={onChange}
|
||||
value={inputValues.name}
|
||||
error={getErrorTranslation(errors.name)}
|
||||
error={getFirstErrorTranslation(errors.name)}
|
||||
data-cy='guild-name-input'
|
||||
/>
|
||||
<Textarea
|
||||
@ -204,7 +203,11 @@ export const GuildSettings: React.FC = () => {
|
||||
</div>
|
||||
<FormState
|
||||
state={fetchState}
|
||||
message={getErrorTranslation(errors.description) ?? message}
|
||||
message={
|
||||
message != null
|
||||
? t(message)
|
||||
: getFirstErrorTranslation(errors.email)
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</Form>
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { memo } from 'react'
|
||||
import Image from 'next/image'
|
||||
|
||||
import { GuildWithDefaultChannelId } from '../../../models/Guild'
|
||||
@ -8,7 +9,7 @@ export interface GuildProps {
|
||||
selected?: boolean
|
||||
}
|
||||
|
||||
export const Guild: React.FC<GuildProps> = (props) => {
|
||||
const GuildMemo: React.FC<GuildProps> = (props) => {
|
||||
const { guild, selected } = props
|
||||
|
||||
return (
|
||||
@ -34,3 +35,5 @@ export const Guild: React.FC<GuildProps> = (props) => {
|
||||
</IconLink>
|
||||
)
|
||||
}
|
||||
|
||||
export const Guild = memo(GuildMemo)
|
||||
|
@ -2,7 +2,7 @@ import Image from 'next/image'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useState } from 'react'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
import axios from 'axios'
|
||||
|
||||
import { Emoji } from '../../Emoji'
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { memo } from 'react'
|
||||
import Image from 'next/image'
|
||||
import Link from 'next/link'
|
||||
|
||||
@ -8,7 +9,7 @@ export interface MemberProps {
|
||||
member: MemberWithPublicUser
|
||||
}
|
||||
|
||||
export const Member: React.FC<MemberProps> = (props) => {
|
||||
const MemberMemo: React.FC<MemberProps> = (props) => {
|
||||
const { member } = props
|
||||
|
||||
return (
|
||||
@ -45,3 +46,5 @@ export const Member: React.FC<MemberProps> = (props) => {
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
|
||||
export const Member = memo(MemberMemo)
|
||||
|
@ -1,11 +1,16 @@
|
||||
import { useState, useRef } from 'react'
|
||||
import Image from 'next/image'
|
||||
import Link from 'next/link'
|
||||
import date from 'date-and-time'
|
||||
import TextareaAutosize from 'react-textarea-autosize'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
|
||||
import { MessageWithMember } from '../../../../models/Message'
|
||||
import { MessageText } from './MessageText'
|
||||
import { Loader } from '../../../design/Loader'
|
||||
import { MessageFile } from './MessageFile'
|
||||
import { Emoji } from '../../../Emoji'
|
||||
import { useAuthentication } from '../../../../tools/authentication'
|
||||
|
||||
export interface MessageProps {
|
||||
message: MessageWithMember
|
||||
@ -14,9 +19,60 @@ export interface MessageProps {
|
||||
export const Message: React.FC<MessageProps> = (props) => {
|
||||
const { message } = props
|
||||
|
||||
const textareaReference = useRef<HTMLTextAreaElement>(null)
|
||||
const [isEditing, setIsEditing] = useState(false)
|
||||
const { t } = useTranslation()
|
||||
const { authentication, user } = useAuthentication()
|
||||
|
||||
const handleDeleteMessage = async (): Promise<void> => {
|
||||
try {
|
||||
await authentication.api.delete(`/messages/${message.id}`)
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const handleTextareaKeyDown: React.KeyboardEventHandler<HTMLFormElement> = (
|
||||
event
|
||||
) => {
|
||||
if (event.key === 'Enter' && !event.shiftKey) {
|
||||
event.preventDefault()
|
||||
event.currentTarget.dispatchEvent(
|
||||
new Event('submit', { cancelable: true, bubbles: true })
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const handleEdit = async (): Promise<void> => {
|
||||
const newMessage = textareaReference.current?.value ?? message.value
|
||||
if (
|
||||
typeof newMessage === 'string' &&
|
||||
newMessage.length > 0 &&
|
||||
newMessage !== message.value
|
||||
) {
|
||||
try {
|
||||
await authentication.api.put(`/messages/${message.id}`, {
|
||||
value: newMessage
|
||||
})
|
||||
} catch {}
|
||||
}
|
||||
handleEditMode()
|
||||
}
|
||||
|
||||
const handleEditSubmit: React.FormEventHandler<HTMLFormElement> = async (
|
||||
event
|
||||
) => {
|
||||
event.preventDefault()
|
||||
await handleEdit()
|
||||
}
|
||||
|
||||
const handleEditMode = (): void => {
|
||||
setIsEditing((oldIsEditing) => {
|
||||
return !oldIsEditing
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className='flex p-4 transition hover:bg-gray-200 dark:hover:bg-gray-900'
|
||||
className='group flex w-full p-4 transition hover:bg-gray-200 dark:hover:bg-gray-900'
|
||||
data-cy={`message-${message.id}`}
|
||||
>
|
||||
<Link href={`/application/users/${message.member.user.id}`}>
|
||||
@ -40,7 +96,7 @@ export const Message: React.FC<MessageProps> = (props) => {
|
||||
</div>
|
||||
</a>
|
||||
</Link>
|
||||
<div className='w-full'>
|
||||
<div className='relative w-full whitespace-pre-wrap break-words break-all'>
|
||||
<div className='flex w-max items-center'>
|
||||
<Link href={`/application/users/${message.member.user.id}`}>
|
||||
<a>
|
||||
@ -59,8 +115,58 @@ export const Message: React.FC<MessageProps> = (props) => {
|
||||
{date.format(new Date(message.createdAt), 'DD/MM/YYYY - HH:mm:ss')}
|
||||
</span>
|
||||
</div>
|
||||
{message.member.userId === user.id ? (
|
||||
<div className='absolute right-6 -top-8 flex opacity-0 transition-opacity group-hover:opacity-100'>
|
||||
{message.type === 'text' ? (
|
||||
<div
|
||||
className='message-options rounded-l-lg border-l-slate-600'
|
||||
title={t('application:edit')}
|
||||
onClick={isEditing ? handleEdit : handleEditMode}
|
||||
>
|
||||
<Emoji
|
||||
value={isEditing ? ':white_check_mark:' : ':pencil2:'}
|
||||
size={18}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
<div
|
||||
className='message-options rounded-r-lg border-r-slate-600'
|
||||
title={t('application:delete')}
|
||||
onClick={handleDeleteMessage}
|
||||
>
|
||||
<Emoji value=':wastebasket:' size={18} />
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{message.type === 'text' ? (
|
||||
<MessageText message={message} />
|
||||
<>
|
||||
{isEditing ? (
|
||||
<form
|
||||
className='flex h-full w-full items-center'
|
||||
onSubmit={handleEditSubmit}
|
||||
onKeyDown={handleTextareaKeyDown}
|
||||
>
|
||||
<TextareaAutosize
|
||||
className='scrollbar-firefox-support w-full resize-none bg-transparent p-2 tracking-wide outline-none'
|
||||
placeholder={t('application:write-a-message')}
|
||||
wrap='soft'
|
||||
maxRows={6}
|
||||
name='message'
|
||||
defaultValue={message.value}
|
||||
ref={textareaReference}
|
||||
autoFocus
|
||||
onFocus={(event) => {
|
||||
event.currentTarget.setSelectionRange(
|
||||
event.currentTarget.value.length,
|
||||
event.currentTarget.value.length
|
||||
)
|
||||
}}
|
||||
/>
|
||||
</form>
|
||||
) : (
|
||||
<MessageText message={message} />
|
||||
)}
|
||||
</>
|
||||
) : message.type === 'file' ? (
|
||||
<MessageFile message={message} />
|
||||
) : (
|
||||
|
@ -27,7 +27,7 @@ export const MessageText: React.FC<MessageContentProps> = (props) => {
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
<Emoji value={message.value} size={40} />
|
||||
<Emoji value={message.value} size={40} tooltip />
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
@ -50,7 +50,7 @@ export const MessageText: React.FC<MessageContentProps> = (props) => {
|
||||
)
|
||||
},
|
||||
emoji: (props) => {
|
||||
return <Emoji value={props.value} size={20} />
|
||||
return <Emoji value={props.value} size={20} tooltip />
|
||||
},
|
||||
code: (properties) => {
|
||||
const { inline, className, children, ...props } = properties
|
||||
|
@ -1,6 +1,6 @@
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import { PlusSmIcon, ArrowDownIcon } from '@heroicons/react/solid'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
import Image from 'next/image'
|
||||
|
||||
import { PopupGuildCard } from './PopupGuildCard'
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { useState, useRef } from 'react'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import TextareaAutosize from 'react-textarea-autosize'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
import { GuildsChannelsPath } from '..'
|
||||
import { useAuthentication } from '../../../tools/authentication'
|
||||
@ -96,7 +96,7 @@ export const SendMessage: React.FC<SendMessageProps> = (props) => {
|
||||
onKeyDown={handleTextareaKeyDown}
|
||||
>
|
||||
<TextareaAutosize
|
||||
className='scrollbar-firefox-support my-2 w-full resize-none bg-transparent p-2 px-6 font-paragraph tracking-wide outline-none'
|
||||
className='scrollbar-firefox-support my-2 w-full resize-none bg-transparent p-2 px-6 tracking-wide outline-none'
|
||||
placeholder={t('application:write-a-message')}
|
||||
wrap='soft'
|
||||
maxRows={6}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
import { ApplicationProps } from '..'
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
import Image from 'next/image'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import { useState, useMemo } from 'react'
|
||||
import { Form } from 'react-component-form'
|
||||
import { Form, useForm } from 'react-component-form'
|
||||
import { EyeIcon, PhotographIcon } from '@heroicons/react/solid'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import axios from 'axios'
|
||||
import Link from 'next/link'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
import { Input } from '../../design/Input'
|
||||
import { Checkbox } from '../../design/Checkbox'
|
||||
@ -16,10 +17,20 @@ import { Language } from '../../Header/Language'
|
||||
import { useAuthentication } from '../../../tools/authentication'
|
||||
import { Button } from '../../design/Button'
|
||||
import { FormState } from '../../design/FormState'
|
||||
import { useForm, HandleSubmitCallback } from '../../../hooks/useForm'
|
||||
import { userSchema } from '../../../models/User'
|
||||
import { userSettingsSchema } from '../../../models/UserSettings'
|
||||
import { ProviderOAuth, providers } from '../../../models/OAuth'
|
||||
import { useFormTranslation } from '../../../hooks/useFormTranslation'
|
||||
|
||||
const schema = {
|
||||
name: userSchema.name,
|
||||
status: Type.Optional(userSchema.status),
|
||||
email: Type.Optional(Type.Union([userSchema.email, Type.Null()])),
|
||||
website: Type.Optional(userSchema.website),
|
||||
biography: Type.Optional(userSchema.biography),
|
||||
isPublicGuilds: userSettingsSchema.isPublicGuilds,
|
||||
isPublicEmail: userSettingsSchema.isPublicEmail
|
||||
}
|
||||
|
||||
export const UserSettings: React.FC = () => {
|
||||
const { user, setUser, authentication } = useAuthentication()
|
||||
@ -35,32 +46,20 @@ export const UserSettings: React.FC = () => {
|
||||
})
|
||||
|
||||
const {
|
||||
handleUseForm,
|
||||
fetchState,
|
||||
setFetchState,
|
||||
message,
|
||||
setMessageTranslationKey,
|
||||
errors,
|
||||
getErrorTranslation,
|
||||
handleSubmit
|
||||
} = useForm({
|
||||
validateSchema: {
|
||||
name: userSchema.name,
|
||||
status: Type.Optional(userSchema.status),
|
||||
email: Type.Optional(Type.Union([userSchema.email, Type.Null()])),
|
||||
website: Type.Optional(userSchema.website),
|
||||
biography: Type.Optional(userSchema.biography),
|
||||
isPublicGuilds: userSettingsSchema.isPublicGuilds,
|
||||
isPublicEmail: userSettingsSchema.isPublicEmail
|
||||
},
|
||||
replaceEmptyStringToNull: true,
|
||||
resetOnSuccess: false
|
||||
})
|
||||
setMessage,
|
||||
errors
|
||||
} = useForm(schema)
|
||||
const { getFirstErrorTranslation } = useFormTranslation()
|
||||
|
||||
const hasAllProviders = useMemo(() => {
|
||||
return providers.every((provider) => user.strategies.includes(provider))
|
||||
}, [user.strategies])
|
||||
|
||||
const onSubmit: HandleSubmitCallback = async (formData) => {
|
||||
const onSubmit: HandleUseFormCallback<typeof schema> = async (formData) => {
|
||||
try {
|
||||
const { isPublicGuilds, isPublicEmail, ...userData } = formData
|
||||
const userSettings = { isPublicEmail, isPublicGuilds }
|
||||
@ -68,7 +67,7 @@ export const UserSettings: React.FC = () => {
|
||||
`/users/current?redirectURI=${window.location.origin}/authentication/signin`,
|
||||
userData
|
||||
)
|
||||
setInputValues(formData as any)
|
||||
setInputValues(formData as unknown as any)
|
||||
const hasEmailChanged = user.email !== userCurrentData.user.email
|
||||
if (hasEmailChanged) {
|
||||
return {
|
||||
@ -165,19 +164,19 @@ export const UserSettings: React.FC = () => {
|
||||
setFetchState('idle')
|
||||
} catch (error) {
|
||||
setFetchState('error')
|
||||
setMessageTranslationKey('errors:server-error')
|
||||
setMessage('errors:server-error')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleSignout = async (): Promise<void> => {
|
||||
try {
|
||||
setFetchState('loading')
|
||||
await authentication.signoutServerSide()
|
||||
} catch (error) {
|
||||
setFetchState('error')
|
||||
setMessageTranslationKey('errors:server-error')
|
||||
}
|
||||
setFetchState('loading')
|
||||
await authentication.signoutServerSide()
|
||||
}
|
||||
|
||||
const handleSignoutAllDevices = async (): Promise<void> => {
|
||||
setFetchState('loading')
|
||||
await authentication.signoutAllDevicesServerSide()
|
||||
}
|
||||
|
||||
const handleDeletionProvider = (
|
||||
@ -195,10 +194,10 @@ export const UserSettings: React.FC = () => {
|
||||
)
|
||||
}
|
||||
})
|
||||
setMessageTranslationKey('application:success-deleted-provider')
|
||||
setMessage('application:success-deleted-provider')
|
||||
} catch (error) {
|
||||
setFetchState('error')
|
||||
setMessageTranslationKey('errors:server-error')
|
||||
setMessage('errors:server-error')
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -217,7 +216,7 @@ export const UserSettings: React.FC = () => {
|
||||
|
||||
return (
|
||||
<Form
|
||||
onSubmit={handleSubmit(onSubmit)}
|
||||
onSubmit={handleUseForm(onSubmit)}
|
||||
className='my-auto flex flex-col items-center justify-center py-12 lg:min-w-[875px]'
|
||||
>
|
||||
<div className='flex w-full flex-col items-center justify-center sm:w-fit lg:flex-row'>
|
||||
@ -257,7 +256,7 @@ export const UserSettings: React.FC = () => {
|
||||
className='!mt-0'
|
||||
onChange={onChange}
|
||||
value={inputValues.name ?? ''}
|
||||
error={getErrorTranslation(errors.name)}
|
||||
error={getFirstErrorTranslation(errors.name)}
|
||||
/>
|
||||
<Input
|
||||
name='status'
|
||||
@ -266,7 +265,7 @@ export const UserSettings: React.FC = () => {
|
||||
className='!mt-4'
|
||||
onChange={onChange}
|
||||
value={inputValues.status ?? ''}
|
||||
error={getErrorTranslation(errors.status)}
|
||||
error={getFirstErrorTranslation(errors.status)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@ -279,7 +278,7 @@ export const UserSettings: React.FC = () => {
|
||||
placeholder='Email'
|
||||
onChange={onChange}
|
||||
value={inputValues.email ?? ''}
|
||||
error={getErrorTranslation(errors.email)}
|
||||
error={getFirstErrorTranslation(errors.email)}
|
||||
/>
|
||||
<Checkbox
|
||||
name='isPublicEmail'
|
||||
@ -294,7 +293,7 @@ export const UserSettings: React.FC = () => {
|
||||
placeholder={t('application:website')}
|
||||
onChange={onChange}
|
||||
value={inputValues.website ?? ''}
|
||||
error={getErrorTranslation(errors.website)}
|
||||
error={getFirstErrorTranslation(errors.website)}
|
||||
/>
|
||||
<Textarea
|
||||
name='biography'
|
||||
@ -376,7 +375,15 @@ export const UserSettings: React.FC = () => {
|
||||
{t('application:signout')}
|
||||
</Button>
|
||||
</div>
|
||||
<FormState state={fetchState} message={message} />
|
||||
<div className='mt-4'>
|
||||
<Button type='button' color='red' onClick={handleSignoutAllDevices}>
|
||||
{t('application:signout-all-devices')}
|
||||
</Button>
|
||||
</div>
|
||||
<FormState
|
||||
state={fetchState}
|
||||
message={message != null ? t(message) : undefined}
|
||||
/>
|
||||
</div>
|
||||
</Form>
|
||||
)
|
||||
|
@ -1,8 +1,11 @@
|
||||
import { useMemo } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import Link from 'next/link'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import { useTheme } from 'next-themes'
|
||||
import axios from 'axios'
|
||||
import { useForm } from 'react-component-form'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
import { Main } from '../design/Main'
|
||||
import { Input } from '../design/Input'
|
||||
@ -15,8 +18,8 @@ import {
|
||||
Tokens,
|
||||
Authentication as AuthenticationClass
|
||||
} from '../../tools/authentication'
|
||||
import { useForm, HandleSubmitCallback } from '../../hooks/useForm'
|
||||
import { AuthenticationSocialMedia } from './AuthenticationSocialMedia'
|
||||
import { useFormTranslation } from '../../hooks/useFormTranslation'
|
||||
|
||||
export interface AuthenticationProps {
|
||||
mode: 'signup' | 'signin'
|
||||
@ -29,23 +32,28 @@ export const Authentication: React.FC<AuthenticationProps> = (props) => {
|
||||
const { lang, t } = useTranslation()
|
||||
const { theme } = useTheme()
|
||||
|
||||
const { errors, fetchState, message, getErrorTranslation, handleSubmit } =
|
||||
useForm({
|
||||
validateSchema: {
|
||||
...(mode === 'signup' && { name: userSchema.name }),
|
||||
email: userSchema.email,
|
||||
password: userSchema.password
|
||||
},
|
||||
resetOnSuccess: true
|
||||
})
|
||||
const schema = useMemo(() => {
|
||||
return {
|
||||
...(mode === 'signup' && { name: userSchema.name }),
|
||||
email: userSchema.email,
|
||||
password: userSchema.password
|
||||
}
|
||||
}, [mode])
|
||||
|
||||
const onSubmit: HandleSubmitCallback = async (formData) => {
|
||||
const { handleUseForm, errors, fetchState, message } = useForm(schema)
|
||||
const { getFirstErrorTranslation } = useFormTranslation()
|
||||
|
||||
const onSubmit: HandleUseFormCallback<typeof schema> = async (
|
||||
formData,
|
||||
formElement
|
||||
) => {
|
||||
if (mode === 'signup') {
|
||||
try {
|
||||
await api.post(
|
||||
`/users/signup?redirectURI=${window.location.origin}/authentication/signin`,
|
||||
{ ...formData, language: lang, theme }
|
||||
)
|
||||
formElement.reset()
|
||||
return {
|
||||
type: 'success',
|
||||
value: 'authentication:success-signup'
|
||||
@ -97,14 +105,14 @@ export const Authentication: React.FC<AuthenticationProps> = (props) => {
|
||||
<div className='pt-8 text-center font-paragraph text-lg'>
|
||||
{t('authentication:or')}
|
||||
</div>
|
||||
<AuthenticationForm onSubmit={handleSubmit(onSubmit)}>
|
||||
<AuthenticationForm onSubmit={handleUseForm(onSubmit)}>
|
||||
{mode === 'signup' && (
|
||||
<Input
|
||||
type='text'
|
||||
placeholder={t('common:name')}
|
||||
name='name'
|
||||
label={t('common:name')}
|
||||
error={getErrorTranslation(errors.name)}
|
||||
error={getFirstErrorTranslation(errors.name)}
|
||||
/>
|
||||
)}
|
||||
<Input
|
||||
@ -112,7 +120,7 @@ export const Authentication: React.FC<AuthenticationProps> = (props) => {
|
||||
placeholder='Email'
|
||||
name='email'
|
||||
label='Email'
|
||||
error={getErrorTranslation(errors.email)}
|
||||
error={getFirstErrorTranslation(errors.email)}
|
||||
/>
|
||||
<Input
|
||||
type='password'
|
||||
@ -120,7 +128,7 @@ export const Authentication: React.FC<AuthenticationProps> = (props) => {
|
||||
name='password'
|
||||
label={t('authentication:password')}
|
||||
showForgotPassword={mode === 'signin'}
|
||||
error={getErrorTranslation(errors.password)}
|
||||
error={getFirstErrorTranslation(errors.password)}
|
||||
/>
|
||||
<Button data-cy='submit' className='mt-6 w-full' type='submit'>
|
||||
{t('authentication:submit')}
|
||||
@ -141,7 +149,11 @@ export const Authentication: React.FC<AuthenticationProps> = (props) => {
|
||||
</Link>
|
||||
</p>
|
||||
</AuthenticationForm>
|
||||
<FormState id='message' state={fetchState} message={message} />
|
||||
<FormState
|
||||
id='message'
|
||||
state={fetchState}
|
||||
message={message != null ? t(message) : undefined}
|
||||
/>
|
||||
</Main>
|
||||
)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
import { Form, FormProps } from 'react-component-form'
|
||||
|
||||
export const AuthenticationForm: React.FC<FormProps> = (props) => {
|
||||
|
@ -5,17 +5,18 @@ import { EMOJI_SET } from './emojiPlugin'
|
||||
export interface EmojiProps {
|
||||
value: string
|
||||
size: number
|
||||
tooltip?: boolean
|
||||
}
|
||||
|
||||
export const Emoji: React.FC<EmojiProps> = (props) => {
|
||||
const { value, size } = props
|
||||
const { value, size, tooltip = false } = props
|
||||
|
||||
return (
|
||||
<EmojiMart
|
||||
set={EMOJI_SET}
|
||||
emoji={value}
|
||||
size={size}
|
||||
tooltip
|
||||
tooltip={tooltip}
|
||||
fallback={() => {
|
||||
return <>{value}</>
|
||||
}}
|
||||
|
@ -1,10 +0,0 @@
|
||||
import { render } from '@testing-library/react'
|
||||
|
||||
import { Header } from '.'
|
||||
|
||||
describe('<Header />', () => {
|
||||
it('should render', () => {
|
||||
const { getByText } = render(<Header />)
|
||||
expect(getByText('Thream')).toBeInTheDocument()
|
||||
})
|
||||
})
|
@ -1,11 +1,12 @@
|
||||
import { useCallback, useEffect, useState, useRef } from 'react'
|
||||
import { useCallback, useState, useRef } from 'react'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import setLanguage from 'next-translate/setLanguage'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
import i18n from '../../../i18n.json'
|
||||
import { Arrow } from './Arrow'
|
||||
import { LanguageFlag } from './LanguageFlag'
|
||||
import i18n from '../../../i18n.json'
|
||||
import { useClickOutsideAlerter } from '../../../hooks/useClickOutsideAlerter'
|
||||
|
||||
export interface LanguageProps {
|
||||
className?: string
|
||||
@ -21,31 +22,19 @@ export const Language: React.FC<LanguageProps> = (props) => {
|
||||
setHiddenMenu((oldHiddenMenu) => !oldHiddenMenu)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const handleClickEvent = (event: MouseEvent): void => {
|
||||
if (languageClickRef.current == null || event.target == null) {
|
||||
return
|
||||
}
|
||||
if (!languageClickRef.current.contains(event.target as Node)) {
|
||||
setHiddenMenu(true)
|
||||
}
|
||||
}
|
||||
|
||||
window.document.addEventListener('click', handleClickEvent)
|
||||
|
||||
return () => {
|
||||
return window.removeEventListener('click', handleClickEvent)
|
||||
}
|
||||
}, [])
|
||||
useClickOutsideAlerter(languageClickRef, () => setHiddenMenu(true))
|
||||
|
||||
const handleLanguage = async (language: string): Promise<void> => {
|
||||
await setLanguage(language)
|
||||
handleHiddenMenu()
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='relative flex cursor-pointer flex-col items-center justify-center'>
|
||||
<div
|
||||
className='relative flex cursor-pointer flex-col items-center justify-center'
|
||||
ref={languageClickRef}
|
||||
>
|
||||
<div
|
||||
ref={languageClickRef}
|
||||
data-cy='language-click'
|
||||
className='mr-5 flex items-center'
|
||||
onClick={handleHiddenMenu}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { forwardRef } from 'react'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
const className =
|
||||
'py-2 px-6 font-paragraph rounded-lg bg-transparent border hover:text-white dark:hover:text-black fill-current stroke-current transform transition-colors duration-300 ease-in-out focus:outline-none focus:text-white dark:focus:text-black'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
export interface CheckboxProps extends React.ComponentPropsWithRef<'input'> {
|
||||
className?: string
|
||||
|
@ -1,7 +1,7 @@
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import type { FetchState as FormStateType } from 'react-component-form'
|
||||
|
||||
import { FetchState as FormStateType } from '../../../hooks/useFetchState'
|
||||
import { Loader } from '../Loader'
|
||||
|
||||
export interface FormStateProps extends React.ComponentPropsWithoutRef<'div'> {
|
||||
@ -27,31 +27,21 @@ export const FormState: React.FC<FormStateProps> = (props) => {
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
{...rest}
|
||||
className={classNames(
|
||||
props.className,
|
||||
'mt-6 flex max-w-xl items-center text-center font-medium',
|
||||
{
|
||||
'text-red-800 dark:text-red-400': state === 'error',
|
||||
'text-green-800 dark:text-green-400': state === 'success'
|
||||
}
|
||||
)}
|
||||
>
|
||||
<div className='thumbnail inline bg-cover font-headline' />
|
||||
<span id={id} className='pl-2'>
|
||||
<b>{t(`errors:${state}`)}:</b> {message}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<style jsx>{`
|
||||
.thumbnail {
|
||||
height: 20px;
|
||||
width: 20px;
|
||||
background-image: url('/images/svg/icons/input/${state}.svg');
|
||||
<div
|
||||
{...rest}
|
||||
className={classNames(
|
||||
props.className,
|
||||
'mt-6 flex max-w-xl items-center text-center font-medium',
|
||||
{
|
||||
'text-red-800 dark:text-red-400': state === 'error',
|
||||
'text-green-800 dark:text-green-400': state === 'success'
|
||||
}
|
||||
`}</style>
|
||||
</>
|
||||
)}
|
||||
>
|
||||
<div className='inline bg-cover font-headline' />
|
||||
<span id={id} className='pl-2'>
|
||||
<b>{t(`errors:${state}`)}:</b> {message}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
export interface IconButtonProps
|
||||
extends React.ComponentPropsWithoutRef<'button'> {}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Link from 'next/link'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
export interface IconLinkProps {
|
||||
selected?: boolean
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
import { FormState } from '../FormState'
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
export interface MainProps {
|
||||
className?: string
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { useMemo } from 'react'
|
||||
import Image from 'next/image'
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
import { ProviderOAuth } from '../../../models/OAuth'
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { createContext, useContext, useEffect } from 'react'
|
||||
import { createContext, useContext, useEffect, useMemo } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
import { NextPage, usePagination } from '../hooks/usePagination'
|
||||
@ -28,7 +28,9 @@ export const ChannelsProvider: React.FC<
|
||||
const router = useRouter()
|
||||
const { authentication } = useAuthentication()
|
||||
|
||||
const cacheKey: CacheKey = `${path.guildId}-${CHANNELS_CACHE_KEY}`
|
||||
const cacheKey = useMemo<CacheKey>(() => {
|
||||
return `${path.guildId}-${CHANNELS_CACHE_KEY}`
|
||||
}, [path.guildId])
|
||||
|
||||
const {
|
||||
items: channels,
|
||||
@ -75,7 +77,7 @@ export const ChannelsProvider: React.FC<
|
||||
export const useChannels = (): Channels => {
|
||||
const channels = useContext(ChannelsContext)
|
||||
if (channels === defaultChannelsContext) {
|
||||
throw new Error('useChannels must be used within ChannelsProvider')
|
||||
throw new Error('`useChannels` must be used within `ChannelsProvider`')
|
||||
}
|
||||
return channels
|
||||
}
|
||||
|
@ -11,9 +11,7 @@ export interface GuildMember {
|
||||
member: Member
|
||||
}
|
||||
|
||||
export interface GuildMemberResult extends GuildMember {
|
||||
setGuildMember: React.Dispatch<React.SetStateAction<GuildMember>>
|
||||
}
|
||||
export interface GuildMemberResult extends GuildMember {}
|
||||
|
||||
export interface GuildMemberProps {
|
||||
guildMember: GuildMember
|
||||
@ -79,8 +77,7 @@ export const GuildMemberProvider: React.FC<
|
||||
return (
|
||||
<GuildMemberContext.Provider
|
||||
value={{
|
||||
...guildMember,
|
||||
setGuildMember
|
||||
...guildMember
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
@ -91,7 +88,9 @@ export const GuildMemberProvider: React.FC<
|
||||
export const useGuildMember = (): GuildMemberResult => {
|
||||
const guildMember = useContext(GuildMemberContext)
|
||||
if (guildMember === defaultGuildMemberContext) {
|
||||
throw new Error('useGuildMember must be used within GuildMemberProvider')
|
||||
throw new Error(
|
||||
'`useGuildMember` must be used within `GuildMemberProvider`'
|
||||
)
|
||||
}
|
||||
return guildMember
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ export const GuildsProvider: React.FC<React.PropsWithChildren<{}>> = (
|
||||
export const useGuilds = (): Guilds => {
|
||||
const guilds = useContext(GuildsContext)
|
||||
if (guilds === defaultGuildsContext) {
|
||||
throw new Error('useGuilds must be used within GuildsProvider')
|
||||
throw new Error('`useGuilds` must be used within `GuildsProvider`')
|
||||
}
|
||||
return guilds
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { createContext, useContext, useEffect } from 'react'
|
||||
import { createContext, useContext, useEffect, useMemo } from 'react'
|
||||
|
||||
import { NextPage, usePagination } from '../hooks/usePagination'
|
||||
import { useAuthentication } from '../tools/authentication'
|
||||
@ -28,7 +28,9 @@ export const MembersProviders: React.FC<
|
||||
|
||||
const { authentication } = useAuthentication()
|
||||
|
||||
const cacheKey: CacheKey = `${path.guildId}-${MEMBERS_CACHE_KEY}`
|
||||
const cacheKey = useMemo<CacheKey>(() => {
|
||||
return `${path.guildId}-${MEMBERS_CACHE_KEY}`
|
||||
}, [path.guildId])
|
||||
|
||||
const {
|
||||
items: members,
|
||||
@ -89,7 +91,7 @@ export const MembersProviders: React.FC<
|
||||
export const useMembers = (): Members => {
|
||||
const members = useContext(MembersContext)
|
||||
if (members === defaultMembersContext) {
|
||||
throw new Error('useMembers must be used within MembersProvider')
|
||||
throw new Error('`useMembers` must be used within `MembersProvider`')
|
||||
}
|
||||
return members
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { createContext, useContext, useEffect } from 'react'
|
||||
import { createContext, useContext, useEffect, useMemo } from 'react'
|
||||
|
||||
import { NextPage, usePagination } from '../hooks/usePagination'
|
||||
import { useAuthentication } from '../tools/authentication'
|
||||
@ -26,7 +26,9 @@ export const MessagesProvider: React.FC<
|
||||
const { path, children } = props
|
||||
const { authentication, user } = useAuthentication()
|
||||
|
||||
const cacheKey: CacheKey = `${path.channelId}-${MESSAGES_CACHE_KEY}`
|
||||
const cacheKey = useMemo<CacheKey>(() => {
|
||||
return `${path.channelId}-${MESSAGES_CACHE_KEY}`
|
||||
}, [path.channelId])
|
||||
|
||||
const {
|
||||
items: messages,
|
||||
@ -88,7 +90,7 @@ export const MessagesProvider: React.FC<
|
||||
export const useMessages = (): Messages => {
|
||||
const messages = useContext(MessagesContext)
|
||||
if (messages === defaultMessagesContext) {
|
||||
throw new Error('useMessages must be used within a MessagesProvider')
|
||||
throw new Error('`useMessages` must be used within a `MessagesProvider`')
|
||||
}
|
||||
return messages
|
||||
}
|
||||
|
@ -1,71 +0,0 @@
|
||||
import type { ErrorObject } from 'ajv'
|
||||
|
||||
import { getErrorTranslationKey } from '../../../../hooks/useForm/getErrorTranslationKey'
|
||||
|
||||
const errorObject: ErrorObject = {
|
||||
instancePath: '/path',
|
||||
keyword: 'keyword',
|
||||
params: {},
|
||||
schemaPath: '/path'
|
||||
}
|
||||
|
||||
describe('hooks/useForm/getErrorTranslationKey', () => {
|
||||
it('returns `errors:invalid` with unknown keyword', () => {
|
||||
expect(
|
||||
getErrorTranslationKey({
|
||||
...errorObject,
|
||||
keyword: 'unknownkeyword'
|
||||
})
|
||||
).equal('errors:invalid')
|
||||
})
|
||||
|
||||
it('returns `errors:invalid` with format != email', () => {
|
||||
expect(
|
||||
getErrorTranslationKey({
|
||||
...errorObject,
|
||||
keyword: 'format',
|
||||
params: { format: 'email' }
|
||||
})
|
||||
).equal('errors:email')
|
||||
})
|
||||
|
||||
it('returns `errors:email` with format = email', () => {
|
||||
expect(
|
||||
getErrorTranslationKey({
|
||||
...errorObject,
|
||||
keyword: 'format',
|
||||
params: { format: 'email' }
|
||||
})
|
||||
).equal('errors:email')
|
||||
})
|
||||
|
||||
it('returns `errors:required` with minLength and limit = 1', () => {
|
||||
expect(
|
||||
getErrorTranslationKey({
|
||||
...errorObject,
|
||||
keyword: 'minLength',
|
||||
params: { limit: 1 }
|
||||
})
|
||||
).equal('errors:required')
|
||||
})
|
||||
|
||||
it('returns `errors:minLength` with minLength and limit > 1', () => {
|
||||
expect(
|
||||
getErrorTranslationKey({
|
||||
...errorObject,
|
||||
keyword: 'minLength',
|
||||
params: { limit: 5 }
|
||||
})
|
||||
).equal('errors:minLength')
|
||||
})
|
||||
|
||||
it('returns `errors:maxLength` with maxLength', () => {
|
||||
expect(
|
||||
getErrorTranslationKey({
|
||||
...errorObject,
|
||||
keyword: 'maxLength',
|
||||
params: { limit: 5 }
|
||||
})
|
||||
).equal('errors:maxLength')
|
||||
})
|
||||
})
|
@ -1,22 +0,0 @@
|
||||
import { Type } from '@sinclair/typebox'
|
||||
|
||||
import { handleCheckboxBoolean } from '../../../../hooks/useForm/handleCheckboxBoolean'
|
||||
|
||||
const schema = Type.Object({
|
||||
myBoolean: Type.Boolean(),
|
||||
myString: Type.String()
|
||||
})
|
||||
|
||||
describe('hooks/useForm/handleCheckboxBoolean', () => {
|
||||
it('should convert all checkbox property to boolean', () => {
|
||||
const object = {
|
||||
myBoolean: 'on',
|
||||
myString: 'on'
|
||||
}
|
||||
const result = handleCheckboxBoolean(object, schema)
|
||||
expect(result).deep.equal({
|
||||
myBoolean: true,
|
||||
myString: 'on'
|
||||
})
|
||||
})
|
||||
})
|
@ -1,20 +0,0 @@
|
||||
import { replaceEmptyStringInObjectToNull } from '../../../../hooks/useForm/replaceEmptyStringInObjectToNull'
|
||||
|
||||
describe('hooks/useForm/replaceEmptyStringInObjectToNull', () => {
|
||||
it('should replace empty string in object to null except for required properties', () => {
|
||||
expect(
|
||||
replaceEmptyStringInObjectToNull(
|
||||
{
|
||||
foo: '',
|
||||
bar: 'bar',
|
||||
baz: ''
|
||||
},
|
||||
['baz']
|
||||
)
|
||||
).deep.equal({
|
||||
foo: null,
|
||||
bar: 'bar',
|
||||
baz: ''
|
||||
})
|
||||
})
|
||||
})
|
@ -241,8 +241,8 @@ describe('Pages > /application/[guildId]/[channelId]', () => {
|
||||
cy.visit('/application/abc/abc', {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
|
||||
it("should redirect the user to `/404` if `guildId` doesn't exist", () => {
|
||||
@ -253,8 +253,8 @@ describe('Pages > /application/[guildId]/[channelId]', () => {
|
||||
cy.visit(`/application/123/${channelExample.id}`, {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
|
||||
it("should redirect the user to `/404` if `channelId` doesn't exist", () => {
|
||||
@ -263,8 +263,8 @@ describe('Pages > /application/[guildId]/[channelId]', () => {
|
||||
getGuildMemberWithGuildIdHandler
|
||||
]).setCookie('refreshToken', 'refresh-token')
|
||||
cy.visit(`/application/${guildExample.id}/123`, { failOnStatusCode: false })
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -92,8 +92,8 @@ describe('Pages > /application/[guildId]/[channelId]/settings', () => {
|
||||
cy.visit(`/application/${guildExample.id}/${channelExample.id}/settings`, {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
|
||||
it('should redirect the user to `/404` if `guildId` or `channelId` are not numbers', () => {
|
||||
@ -104,8 +104,8 @@ describe('Pages > /application/[guildId]/[channelId]/settings', () => {
|
||||
cy.visit('/application/abc/abc/settings', {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
|
||||
it("should redirect the user to `/404` if `guildId` doesn't exist", () => {
|
||||
@ -116,8 +116,8 @@ describe('Pages > /application/[guildId]/[channelId]/settings', () => {
|
||||
cy.visit(`/application/123/${channelExample.id}/settings`, {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
|
||||
it("should redirect the user to `/404` if `channelId` doesn't exist", () => {
|
||||
@ -128,8 +128,8 @@ describe('Pages > /application/[guildId]/[channelId]/settings', () => {
|
||||
cy.visit(`/application/${guildExample.id}/123/settings`, {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -56,8 +56,8 @@ describe('Pages > /application/[guildId]/channels/create', () => {
|
||||
cy.visit('/application/abc/channels/create', {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
|
||||
it("should redirect the user to `/404` if `guildId` doesn't exist", () => {
|
||||
@ -68,8 +68,8 @@ describe('Pages > /application/[guildId]/channels/create', () => {
|
||||
cy.visit(`/application/123/channels/create`, {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -86,8 +86,8 @@ describe('Pages > /application/[guildId]/settings', () => {
|
||||
cy.visit('/application/abc/settings', {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
|
||||
it("should redirect the user to `/404` if `guildId` doesn't exist", () => {
|
||||
@ -98,8 +98,8 @@ describe('Pages > /application/[guildId]/settings', () => {
|
||||
cy.visit(`/application/123/settings`, {
|
||||
failOnStatusCode: false
|
||||
})
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -29,8 +29,8 @@ describe('Pages > /application/users/[userId]', () => {
|
||||
'refresh-token'
|
||||
)
|
||||
cy.visit(`/application/users/123`, { failOnStatusCode: false })
|
||||
.location('pathname')
|
||||
.should('eq', '/404')
|
||||
.get('[data-cy=status-code]')
|
||||
.contains('404')
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import classNames from 'classnames'
|
||||
import classNames from 'clsx'
|
||||
|
||||
export interface {{ properCase name }}Props {
|
||||
className?: string
|
||||
|
22
hooks/useClickOutsideAlerter.ts
Normal file
22
hooks/useClickOutsideAlerter.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { useEffect } from 'react'
|
||||
|
||||
export const useClickOutsideAlerter = (
|
||||
ref: React.RefObject<HTMLElement>,
|
||||
callback: () => void
|
||||
): void => {
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (event: MouseEvent): void => {
|
||||
if (
|
||||
event.target != null &&
|
||||
ref.current != null &&
|
||||
!ref.current.contains(event.target as Node)
|
||||
) {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
document.addEventListener('mousedown', handleClickOutside)
|
||||
return () => {
|
||||
document.removeEventListener('mousedown', handleClickOutside)
|
||||
}
|
||||
}, [ref, callback])
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
import { useState } from 'react'
|
||||
|
||||
export const fetchState = ['idle', 'loading', 'error', 'success'] as const
|
||||
|
||||
export type FetchState = typeof fetchState[number]
|
||||
|
||||
export const useFetchState = (
|
||||
initialFetchState: FetchState = 'idle'
|
||||
): [
|
||||
fetchState: FetchState,
|
||||
setFetchState: React.Dispatch<React.SetStateAction<FetchState>>
|
||||
] => {
|
||||
const [fetchState, setFetchState] = useState<FetchState>(initialFetchState)
|
||||
return [fetchState, setFetchState]
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
import type { ErrorObject } from 'ajv'
|
||||
|
||||
const knownErrorKeywords = ['minLength', 'maxLength', 'format']
|
||||
|
||||
export const getErrorTranslationKey = (error: ErrorObject): string => {
|
||||
if (knownErrorKeywords.includes(error?.keyword)) {
|
||||
if (error.keyword === 'minLength' && error.params.limit === 1) {
|
||||
return 'errors:required'
|
||||
}
|
||||
if (error.keyword === 'format') {
|
||||
if (error.params.format === 'email') {
|
||||
return 'errors:email'
|
||||
}
|
||||
return 'errors:invalid'
|
||||
}
|
||||
return `errors:${error.keyword}`
|
||||
}
|
||||
return 'errors:invalid'
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
import type { TObject } from '@sinclair/typebox'
|
||||
|
||||
import type { ObjectAny } from '../../tools/types'
|
||||
|
||||
export const handleCheckboxBoolean = (
|
||||
object: ObjectAny,
|
||||
validateSchemaObject: TObject<ObjectAny>
|
||||
): ObjectAny => {
|
||||
const booleanProperties: string[] = []
|
||||
for (const property in validateSchemaObject.properties) {
|
||||
const rule = validateSchemaObject.properties[property]
|
||||
if (rule.type === 'boolean') {
|
||||
booleanProperties.push(property)
|
||||
}
|
||||
}
|
||||
for (const booleanProperty of booleanProperties) {
|
||||
if (object[booleanProperty] == null) {
|
||||
object[booleanProperty] =
|
||||
validateSchemaObject.properties[booleanProperty].default
|
||||
} else {
|
||||
object[booleanProperty] = object[booleanProperty] === 'on'
|
||||
}
|
||||
}
|
||||
return object
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './useForm'
|
@ -1,19 +0,0 @@
|
||||
import type { ObjectAny } from '../../tools/types'
|
||||
|
||||
export const replaceEmptyStringInObjectToNull = (
|
||||
object: ObjectAny,
|
||||
required: string[] = []
|
||||
): ObjectAny => {
|
||||
return Object.fromEntries(
|
||||
Object.entries(object).map(([key, value]) => {
|
||||
if (
|
||||
typeof value === 'string' &&
|
||||
value.length === 0 &&
|
||||
!required.includes(key)
|
||||
) {
|
||||
return [key, null]
|
||||
}
|
||||
return [key, value]
|
||||
})
|
||||
)
|
||||
}
|
@ -1,134 +0,0 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import type { HandleForm } from 'react-component-form'
|
||||
import type { ErrorObject } from 'ajv'
|
||||
|
||||
import { FetchState, useFetchState } from '../useFetchState'
|
||||
import { ajv } from '../../tools/ajv'
|
||||
import { getErrorTranslationKey } from './getErrorTranslationKey'
|
||||
import { replaceEmptyStringInObjectToNull } from './replaceEmptyStringInObjectToNull'
|
||||
import type { ObjectAny } from '../../tools/types'
|
||||
import { handleCheckboxBoolean } from './handleCheckboxBoolean'
|
||||
|
||||
interface Errors {
|
||||
[key: string]: ErrorObject<string, any> | null | undefined
|
||||
}
|
||||
|
||||
const findError = (
|
||||
field: string
|
||||
): ((value: ErrorObject, index: number, object: ErrorObject[]) => boolean) => {
|
||||
return (validationError) => validationError.instancePath === field
|
||||
}
|
||||
|
||||
export type GetErrorTranslation = (error?: ErrorObject | null) => string | null
|
||||
|
||||
export interface UseFormOptions {
|
||||
validateSchema: { [key: string]: any }
|
||||
replaceEmptyStringToNull?: boolean
|
||||
resetOnSuccess?: boolean
|
||||
}
|
||||
|
||||
export type HandleSubmit = (callback: HandleSubmitCallback) => HandleForm
|
||||
|
||||
interface Message {
|
||||
type: 'error' | 'success'
|
||||
value: string
|
||||
}
|
||||
|
||||
export type HandleSubmitCallback = (
|
||||
formData: ObjectAny,
|
||||
formElement: HTMLFormElement
|
||||
) => Promise<Message | null>
|
||||
|
||||
export interface UseFormResult {
|
||||
message: string | null
|
||||
setMessageTranslationKey: React.Dispatch<
|
||||
React.SetStateAction<string | undefined>
|
||||
>
|
||||
fetchState: FetchState
|
||||
setFetchState: React.Dispatch<React.SetStateAction<FetchState>>
|
||||
getErrorTranslation: GetErrorTranslation
|
||||
handleSubmit: HandleSubmit
|
||||
errors: Errors
|
||||
}
|
||||
|
||||
export const useForm = (options: UseFormOptions): UseFormResult => {
|
||||
const {
|
||||
validateSchema,
|
||||
replaceEmptyStringToNull = false,
|
||||
resetOnSuccess = false
|
||||
} = options
|
||||
const { t } = useTranslation()
|
||||
const [fetchState, setFetchState] = useFetchState()
|
||||
const [messageTranslationKey, setMessageTranslationKey] = useState<
|
||||
string | undefined
|
||||
>(undefined)
|
||||
const [errors, setErrors] = useState<Errors>({})
|
||||
|
||||
const validateSchemaObject = useMemo(() => {
|
||||
return Type.Object(validateSchema)
|
||||
}, [validateSchema])
|
||||
|
||||
const validate = useMemo(() => {
|
||||
return ajv.compile(validateSchemaObject)
|
||||
}, [validateSchemaObject])
|
||||
|
||||
const getErrorTranslation = (error?: ErrorObject | null): string | null => {
|
||||
if (error != null) {
|
||||
return t(getErrorTranslationKey(error)).replace(
|
||||
'{expected}',
|
||||
error?.params?.limit
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const handleSubmit: HandleSubmit = (callback) => {
|
||||
return async (formData: ObjectAny, formElement) => {
|
||||
setErrors({})
|
||||
setMessageTranslationKey(undefined)
|
||||
if (replaceEmptyStringToNull) {
|
||||
formData = replaceEmptyStringInObjectToNull(
|
||||
formData,
|
||||
validateSchemaObject.required
|
||||
)
|
||||
}
|
||||
formData = handleCheckboxBoolean(formData, validateSchemaObject)
|
||||
const isValid = validate(formData)
|
||||
if (!isValid) {
|
||||
setFetchState('error')
|
||||
const errors: Errors = {}
|
||||
for (const property in validateSchemaObject.properties) {
|
||||
errors[property] = validate.errors?.find(findError(`/${property}`))
|
||||
}
|
||||
setErrors(errors)
|
||||
} else {
|
||||
setErrors({})
|
||||
setFetchState('loading')
|
||||
const message = await callback(formData, formElement)
|
||||
if (message != null) {
|
||||
setMessageTranslationKey(message.value)
|
||||
if (message.type === 'success') {
|
||||
setFetchState('success')
|
||||
if (resetOnSuccess) {
|
||||
formElement.reset()
|
||||
}
|
||||
} else {
|
||||
setFetchState('error')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
getErrorTranslation,
|
||||
errors,
|
||||
fetchState,
|
||||
setFetchState,
|
||||
handleSubmit,
|
||||
message: messageTranslationKey != null ? t(messageTranslationKey) : null,
|
||||
setMessageTranslationKey
|
||||
}
|
||||
}
|
60
hooks/useFormTranslation.ts
Normal file
60
hooks/useFormTranslation.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import type { Error } from 'react-component-form'
|
||||
|
||||
const knownErrorKeywords = ['minLength', 'maxLength', 'format']
|
||||
|
||||
const getErrorTranslationKey = (error: Error): string => {
|
||||
if (knownErrorKeywords.includes(error?.keyword)) {
|
||||
if (
|
||||
error.keyword === 'minLength' &&
|
||||
typeof error.data === 'string' &&
|
||||
error.data.length === 0
|
||||
) {
|
||||
return 'errors:required'
|
||||
}
|
||||
if (error.keyword === 'format') {
|
||||
if (error.params.format === 'email') {
|
||||
return 'errors:invalid-email'
|
||||
}
|
||||
return 'errors:invalid'
|
||||
}
|
||||
return `errors:${error.keyword}`
|
||||
}
|
||||
return 'errors:invalid'
|
||||
}
|
||||
|
||||
export type GetErrorTranslation = (
|
||||
error: Error | undefined
|
||||
) => string | undefined
|
||||
|
||||
export type GetFirstErrorTranslation = (
|
||||
errors: Error[] | undefined
|
||||
) => string | undefined
|
||||
|
||||
export interface UseFormTranslationResult {
|
||||
getErrorTranslation: GetErrorTranslation
|
||||
getFirstErrorTranslation: GetFirstErrorTranslation
|
||||
}
|
||||
|
||||
export const useFormTranslation = (): UseFormTranslationResult => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const getErrorTranslation: GetErrorTranslation = (error) => {
|
||||
if (error != null) {
|
||||
return t(getErrorTranslationKey(error)).replace(
|
||||
'{expected}',
|
||||
error?.params?.limit
|
||||
)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
const getFirstErrorTranslation: GetFirstErrorTranslation = (errors) => {
|
||||
if (errors != null) {
|
||||
return getErrorTranslation(errors[0])
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
return { getFirstErrorTranslation, getErrorTranslation }
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import { useState, useRef, useCallback } from 'react'
|
||||
import { AxiosInstance } from 'axios'
|
||||
import type { FetchState } from 'react-component-form'
|
||||
|
||||
import { FetchState } from './useFetchState'
|
||||
import {
|
||||
CacheKey,
|
||||
getPaginationCache,
|
||||
@ -45,15 +45,6 @@ export const usePagination = <T extends PaginationItem>(
|
||||
const fetchState = useRef<FetchState>('idle')
|
||||
const afterId = useRef<number | null>(null)
|
||||
|
||||
const updateAfterId = (newItems: T[]): void => {
|
||||
if (!inverse) {
|
||||
afterId.current =
|
||||
newItems.length > 0 ? newItems[newItems.length - 1].id : null
|
||||
} else {
|
||||
afterId.current = newItems.length > 0 ? newItems[0].id : null
|
||||
}
|
||||
}
|
||||
|
||||
const nextPageAsync: NextPageAsync = useCallback(
|
||||
async (query) => {
|
||||
if (fetchState.current !== 'idle') {
|
||||
@ -68,21 +59,33 @@ export const usePagination = <T extends PaginationItem>(
|
||||
const { data: newItems } = await api.get<T[]>(
|
||||
`${url}?${searchParameters.toString()}`
|
||||
)
|
||||
updateAfterId(newItems)
|
||||
if (!inverse) {
|
||||
afterId.current =
|
||||
newItems.length > 0 ? newItems[newItems.length - 1].id : null
|
||||
} else {
|
||||
afterId.current = newItems.length > 0 ? newItems[0].id : null
|
||||
}
|
||||
setItems((oldItems) => {
|
||||
const updatedItems = inverse
|
||||
? [...newItems, ...oldItems]
|
||||
: [...oldItems, ...newItems]
|
||||
const unique = updatedItems.reduce<T[]>((accumulator, item) => {
|
||||
const isExisting = accumulator.some(
|
||||
(itemSome) => itemSome.id === item.id
|
||||
)
|
||||
if (!isExisting) {
|
||||
accumulator.push(item)
|
||||
}
|
||||
return accumulator
|
||||
}, [])
|
||||
if (cacheKey != null) {
|
||||
savePaginationCache(cacheKey, updatedItems)
|
||||
savePaginationCache(cacheKey, unique)
|
||||
}
|
||||
return updatedItems
|
||||
return unique
|
||||
})
|
||||
setHasMore(newItems.length > 0)
|
||||
fetchState.current = 'idle'
|
||||
},
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- We don't want infinite loops with updateAfterId
|
||||
[api, url, inverse, cacheKey]
|
||||
)
|
||||
|
||||
@ -106,13 +109,18 @@ export const usePagination = <T extends PaginationItem>(
|
||||
afterId.current = null
|
||||
setItems([])
|
||||
} else {
|
||||
fetchState.current = 'loading'
|
||||
const newItems = getPaginationCache<T>(cacheKey)
|
||||
setItems(newItems)
|
||||
updateAfterId(newItems)
|
||||
if (!inverse) {
|
||||
afterId.current =
|
||||
newItems.length > 0 ? newItems[newItems.length - 1].id : null
|
||||
} else {
|
||||
afterId.current = newItems.length > 0 ? newItems[0].id : null
|
||||
}
|
||||
fetchState.current = 'idle'
|
||||
}
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- We don't want infinite loops with updateAfterId
|
||||
}, [cacheKey])
|
||||
}, [cacheKey, inverse])
|
||||
|
||||
return { items, hasMore, nextPage, resetPagination, setItems }
|
||||
}
|
||||
|
@ -21,10 +21,12 @@
|
||||
"label-checkbox-guilds": "Show the list of guilds to everyone.",
|
||||
"nothing-here": "Nothing's here...",
|
||||
"save": "Save",
|
||||
"saved-information": "The information has been saved.",
|
||||
"saved-information": "The information have been saved.",
|
||||
"success-email-changed": "Please check your emails to confirm your new email address. You are now signed out.",
|
||||
"delete": "Delete",
|
||||
"edit": "Edit",
|
||||
"signout": "Sign out",
|
||||
"signout-all-devices": "Sign out of all devices",
|
||||
"signin-with-an-account": "Sign in with an account",
|
||||
"signout-with-an-account": "Sign out with an account",
|
||||
"success-deleted-provider": "The provider has been deleted.",
|
||||
|
@ -7,6 +7,6 @@
|
||||
"required": "Oops, this field is required 🙈.",
|
||||
"minLength": "The field must contain at least {expected} characters.",
|
||||
"maxLength": "The field must contain at most {expected} characters.",
|
||||
"email": "Mmm… It seems that this email is not valid 🤔.",
|
||||
"invalid-email": "Mmm… It seems that this email is not valid 🤔.",
|
||||
"invalid": "Invalid value."
|
||||
}
|
||||
|
@ -24,7 +24,9 @@
|
||||
"saved-information": "Les informations ont été enregistrées.",
|
||||
"success-email-changed": "Veuillez vérifier vos emails pour confirmer votre nouvelle adresse email. Vous êtes maintenant déconnecté.",
|
||||
"delete": "Supprimer",
|
||||
"edit": "Modifier",
|
||||
"signout": "Se déconnecter",
|
||||
"signout-all-devices": "Se déconnecter de tous les appareils",
|
||||
"signin-with-an-account": "Se connecter avec un compte",
|
||||
"signout-with-an-account": "Se déconnecter avec un compte",
|
||||
"success-deleted-provider": "Le moyen de connexion a été supprimé.",
|
||||
|
@ -7,6 +7,6 @@
|
||||
"required": "Oups, ce champ est obligatoire 🙈.",
|
||||
"minLength": "Le champ doit contenir au moins {expected} caractères.",
|
||||
"maxLength": "Le champ doit contenir au plus {expected} caractères.",
|
||||
"email": "Mmm… Il semblerait que cet email ne soit pas valide 🤔.",
|
||||
"invalid-email": "Mmm… Il semblerait que cet email ne soit pas valide 🤔.",
|
||||
"invalid": "Valeur invalide."
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ export const guildSchema = {
|
||||
id,
|
||||
name: Type.String({ minLength: 1, maxLength: 30 }),
|
||||
icon: Type.Union([Type.String({ format: 'uri-reference' }), Type.Null()]),
|
||||
description: Type.Union([Type.String({ maxLength: 160 }), Type.Null()]),
|
||||
description: Type.String({ maxLength: 160 }),
|
||||
createdAt: date.createdAt,
|
||||
updatedAt: date.updatedAt
|
||||
}
|
||||
|
1226
package-lock.json
generated
1226
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@thream/website",
|
||||
"version": "1.1.1",
|
||||
"version": "1.2.2",
|
||||
"private": true,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -20,7 +20,7 @@
|
||||
"lint:editorconfig": "editorconfig-checker",
|
||||
"lint:markdown": "markdownlint-cli2",
|
||||
"lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"",
|
||||
"lint:prettier": "prettier \".\" --check",
|
||||
"lint:prettier": "prettier \".\" --check --ignore-path \".gitignore\"",
|
||||
"lint:staged": "lint-staged",
|
||||
"test:unit": "cypress run --component",
|
||||
"test:html-w3c-validator": "start-server-and-test \"start\" \"http://localhost:3000\" \"html-w3c-validator\"",
|
||||
@ -36,20 +36,18 @@
|
||||
"@fontsource/roboto": "4.5.8",
|
||||
"@heroicons/react": "1.0.6",
|
||||
"@sinclair/typebox": "0.24.28",
|
||||
"ajv": "8.11.0",
|
||||
"ajv-formats": "2.1.1",
|
||||
"axios": "0.26.1",
|
||||
"classnames": "2.3.1",
|
||||
"clsx": "1.2.1",
|
||||
"date-and-time": "2.4.1",
|
||||
"emoji-mart": "3.0.1",
|
||||
"katex": "0.16.0",
|
||||
"katex": "0.16.1",
|
||||
"next": "12.2.5",
|
||||
"next-pwa": "5.6.0",
|
||||
"next-themes": "0.2.0",
|
||||
"next-translate": "1.5.0",
|
||||
"pretty-bytes": "6.0.0",
|
||||
"react": "18.2.0",
|
||||
"react-component-form": "3.0.1",
|
||||
"react-component-form": "3.1.0",
|
||||
"react-dom": "18.2.0",
|
||||
"react-infinite-scroll-component": "6.1.0",
|
||||
"react-markdown": "8.0.3",
|
||||
@ -69,31 +67,31 @@
|
||||
"universal-cookie": "4.0.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "17.0.3",
|
||||
"@commitlint/config-conventional": "17.0.3",
|
||||
"@commitlint/cli": "17.1.1",
|
||||
"@commitlint/config-conventional": "17.1.0",
|
||||
"@lhci/cli": "0.9.0",
|
||||
"@saithodev/semantic-release-backmerge": "2.1.2",
|
||||
"@semantic-release/git": "10.0.1",
|
||||
"@types/emoji-mart": "3.0.9",
|
||||
"@types/hast": "2.3.4",
|
||||
"@types/katex": "0.14.0",
|
||||
"@types/node": "18.7.11",
|
||||
"@types/node": "18.7.13",
|
||||
"@types/react": "18.0.17",
|
||||
"@types/react-responsive": "8.0.5",
|
||||
"@types/react-syntax-highlighter": "15.5.4",
|
||||
"@types/react-syntax-highlighter": "15.5.5",
|
||||
"@types/unist": "2.0.6",
|
||||
"@typescript-eslint/eslint-plugin": "5.34.0",
|
||||
"@typescript-eslint/parser": "5.34.0",
|
||||
"@typescript-eslint/eslint-plugin": "5.35.1",
|
||||
"@typescript-eslint/parser": "5.35.1",
|
||||
"autoprefixer": "10.4.8",
|
||||
"cypress": "10.6.0",
|
||||
"editorconfig-checker": "4.0.2",
|
||||
"eslint": "8.22.0",
|
||||
"eslint": "8.23.0",
|
||||
"eslint-config-conventions": "3.0.0",
|
||||
"eslint-config-next": "12.2.5",
|
||||
"eslint-config-prettier": "8.5.0",
|
||||
"eslint-plugin-import": "2.26.0",
|
||||
"eslint-plugin-prettier": "4.2.1",
|
||||
"eslint-plugin-promise": "6.0.0",
|
||||
"eslint-plugin-promise": "6.0.1",
|
||||
"eslint-plugin-unicorn": "43.0.2",
|
||||
"html-w3c-validator": "1.2.0",
|
||||
"husky": "8.0.1",
|
||||
@ -108,7 +106,7 @@
|
||||
"serve": "14.0.1",
|
||||
"start-server-and-test": "1.14.0",
|
||||
"tailwindcss": "3.1.8",
|
||||
"typescript": "4.7.4",
|
||||
"vercel": "28.1.2"
|
||||
"typescript": "4.8.2",
|
||||
"vercel": "28.2.0"
|
||||
}
|
||||
}
|
||||
|
@ -68,10 +68,7 @@ export const getServerSideProps = authenticationFromServerSide({
|
||||
const guildId = Number(context?.params?.guildId)
|
||||
if (isNaN(channelId) || isNaN(guildId)) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/404',
|
||||
permanent: false
|
||||
}
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
const { data: guildMember } = await api.get(`/guilds/${guildId}`)
|
||||
|
@ -63,10 +63,7 @@ export const getServerSideProps = authenticationFromServerSide({
|
||||
const guildId = Number(context?.params?.guildId)
|
||||
if (isNaN(channelId) || isNaN(guildId)) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/404',
|
||||
permanent: false
|
||||
}
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
const { data: guildMember } = await api.get<GuildMember>(
|
||||
@ -74,10 +71,7 @@ export const getServerSideProps = authenticationFromServerSide({
|
||||
)
|
||||
if (!guildMember.member.isOwner) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/404',
|
||||
permanent: false
|
||||
}
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
const { data: selectedChannelData } = await api.get(
|
||||
|
@ -49,10 +49,7 @@ export const getServerSideProps = authenticationFromServerSide({
|
||||
const guildId = Number(context?.params?.guildId)
|
||||
if (isNaN(guildId)) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/404',
|
||||
permanent: false
|
||||
}
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
const { data: guildMember } = await api.get(`/guilds/${guildId}`)
|
||||
|
@ -43,10 +43,7 @@ export const getServerSideProps = authenticationFromServerSide({
|
||||
const guildId = Number(context?.params?.guildId)
|
||||
if (isNaN(guildId)) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/404',
|
||||
permanent: false
|
||||
}
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
const { data: guildMember } = await api.get<GuildMember>(
|
||||
|
@ -38,10 +38,7 @@ export const getServerSideProps = authenticationFromServerSide({
|
||||
const userId = Number(context?.params?.userId)
|
||||
if (isNaN(userId)) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/404',
|
||||
permanent: false
|
||||
}
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
const { data } = await api.get(`/users/${userId}`)
|
||||
|
@ -2,6 +2,8 @@ import { NextPage } from 'next'
|
||||
import Link from 'next/link'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import axios from 'axios'
|
||||
import { useForm } from 'react-component-form'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
import { AuthenticationForm } from '../../components/Authentication'
|
||||
import { Head } from '../../components/Head'
|
||||
@ -15,24 +17,29 @@ import { authenticationFromServerSide } from '../../tools/authentication'
|
||||
import { ScrollableBody } from '../../components/ScrollableBody'
|
||||
import { userSchema } from '../../models/User'
|
||||
import { api } from '../../tools/api'
|
||||
import { HandleSubmitCallback, useForm } from '../../hooks/useForm'
|
||||
import { useFormTranslation } from '../../hooks/useFormTranslation'
|
||||
|
||||
const schema = {
|
||||
email: userSchema.email
|
||||
}
|
||||
|
||||
const ForgotPassword: NextPage<FooterProps> = (props) => {
|
||||
const { t } = useTranslation()
|
||||
const { version } = props
|
||||
|
||||
const { fetchState, message, errors, getErrorTranslation, handleSubmit } =
|
||||
useForm({
|
||||
validateSchema: { email: userSchema.email },
|
||||
resetOnSuccess: true
|
||||
})
|
||||
const { handleUseForm, fetchState, message, errors } = useForm(schema)
|
||||
const { getFirstErrorTranslation } = useFormTranslation()
|
||||
|
||||
const onSubmit: HandleSubmitCallback = async (formData) => {
|
||||
const onSubmit: HandleUseFormCallback<typeof schema> = async (
|
||||
formData,
|
||||
formElement
|
||||
) => {
|
||||
try {
|
||||
await api.post(
|
||||
`/users/reset-password?redirectURI=${window.location.origin}/authentication/reset-password`,
|
||||
formData
|
||||
)
|
||||
formElement.reset()
|
||||
return {
|
||||
type: 'success',
|
||||
value: 'authentication:success-forgot-password'
|
||||
@ -41,7 +48,7 @@ const ForgotPassword: NextPage<FooterProps> = (props) => {
|
||||
if (axios.isAxiosError(error) && error.response?.status === 400) {
|
||||
return {
|
||||
type: 'error',
|
||||
value: 'errors:email'
|
||||
value: 'errors:invalid-email'
|
||||
}
|
||||
}
|
||||
return {
|
||||
@ -56,7 +63,7 @@ const ForgotPassword: NextPage<FooterProps> = (props) => {
|
||||
<Head title={`Thream | ${t('authentication:forgot-password')}`} />
|
||||
<Header />
|
||||
<Main>
|
||||
<AuthenticationForm onSubmit={handleSubmit(onSubmit)}>
|
||||
<AuthenticationForm onSubmit={handleUseForm(onSubmit)}>
|
||||
<Input type='email' placeholder='Email' name='email' label='Email' />
|
||||
<Button data-cy='submit' className='mt-6 w-full' type='submit'>
|
||||
{t('authentication:submit')}
|
||||
@ -71,7 +78,9 @@ const ForgotPassword: NextPage<FooterProps> = (props) => {
|
||||
id='message'
|
||||
state={fetchState}
|
||||
message={
|
||||
message != null ? message : getErrorTranslation(errors.email)
|
||||
message != null
|
||||
? t(message)
|
||||
: getFirstErrorTranslation(errors.email)
|
||||
}
|
||||
/>
|
||||
</Main>
|
||||
|
@ -2,6 +2,8 @@ import { NextPage } from 'next'
|
||||
import { useRouter } from 'next/router'
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import axios from 'axios'
|
||||
import { useForm } from 'react-component-form'
|
||||
import type { HandleUseFormCallback } from 'react-component-form'
|
||||
|
||||
import { Head } from '../../components/Head'
|
||||
import { Header } from '../../components/Header'
|
||||
@ -13,22 +15,23 @@ import { Button } from '../../components/design/Button'
|
||||
import { authenticationFromServerSide } from '../../tools/authentication'
|
||||
import { AuthenticationForm } from '../../components/Authentication'
|
||||
import { ScrollableBody } from '../../components/ScrollableBody'
|
||||
import { HandleSubmitCallback, useForm } from '../../hooks/useForm'
|
||||
import { api } from '../../tools/api'
|
||||
import { userSchema } from '../../models/User'
|
||||
import { useFormTranslation } from '../../hooks/useFormTranslation'
|
||||
|
||||
const schema = {
|
||||
password: userSchema.password
|
||||
}
|
||||
|
||||
const ResetPassword: NextPage<FooterProps> = (props) => {
|
||||
const { t } = useTranslation()
|
||||
const router = useRouter()
|
||||
const { version } = props
|
||||
|
||||
const { fetchState, message, errors, getErrorTranslation, handleSubmit } =
|
||||
useForm({
|
||||
validateSchema: { password: userSchema.password },
|
||||
resetOnSuccess: true
|
||||
})
|
||||
const { handleUseForm, fetchState, message, errors } = useForm(schema)
|
||||
const { getFirstErrorTranslation } = useFormTranslation()
|
||||
|
||||
const onSubmit: HandleSubmitCallback = async (formData) => {
|
||||
const onSubmit: HandleUseFormCallback<typeof schema> = async (formData) => {
|
||||
try {
|
||||
await api.put(`/users/reset-password`, {
|
||||
...formData,
|
||||
@ -55,7 +58,7 @@ const ResetPassword: NextPage<FooterProps> = (props) => {
|
||||
<Head title={`Thream | ${t('authentication:reset-password')}`} />
|
||||
<Header />
|
||||
<Main>
|
||||
<AuthenticationForm onSubmit={handleSubmit(onSubmit)}>
|
||||
<AuthenticationForm onSubmit={handleUseForm(onSubmit)}>
|
||||
<Input
|
||||
type='password'
|
||||
placeholder='Password'
|
||||
@ -70,7 +73,9 @@ const ResetPassword: NextPage<FooterProps> = (props) => {
|
||||
id='message'
|
||||
state={fetchState}
|
||||
message={
|
||||
message != null ? message : getErrorTranslation(errors.password)
|
||||
message != null
|
||||
? t(message)
|
||||
: getFirstErrorTranslation(errors.password)
|
||||
}
|
||||
/>
|
||||
</Main>
|
||||
|
@ -1,7 +0,0 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 22 22">
|
||||
<g fill="none" fill-rule="evenodd" transform="matrix(1 0 0 -1 0 22)">
|
||||
<path fill="#F95555" fill-rule="nonzero" d="M11 0C4.925 0 0 4.925 0 11s4.925 11 11 11 11-4.925 11-11S17.075 0 11 0z" />
|
||||
<path fill="#FFF" fill-rule="nonzero" stroke="#FFF" stroke-width=".5" d="M9.875 7.063c0 .62.504 1.125 1.125 1.125s1.125-.504 1.125-1.126c0-.62-.504-1.125-1.125-1.125s-1.125.504-1.125 1.125z" />
|
||||
<rect width="2.25" height="6.75" x="9.875" y="9.875" fill="#FFF" rx="1" />
|
||||
</g>
|
||||
</svg>
|
Before Width: | Height: | Size: 571 B |
@ -1,4 +0,0 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="20" height="20" preserveAspectRatio="xMidYMid meet" viewBox="0 0 36 36">
|
||||
<path fill="#41ad49" d="M18 2a16 16 0 1 0 16 16A16 16 0 0 0 18 2Zm10.45 10.63L15.31 25.76L7.55 18a1.4 1.4 0 0 1 2-2l5.78 5.78l11.14-11.13a1.4 1.4 0 1 1 2 2Z" class="clr-i-solid clr-i-solid-path-1" />
|
||||
<path fill="none" d="M0 0h36v36H0z" />
|
||||
</svg>
|
Before Width: | Height: | Size: 443 B |
@ -43,3 +43,7 @@ body {
|
||||
scrollbar-color: var(--scroll-bar-color) var(--scroll-bar-bg-color);
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.message-options {
|
||||
@apply flex h-16 w-9 cursor-pointer items-center justify-center bg-gray-200 transition-colors hover:bg-gray-300 dark:bg-slate-900 hover:dark:bg-slate-800;
|
||||
}
|
||||
|
26
tools/ajv.ts
26
tools/ajv.ts
@ -1,26 +0,0 @@
|
||||
import Ajv from 'ajv'
|
||||
import addFormats from 'ajv-formats'
|
||||
|
||||
export const ajv = addFormats(
|
||||
new Ajv({
|
||||
allErrors: true
|
||||
}),
|
||||
[
|
||||
'date-time',
|
||||
'time',
|
||||
'date',
|
||||
'email',
|
||||
'hostname',
|
||||
'ipv4',
|
||||
'ipv6',
|
||||
'uri',
|
||||
'uri-reference',
|
||||
'uuid',
|
||||
'uri-template',
|
||||
'json-pointer',
|
||||
'relative-json-pointer',
|
||||
'regex'
|
||||
]
|
||||
)
|
||||
.addKeyword('kind')
|
||||
.addKeyword('modifier')
|
@ -1,6 +1,6 @@
|
||||
import axios from 'axios'
|
||||
|
||||
export const API_VERSION = '1.1.0'
|
||||
export const API_VERSION = '1.2.0'
|
||||
|
||||
export const API_DEFAULT_PORT = 8080
|
||||
|
||||
|
@ -5,6 +5,7 @@ import { API_URL } from '../api'
|
||||
import { cookies } from '../cookies'
|
||||
import { Tokens } from '.'
|
||||
import { fetchRefreshToken } from './authenticationFromServerSide'
|
||||
import { clearCache } from '../cache'
|
||||
|
||||
export class Authentication {
|
||||
public tokens: Tokens
|
||||
@ -15,7 +16,7 @@ export class Authentication {
|
||||
constructor(tokens: Tokens, disableSocketIO: boolean = false) {
|
||||
this.tokens = tokens
|
||||
this.accessTokenAge = Date.now()
|
||||
if (disableSocketIO || typeof window === 'undefined') {
|
||||
if (typeof window === 'undefined' || disableSocketIO) {
|
||||
this.socket = undefined
|
||||
} else {
|
||||
this.socket = io(API_URL, {
|
||||
@ -99,18 +100,28 @@ export class Authentication {
|
||||
|
||||
public signout(): void {
|
||||
cookies.remove('refreshToken')
|
||||
window.localStorage.clear()
|
||||
clearCache()
|
||||
window.location.href = '/authentication/signin'
|
||||
}
|
||||
|
||||
public async signoutServerSide(): Promise<void> {
|
||||
await this.api.post('/users/signout', {
|
||||
refreshToken: this.tokens.refreshToken
|
||||
})
|
||||
try {
|
||||
await this.api.post('/users/signout', {
|
||||
refreshToken: this.tokens.refreshToken
|
||||
})
|
||||
} catch {}
|
||||
this.signout()
|
||||
}
|
||||
|
||||
public async signoutAllDevicesServerSide(): Promise<void> {
|
||||
try {
|
||||
await this.api.delete('/users/signout')
|
||||
} catch {}
|
||||
this.signout()
|
||||
}
|
||||
|
||||
public signin(): void {
|
||||
clearCache()
|
||||
cookies.set('refreshToken', this.tokens.refreshToken)
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,7 @@ export const AuthenticationProvider: React.FC<
|
||||
const [user, setUser] = useState<UserCurrent>(props.authentication.user)
|
||||
|
||||
const authentication = useMemo(() => {
|
||||
const disableSocketIO = typeof window === 'undefined'
|
||||
return new Authentication(props.authentication.tokens, disableSocketIO)
|
||||
return new Authentication(props.authentication.tokens)
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- We only want to run this memo once
|
||||
}, [])
|
||||
|
@ -54,10 +54,13 @@ export const authenticationFromServerSide = (
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let data = {}
|
||||
let data: any = {}
|
||||
if (fetchData != null) {
|
||||
data = await fetchData(context, api)
|
||||
}
|
||||
if (data.notFound != null) {
|
||||
return data
|
||||
}
|
||||
return { props: data }
|
||||
}
|
||||
} else {
|
||||
@ -71,7 +74,7 @@ export const authenticationFromServerSide = (
|
||||
} else {
|
||||
try {
|
||||
let data: any = {}
|
||||
const authentication = new Authentication(tokens, true)
|
||||
const authentication = new Authentication(tokens)
|
||||
const { data: currentUser } = await authentication.api.get<
|
||||
unknown,
|
||||
AxiosResponse<UserCurrent>
|
||||
@ -79,7 +82,7 @@ export const authenticationFromServerSide = (
|
||||
if (fetchData != null) {
|
||||
data = await fetchData(context, authentication.api)
|
||||
}
|
||||
if (data.redirect != null) {
|
||||
if (data.notFound != null) {
|
||||
return data
|
||||
}
|
||||
return {
|
||||
@ -87,10 +90,7 @@ export const authenticationFromServerSide = (
|
||||
}
|
||||
} catch {
|
||||
return {
|
||||
redirect: {
|
||||
destination: '/404',
|
||||
permanent: false
|
||||
}
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ export type CacheKey =
|
||||
export const getPaginationCache = <T extends PaginationItem>(
|
||||
key: CacheKey
|
||||
): T[] => {
|
||||
const cache = localStorage.getItem(key)
|
||||
const cache = sessionStorage.getItem(key)
|
||||
if (cache != null) {
|
||||
try {
|
||||
const data = JSON.parse(cache)
|
||||
@ -30,5 +30,9 @@ export const savePaginationCache = <T extends PaginationItem>(
|
||||
key: CacheKey,
|
||||
data: T[]
|
||||
): void => {
|
||||
localStorage.setItem(key, JSON.stringify(data))
|
||||
sessionStorage.setItem(key, JSON.stringify(data))
|
||||
}
|
||||
|
||||
export const clearCache = (): void => {
|
||||
sessionStorage.clear()
|
||||
}
|
||||
|
Reference in New Issue
Block a user