From 5b8a560024c36ba990fcaadb737903654442290a Mon Sep 17 00:00:00 2001 From: miteruzo Date: Tue, 10 Jun 2025 01:23:24 +0900 Subject: [PATCH] =?UTF-8?q?#19=20=E3=83=88=E3=83=83=E3=83=97=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=81=A8=E6=96=B0=E8=A6=8F=E4=BD=9C=E6=88=90=E3=81=A1?= =?UTF-8?q?=E3=82=87=E3=81=A3=E3=81=A8=E3=81=A0=E3=81=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/controllers/posts_controller.rb | 3 - .../app/controllers/wiki_pages_controller.rb | 24 +- backend/app/models/wiki_page.rb | 25 +- backend/config/routes.rb | 5 +- frontend/src/App.tsx | 25 +- frontend/src/components/TopNav.tsx | 38 +++- frontend/src/pages/WikiDetailPage.tsx | 31 +++ frontend/src/pages/WikiNewPage.tsx | 214 ++++++++++++++++++ frontend/src/pages/WikiPage.tsx | 3 + frontend/src/pages/WikiShowPage.tsx | 26 --- 10 files changed, 327 insertions(+), 67 deletions(-) create mode 100644 frontend/src/pages/WikiDetailPage.tsx create mode 100644 frontend/src/pages/WikiNewPage.tsx delete mode 100644 frontend/src/pages/WikiShowPage.tsx diff --git a/backend/app/controllers/posts_controller.rb b/backend/app/controllers/posts_controller.rb index de2c9fd..990bd89 100644 --- a/backend/app/controllers/posts_controller.rb +++ b/backend/app/controllers/posts_controller.rb @@ -36,9 +36,6 @@ class PostsController < ApplicationController # POST /posts def create - logger.info ">>> thumbnail: #{params[:thumbnail]&.content_type}" -logger.info ">>> filename: #{params[:thumbnail]&.original_filename}" - return head :unauthorized unless current_user return head :forbidden unless ['admin', 'member'].include?(current_user.role) diff --git a/backend/app/controllers/wiki_pages_controller.rb b/backend/app/controllers/wiki_pages_controller.rb index 619eee7..477fa83 100644 --- a/backend/app/controllers/wiki_pages_controller.rb +++ b/backend/app/controllers/wiki_pages_controller.rb @@ -1,6 +1,5 @@ class WikiPagesController < ApplicationController def show - p params wiki_page = WikiPage.find_by(title: params[:title]) if wiki_page render plain: wiki_page.markdown @@ -9,15 +8,26 @@ class WikiPagesController < ApplicationController end end - def update + def create return head :unauthorized unless current_user - title = params[:title] - wiki_page = WikiPage.find_by(title: title) - unless wiki_page - wiki_page = WikiPage.new(title: title, created_user: current_user, updated_user: current_user) + wiki_page = WikiPage.new(title: params[:title], tag_id: params[:tag_id], created_user: current_user, updated_user: current_user) + wiki_page.markdown = params[:markdown], user: current_user + if wiki_page.save + render json: wiki_page, status: :created + else + render json: { errors: wiki_page.errors.full_messages }, status: :unprocessable_entity end - wiki_page.markdown = params[:markdown] + end + + def update + return head :unauthorized unless current_user + + wiki_page = WikiPage.find(params[:id]) + return head :not_found unless wiki_pages + + wiki_page.updated_user = current_user + wiki_page.markdown = params[:markdown], user: current_user wiki_page.save! head :ok end diff --git a/backend/app/models/wiki_page.rb b/backend/app/models/wiki_page.rb index e270dec..2722238 100644 --- a/backend/app/models/wiki_page.rb +++ b/backend/app/models/wiki_page.rb @@ -2,33 +2,36 @@ require 'gollum-lib' class WikiPage < ApplicationRecord - WIKI_PATH = Rails.root.join('wiki').to_s - belongs_to :tag, optional: true belongs_to :created_user, class_name: 'User', foreign_key: 'created_user_id' belongs_to :updated_user, class_name: 'User', foreign_key: 'updated_user_id' validates :title, presence: true, length: { maximum: 255 }, uniqueness: true - def markdown - wiki = Gollum::Wiki.new(WIKI_PATH) - page = wiki.page(title) + def body + page = wiki.page("#{ id }.md") page&.raw_data end - def markdown= content - wiki = Gollum::Wiki.new(WIKI_PATH) - - page = wiki.page(title) + def body= content, user: + page = wiki.page("#{ id }.md") commit_info = { message: "Update #{ title }", - name: current_user.id, + name: user.id, email: 'dummy@example.com' } if page page.update(content, commit: commit_info) else - wiki.write_page(title, :markdown, content, commit_info) + wiki.write_page("#{ id }.md", :markdown, content, commit_info) end end + + private + + WIKI_PATH = Rails.root.join('wiki').to_s + + def wiki + @wiki ||= Gollum::Wiki.new(WIKI_PATH) + end end diff --git a/backend/config/routes.rb b/backend/config/routes.rb index cfd3433..4640fb9 100644 --- a/backend/config/routes.rb +++ b/backend/config/routes.rb @@ -20,8 +20,9 @@ Rails.application.routes.draw do delete 'posts/:id/viewed', to: 'posts#unviewed' get 'preview/title', to: 'preview#title' get 'preview/thumbnail', to: 'preview#thumbnail' - get 'wiki/*title', to: 'wiki_pages#show', format: false - post 'wiki/*title', to: 'wiki_pages#save', format: false + get 'wiki/:title', to: 'wiki_pages#show' + post 'wiki', to: 'wiki_pages#create' + put 'wiki/:id', to: 'wiki_pages#update' # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 2bd7754..649c409 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,14 +1,16 @@ import React, { useEffect, useState } from 'react' import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom' -import HomePage from './pages/HomePage' -import TagPage from './pages/TagPage' -import TopNav from './components/TopNav' -import TagSidebar from './components/TagSidebar' -import PostPage from './pages/PostPage' -import PostNewPage from './pages/PostNewPage' -import PostDetailPage from './pages/PostDetailPage' -import WikiShowPage from './pages/WikiShowPage' -import { API_BASE_URL } from './config' +import HomePage from '@/pages/HomePage' +import TagPage from '@/pages/TagPage' +import TopNav from '@/components/TopNav' +import TagSidebar from '@/components/TagSidebar' +import PostPage from '@/pages/PostPage' +import PostNewPage from '@/pages/PostNewPage' +import PostDetailPage from '@/pages/PostDetailPage' +import WikiPage from '@/pages/WikiPage' +import WikiNewPage from '@/pages/WikiNewPage' +import WikiDetailPage from '@/pages/WikiDetailPage' +import { API_BASE_URL } from '@/config' import axios from 'axios' import { Toaster } from '@/components/ui/toaster' import { camelizeKeys } from 'humps' @@ -81,7 +83,10 @@ const App = () => { } /> } /> } /> - } /> + } /> + } /> + } /> + {/* } /> */} diff --git a/frontend/src/components/TopNav.tsx b/frontend/src/components/TopNav.tsx index 01ce35a..83e1468 100644 --- a/frontend/src/components/TopNav.tsx +++ b/frontend/src/components/TopNav.tsx @@ -1,5 +1,5 @@ -import React, { useState, useEffect } from "react" -import { Link, useLocation } from 'react-router-dom' +import React, { useState, useEffect } from 'react' +import { Link, useLocation, useParams } from 'react-router-dom' import SettingsDialogue from './SettingsDialogue' import { Button } from './ui/button' import clsx from 'clsx' @@ -25,9 +25,12 @@ const TopNav: React.FC = ({ user, setUser }: Props) => { const [settingsVisible, setSettingsVisible] = useState (false) const [selectedMenu, setSelectedMenu] = useState (Menu.None) - const MyLink = ({ to, title, menu }: { to: string; title: string; menu?: Menu }) => ( + const MyLink = ({ to, title, menu, base }: { to: string + title: string + menu?: Menu + base?: string }) => ( {title} @@ -50,11 +53,11 @@ const TopNav: React.FC = ({ user, setUser }: Props) => { <> {(() => { const className = 'bg-gray-700 text-white px-3 flex items-center w-full min-h-[40px]' - const subClass = 'hover:text-orange-500 h-full flex items-center px-4' + const subClass = 'hover:text-orange-500 h-full flex items-center px-3' + const inputBox = 'flex items-center px-3 mx-2' + const Separator = () => | switch (selectedMenu) { case Menu.Post: return (
一覧 - 投稿 + 投稿追加 +
) + case Menu.Wiki: + return ( +
+ + 検索 + 新規 + 全体履歴 + ヘルプ + {/^\/wiki\/(?!new|changes)[^\/]+/.test (location.pathname) && + <> + + 投稿 + 履歴 + 編輯 + }
) } }) ()} diff --git a/frontend/src/pages/WikiDetailPage.tsx b/frontend/src/pages/WikiDetailPage.tsx new file mode 100644 index 0000000..488360e --- /dev/null +++ b/frontend/src/pages/WikiDetailPage.tsx @@ -0,0 +1,31 @@ +import { useEffect, useState } from 'react' +import { Link, useParams } from 'react-router-dom' +import ReactMarkdown from 'react-markdown' +import axios from 'axios' +import { API_BASE_URL } from '@/config' + + +const WikiDetailPage = () => { + const { name } = useParams () + + const [markdown, setMarkdown] = useState (null) + + useEffect (() => { + void (axios.get (`${ API_BASE_URL }/wiki/${ encodeURIComponent (name) }`) + .then (res => setMarkdown (res.data)) + .catch (() => setMarkdown (null))) + }, [name]) + + return ( +
+ (href?.startsWith ('/') + ? {children} + : {children})) }}> + {markdown || `このページは存在しません。[新規作成してください](/wiki/new?title=${ name })。`} + +
) +} + + +export default WikiDetailPage diff --git a/frontend/src/pages/WikiNewPage.tsx b/frontend/src/pages/WikiNewPage.tsx new file mode 100644 index 0000000..f5c566a --- /dev/null +++ b/frontend/src/pages/WikiNewPage.tsx @@ -0,0 +1,214 @@ +import React, { useEffect, useState, useRef } from 'react' +import { Link, useLocation, useParams, useNavigate } from 'react-router-dom' +import axios from 'axios' +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' + +type Tag = { id: number + name: string + category: string } + +type Post = { id: number + url: string + title: string + thumbnail: string + tags: Tag[] + viewed: boolean } + + +const WikiNewPage = () => { + const location = useLocation () + const navigate = useNavigate () + + const [title, setTitle] = useState ('') + const [titleAutoFlg, setTitleAutoFlg] = useState (true) + const [titleLoading, setTitleLoading] = useState (false) + const [url, setURL] = useState ('') + const [thumbnailFile, setThumbnailFile] = useState (null) + const [thumbnailPreview, setThumbnailPreview] = useState ('') + const [thumbnailAutoFlg, setThumbnailAutoFlg] = useState (true) + const [thumbnailLoading, setThumbnailLoading] = useState (false) + const [tags, setTags] = useState ([]) + const [tagIds, setTagIds] = useState ([]) + const previousURLRef = useRef ('') + + const handleSubmit = () => { + const formData = new FormData () + formData.append ('title', title) + formData.append ('url', url) + formData.append ('tags', JSON.stringify (tagIds)) + formData.append ('thumbnail', thumbnailFile) + + void (axios.post (`${ API_BASE_URL }/posts`, formData, { headers: { + 'Content-Type': 'multipart/form-data', + 'X-Transfer-Code': localStorage.getItem ('user_code') || '' } }) + .then (() => { + toast ({ title: '投稿成功!' }) + navigate ('/posts') + }) + .catch (e => toast ({ title: '投稿失敗', + description: '入力を確認してください。' }))) + } + + useEffect (() => { + document.title = `新規 Wiki ページ | ${ SITE_TITLE }` + void (axios.get ('/api/tags') + .then (res => setTags (res.data)) + .catch (() => toast ({ title: 'タグ一覧の取得失敗' }))) + }, []) + + useEffect (() => { + if (titleAutoFlg && url) + fetchTitle () + }, [titleAutoFlg]) + + useEffect (() => { + if (thumbnailAutoFlg && url) + fetchThumbnail () + }, [thumbnailAutoFlg]) + + const handleURLBlur = () => { + if (!(url) || url === previousURLRef.current) + return + + if (titleAutoFlg) + fetchTitle () + if (thumbnailAutoFlg) + fetchThumbnail () + previousURLRef.current = url + } + + const fetchTitle = () => { + setTitle ('') + setTitleLoading (true) + void (axios.get (`${ API_BASE_URL }/preview/title`, { + params: { url }, + headers: { 'X-Transfer-Code': localStorage.getItem ('user_code') || '' } }) + .then (res => { + setTitle (res.data.title || '') + setTitleLoading (false) + }) + .finally (() => setTitleLoading (false))) + } + + const fetchThumbnail = () => { + setThumbnailPreview ('') + setThumbnailFile (null) + setThumbnailLoading (true) + if (thumbnailPreview) + URL.revokeObjectURL (thumbnailPreview) + void (axios.get (`${ API_BASE_URL }/preview/thumbnail`, { + params: { url }, + headers: { 'X-Transfer-Code': localStorage.getItem ('user_code') || '' }, + responseType: 'blob' }) + .then (res => { + const imageURL = URL.createObjectURL (res.data) + setThumbnailPreview (imageURL) + setThumbnailFile (new File ([res.data], + 'thumbnail.png', + { type: res.data.type || 'image/png' })) + setThumbnailLoading (false) + }) + .finally (() => setThumbnailLoading (false))) + } + + return ( +
+

