@@ -1,32 +1,33 @@
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { Link, useLocation, useParams } from 'react-router-dom'
|
||||
import axios from 'axios'
|
||||
import { API_BASE_URL, SITE_TITLE } from '../config'
|
||||
import NicoViewer from '../components/NicoViewer'
|
||||
import { API_BASE_URL, SITE_TITLE } from '@/config'
|
||||
import NicoViewer from '@/components/NicoViewer'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { toast } from '@/components/ui/use-toast'
|
||||
import { cn } from '@/lib/utils'
|
||||
import MainArea from '@/components/layout/MainArea'
|
||||
import TagDetailSidebar from '@/components/TagDetailSidebar'
|
||||
|
||||
import type { Post, Tag } from '@/types'
|
||||
|
||||
type Props = { posts: Post[]
|
||||
setPosts: (posts: Post[]) => void }
|
||||
|
||||
|
||||
const PostDetailPage = ({ posts, setPosts }: Props) => {
|
||||
const PostDetailPage = () => {
|
||||
const { id } = useParams ()
|
||||
|
||||
const [post, setPost] = useState<Post | null> (null)
|
||||
|
||||
const location = useLocation ()
|
||||
|
||||
const changeViewedFlg = () => {
|
||||
if (posts[0]?.viewed)
|
||||
if (post?.viewed)
|
||||
{
|
||||
void (axios.delete (
|
||||
`${ API_BASE_URL }/posts/${ id }/viewed`,
|
||||
{ headers: { 'X-Transfer-Code': localStorage.getItem ('user_code') || '' } })
|
||||
.then (res => setPosts (([post]) => {
|
||||
.then (res => setPost (post => {
|
||||
post.viewed = false
|
||||
return [post]
|
||||
return post
|
||||
}))
|
||||
.catch (err => toast ({ title: '失敗……',
|
||||
description: '通信に失敗しました……' })))
|
||||
@@ -37,9 +38,9 @@ const PostDetailPage = ({ posts, setPosts }: Props) => {
|
||||
`${ API_BASE_URL }/posts/${ id }/viewed`,
|
||||
{ },
|
||||
{ headers: { 'X-Transfer-Code': localStorage.getItem ('user_code') || '' } })
|
||||
.then (res => setPosts (([post]) => {
|
||||
.then (res => setPost (post => {
|
||||
post.viewed = true
|
||||
return [post]
|
||||
return post
|
||||
}))
|
||||
.catch (err => toast ({ title: '失敗……',
|
||||
description: '通信に失敗しました……' })))
|
||||
@@ -51,40 +52,51 @@ const PostDetailPage = ({ posts, setPosts }: Props) => {
|
||||
return
|
||||
void (axios.get (`${ API_BASE_URL }/posts/${ id }`, { headers: {
|
||||
'X-Transfer-Code': localStorage.getItem ('user_code') || '' } })
|
||||
.then (res => setPosts ([res.data]))
|
||||
.then (res => setPost (res.data))
|
||||
.catch (err => console.error ('うんち!', err)))
|
||||
}, [id])
|
||||
|
||||
if (!(posts.length))
|
||||
return <div>Loading...</div>
|
||||
if (!(post))
|
||||
return (
|
||||
<>
|
||||
<TagDetailSidebar post={null} />
|
||||
<MainArea>Loading...</MainArea>
|
||||
</>)
|
||||
|
||||
const post = posts[0]
|
||||
if (post)
|
||||
document.title = `${ post.title || post.url } | ${ SITE_TITLE }`
|
||||
|
||||
document.title = `${ post.title || post.url } | ${ SITE_TITLE }`
|
||||
|
||||
const url = new URL (post.url)
|
||||
const url = post ? new URL (post.url) : undefined
|
||||
|
||||
return (
|
||||
<div className="p-4">
|
||||
{(() => {
|
||||
if (url.hostname.split ('.').slice (-2).join ('.') === 'nicovideo.jp')
|
||||
{
|
||||
return (
|
||||
<NicoViewer
|
||||
id={url.pathname.match (
|
||||
/(?<=\/watch\/)[a-zA-Z0-9]+?(?=\/|$)/)[0]}
|
||||
width="640"
|
||||
height="360" />)
|
||||
}
|
||||
else
|
||||
return <img src={post.thumbnail} alt={post.url} className="mb-4 w-full" />
|
||||
}) ()}
|
||||
<Button onClick={changeViewedFlg}
|
||||
className={cn ('text-white',
|
||||
posts[0]?.viewed ? 'bg-blue-600 hover:bg-blue-700' : 'bg-gray-500 hover:bg-gray-600')}>
|
||||
{posts[0]?.viewed ? '閲覧済' : '未閲覧'}
|
||||
</Button>
|
||||
</div>)
|
||||
<>
|
||||
<TagDetailSidebar post={post} />
|
||||
<MainArea>
|
||||
{post
|
||||
? (
|
||||
<div className="p-4">
|
||||
{(() => {
|
||||
if (url.hostname.split ('.').slice (-2).join ('.') === 'nicovideo.jp')
|
||||
{
|
||||
return (
|
||||
<NicoViewer
|
||||
id={url.pathname.match (
|
||||
/(?<=\/watch\/)[a-zA-Z0-9]+?(?=\/|$)/)[0]}
|
||||
width="640"
|
||||
height="360" />)
|
||||
}
|
||||
else
|
||||
return <img src={post.thumbnail} alt={post.url} className="mb-4 w-full" />
|
||||
}) ()}
|
||||
<Button onClick={changeViewedFlg}
|
||||
className={cn ('text-white',
|
||||
post.viewed ? 'bg-blue-600 hover:bg-blue-700' : 'bg-gray-500 hover:bg-gray-600')}>
|
||||
{post.viewed ? '閲覧済' : '未閲覧'}
|
||||
</Button>
|
||||
</div>)
|
||||
: 'Loading...'}
|
||||
</MainArea>
|
||||
</>)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import NicoViewer from '../components/NicoViewer'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { toast } from '@/components/ui/use-toast'
|
||||
import { cn } from '@/lib/utils'
|
||||
import MainArea from '@/components/layout/MainArea'
|
||||
|
||||
import type { Post, Tag } from '@/types'
|
||||
|
||||
@@ -14,7 +15,7 @@ type Props = { posts: Post[]
|
||||
setPosts: (posts: Post[]) => void }
|
||||
|
||||
|
||||
const PostNewPage = () => {
|
||||
export default () => {
|
||||
const location = useLocation ()
|
||||
const navigate = useNavigate ()
|
||||
|
||||
@@ -112,99 +113,98 @@ const PostNewPage = () => {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="max-w-xl mx-auto p-4 space-y-4">
|
||||
<h1 className="text-2xl font-bold mb-2">広場に投稿を追加する</h1>
|
||||
<MainArea>
|
||||
<div className="max-w-xl mx-auto p-4 space-y-4">
|
||||
<h1 className="text-2xl font-bold mb-2">広場に投稿を追加する</h1>
|
||||
|
||||
{/* URL */}
|
||||
<div>
|
||||
<label className="block font-semibold mb-1">URL</label>
|
||||
<input type="text"
|
||||
placeholder="例:https://www.nicovideo.jp/watch/..."
|
||||
value={url}
|
||||
onChange={e => setURL (e.target.value)}
|
||||
className="w-full border p-2 rounded"
|
||||
onBlur={handleURLBlur} />
|
||||
</div>
|
||||
|
||||
{/* タイトル */}
|
||||
<div>
|
||||
<div className="flex gap-2 mb-1">
|
||||
<label className="flex-1 block font-semibold">タイトル</label>
|
||||
<label className="flex items-center block gap-1">
|
||||
<input type="checkbox"
|
||||
checked={titleAutoFlg}
|
||||
onChange={e => setTitleAutoFlg (e.target.checked)} />
|
||||
自動
|
||||
</label>
|
||||
{/* URL */}
|
||||
<div>
|
||||
<label className="block font-semibold mb-1">URL</label>
|
||||
<input type="text"
|
||||
placeholder="例:https://www.nicovideo.jp/watch/..."
|
||||
value={url}
|
||||
onChange={e => setURL (e.target.value)}
|
||||
className="w-full border p-2 rounded"
|
||||
onBlur={handleURLBlur} />
|
||||
</div>
|
||||
<input type="text"
|
||||
className="w-full border rounded p-2"
|
||||
value={title}
|
||||
placeholder={titleLoading ? 'Loading...' : ''}
|
||||
onChange={e => setTitle (e.target.value)}
|
||||
disabled={titleAutoFlg} />
|
||||
</div>
|
||||
|
||||
{/* サムネール */}
|
||||
<div>
|
||||
<div className="flex gap-2 mb-1">
|
||||
<label className="block font-semibold flex-1">サムネール</label>
|
||||
<label className="flex items-center gap-1">
|
||||
<input type="checkbox"
|
||||
checked={thumbnailAutoFlg}
|
||||
onChange={e => setThumbnailAutoFlg (e.target.checked)} />
|
||||
自動
|
||||
</label>
|
||||
{/* タイトル */}
|
||||
<div>
|
||||
<div className="flex gap-2 mb-1">
|
||||
<label className="flex-1 block font-semibold">タイトル</label>
|
||||
<label className="flex items-center block gap-1">
|
||||
<input type="checkbox"
|
||||
checked={titleAutoFlg}
|
||||
onChange={e => setTitleAutoFlg (e.target.checked)} />
|
||||
自動
|
||||
</label>
|
||||
</div>
|
||||
<input type="text"
|
||||
className="w-full border rounded p-2"
|
||||
value={title}
|
||||
placeholder={titleLoading ? 'Loading...' : ''}
|
||||
onChange={e => setTitle (e.target.value)}
|
||||
disabled={titleAutoFlg} />
|
||||
</div>
|
||||
{thumbnailAutoFlg
|
||||
? (thumbnailLoading
|
||||
? <p className="text-gray-500 text-sm">Loading...</p>
|
||||
: !(thumbnailPreview) && (
|
||||
<p className="text-gray-500 text-sm">
|
||||
URL から自動取得されます。
|
||||
</p>))
|
||||
: (
|
||||
<input type="file"
|
||||
accept="image/*"
|
||||
onChange={e => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file)
|
||||
{
|
||||
setThumbnailFile (file)
|
||||
setThumbnailPreview (URL.createObjectURL (file))
|
||||
}
|
||||
}} />)}
|
||||
{thumbnailPreview && (
|
||||
<img src={thumbnailPreview}
|
||||
alt="preview"
|
||||
className="mt-2 max-h-48 rounded border" />)}
|
||||
</div>
|
||||
|
||||
{/* タグ */}
|
||||
<div>
|
||||
<label className="block font-semibold">タグ</label>
|
||||
<select multiple
|
||||
value={tagIds.map (String)}
|
||||
onChange={e => {
|
||||
const values = Array.from (e.target.selectedOptions).map (o => Number (o.value))
|
||||
setTagIds (values)
|
||||
}}
|
||||
className="w-full p-2 border rounded h-32">
|
||||
{tags.map ((tag: Tag) => (
|
||||
<option key={tag.id} value={tag.id}>
|
||||
{tag.name}
|
||||
</option>))}
|
||||
</select>
|
||||
</div>
|
||||
{/* サムネール */}
|
||||
<div>
|
||||
<div className="flex gap-2 mb-1">
|
||||
<label className="block font-semibold flex-1">サムネール</label>
|
||||
<label className="flex items-center gap-1">
|
||||
<input type="checkbox"
|
||||
checked={thumbnailAutoFlg}
|
||||
onChange={e => setThumbnailAutoFlg (e.target.checked)} />
|
||||
自動
|
||||
</label>
|
||||
</div>
|
||||
{thumbnailAutoFlg
|
||||
? (thumbnailLoading
|
||||
? <p className="text-gray-500 text-sm">Loading...</p>
|
||||
: !(thumbnailPreview) && (
|
||||
<p className="text-gray-500 text-sm">
|
||||
URL から自動取得されます。
|
||||
</p>))
|
||||
: (
|
||||
<input type="file"
|
||||
accept="image/*"
|
||||
onChange={e => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file)
|
||||
{
|
||||
setThumbnailFile (file)
|
||||
setThumbnailPreview (URL.createObjectURL (file))
|
||||
}
|
||||
}} />)}
|
||||
{thumbnailPreview && (
|
||||
<img src={thumbnailPreview}
|
||||
alt="preview"
|
||||
className="mt-2 max-h-48 rounded border" />)}
|
||||
</div>
|
||||
|
||||
{/* 送信 */}
|
||||
<button onClick={handleSubmit}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400"
|
||||
disabled={titleLoading || thumbnailLoading}>
|
||||
追加
|
||||
</button>
|
||||
</div>)
|
||||
{/* タグ */}
|
||||
<div>
|
||||
<label className="block font-semibold">タグ</label>
|
||||
<select multiple
|
||||
value={tagIds.map (String)}
|
||||
onChange={e => {
|
||||
const values = Array.from (e.target.selectedOptions).map (o => Number (o.value))
|
||||
setTagIds (values)
|
||||
}}
|
||||
className="w-full p-2 border rounded h-32">
|
||||
{tags.map ((tag: Tag) => (
|
||||
<option key={tag.id} value={tag.id}>
|
||||
{tag.name}
|
||||
</option>))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{/* 送信 */}
|
||||
<button onClick={handleSubmit}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400"
|
||||
disabled={titleLoading || thumbnailLoading}>
|
||||
追加
|
||||
</button>
|
||||
</div>
|
||||
</MainArea>)
|
||||
}
|
||||
|
||||
|
||||
export default PostNewPage
|
||||
|
||||
@@ -2,15 +2,14 @@ import React, { useEffect, useState } from 'react'
|
||||
import { Link, useLocation } from 'react-router-dom'
|
||||
import axios from 'axios'
|
||||
import { API_BASE_URL, SITE_TITLE } from '@/config'
|
||||
import TagSidebar from '@/components/TagSidebar'
|
||||
import MainArea from '@/components/layout/MainArea'
|
||||
|
||||
import type { Post, Tag } from '@/types'
|
||||
|
||||
type Props = { posts: Post[]
|
||||
setPosts: (posts: Post[]) => void }
|
||||
|
||||
|
||||
const PostPage = (props: Props) => {
|
||||
const { posts, setPosts } = props
|
||||
export default () => {
|
||||
const [posts, setPosts] = useState<Post[]> ([])
|
||||
|
||||
const location = useLocation ()
|
||||
const query = new URLSearchParams (location.search)
|
||||
@@ -42,17 +41,19 @@ const PostPage = (props: Props) => {
|
||||
}, [location.search])
|
||||
|
||||
return (
|
||||
<div className="flex flex-wrap gap-4 p-4">
|
||||
{posts.map (post => (
|
||||
<Link to={`/posts/${ post.id }`}
|
||||
key={post.id}
|
||||
className="w-40 h-40 overflow-hidden rounded-lg shadow-md hover:shadow-lg">
|
||||
<img src={post.thumbnail ?? post.thumbnail_base}
|
||||
className="object-none w-full h-full" />
|
||||
</Link>
|
||||
))}
|
||||
</div>)
|
||||
<>
|
||||
<TagSidebar posts={posts} />
|
||||
<MainArea>
|
||||
<div className="flex flex-wrap gap-4 p-4">
|
||||
{posts.map (post => (
|
||||
<Link to={`/posts/${ post.id }`}
|
||||
key={post.id}
|
||||
className="w-40 h-40 overflow-hidden rounded-lg shadow-md hover:shadow-lg">
|
||||
<img src={post.thumbnail ?? post.thumbnail_base}
|
||||
className="object-none w-full h-full" />
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
</MainArea>
|
||||
</>)
|
||||
}
|
||||
|
||||
|
||||
export default PostPage
|
||||
|
||||
@@ -2,8 +2,10 @@ import React, { useEffect, useState } from 'react'
|
||||
import axios from 'axios'
|
||||
import { useParams } from 'react-router-dom'
|
||||
import { API_BASE_URL } from '../config'
|
||||
import MainArea from '@/components/layout/MainArea'
|
||||
|
||||
const TagPage = () => {
|
||||
|
||||
export default () => {
|
||||
const { id } = useParams()
|
||||
const [posts, setPosts] = useState([])
|
||||
const [tagName, setTagName] = useState('')
|
||||
@@ -32,18 +34,17 @@ const TagPage = () => {
|
||||
}, [id])
|
||||
|
||||
return (
|
||||
<div className="container mx-auto p-4">
|
||||
<h1 className="text-2xl font-bold mb-4">タグ: {tagName}</h1>
|
||||
<div className="grid grid-cols-4 gap-4">
|
||||
{posts.map ((post, index) => (
|
||||
<div key={index} className="border rounded p-2">
|
||||
<img src={post.image_url} alt={post.title} className="w-full h-48 object-cover mb-2" />
|
||||
<h2 className="text-lg font-semibold">{post.title}</h2>
|
||||
<MainArea>
|
||||
<div className="container mx-auto p-4">
|
||||
<h1 className="text-2xl font-bold mb-4">タグ: {tagName}</h1>
|
||||
<div className="grid grid-cols-4 gap-4">
|
||||
{posts.map ((post, index) => (
|
||||
<div key={index} className="border rounded p-2">
|
||||
<img src={post.image_url} alt={post.title} className="w-full h-48 object-cover mb-2" />
|
||||
<h2 className="text-lg font-semibold">{post.title}</h2>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
</div>
|
||||
</MainArea>)
|
||||
}
|
||||
|
||||
export default TagPage
|
||||
|
||||
@@ -3,9 +3,10 @@ import { Link, useParams, useNavigate } from 'react-router-dom'
|
||||
import ReactMarkdown from 'react-markdown'
|
||||
import axios from 'axios'
|
||||
import { API_BASE_URL } from '@/config'
|
||||
import MainArea from '@/components/layout/MainArea'
|
||||
|
||||
|
||||
const WikiDetailPage = () => {
|
||||
export default () => {
|
||||
const { name } = useParams ()
|
||||
|
||||
const navigate = useNavigate ()
|
||||
@@ -26,15 +27,14 @@ const WikiDetailPage = () => {
|
||||
}, [name])
|
||||
|
||||
return (
|
||||
<div className="prose mx-auto p-4">
|
||||
<ReactMarkdown components={{ a: (
|
||||
({ href, children }) => (href?.startsWith ('/')
|
||||
? <Link to={href!}>{children}</Link>
|
||||
: <a href={href} target="_blank" rel="noopener noreferrer">{children}</a>)) }}>
|
||||
{markdown || `このページは存在しません。[新規作成してください](/wiki/new?title=${ name })。`}
|
||||
</ReactMarkdown>
|
||||
</div>)
|
||||
<MainArea>
|
||||
<div className="prose mx-auto p-4">
|
||||
<ReactMarkdown components={{ a: (
|
||||
({ href, children }) => (href?.startsWith ('/')
|
||||
? <Link to={href!}>{children}</Link>
|
||||
: <a href={href} target="_blank" rel="noopener noreferrer">{children}</a>)) }}>
|
||||
{markdown || `このページは存在しません。[新規作成してください](/wiki/new?title=${ name })。`}
|
||||
</ReactMarkdown>
|
||||
</div>
|
||||
</MainArea>)
|
||||
}
|
||||
|
||||
|
||||
export default WikiDetailPage
|
||||
|
||||
@@ -9,13 +9,14 @@ import { cn } from '@/lib/utils'
|
||||
import MarkdownIt from 'markdown-it'
|
||||
import MdEditor from 'react-markdown-editor-lite'
|
||||
import 'react-markdown-editor-lite/lib/index.css'
|
||||
import MainArea from '@/components/layout/MainArea'
|
||||
|
||||
import type { Tag } from '@/types'
|
||||
|
||||
const mdParser = new MarkdownIt
|
||||
|
||||
|
||||
const WikiNewPage = () => {
|
||||
export default () => {
|
||||
const location = useLocation ()
|
||||
const navigate = useNavigate ()
|
||||
|
||||
@@ -46,35 +47,34 @@ const WikiNewPage = () => {
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className="max-w-xl mx-auto p-4 space-y-4">
|
||||
<h1 className="text-2xl font-bold mb-2">新規 Wiki ページ</h1>
|
||||
<MainArea>
|
||||
<div className="max-w-xl mx-auto p-4 space-y-4">
|
||||
<h1 className="text-2xl font-bold mb-2">新規 Wiki ページ</h1>
|
||||
|
||||
{/* タイトル */}
|
||||
{/* TODO: タグ補完 */}
|
||||
<div>
|
||||
<label className="block font-semibold mb-1">タイトル</label>
|
||||
<input type="text"
|
||||
value={title}
|
||||
onChange={e => setTitle (e.target.value)}
|
||||
className="w-full border p-2 rounded" />
|
||||
{/* タイトル */}
|
||||
{/* TODO: タグ補完 */}
|
||||
<div>
|
||||
<label className="block font-semibold mb-1">タイトル</label>
|
||||
<input type="text"
|
||||
value={title}
|
||||
onChange={e => setTitle (e.target.value)}
|
||||
className="w-full border p-2 rounded" />
|
||||
</div>
|
||||
|
||||
{/* 本文 */}
|
||||
<div>
|
||||
<label className="block font-semibold mb-1">本文</label>
|
||||
<MdEditor value={body}
|
||||
style={{ height: '500px' }}
|
||||
renderHTML={text => mdParser.render (text)}
|
||||
onChange={({ text }) => setBody (text)} />
|
||||
</div>
|
||||
|
||||
{/* 送信 */}
|
||||
<button onClick={handleSubmit}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
|
||||
追加
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 本文 */}
|
||||
<div>
|
||||
<label className="block font-semibold mb-1">本文</label>
|
||||
<MdEditor value={body}
|
||||
style={{ height: '500px' }}
|
||||
renderHTML={text => mdParser.render (text)}
|
||||
onChange={({ text }) => setBody (text)} />
|
||||
</div>
|
||||
|
||||
{/* 送信 */}
|
||||
<button onClick={handleSubmit}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
|
||||
追加
|
||||
</button>
|
||||
</div>)
|
||||
</MainArea>)
|
||||
}
|
||||
|
||||
|
||||
export default WikiNewPage
|
||||
|
||||
新しい課題から参照
ユーザをブロックする