feat/0.2:add theme setting and about page setting and support use email and password login

This commit is contained in:
lethe committed 2025-07-24 10:28:42 +08:00
1 parent 58292b0e6e
commit ca2b732952
85 files changed
+5797 -816

No files matched your search

@@ -1,136 +0,0 @@
'use client';
import React, { useEffect, useState } from 'react';
import { Input, Button, Switch, AccordionItem, Accordion } from '@nextui-org/react';
import { Eye, EyeOff } from 'lucide-react';
import { useTranslation } from "@/shared/lib/useTranslation";
import { getGoogleRedirectUri } from '@/hooks/useLogin';
import { GoogleAuthConfig } from '@/shared/types/system';
interface GoogleAuthConfigFormProps {
config: GoogleAuthConfig;
onSave: (config: GoogleAuthConfig) => void;
isLoading?: boolean;
}
export const GoogleAuthConfigForm: React.FC<GoogleAuthConfigFormProps> = ({
config,
onSave,
isLoading = false,
}) => {
const { t } = useTranslation();
const [formData, setFormData] = useState<GoogleAuthConfig>(config);
const [showClientSecret, setShowClientSecret] = useState(false);
useEffect(() => {
setFormData(config);
}, [config]);
const handleInputChange = (field: keyof GoogleAuthConfig, value: string | boolean) => {
setFormData(prev => ({
...prev,
[field]: value
}));
};
const handleSave = () => {
onSave(formData);
};
const isFormValid = formData.client_id.trim() !== '';
return (
<Accordion variant='splitted' itemClasses={{
base: 'shadow-none border-1',
}} defaultExpandedKeys={['google-auth-config']} className='px-0'>
<AccordionItem key="google-auth-config" title={
<div className="flex flex-col justify-between">
<h3 className="text-lg font-semibold">{t('Google Login Settings')}</h3>
<p className="text-sm text-gray-500">{t('Configure Google OAuth integration')}</p>
</div>
}>
<div className="space-y-4">
{/* enable google login */}
<div className="flex items-center justify-between p-4 bg-gray-50 rounded-lg">
<div className="flex flex-col gap-1">
<span className="text-sm font-medium">{t('Enable Google Login')}</span>
<span className="text-xs text-gray-500">{t('Allow users to sign in with Google')}</span>
</div>
<Switch
isSelected={formData.is_enabled}
onValueChange={(value) => {
handleInputChange('is_enabled', value);
onSave({
...formData,
is_enabled: value
});
}}
color="primary"
size='sm'
/>
</div>
{/* Google Client ID */}
<Input
label={t('Google Client ID')}
placeholder={t('OAuth 2.0 Client ID from Google Cloud Console')}
value={formData.client_id}
onChange={(e) => handleInputChange('client_id', e.target.value)}
isRequired
isDisabled={!formData.is_enabled}
/>
{/* Google Client Secret */}
<Input
label={t('Google Client Secret')}
placeholder={t('OAuth 2.0 Client Secret (optional, for server-side verification)')}
value={formData.client_secret || ''}
onChange={(e) => handleInputChange('client_secret', e.target.value)}
type={showClientSecret ? "text" : "password"}
endContent={
<button
type="button"
onClick={() => setShowClientSecret(!showClientSecret)}
className="focus:outline-none"
disabled={!formData.is_enabled}
>
{showClientSecret ? (
<EyeOff className="w-4 h-4 text-gray-400" />
) : (
<Eye className="w-4 h-4 text-gray-400" />
)}
</button>
}
isDisabled={!formData.is_enabled}
/>
{/* Redirect URI */}
<Input
label={t('Redirect URI')}
placeholder="https://yourdomain.com/auth/google/callback"
description={t('Authorized redirect URI configured in Google Cloud Console')}
value={getGoogleRedirectUri()}
isDisabled={true}
/>
</div>
{/* Action Buttons */}
{
formData.is_enabled && (
<div className="flex gap-3 py-4">
<Button
color="primary"
variant="solid"
onPress={handleSave}
isLoading={isLoading}
isDisabled={!isFormValid}
size='sm'
>
{t('Save')}
</Button>
</div>
)
}
</AccordionItem>
</Accordion>
);
};
@@ -1,21 +1,41 @@
'use client';
"use client";
import React, { useState, useEffect } from 'react';
import { Input, Button, AccordionItem, Accordion, Select, SelectItem } from '@nextui-org/react';
import { PlatformConfig } from '@/shared/types/system';
import React, { useState, useEffect } from "react";
import { calculateFileSha256 } from "@/shared/utils/crypto";
import {
Input,
Button,
AccordionItem,
Accordion,
Select,
SelectItem,
} from "@nextui-org/react";
import MDEditor, { commands } from "@uiw/react-md-editor";
import { PlatformConfig, Theme } from "@/shared/types/system";
import { useTranslation } from "@/shared/lib/useTranslation";
import { SUPPORTED_LANGUAGES } from '@/shared/lib/i18n';
import { SUPPORTED_LANGUAGES } from "@/shared/lib/i18n";
import { ThemeSelector } from "./ThemeSelector";
import "@uiw/react-md-editor/markdown-editor.css";
import "@uiw/react-markdown-preview/markdown.css";
interface PlatformConfigFormProps {
config: PlatformConfig;
onSave: (config: PlatformConfig) => void;
onThemeChange?: (theme: Theme) => Promise<void>;
onAboutSave?: (aboutContent: string) => Promise<void>;
onImageUpload?: (file: File, sha256?: string) => Promise<string>;
isLoading?: boolean;
isAboutLoading?: boolean;
}
export const PlatformConfigForm: React.FC<PlatformConfigFormProps> = ({
config,
onSave,
onThemeChange,
onAboutSave,
onImageUpload,
isLoading = false,
isAboutLoading = false,
}) => {
const { t } = useTranslation();
const [formData, setFormData] = useState<PlatformConfig>(config);
@@ -26,9 +46,9 @@ export const PlatformConfigForm: React.FC<PlatformConfigFormProps> = ({
}, [config]);
const handleInputChange = (field: keyof PlatformConfig, value: string) => {
setFormData(prev => ({
setFormData((prev) => ({
...prev,
[field]: value
[field]: value,
}));
};
@@ -36,122 +56,277 @@ export const PlatformConfigForm: React.FC<PlatformConfigFormProps> = ({
onSave(formData);
};
const isFormValid = formData.name.trim() !== '' && formData.currency?.trim() !== '';
const handleThemeChange = async (theme: Theme) => {
handleInputChange("theme", theme);
onThemeChange?.(theme);
};
const handleAboutSave = async () => {
onAboutSave?.(formData.about_page || "");
};
const handleImageUpload = async (file: File): Promise<string> => {
try {
// 计算文件的 SHA256 哈希值
const sha256 = await calculateFileSha256(file);
return await onImageUpload!(file, sha256);
} catch (error) {
console.error("Image upload failed:", error);
throw error;
}
};
return (
<Accordion variant='splitted' itemClasses={{
base: 'shadow-none border-1',
}} defaultExpandedKeys={['platform-config']} className='px-0'>
<AccordionItem key="platform-config" title={
<div className="flex flex-col justify-between">
<h3 className="text-lg font-semibold">{t('Platform Information')}</h3>
<p className="text-sm text-gray-500">{t('Configure platform logo, name, titles, language')}</p>
</div>
}>
<div className="space-y-4">
{/* platform name */}
<Input
label={t('Platform Name')}
placeholder={t('Enter platform name')}
description={t('The name displayed across the platform')}
value={formData.name}
onChange={(e) => handleInputChange('name', e.target.value)}
isRequired
/>
<div className="space-y-4">
{/* Platform Information Accordion */}
<Accordion
variant="splitted"
itemClasses={{
base: "shadow-none border-1",
}}
defaultExpandedKeys={["platform-config"]}
className="px-0"
>
<AccordionItem
key="platform-config"
title={
<div className="flex flex-col justify-between">
<h3 className="text-lg font-semibold">
{t("Platform Information")}
</h3>
<p className="text-sm text-gray-500">
{t("Configure platform logo, name, titles, and language")}
</p>
</div>
}
>
<div className="space-y-4">
{/* platform name */}
<Input
label={t("Platform Name")}
placeholder={t("Enter platform name")}
description={t("The name displayed across the platform")}
value={formData.name}
onChange={(e) => handleInputChange("name", e.target.value)}
/>
{/* platform logo */}
<Input
label={t('Platform Logo URL')}
placeholder="https://example.com/logo.png"
description={t('URL of the platform logo image (optional)')}
value={formData.logo || ''}
onChange={(e) => handleInputChange('logo', e.target.value)}
/>
{/* platform logo */}
<Input
label={t("Platform Logo URL")}
placeholder="https://example.com/logo.png"
description={t("URL of the platform logo image (optional)")}
value={formData.logo || ""}
onChange={(e) => handleInputChange("logo", e.target.value)}
/>
{/* website title */}
<Input
label={t('Website Title')}
placeholder={t('Enter website title')}
description={t('The title displayed in browser tab and search results')}
value={formData.website_title || ''}
onChange={(e) => handleInputChange('website_title', e.target.value)}
/>
{/* homepage headline */}
<Input
label={t('Headline')}
placeholder={t('Enter homepage headline')}
description={t('Main headline displayed on the homepage')}
value={formData.headline || ''}
onChange={(e) => handleInputChange('headline', e.target.value)}
/>
{/* homepage subheadline */}
<Input
label={t('Subheadline')}
placeholder={t('Enter homepage subheadline')}
description={t('Secondary headline displayed below the main headline')}
value={formData.subheadline || ''}
onChange={(e) => handleInputChange('subheadline', e.target.value)}
/>
{/* platform language */}
<Select
label={t('Platform Language')}
placeholder={t('Select platform language')}
selectedKeys={formData.language ? [formData.language] : ['en']}
onSelectionChange={(keys) => {
const selectedKey = Array.from(keys)[0] as string;
if (selectedKey) {
handleInputChange('language', selectedKey);
{/* website title */}
<Input
label={t("Website Title")}
placeholder={t("Enter website title")}
description={t(
"The title displayed in browser tab and search results"
)}
value={formData.website_title || ""}
onChange={(e) =>
handleInputChange("website_title", e.target.value)
}
}}
>
{SUPPORTED_LANGUAGES.map((language) => (
<SelectItem key={language.name} value={language.name}>
{language.label}
</SelectItem>
))}
</Select>
/>
{/* comming soon:currency unit */}
{/* <Select
label={t('Currency Unit')}
placeholder={t('Select currency')}
description={t('Default currency for the platform')}
selectedKeys={formData.currency ? [formData.currency] : []}
onSelectionChange={(keys) => {
const selectedKey = Array.from(keys)[0] as string;
if (selectedKey) {
handleInputChange('currency', selectedKey);
{/* homepage headline */}
<Input
label={t("Headline")}
placeholder={t("Enter homepage headline")}
description={t("Main headline displayed on the homepage")}
value={formData.headline || ""}
onChange={(e) => handleInputChange("headline", e.target.value)}
/>
{/* homepage subheadline */}
<Input
label={t("Subheadline")}
placeholder={t("Enter homepage subheadline")}
description={t(
"Secondary headline displayed below the main headline"
)}
value={formData.subheadline || ""}
onChange={(e) => handleInputChange("subheadline", e.target.value)}
/>
{/* platform language */}
<Select
label={t("Platform Language")}
placeholder={t("Select platform language")}
selectedKeys={formData.language ? [formData.language] : ["en"]}
onSelectionChange={(keys) => {
const selectedKey = Array.from(keys)[0] as string;
if (selectedKey) {
handleInputChange("language", selectedKey);
}
}}
>
{SUPPORTED_LANGUAGES.map((language) => (
<SelectItem key={language.name} value={language.name}>
{language.label}
</SelectItem>
))}
</Select>
</div>
{/* Action Buttons */}
<div className="flex gap-3 py-4">
<Button
color="primary"
variant="solid"
onPress={handleSave}
isLoading={isLoading}
size="sm"
>
{t("Save")}
</Button>
</div>
</AccordionItem>
</Accordion>
{/* About Page Accordion */}
<Accordion
variant="splitted"
itemClasses={{
base: "shadow-none border-1",
}}
className="px-0"
>
<AccordionItem
key="about-config"
title={
<div className="flex flex-col justify-between">
<h3 className="text-lg font-semibold">
{t("About Page Content")}
</h3>
<p className="text-sm text-gray-500">
{t("Configure about page content")}
</p>
</div>
}
>
<div className="space-y-4">
<MDEditor
value={formData.about_page || ""}
onChange={(value?: string) =>
handleInputChange("about_page", value || "")
}
}}
isRequired
>
{getCurrencyOptions().map((currency) => (
<SelectItem key={currency.value} value={currency.value}>
{currency.label}
</SelectItem>
))}
</Select> */}
</div>
preview="live"
hideToolbar={false}
visibleDragbar={false}
data-color-mode="light"
textareaProps={{
placeholder: t("Enter about page content (supports Markdown)"),
style: {
fontSize: 14,
lineHeight: 1.6,
fontFamily:
'ui-monospace, SFMono-Regular, "SF Mono", Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
},
}}
height={300}
commands={[
// 默认命令
commands.bold,
commands.italic,
commands.strikethrough,
commands.hr,
commands.title,
commands.divider,
commands.link,
commands.quote,
commands.code,
commands.codeBlock,
commands.comment,
commands.divider,
commands.unorderedListCommand,
commands.orderedListCommand,
commands.checkedListCommand,
commands.divider,
// 自定义图片上传命令
{
name: "image-upload",
keyCommand: "image-upload",
buttonProps: {
"aria-label": "Upload image",
title: "Upload image",
},
icon: (
<svg width="12" height="12" viewBox="0 0 20 20">
<path
fill="currentColor"
d="M15 9c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm4-7H1c-.55 0-1 .45-1 1v14c0 .55.45 1 1 1h18c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zM2 17l4.5-6 3.5 4.51 2.5-3.01L17 17H2z"
/>
</svg>
),
execute: (state, api) => {
const input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
input.onchange = async (e) => {
const file = (e.target as HTMLInputElement).files?.[0];
if (file) {
try {
const dataUrl = await handleImageUpload(file);
const imageMarkdown = `![${file.name}](${window.location.origin}${dataUrl})`;
api.replaceSelection(imageMarkdown);
} catch (error) {
console.error("Image upload failed:", error);
}
}
};
input.click();
},
},
]}
/>
</div>
{/* Action Buttons */}
<div className="flex gap-3 py-4">
<Button
color="primary"
variant="solid"
onPress={handleSave}
isLoading={isLoading}
isDisabled={!isFormValid}
size='sm'
>
{t('Save')}
</Button>
{/* About Save Button */}
<div className="flex gap-3 py-4">
<Button
color="primary"
variant="solid"
onPress={handleAboutSave}
isLoading={isAboutLoading}
size="sm"
>
{t("Save")}
</Button>
</div>
</AccordionItem>
</Accordion>
</div>
</AccordionItem>
</Accordion>
{/* Theme Accordion */}
<Accordion
variant="splitted"
itemClasses={{
base: "shadow-none border-1",
}}
className="px-0"
>
<AccordionItem
key="theme-config"
title={
<div className="flex flex-col justify-between">
<h3 className="text-lg font-semibold">{t("Theme")}</h3>
<p className="text-sm text-gray-500">
{t("Choose your preferred theme style")}
</p>
</div>
}
>
<div className="space-y-4">
<ThemeSelector
value={formData.theme || Theme.DEFAULT}
onChange={handleThemeChange}
/>
</div>
</AccordionItem>
</Accordion>
</div>
);
};
};
@@ -0,0 +1,5 @@
.xpack-theme-card-panel-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
max-width: 100%;
}
@@ -0,0 +1,111 @@
"use client";
import React from "react";
import { Card, CardBody, CardFooter } from "@nextui-org/react";
import { Theme } from "@/shared/types/system";
import DefaultDemo from "../theme/default/Demo";
import ModernDemo from "../theme/modern/Demo";
import ClassicDemo from "../theme/classic/Demo";
import CreativeDemo from "../theme/creative/Demo";
import TemuDemo from "../theme/temu/Demo";
import "./ThemeCardPanel.css";
import { useTranslation } from "react-i18next";
// Available theme options
export const THEME_OPTIONS = [
{
value: Theme.DEFAULT,
label: "Default",
description: "Standard layout with clean and simple design",
component: DefaultDemo,
},
{
value: Theme.MODERN,
label: "Modern",
description: "Clean and minimalist design with modern UI elements",
component: ModernDemo,
},
{
value: Theme.CLASSIC,
label: "Classic",
description: "Traditional layout with professional appearance",
component: ClassicDemo,
},
{
value: Theme.CREATIVE,
label: "Creative",
description: "Bold and artistic design with vibrant colors",
component: CreativeDemo,
},
{
value: Theme.TEMU,
label: "Temu",
description: "E-commerce style with vibrant orange gradient background",
component: TemuDemo,
},
] as const;
interface ThemeSelectorProps {
value: Theme;
onChange: (theme: Theme) => void;
}
export const ThemeSelector: React.FC<ThemeSelectorProps> = ({
value,
onChange,
}) => {
const { t } = useTranslation();
return (
<div className="xpack-theme-card-panel-grid gap-4">
{THEME_OPTIONS.map((theme) => {
const DemoComponent = theme.component;
const isSelected = value === theme.value;
return (
<Card
key={theme.value}
isPressable
onPress={() => onChange(theme.value)}
className={`transition-all duration-200 border-1 hover:border-blue-300 ${
isSelected
? "border-blue-200 ring-1 ring-blue-200"
: "border-gray-200"
}`}
shadow="none"
radius="lg"
>
<CardBody className="p-0">
{/* Theme Preview */}
<div className="border-b-1">
<div className="aspect-video w-full">
<DemoComponent />
</div>
</div>
</CardBody>
<CardFooter>
{/* Theme Info */}
<div className="space-y-2 w-full text-left">
<div className="flex items-center justify-between">
<h4 className="font-semibold text-gray-900">
{t(theme.label)}
</h4>
<div
className={`h-4 w-4 rounded-full border-2 transition-colors duration-200 ${
isSelected
? "border-blue-500 bg-blue-500"
: "border-gray-300"
}`}
>
{isSelected && (
<div className="h-full w-full rounded-full bg-white scale-50"></div>
)}
</div>
</div>
<p className="text-sm text-gray-600">{t(theme.description)}</p>
</div>
</CardFooter>
</Card>
);
})}
</div>
);
};