memos/web/src/components/UpdateCustomizedProfileDialog.tsx
Lincoln Nogueira 557278fac0
feat: improve i18n support as a whole (#1526)
* feat: improve i18n support as a whole

- Remove dayjs in favor of /helpers/datetime.ts, which uses
Intl.DateTimeFormat and Date. Dayjs is not exactly i18n friendly
and has several locale related opened issues.

- Move/refactor date/time code from /helpers/utils.ts to
/helpers/datetime.ts.

- Fix Daily Review weekday not changing according to selected date.

- Localize Daily review weekday and month.

- Load i18n listed strings from /locales/{locale}.json in a dynamic way.
This makes much easier to add new locales, by just adding a properly
named json file and listing it only in /web/src/i18n.ts and
/api/user_setting.go.

- Fallback languages are now set in /web/src/i18n.ts.

- Full language codes are now preffered, but they fallback to 2-letter
codes when not available.

- The locale dropdown is now populated dynamically from the available
locales. Locale names are populated by the browser via
Intl.DisplayNames(locale).

- /web/src/i18n.ts now exports a type TLocale from availableLocales
array. This is used only by findNearestLanguageMatch(). As I was unable
to use this type in ".d.ts" files, I switched the Locale type from
/web/src/types/i18n.d.ts to string.

- Move pretty much all hardcoded text strings to i18n strings.

- Add pt-BR translation.

- Remove site.ts and move its content to a i18n string.

- Rename zh.json to zh-Hans.json to get the correct language name on
selector dropdown.

- Remove pt_BR.json and replace with pt-BR.json.

- Some minor layout spacing fixes to accommodate larger texts.

- Improve some error messages.

* Delete .yarnrc.yml

* Delete package-lock.json

* fix: 158:28  error  Insert `⏎`  prettier/prettier
2023-04-15 08:56:03 +08:00

144 lines
4.7 KiB
TypeScript

import { useState } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "react-hot-toast";
import { useGlobalStore } from "@/store/module";
import * as api from "@/helpers/api";
import Icon from "./Icon";
import { generateDialog } from "./Dialog";
import LocaleSelect from "./LocaleSelect";
import AppearanceSelect from "./AppearanceSelect";
type Props = DialogProps;
const UpdateCustomizedProfileDialog: React.FC<Props> = ({ destroy }: Props) => {
const { t } = useTranslation();
const globalStore = useGlobalStore();
const [state, setState] = useState<CustomizedProfile>(globalStore.state.systemStatus.customizedProfile);
const handleCloseButtonClick = () => {
destroy();
};
const setPartialState = (partialState: Partial<CustomizedProfile>) => {
setState((state) => {
return {
...state,
...partialState,
};
});
};
const handleNameChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
setPartialState({
name: e.target.value as string,
});
};
const handleLogoUrlChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
setPartialState({
logoUrl: e.target.value as string,
});
};
const handleDescriptionChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
setPartialState({
description: e.target.value as string,
});
};
const handleLocaleSelectChange = (locale: Locale) => {
setPartialState({
locale: locale,
});
};
const handleAppearanceSelectChange = (appearance: Appearance) => {
setPartialState({
appearance: appearance,
});
};
const handleRestoreButtonClick = () => {
setPartialState({
name: "memos",
logoUrl: "/logo.webp",
description: "",
locale: "en",
appearance: "system",
externalUrl: "",
});
};
const handleSaveButtonClick = async () => {
if (state.name === "") {
toast.error(t("message.fill-server-name"));
return;
}
try {
await api.upsertSystemSetting({
name: "customized-profile",
value: JSON.stringify(state),
});
await globalStore.fetchSystemStatus();
} catch (error) {
console.error(error);
return;
}
toast.success(t("message.succeed-update-customized-profile"));
destroy();
};
return (
<>
<div className="dialog-header-container">
<p className="title-text">{t("setting.system-section.customize-server.title")}</p>
<button className="btn close-btn" onClick={handleCloseButtonClick}>
<Icon.X />
</button>
</div>
<div className="dialog-content-container !w-80">
<p className="text-sm mb-1">
{t("setting.system-section.server-name")}
<span className="text-sm text-gray-400 ml-1">({t("setting.system-section.customize-server.default")})</span>
</p>
<input type="text" className="input-text" value={state.name} onChange={handleNameChanged} />
<p className="text-sm mb-1 mt-2">{t("setting.system-section.customize-server.icon-url")}</p>
<input type="text" className="input-text" value={state.logoUrl} onChange={handleLogoUrlChanged} />
<p className="text-sm mb-1 mt-2">{t("setting.system-section.customize-server.description")}</p>
<input type="text" className="input-text" value={state.description} onChange={handleDescriptionChanged} />
<p className="text-sm mb-1 mt-2">{t("setting.system-section.customize-server.locale")}</p>
<LocaleSelect className="!w-full" value={state.locale} onChange={handleLocaleSelectChange} />
<p className="text-sm mb-1 mt-2">{t("setting.system-section.customize-server.appearance")}</p>
<AppearanceSelect className="!w-full" value={state.appearance} onChange={handleAppearanceSelectChange} />
<div className="mt-4 w-full flex flex-row justify-between items-center space-x-2">
<div className="flex flex-row justify-start items-center">
<button className="btn-normal" onClick={handleRestoreButtonClick}>
{t("common.restore")}
</button>
</div>
<div className="flex flex-row justify-end items-center">
<button className="btn-text" onClick={handleCloseButtonClick}>
{t("common.cancel")}
</button>
<button className="btn-primary" onClick={handleSaveButtonClick}>
{t("common.save")}
</button>
</div>
</div>
</div>
</>
);
};
function showUpdateCustomizedProfileDialog() {
generateDialog(
{
className: "update-customized-profile-dialog",
dialogName: "update-customized-profile-dialog",
},
UpdateCustomizedProfileDialog
);
}
export default showUpdateCustomizedProfileDialog;