新規 Wiki ページ

+ + {/* URL */} +
+ + setURL (e.target.value)} + className="w-full border p-2 rounded" + onBlur={handleURLBlur} /> +
+ + {/* タイトル */} +
+
+ + +
+ setTitle (e.target.value)} + disabled={titleAutoFlg} /> +
+ + {/* サムネール */} +
+
+ + +
+ {thumbnailAutoFlg + ? (thumbnailLoading + ?

Loading...

+ : !(thumbnailPreview) && ( +

+ URL から自動取得されます。 +

)) + : ( + { + const file = e.target.files?.[0] + if (file) + { + setThumbnailFile (file) + setThumbnailPreview (URL.createObjectURL (file)) + } + }} />)} + {thumbnailPreview && ( + preview)} +
+ + {/* タグ */} +
+ + +
+ + {/* 送信 */} + +
) +} + + +export default WikiNewPage diff --git a/frontend/src/pages/WikiPage.tsx b/frontend/src/pages/WikiPage.tsx index 9ac48e1..49343eb 100644 --- a/frontend/src/pages/WikiPage.tsx +++ b/frontend/src/pages/WikiPage.tsx @@ -20,3 +20,6 @@ const WikiPage = () => { return } + + +export default WikiPage diff --git a/frontend/src/pages/WikiShowPage.tsx b/frontend/src/pages/WikiShowPage.tsx deleted file mode 100644 index 2185527..0000000 --- a/frontend/src/pages/WikiShowPage.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import { useParams } from 'react-router-dom' -import ReactMarkdown from 'react-markdown' -import { useEffect, useState } from 'react' -import axios from 'axios' -import { API_BASE_URL } from '@/config' - - -const WikiShowPage = () => { - const { name } = useParams () - - const [markdown, setMarkdown] = useState ('') - - useEffect (() => { - void (axios.get (`${ API_BASE_URL }/wiki/${ encodeURIComponent (name) }`) - .then (res => setMarkdown (res.data)) - .catch (() => setMarkdown ('# ページが存在しません'))) - }, [name]) - - return ( -
- {markdown} -
) -} - - -export default WikiShowPage