Skip to content

Commit

Permalink
feat: make config json work
Browse files Browse the repository at this point in the history
  • Loading branch information
AprilNEA committed Nov 18, 2023
1 parent 7f48a90 commit 6d3a044
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 121 deletions.
97 changes: 0 additions & 97 deletions config.example.yaml

This file was deleted.

14 changes: 8 additions & 6 deletions packages/backend/src/common/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ export class ConfigService {
: this.defaultConfig;
}

getConfigSchema() {
// if (fs.existsSync(this.configFilePath)) {
// throw new BizException(ErrorCodeEnum.ConfigExists);
// }
/* 结构 */
getConfigSchema(isPublic = false) {
if (isPublic && fs.existsSync(this.configFilePath)) {
throw new BizException(ErrorCodeEnum.ConfigExists);
}
return CONFIG_SCHEMA;
}

getConfigSchemaValue() {
return {};
/* 获取默认值 */
getDefaultValue() {
return this.defaultConfig;
}

get<K extends keyof ConfigType>(key: K): ConfigType[K] {
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/modules/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export class ChatService {
constructor(
@Inject('PrismaService')
private prisma: CustomPrismaService<ExtendedPrismaClient>,
) // config: ConfigService,
{
// this.openaiConfig = config.get('openai');
configService: ConfigService,
) {
this.openaiConfig = configService.get('openai');
}

/* 获取指定用户最近时间内消息的总计,用于limit */
Expand Down
16 changes: 14 additions & 2 deletions packages/backend/src/modules/dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,24 @@ export class DashboardController {
@Public()
@Get('install')
install() {
return this.configService.getConfigSchema();
return {
success: true,
data: {
schema: this.configService.getConfigSchema(true),
value: this.configService.getDefaultValue(),
},
};
}

@Get('config')
getAllConfig() {
return this.configService.getAll();
return {
success: true,
data: {
schema: this.configService.getConfigSchema(),
value: this.configService.getAll(),
},
};
}

@Put('config')
Expand Down
13 changes: 8 additions & 5 deletions packages/frontend/src/app/dashboard/setting/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ import useSWR from 'swr';
import { Loading } from '@/components/loading';
import { OptionListRoot, OptionNode } from '@/components/radix-ui-lib';
import { useStore } from '@/store';
import useInstallStore from '@/store/install';

export default function AdminSettingPage() {
const { fetcher } = useStore();
const { data } = useSWR('/dashboard/install', (url) =>
const { updateSettingItem } = useInstallStore();
const { data, isLoading } = useSWR('/dashboard/config', (url) =>
fetcher(url)
.then((res) => res.json())
.then((res) => {
console.log(res);
return res;
updateSettingItem([], res.data.value);
return res.data;
}),
);
if (!data) return <Loading />;
if (isLoading || !data) return <Loading />;

return (
<OptionListRoot>
{data.map((item: any) => (
{data.schema.map((item: any) => (
<OptionNode key={item.key} schema={item} />
))}
</OptionListRoot>
Expand Down
12 changes: 10 additions & 2 deletions packages/frontend/src/components/radix-ui-lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ function SwitchItem(props: {
/* 输入选项 */
function InputItem(props: {
schema: TypeSettingSchema;
value: string;
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
}) {
return (
<TextField.Input
width={160}
value={props.value ?? props.schema.value ?? ''}
onChange={(e) => {
console.log(e.target.value);
props.onChange(e);
Expand All @@ -56,10 +58,14 @@ function InputItem(props: {
/* 选择 */
function SelectItem(props: {
schema: SelectSettingSchema;
value: string;
onValueChange: (value: string) => void;
}) {
return (
<Select.Root onValueChange={props.onValueChange}>
<Select.Root
value={props.value ?? props.schema.value ?? ''}
onValueChange={props.onValueChange}
>
<Select.Trigger />
<Select.Content position="popper">
{props.schema.selectOptions.map((option) => (
Expand Down Expand Up @@ -221,6 +227,7 @@ export function OptionNode({
headerArea = (
<InputItem
schema={schema}
value={getSettingItem(getKeyTree()) as string}
onChange={(e) => updateSettingItem(getKeyTree(), e.target.value)}
/>
);
Expand All @@ -229,6 +236,7 @@ export function OptionNode({
headerArea = (
<SelectItem
schema={schema}
value={getSettingItem(getKeyTree()) as string}
onValueChange={(value) => updateSettingItem(getKeyTree(), value)}
/>
);
Expand Down Expand Up @@ -273,7 +281,7 @@ export function OptionNode({
default:
break;
}
console.log('[Setting]', settings);

return (
<div
className={
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/store/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createSharedStore: StateCreator<StoreType, [], [], SharedSlice> = (
get,
) => ({
// Auth
setSessionToken(token: string) {
setSessionToken(token: string | undefined) {
set({ sessionToken: token });
},

Expand Down
10 changes: 5 additions & 5 deletions packages/frontend/src/styles/module/radix-ui-lib.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
background-color: var(--gray-2);
display: flex;
flex-direction: column;
width: 100%;
max-width: 100%;
box-shadow: 0 2px 10px var(--gray-11);
padding: 20px;
}
Expand All @@ -32,12 +32,12 @@
display: flex;
flex-direction: column;
align-items: baseline;

.label {
font-size: 1rem;
font-weight: 600;
}

.description {
font-size: 0.8rem;
font-weight: 300;
Expand All @@ -58,6 +58,6 @@

.option-list-item-nested {
.item-main-area {

}
}
}

0 comments on commit 6d3a044

Please sign in to comment.