This commit is contained in:
dsyoon
2025-08-18 09:40:54 +09:00
commit d56d136522
26 changed files with 4593 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
const AuthContext = createContext();
export function AuthProvider({ children }) {
const [user, setUser] = useState(null); // { user_id, email }
const [loading, setLoading] = useState(true);
useEffect(() => {
try {
const saved = JSON.parse(localStorage.getItem('auth_user') || 'null');
if (saved && saved.user_id) setUser(saved);
} catch {}
setLoading(false);
}, []);
const login = async (email, password) => {
const res = await fetch('http://localhost:8010/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ email, password }),
});
if (!res.ok) {
// 서버가 에러 메시지를 JSON 으로 보낼 수도 있으므로 파싱 시도
let msg = '로그인 실패';
try { msg = (await res.json())?.detail || msg; } catch { /* ignore */ }
throw new Error(msg);
}
const data = await res.json(); // { user_id, email }
setUser(data);
try { localStorage.setItem('auth_user', JSON.stringify(data)); } catch {}
return data;
};
const logout = async () => {
try {
await fetch('http://localhost:8010/auth/logout', { method: 'POST', credentials: 'include' });
} catch {}
setUser(null);
try { localStorage.removeItem('auth_user'); } catch {}
};
const value = { user, loading, login, logout };
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
export function useAuth() {
return useContext(AuthContext);
}

View File

@@ -0,0 +1,95 @@
import React, { createContext, useState, useContext, useEffect } from 'react';
const ToolContext = createContext();
export function ToolProvider({ children }) {
const [tools, setTools] = useState([]);
const [selectedTool, setSelectedTool] = useState(null);
const [favorites, setFavorites] = useState(() => {
try {
return JSON.parse(localStorage.getItem('favorites')) || [];
} catch {
return [];
}
});
useEffect(() => {
async function load() {
try {
const res = await fetch('http://localhost:8010/tools');
const data = await res.json();
// 'GxP 챗봇'과 'chatgpt' 카드는 도구 목록에서 제거
const filtered = (Array.isArray(data) ? data : []).filter((tool) => !['chatbot_gxp','chatgpt'].includes(tool.id));
const mapped = filtered.map((tool) => {
const category = ['전체'];
if (['dev_chatbot', 'doc_translation'].includes(tool.id)) {
category.push('오픈AI');
} else if (['lims_text2sql', 'research_qa'].includes(tool.id)) {
category.push('내부AI');
}
return { ...tool, category };
});
setTools(mapped);
// 선택된 도구 복원 (마지막 카드 유지)
try {
const savedId = localStorage.getItem('selectedToolId');
if (savedId) {
const found = mapped.find((t) => t.id === savedId);
if (found) setSelectedTool(found);
}
// URL 경로가 카드 ID일 경우 해당 카드로 강제 설정 (예: /dev_chatbot)
const path = window.location.pathname.replace(/^\/+/, '');
const menuPaths = new Set(['chatting', 'tools', 'lecture', 'community', '']);
if (path && !menuPaths.has(path)) {
const fromUrl = mapped.find((t) => t.id === path);
if (fromUrl) setSelectedTool(fromUrl);
}
} catch {}
// 즐겨찾기에 남아 있을 수 있는 제거 대상 정리
setFavorites((prev) => prev.filter((id) => !['chatbot_gxp','chatgpt'].includes(id)));
} catch (_) {}
}
load();
}, []);
useEffect(() => {
localStorage.setItem('favorites', JSON.stringify(favorites));
}, [favorites]);
// 선택된 도구 지속화: 메뉴 이동 후에도 마지막 카드가 유지되도록 저장
useEffect(() => {
try {
if (selectedTool?.id) localStorage.setItem('selectedToolId', selectedTool.id);
} catch {}
}, [selectedTool?.id]);
const toggleFavorite = (id) => {
setFavorites((prev) => (prev.includes(id) ? prev.filter((f) => f !== id) : [...prev, id]));
};
// 카드 선택 시 URL을 카드 경로로 반영 (메뉴 페이지가 아닌 경우)
useEffect(() => {
if (!selectedTool?.id) return;
const path = `/${selectedTool.id}`;
const menuPaths = new Set(['/chatting','/tools','/lecture','/community','/']);
if (!menuPaths.has(window.location.pathname) && window.location.pathname === path) return;
// 도구 화면에서 카드 선택 시 URL을 카드 경로로 업데이트
if (window.location.pathname !== path) {
window.history.pushState({}, '', path);
}
}, [selectedTool?.id]);
const value = {
tools,
selectedTool,
setSelectedTool,
favorites,
toggleFavorite,
};
return <ToolContext.Provider value={value}>{children}</ToolContext.Provider>;
}
export function useTool() {
return useContext(ToolContext);
}