Skip to content

Commit

Permalink
feat(web): add laf site setting config
Browse files Browse the repository at this point in the history
  • Loading branch information
LeezQ committed May 12, 2023
1 parent 9911a5d commit 214bd1b
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 25 deletions.
7 changes: 7 additions & 0 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ClickToComponent } from "click-to-react-component";

import "@/utils/i18n";

import useSiteSettingStore from "./pages/siteSetting";
import theme from "./chakraTheme";
import darkTheme from "./chakraThemeDark";
import { CHAKRA_UI_COLOR_MODE_KEY } from "./constants";
Expand All @@ -33,6 +34,8 @@ const queryClient = new QueryClient({
function APP() {
useTranslation();

const getSiteSettings = useSiteSettingStore((state) => state.getSiteSettings);

const [colorMode, setColorMode] = useState(localStorage.getItem(CHAKRA_UI_COLOR_MODE_KEY));
useEffect(() => {
function onColorModeChange() {
Expand All @@ -45,6 +48,10 @@ function APP() {
};
});

useEffect(() => {
getSiteSettings();
}, [getSiteSettings]);

return (
<>
<QueryClientProvider client={queryClient}>
Expand Down
8 changes: 8 additions & 0 deletions web/src/apis/typing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ export type TWebsiteHosting = {
lockedAt: string;
};

export type TSetting = {
id: string;
key: string;
value: string;
desc: string;
metadata: any;
};

export type Spec = {
policy: string;
storage: string;
Expand Down
16 changes: 16 additions & 0 deletions web/src/apis/v1/api-auto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,4 +761,20 @@ declare namespace Paths {

export type Responses = any;
}

namespace SettingControllerGetSettings {
export type QueryParameters = any;

export type BodyParameters = any;

export type Responses = any;
}

namespace SettingControllerGetSettingByKey {
export type QueryParameters = any;

export type BodyParameters = any;

export type Responses = any;
}
}
45 changes: 45 additions & 0 deletions web/src/apis/v1/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @ts-ignore
/* eslint-disable */
///////////////////////////////////////////////////////////////////////
// //
// this file is autogenerated by service-generate //
// do not edit this file manually //
// //
///////////////////////////////////////////////////////////////////////
/// <reference path = "api-auto.d.ts" />
import request from "@/utils/request";
import useGlobalStore from "@/pages/globalStore";

/**
* Get site settings
*/
export async function SettingControllerGetSettings(
params: Paths.SettingControllerGetSettings.BodyParameters | any,
): Promise<Paths.SettingControllerGetSettings.Responses> {
// /v1/settings
let _params: { [key: string]: any } = {
appid: useGlobalStore.getState().currentApp?.appid || "",
...params,
};
return request(`/v1/settings`, {
method: "GET",
params: params,
});
}

/**
* Get one site setting by key
*/
export async function SettingControllerGetSettingByKey(
params: Paths.SettingControllerGetSettingByKey.BodyParameters | any,
): Promise<Paths.SettingControllerGetSettingByKey.Responses> {
// /v1/settings/{key}
let _params: { [key: string]: any } = {
appid: useGlobalStore.getState().currentApp?.appid || "",
...params,
};
return request(`/v1/settings/${_params.key}`, {
method: "GET",
params: params,
});
}
3 changes: 1 addition & 2 deletions web/src/layouts/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export default function Header(props: { size: "sm" | "lg" }) {
<img
src={colorMode === COLOR_MODE.dark ? "/logo_light.png" : "/logo_light.png"}
alt="logo"
width={80}
className="mr-4"
className="mr-4 h-[40px]"
/>
</a>
<a
Expand Down
7 changes: 7 additions & 0 deletions web/src/pages/homepage/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import { useColorMode } from "@chakra-ui/react";
import { DiscordIcon, ForumIcon, WechatIcon } from "@/components/CommonIcon";
import { COLOR_MODE } from "@/constants";

import useSiteSettingStore from "../siteSetting";

type Props = {};

const Footer = (props: Props) => {
const { t } = useTranslation();
const { colorMode } = useColorMode();
const darkMode = colorMode === COLOR_MODE.dark;

const siteSettings = useSiteSettingStore((state) => state.siteSettings);
console.log(123, siteSettings);

return (
<>
<div
Expand Down Expand Up @@ -162,6 +167,7 @@ const Footer = (props: Props) => {
}
>
laf. all rights reserved. © {new Date().getFullYear()}
<div dangerouslySetInnerHTML={{ __html: siteSettings.site_footer?.value || "" }} />
</div>
<div className="flex w-36 justify-evenly">
<a
Expand Down Expand Up @@ -314,6 +320,7 @@ const Footer = (props: Props) => {
<div className="flex h-auto flex-col gap-6 py-6 ">
<div className={darkMode ? "text-lg text-lafWhite-700" : "text-lg text-[#3C455D]"}>
laf. all rights reserved. © {new Date().getFullYear()}
<div dangerouslySetInnerHTML={{ __html: siteSettings.site_footer?.value || "" }} />
</div>
<div className="ml-[-10px] flex w-32 justify-around">
<a href="https://w4mci7-images.oss.laf.run/wechat.png" target="_blank" rel="noreferrer">
Expand Down
31 changes: 8 additions & 23 deletions web/src/pages/homepage/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,16 @@ const Navbar = (props: Props) => {
alt={"logo"}
/>
</div>
{/* <a href="/" className="ml-10">
{t("HomePage.NavBar.home")}
</a>
<a
target="_blank"
href={String(t("HomePage.DocsLink"))}
className="ml-10"
rel="noreferrer"
>
{t("HomePage.NavBar.docs")}
</a>
<a href="https://forum.laf.run/" target="_blank" className="ml-10" rel="noreferrer">
{t("HomePage.NavBar.forum")}
</a>
<a
target="_blank"
href="https://www.wenjuan.com/s/I36ZNbl/"
className="ml-10"
rel="noreferrer"
>
{t("HomePage.NavBar.contact")}
</a> */}

{navList.map((item, index) => {
return (
<a key={index} target="_blank" href={item.ref} className="ml-10" rel="noreferrer">
<a
key={index}
target={item.ref.startsWith("http") ? "_blank" : "_self"}
href={item.ref}
className="ml-10"
rel="noreferrer"
>
{item.text}
</a>
);
Expand Down
46 changes: 46 additions & 0 deletions web/src/pages/siteSetting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import { immer } from "zustand/middleware/immer";

import { TSetting } from "@/apis/typing";
import { SettingControllerGetSettings } from "@/apis/v1/settings";

type SITE_KEY = "site_footer";

type State = {
siteSettings: {
// eslint-disable-next-line no-unused-vars
[key in SITE_KEY]?: TSetting;
};
getSiteSettings: () => void;
};

const useSiteSettingStore = create<State>()(
devtools(
persist(
immer((set, get) => ({
siteSettings: {},
getSiteSettings: async () => {
const settings = await SettingControllerGetSettings({});
set((state) => {
// convert array to object
state.siteSettings = settings.data.reduce(
(acc: { [x: string]: TSetting }, cur: TSetting) => {
acc[cur.key] = cur;
return acc;
},
{} as Record<string, TSetting>,
);
});
},
})),

{
name: "laf_site_settings",
version: 1,
},
),
),
);

export default useSiteSettingStore;

0 comments on commit 214bd1b

Please sign in to comment.