2026-07-05 01:15:55 +04:00
'use client'
import { useState } from 'react'
import PropTypes from 'prop-types'
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'
import Dialog from '@mui/material/Dialog'
import DialogTitle from '@mui/material/DialogTitle'
import DialogContent from '@mui/material/DialogContent'
import DialogActions from '@mui/material/DialogActions'
import TextField from '@mui/material/TextField'
import Typography from '@mui/material/Typography'
2026-07-07 16:11:46 +04:00
import FormControl from '@mui/material/FormControl'
import InputLabel from '@mui/material/InputLabel'
import Select from '@mui/material/Select'
import MenuItem from '@mui/material/MenuItem'
2026-07-05 01:15:55 +04:00
import Link from 'next/link'
2026-07-07 16:11:46 +04:00
import { Mastodon } from '@icons-pack/react-simple-icons'
2026-07-05 01:15:55 +04:00
const BOKANTE_URL = process . env . NEXT_PUBLIC_BOKANTE_URL || 'https://bokante.o-k-i.net'
const BOKANTE_ACCOUNT = process . env . NEXT_PUBLIC_BOKANTE_ACCOUNT || 'pawol_nu'
2026-07-07 16:11:46 +04:00
const INSTANCES_SUGGEREES = [
{ domain : 'bokante.o-k-i.net' , label : 'BOKANTE - notre instance' },
{ domain : 'mastodon.social' , label : 'mastodon.social' },
{ domain : 'piaille.fr' , label : 'piaille.fr' },
]
const AUTRE_INSTANCE = 'autre'
2026-07-05 01:15:55 +04:00
function extraireInstance ( saisie ) {
const valeur = saisie . trim (). replace ( /^@/ , '' ). replace ( /^https?:\/\// , '' ). replace ( /\/$/ , '' )
return valeur . includes ( '@' ) ? valeur . split ( '@' ). pop () : valeur
}
export default function ReponsMastodon ({ bokanteStatusId }) {
const [ ouvert , setOuvert ] = useState ( false )
2026-07-07 16:11:46 +04:00
const [ instance , setInstance ] = useState ( INSTANCES_SUGGEREES [ 0 ]. domain )
const [ autreInstance , setAutreInstance ] = useState ( '' )
2026-07-05 01:15:55 +04:00
if ( ! bokanteStatusId ) {
return null
}
const tootUrl = ` ${ BOKANTE_URL } /@ ${ BOKANTE_ACCOUNT } / ${ bokanteStatusId } `
const repondre = () => {
2026-07-07 16:11:46 +04:00
const domaine = instance === AUTRE_INSTANCE ? extraireInstance ( autreInstance ) : instance
if ( ! domaine ) {
2026-07-05 01:15:55 +04:00
return
}
2026-07-07 16:11:46 +04:00
window . open ( `https:// ${ domaine } /authorize_interaction?uri= ${ encodeURIComponent ( tootUrl ) } ` , '_blank' , 'noopener,noreferrer' )
2026-07-05 01:15:55 +04:00
setOuvert ( false )
2026-07-07 16:11:46 +04:00
setAutreInstance ( '' )
2026-07-05 01:15:55 +04:00
}
return (
< Box sx = {{ maxWidth : 700 , margin : '1em auto 0' , textAlign : 'center' }}>
< Typography variant = 'body2' sx = {{ mb : 1 }}>
2026-07-07 16:11:46 +04:00
Rejoignez la discussion sur le Fediverse via { ' ' }
< Link href = { tootUrl } target = '_blank' rel = 'noopener noreferrer' > BOKANTE ( Mastodon )</ Link >
2026-07-05 01:15:55 +04:00
</ Typography >
2026-07-07 16:11:46 +04:00
< Button
variant = 'outlined'
size = 'small'
startIcon = {< Mastodon size = { 16 } color = '#6364FF' title = '' />}
onClick = {() => setOuvert ( true )}
>
Répondre sur Mastodon
2026-07-05 01:15:55 +04:00
</ Button >
< Dialog open = { ouvert } onClose = {() => setOuvert ( false )}>
2026-07-07 16:11:46 +04:00
< DialogTitle > Répondre depuis Mastodon </ DialogTitle >
2026-07-05 01:15:55 +04:00
< DialogContent >
< Typography variant = 'body2' sx = {{ mb : 2 }}>
2026-07-07 16:11:46 +04:00
Choisissez l ’ instance Mastodon sur laquelle vous avez un compte .
BOKANTE est l ’ instance de OKI : vous pouvez aussi utiliser
n ’ importe quelle autre instance du Fediverse .
2026-07-05 01:15:55 +04:00
</ Typography >
2026-07-07 16:11:46 +04:00
< FormControl fullWidth sx = {{ mb : 2 }}>
< InputLabel id = 'instance-mastodon-label' > Instance Mastodon </ InputLabel >
< Select
labelId = 'instance-mastodon-label'
id = 'instance-mastodon'
value = { instance }
label = 'Instance Mastodon'
onChange = { event => setInstance ( event . target . value )}
>
{ INSTANCES_SUGGEREES . map (({ domain , label }) => (
< MenuItem key = { domain } value = { domain }>
{ label }
</ MenuItem >
))}
< MenuItem value = { AUTRE_INSTANCE }> Autre instance … </ MenuItem >
</ Select >
</ FormControl >
{ instance === AUTRE_INSTANCE && (
< TextField
autoFocus
fullWidth
label = '@vous@votre-instance'
helperText = 'Indiquez votre identifiant (@vous@votre-instance) ou juste le nom de votre instance, vous serez redirigé·e vers votre compte pour répondre.'
value = { autreInstance }
onChange = { event => setAutreInstance ( event . target . value )}
onKeyDown = { event => {
if ( event . key === 'Enter' ) {
repondre ()
}
}}
/>
)}
2026-07-05 01:15:55 +04:00
</ DialogContent >
< DialogActions >
< Button onClick = {() => setOuvert ( false )}> Annuler </ Button >
< Button variant = 'contained' onClick = { repondre }> Continuer </ Button >
</ DialogActions >
</ Dialog >
</ Box >
)
}
ReponsMastodon . propTypes = {
bokanteStatusId : PropTypes . string
}