ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

58 lines
1.7 KiB

  1. import { useState } from 'react'
  2. import { Button } from '@/components/ui/button'
  3. import { Dialog,
  4. DialogContent,
  5. DialogTitle } from '@/components/ui/dialog'
  6. import { Input } from '@/components/ui/input'
  7. import { toast } from '@/components/ui/use-toast'
  8. import { apiPost } from '@/lib/api'
  9. import type { User } from '@/types'
  10. type Props = { visible: boolean
  11. onVisibleChange: (visible: boolean) => void
  12. setUser: (user: User) => void }
  13. export default ({ visible, onVisibleChange, setUser }: Props) => {
  14. const [inputCode, setInputCode] = useState ('')
  15. const handleTransfer = async () => {
  16. if (!(confirm ('引継ぎを行ってもよろしいですか?\n現在のアカウントからはログアウトされます.')))
  17. return
  18. try
  19. {
  20. const data = await apiPost<{ valid: boolean; user: User }> (
  21. '/users/verify', { code: inputCode })
  22. if (data.valid)
  23. {
  24. localStorage.setItem ('user_code', inputCode)
  25. setUser (data.user)
  26. toast ({ title: '引継ぎ成功!' })
  27. onVisibleChange (false)
  28. }
  29. else
  30. toast ({ title: '認証失敗', description: 'そのコードは使へません.' })
  31. }
  32. catch
  33. {
  34. toast ({ title: '通信エラー', description: 'またあとで試してね.' })
  35. }
  36. }
  37. return (
  38. <Dialog open={visible} onOpenChange={onVisibleChange}>
  39. <DialogContent>
  40. <DialogTitle>ほかのブラウザから引継ぐ</DialogTitle>
  41. <div className="flex gap-2">
  42. <Input placeholder="引継ぎコードを入力"
  43. value={inputCode}
  44. onChange={ev => setInputCode (ev.target.value)}/>
  45. <Button onClick={handleTransfer}>引継ぐ</Button>
  46. </div>
  47. </DialogContent>
  48. </Dialog>)
  49. }