fix
This commit is contained in:
@@ -2,6 +2,8 @@ import React, { createContext, useContext, useState, useEffect } from "react";
|
||||
import { supabase } from "@/config/supabase";
|
||||
import { message } from "antd";
|
||||
import { useNavigate, useSearchParams, useLocation } from "react-router-dom";
|
||||
import { supabaseService } from "@/hooks/supabaseService";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
const AuthContext = createContext({});
|
||||
|
||||
@@ -9,7 +11,7 @@ export const AuthProvider = ({ children }) => {
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const [user, setUser] = useState(null);
|
||||
const [user, setUser] = useState({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -37,7 +39,9 @@ export const AuthProvider = ({ children }) => {
|
||||
data: { session },
|
||||
error,
|
||||
} = await supabase.auth.getSession();
|
||||
setUser(session?.user ?? null);
|
||||
|
||||
const role = await checkInTeam(session?.user ?? null);
|
||||
setUser({ ...session?.user, adminRole: role });
|
||||
} catch (error) {
|
||||
console.error("Error getting session:", error);
|
||||
} finally {
|
||||
@@ -46,17 +50,6 @@ export const AuthProvider = ({ children }) => {
|
||||
};
|
||||
|
||||
initSession();
|
||||
|
||||
// 订阅认证状态变化
|
||||
const {
|
||||
data: { subscription },
|
||||
} = supabase.auth.onAuthStateChange((_event, session) => {
|
||||
setUser(session?.user ?? null);
|
||||
});
|
||||
|
||||
return () => {
|
||||
subscription?.unsubscribe();
|
||||
};
|
||||
}, []);
|
||||
|
||||
// useEffect(() => {
|
||||
@@ -66,6 +59,45 @@ export const AuthProvider = ({ children }) => {
|
||||
// }
|
||||
// }, [location.pathname]);
|
||||
|
||||
//检查时候在管理模块团队中,没有就自动加入
|
||||
const checkInTeam = async (user) => {
|
||||
if (!user) return null;
|
||||
try {
|
||||
const { data: teamData, error: teamError } = await supabase
|
||||
.from("teams")
|
||||
.select("*")
|
||||
.eq("attributes->>type", "uppetaAdmin")
|
||||
.single();
|
||||
if (teamData) {
|
||||
const { data: teamMembers, error: teamMembersError } = await supabase
|
||||
.from("team_membership")
|
||||
.select("*")
|
||||
.eq("user_id", user.id)
|
||||
.eq("team_id", teamData.id)
|
||||
.single();
|
||||
if (!teamMembers) {
|
||||
// 自动加入团队
|
||||
const { data: teamMembershipData, error: teamMembershipError } =
|
||||
await supabaseService.insert("team_membership", {
|
||||
id: uuidv4(),
|
||||
user_id: user.id,
|
||||
team_id: teamData.id,
|
||||
role: "MEMBER",
|
||||
is_creator: false,
|
||||
});
|
||||
return "MEMBER";
|
||||
} else {
|
||||
return teamMembers.role;
|
||||
}
|
||||
} else {
|
||||
return "MEMBER";
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error checking in team:", error);
|
||||
return "MEMBER";
|
||||
}
|
||||
};
|
||||
|
||||
// 邮箱密码登录
|
||||
const login = async (email, password) => {
|
||||
try {
|
||||
@@ -79,7 +111,6 @@ export const AuthProvider = ({ children }) => {
|
||||
message.error(error.message || "登录失败,请稍后重试");
|
||||
return;
|
||||
}
|
||||
|
||||
setUser(data.user);
|
||||
return data;
|
||||
} catch (error) {
|
||||
@@ -152,7 +183,7 @@ export const AuthProvider = ({ children }) => {
|
||||
message.error(error.message || "登出失败,请稍后重试");
|
||||
return;
|
||||
}
|
||||
setUser(null);
|
||||
setUser({});
|
||||
message.success("已成功登出");
|
||||
navigate(`/login?redirectTo=${location.pathname}`, { replace: true });
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,143 +1,194 @@
|
||||
import React, { createContext,useContext, useState, useEffect, } from 'react';
|
||||
import React, { createContext, useContext, useState, useEffect } from "react";
|
||||
import { ConfigProvider, theme } from "antd";
|
||||
const ThemeContext = createContext();
|
||||
|
||||
export const ThemeProvider = ({ children }) => {
|
||||
const [isDarkMode, setIsDarkMode] = useState(() => {
|
||||
const savedTheme = localStorage.getItem('theme');
|
||||
return savedTheme === 'dark';
|
||||
const savedTheme = localStorage.getItem("theme");
|
||||
return savedTheme === "dark";
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
|
||||
document.documentElement.classList.toggle('dark', isDarkMode);
|
||||
localStorage.setItem("theme", isDarkMode ? "dark" : "light");
|
||||
document.documentElement.classList.toggle("dark", isDarkMode);
|
||||
}, [isDarkMode]);
|
||||
|
||||
const toggleTheme = () => {
|
||||
setIsDarkMode(!isDarkMode);
|
||||
const handleTheme = (event) => {
|
||||
const { clientX, clientY } = event;
|
||||
let outIsDark;
|
||||
|
||||
const toggleTheme = () => {
|
||||
setIsDarkMode((prev) => {
|
||||
const newPrev = !prev;
|
||||
outIsDark = newPrev;
|
||||
return newPrev;
|
||||
});
|
||||
};
|
||||
|
||||
if ("startViewTransition" in document) {
|
||||
const transition = document.startViewTransition(() => {
|
||||
toggleTheme();
|
||||
});
|
||||
|
||||
transition.ready.then(() => {
|
||||
const radius = Math.hypot(
|
||||
Math.max(clientX, innerWidth - clientX),
|
||||
Math.max(clientY, innerHeight - clientY)
|
||||
);
|
||||
|
||||
const clipPath = [
|
||||
`circle(0px at ${clientX}px ${clientY}px)`,
|
||||
`circle(${radius}px at ${clientX}px ${clientY}px)`,
|
||||
];
|
||||
|
||||
document.documentElement.animate(
|
||||
{ clipPath: outIsDark ? clipPath.reverse() : clipPath },
|
||||
{
|
||||
duration: 300,
|
||||
easing: "ease-in",
|
||||
pseudoElement: outIsDark
|
||||
? "::view-transition-old(root)"
|
||||
: "::view-transition-new(root)",
|
||||
}
|
||||
);
|
||||
});
|
||||
} else {
|
||||
toggleTheme();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider value={{ isDarkMode, toggleTheme }}>
|
||||
<ConfigProvider
|
||||
componentSize="small"
|
||||
theme={{
|
||||
algorithm: isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm,
|
||||
token: {
|
||||
// 主色调 - Google Blue
|
||||
colorPrimary: '#1a73e8',
|
||||
colorPrimaryHover: '#1557b0',
|
||||
colorPrimaryActive: '#174ea6',
|
||||
|
||||
// 成功色 - Google Green
|
||||
colorSuccess: '#1e8e3e',
|
||||
colorSuccessHover: '#188130',
|
||||
colorSuccessActive: '#137333',
|
||||
|
||||
// 警告色 - Google Yellow
|
||||
colorWarning: '#f9ab00',
|
||||
colorWarningHover: '#f29900',
|
||||
colorWarningActive: '#ea8600',
|
||||
|
||||
// 错误色 - Google Red
|
||||
colorError: '#d93025',
|
||||
colorErrorHover: '#c5221f',
|
||||
colorErrorActive: '#a50e0e',
|
||||
|
||||
// 文字颜色
|
||||
colorText: isDarkMode ? 'rgba(255, 255, 255, 0.87)' : 'rgba(0, 0, 0, 0.87)',
|
||||
colorTextSecondary: isDarkMode ? 'rgba(255, 255, 255, 0.60)' : 'rgba(0, 0, 0, 0.60)',
|
||||
colorTextTertiary: isDarkMode ? 'rgba(255, 255, 255, 0.38)' : 'rgba(0, 0, 0, 0.38)',
|
||||
|
||||
// 背景色
|
||||
colorBgContainer: isDarkMode ? '#1f1f1f' : '#ffffff',
|
||||
colorBgElevated: isDarkMode ? '#2d2d2d' : '#ffffff',
|
||||
colorBgLayout: isDarkMode ? '#121212' : '#f8f9fa',
|
||||
|
||||
// 边框
|
||||
borderRadius: 8,
|
||||
borderRadiusLG: 12,
|
||||
borderRadiusSM: 4,
|
||||
colorBorder: isDarkMode ? 'rgba(255, 255, 255, 0.12)' : 'rgba(0, 0, 0, 0.12)',
|
||||
|
||||
// 阴影
|
||||
boxShadow: '0 1px 3px 0 rgba(60,64,67,0.3), 0 4px 8px 3px rgba(60,64,67,0.15)',
|
||||
boxShadowSecondary: '0 1px 2px 0 rgba(60,64,67,0.3), 0 1px 3px 1px rgba(60,64,67,0.15)',
|
||||
|
||||
// 字体
|
||||
fontFamily: 'Google Sans, Roboto, Arial, sans-serif',
|
||||
fontSize: 14,
|
||||
|
||||
// 控件尺寸
|
||||
controlHeight: 36,
|
||||
controlHeightSM: 32,
|
||||
controlHeightLG: 44,
|
||||
},
|
||||
components: {
|
||||
Button: {
|
||||
algorithm: true,
|
||||
borderRadius: 20, // Google 风格的圆角按钮
|
||||
controlHeight: 36,
|
||||
controlHeightSM: 32,
|
||||
controlHeightLG: 44,
|
||||
paddingContentHorizontal: 24,
|
||||
},
|
||||
Input: {
|
||||
algorithm: true,
|
||||
controlHeight: 36,
|
||||
controlHeightSM: 32,
|
||||
controlHeightLG: 44,
|
||||
paddingContentHorizontal: 16,
|
||||
borderRadius: 4,
|
||||
controlOutline: 'none',
|
||||
controlOutlineWidth: 0,
|
||||
controlPaddingHorizontal: 16,
|
||||
controlPaddingHorizontalSM: 12,
|
||||
addonBg: 'transparent',
|
||||
},
|
||||
Select: {
|
||||
algorithm: true,
|
||||
controlHeight: 36,
|
||||
controlHeightSM: 32,
|
||||
controlHeightLG: 44,
|
||||
borderRadius: 4,
|
||||
controlOutline: 'none',
|
||||
controlOutlineWidth: 0,
|
||||
selectorBg: 'transparent',
|
||||
},
|
||||
Card: {
|
||||
algorithm: true,
|
||||
borderRadiusLG: 8,
|
||||
boxShadow: '0 1px 2px 0 rgba(60,64,67,0.3), 0 1px 3px 1px rgba(60,64,67,0.15)',
|
||||
},
|
||||
Menu: {
|
||||
algorithm: true,
|
||||
itemBorderRadius: 20, // Google 风格的圆角菜单项
|
||||
itemHeight: 36,
|
||||
itemHeightSM: 32,
|
||||
},
|
||||
Tabs: {
|
||||
algorithm: true,
|
||||
inkBarColor: '#1a73e8',
|
||||
itemSelectedColor: '#1a73e8',
|
||||
itemHoverColor: '#174ea6',
|
||||
},
|
||||
Table: {
|
||||
algorithm: true,
|
||||
<ThemeContext.Provider value={{ isDarkMode, toggleTheme: handleTheme }}>
|
||||
<ConfigProvider
|
||||
componentSize="small"
|
||||
theme={{
|
||||
algorithm: isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm,
|
||||
token: {
|
||||
// 主色调 - Google Blue
|
||||
colorPrimary: "#1a73e8",
|
||||
colorPrimaryHover: "#1557b0",
|
||||
colorPrimaryActive: "#174ea6",
|
||||
|
||||
// 成功色 - Google Green
|
||||
colorSuccess: "#1e8e3e",
|
||||
colorSuccessHover: "#188130",
|
||||
colorSuccessActive: "#137333",
|
||||
|
||||
// 警告色 - Google Yellow
|
||||
colorWarning: "#f9ab00",
|
||||
colorWarningHover: "#f29900",
|
||||
colorWarningActive: "#ea8600",
|
||||
|
||||
// 错误色 - Google Red
|
||||
colorError: "#d93025",
|
||||
colorErrorHover: "#c5221f",
|
||||
colorErrorActive: "#a50e0e",
|
||||
|
||||
// 文字颜色
|
||||
colorText: isDarkMode
|
||||
? "rgba(255, 255, 255, 0.87)"
|
||||
: "rgba(0, 0, 0, 0.87)",
|
||||
colorTextSecondary: isDarkMode
|
||||
? "rgba(255, 255, 255, 0.60)"
|
||||
: "rgba(0, 0, 0, 0.60)",
|
||||
colorTextTertiary: isDarkMode
|
||||
? "rgba(255, 255, 255, 0.38)"
|
||||
: "rgba(0, 0, 0, 0.38)",
|
||||
|
||||
// 背景色
|
||||
colorBgContainer: isDarkMode ? "#1f1f1f" : "#ffffff",
|
||||
colorBgElevated: isDarkMode ? "#2d2d2d" : "#ffffff",
|
||||
colorBgLayout: isDarkMode ? "#121212" : "#f8f9fa",
|
||||
|
||||
// 边框
|
||||
borderRadius: 8,
|
||||
headerBg: isDarkMode ? '#2d2d2d' : '#f8f9fa',
|
||||
borderRadiusLG: 12,
|
||||
borderRadiusSM: 4,
|
||||
colorBorder: isDarkMode
|
||||
? "rgba(255, 255, 255, 0.12)"
|
||||
: "rgba(0, 0, 0, 0.12)",
|
||||
|
||||
// 阴影
|
||||
boxShadow:
|
||||
"0 1px 3px 0 rgba(60,64,67,0.3), 0 4px 8px 3px rgba(60,64,67,0.15)",
|
||||
boxShadowSecondary:
|
||||
"0 1px 2px 0 rgba(60,64,67,0.3), 0 1px 3px 1px rgba(60,64,67,0.15)",
|
||||
|
||||
// 字体
|
||||
fontFamily: "Google Sans, Roboto, Arial, sans-serif",
|
||||
fontSize: 14,
|
||||
|
||||
// 控件尺寸
|
||||
controlHeight: 36,
|
||||
controlHeightSM: 32,
|
||||
controlHeightLG: 44,
|
||||
},
|
||||
Modal: {
|
||||
algorithm: true,
|
||||
borderRadius: 28, // Google 风格的大圆角对话框
|
||||
paddingContentHorizontal: 24,
|
||||
paddingContentVertical: 24,
|
||||
}
|
||||
}
|
||||
}}
|
||||
>
|
||||
{ children }
|
||||
</ConfigProvider>
|
||||
components: {
|
||||
Button: {
|
||||
algorithm: true,
|
||||
borderRadius: 20, // Google 风格的圆角按钮
|
||||
controlHeight: 36,
|
||||
controlHeightSM: 32,
|
||||
controlHeightLG: 44,
|
||||
paddingContentHorizontal: 24,
|
||||
},
|
||||
Input: {
|
||||
algorithm: true,
|
||||
controlHeight: 36,
|
||||
controlHeightSM: 32,
|
||||
controlHeightLG: 44,
|
||||
paddingContentHorizontal: 16,
|
||||
borderRadius: 4,
|
||||
controlOutline: "none",
|
||||
controlOutlineWidth: 0,
|
||||
controlPaddingHorizontal: 16,
|
||||
controlPaddingHorizontalSM: 12,
|
||||
addonBg: "transparent",
|
||||
},
|
||||
Select: {
|
||||
algorithm: true,
|
||||
controlHeight: 36,
|
||||
controlHeightSM: 32,
|
||||
controlHeightLG: 44,
|
||||
borderRadius: 4,
|
||||
controlOutline: "none",
|
||||
controlOutlineWidth: 0,
|
||||
selectorBg: "transparent",
|
||||
},
|
||||
Card: {
|
||||
algorithm: true,
|
||||
borderRadiusLG: 8,
|
||||
boxShadow:
|
||||
"0 1px 2px 0 rgba(60,64,67,0.3), 0 1px 3px 1px rgba(60,64,67,0.15)",
|
||||
},
|
||||
Menu: {
|
||||
algorithm: true,
|
||||
itemBorderRadius: 20, // Google 风格的圆角菜单项
|
||||
itemHeight: 36,
|
||||
itemHeightSM: 32,
|
||||
},
|
||||
Tabs: {
|
||||
algorithm: true,
|
||||
inkBarColor: "#1a73e8",
|
||||
itemSelectedColor: "#1a73e8",
|
||||
itemHoverColor: "#174ea6",
|
||||
},
|
||||
Table: {
|
||||
algorithm: true,
|
||||
borderRadius: 8,
|
||||
headerBg: isDarkMode ? "#2d2d2d" : "#f8f9fa",
|
||||
},
|
||||
Modal: {
|
||||
algorithm: true,
|
||||
borderRadius: 28, // Google 风格的大圆角对话框
|
||||
paddingContentHorizontal: 24,
|
||||
paddingContentVertical: 24,
|
||||
},
|
||||
},
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</ConfigProvider>
|
||||
</ThemeContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -145,7 +196,7 @@ export const ThemeProvider = ({ children }) => {
|
||||
export const useTheme = () => {
|
||||
const context = useContext(ThemeContext);
|
||||
if (!context) {
|
||||
throw new Error('useTheme must be used within a ThemeProvider');
|
||||
throw new Error("useTheme must be used within a ThemeProvider");
|
||||
}
|
||||
return context;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user