Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #31

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/generate-doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Generate doc
run-name: Generate doc
on:
push:
branches:
- develop
workflow_dispatch:
inputs:
docLevel:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ next-env.d.ts
.keystone
.keystone/admin
!keystone.db

*.dump
20 changes: 15 additions & 5 deletions app/actions/user-dict-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,33 @@ const addWordsToUserDictAction = async (dictId: string, words: string[]) => {
const getDictList = async (dictId: string) => {
// TODO: 权限做 增删改
const ctx = keystoneContext.sudo();
const res = (await ctx.query.Dict.findOne({
where: { id: dictId },
query: "list { id name trans example exTrans }",
})) as { list: Dict };
return toPlainObject(res.list);
const res = (await ctx.query.DictItem.findMany({
where: { dict: { some: { id: { equals: dictId } } } },
query: "id name trans example exTrans",
orderBy: { createdAt: "asc" },
})) as Dict;
return toPlainObject(res);
};

const importDictItemToUserDict = async (dictId: string, JSONString: string) => {
await addDictItemToDictAction(dictId, JSON.parse(JSONString));
revalidateTag(getDictRevalidateKey(dictId));
};

const removeDictAction = async (dictId: string) => {
const session = await auth();
const ctx = KSwithSession(session);
await ctx.query.Dict.deleteOne({ where: { id: dictId } });
revalidateTag(allDictsRevalidateKey);
revalidateTag(getDictRevalidateKey(dictId));
};

export {
createDictAction,
getAllDicts,
getDictList,
addWordsToUserDictAction,
removeDictItemAction,
removeDictAction,
importDictItemToUserDict,
};
3 changes: 3 additions & 0 deletions app/assets/svg/add.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions app/components/cache-toolbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NextCacheToolbar } from "next-cache-toolbar";
import "next-cache-toolbar/style.css";

function CacheToolbar() {
return <NextCacheToolbar />;
}

export { CacheToolbar };
2 changes: 1 addition & 1 deletion app/components/header/_component/mobile-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const MobileMenu = ({
</label>
<div
className={clsx(
"absolute w-screen h-[calc(100dvh-var(--header-height))] left-0 top-[--header-height] flex flex-col backdrop-blur-lg p-2",
"absolute w-screen h-[calc(100dvh-var(--header-height))] left-0 top-[--header-height] flex flex-col backdrop-blur-xl p-2",
!isOpen && "hidden",
)}
>
Expand Down
59 changes: 35 additions & 24 deletions app/components/home-drawer/dict-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import {
addWordsToUserDictAction,
createDictAction,
importDictItemToUserDict,
removeDictAction,
} from "@/actions/user-dict-action";
import AddIcon from "@/assets/svg/add.svg";
import CloseIcon from "@/assets/svg/close.svg";
import DownloadIcon from "@/assets/svg/download.svg";
import FileImportIcon from "@/assets/svg/file-import.svg";
import SettingIcon from "@/assets/svg/setting.svg";
import ShuffleIcon from "@/assets/svg/shuffle.svg";

import {
createErrorToast,
createLoadingToast,
Expand All @@ -33,7 +37,7 @@ const DictMenu = ({
dictId,
dictList,
onShuffle,
onLocalDictUpdate,
onDictUpdate,
onSettingChange,
setting,
isUserDict,
Expand All @@ -43,13 +47,13 @@ const DictMenu = ({
dictId: string;
dictList: UserDicts;
onShuffle?: () => void;
onLocalDictUpdate?: () => void;
onDictUpdate?: () => void;
onSettingChange?: (val: Partial<HomeSetting>) => void;
setting: HomeSetting;
isUserDict: boolean;
isLocalDict: boolean;
}) => {
const { isLogin } = useUser();
const { isLogin, isAdmin } = useUser();
const tHome = useTranslations("Home");
const router = useRouter();
const tDict = useTranslations("Dict");
Expand All @@ -63,7 +67,7 @@ const DictMenu = ({
};

const createWord = async () => {
const word = prompt(tHome("createWord"), tHome("exampleWord"));
const word = prompt(`✨ ${tHome("createWord")}`, tHome("exampleWord"));
if (word) {
const removeInfoToast = createLoadingToast(tHome("generating"));

Expand All @@ -72,11 +76,11 @@ const DictMenu = ({
if (isLocalDict) {
const result = await generateWordsAction(words);
addLocalDict(...result);
onLocalDictUpdate?.();
} else {
await addWordsToUserDictAction(dictId, words);
await serverActionTimeOut();
}
onDictUpdate?.();
createSuccessToast(tHome("generated"));
} catch (error) {
console.error("[createWord]:\n", error);
Expand All @@ -89,33 +93,30 @@ const DictMenu = ({

const handleImport = async () => {
if (isLocalDict) {
importLocalDict(onLocalDictUpdate);
importLocalDict(onDictUpdate);
} else {
const fileString = await importJSONFile();
// TODO: intl
const cancel = createLoadingToast("importing....");
const cancel = createLoadingToast(tHome("importing"));
await importDictItemToUserDict(dictId, fileString as string);
await serverActionTimeOut();
cancel();
createSuccessToast("success");
createSuccessToast(tHome("imported"));
}
};

const createDict = async () => {
// TODO: intl
if (!isLogin) {
router.push("/api/auth/signin");
return;
}
const dictName = prompt("name:");
const dictName = prompt(tHome("createWordList"));
if (dictName) {
const removeInfoToast = createLoadingToast(tHome("generating"));
const removeInfoToast = createLoadingToast(tHome("creating"));
const res = await createDictAction(dictName);
router.push(`/?dict=${res.id}`);
await serverActionTimeOut();
removeInfoToast();
// TODO: intl
createSuccessToast("success");
createSuccessToast(tHome("created"));
}
};

Expand All @@ -136,8 +137,19 @@ const DictMenu = ({
}
};

const handleRemoveDict = async () => {
const res = confirm();
if (!res) return;
const removeInfoToast = createLoadingToast(tHome("removing"));
await removeDictAction(dictId);
router.push("/");
await serverActionTimeOut();
removeInfoToast();
createSuccessToast("success");
};
const canEdit = isAdmin || isUserDict || dictId === Dicts.local;
return (
<div className="sticky top-2 z-10 bg-base-200 rounded-xl mb-3 shadow-md flex justify-between items-center p-1">
<div className="sticky top-2 z-10 bg-base-200 rounded-xl mb-3 shadow-md flex justify-between items-center p-1 max-w-full">
<div className="pl-3 flex items-center *:mx-1 *:inline-block *:cursor-pointer *:select-none">
<ShuffleIcon
width={20}
Expand Down Expand Up @@ -195,18 +207,17 @@ const DictMenu = ({
</div>
</div>
</div>
{isUserDict && (
<span onClick={createWord} className="text-xl">
+
</span>
{canEdit && <AddIcon className="size-6" onClick={createWord} />}
<DownloadIcon className="size-6" onClick={handleDownload} />
{canEdit && (
<FileImportIcon className="size-6" onClick={handleImport} />
)}
<DownloadIcon width={20} height={20} onClick={handleDownload} />
{isUserDict && (
<FileImportIcon width={20} height={20} onClick={handleImport} />
<CloseIcon className="size-6" onClick={handleRemoveDict} />
)}
</div>
<select
className="select select-bordered w-28 select-sm"
className="select select-bordered w-16 sm:w-32 select-sm"
value={dictId}
onChange={onChange}
>
Expand All @@ -216,8 +227,8 @@ const DictMenu = ({
</option>
))}
<option value="_local">{tDict(Dicts.local)}</option>
{/* // TODO: intl */}
<option value="_create">+ create new</option>

<option value="_create">✨ {tHome("createNewDict")}</option>
</select>
</div>
);
Expand Down
20 changes: 12 additions & 8 deletions app/components/home-drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { removeLocalDict } from "@/utils/local-dict";
import { serverActionTimeOut, timeOut } from "@/utils/time-out";
import { useMemoizedFn } from "ahooks";
import clsx from "clsx";
import { useLocale } from "next-intl";
import { useLocale, useTranslations } from "next-intl";
import { useEffect, useRef } from "react";
import { createPortal } from "react-dom";
import { DictMenu } from "./dict-menu";
Expand Down Expand Up @@ -45,6 +45,7 @@ const HomeDrawer = ({
}) => {
const drawerListRef = useRef<HTMLUListElement>(null);
const locale = useLocale();
const tHome = useTranslations("Home");
const controllerRef = useRef<HTMLInputElement>(null);
const open = useMemoizedFn(() => {
if (controllerRef.current) {
Expand All @@ -60,8 +61,10 @@ const HomeDrawer = ({
}
}, [drawerRef, open]);

const handleUserDictUpdate = useMemoizedFn(async () => {
onLocalDictUpdate();
const handleDictUpdate = useMemoizedFn(async () => {
if (isLocalDict) {
onLocalDictUpdate();
}
await timeOut(100);
drawerListRef.current?.parentElement?.scrollTo({
top: drawerListRef.current?.parentElement?.scrollHeight,
Expand All @@ -76,12 +79,13 @@ const HomeDrawer = ({
removeLocalDict(item.name);
onLocalDictUpdate();
} else {
// TODO: intl
const cancel = createLoadingToast(`【${item.name}】removing....`);
const cancel = createLoadingToast(
`【${item.name}】${tHome("removing")}`,
);
await removeDictItemAction(dictId, item.id!);
await serverActionTimeOut();
cancel();
createSuccessToast("success");
createSuccessToast(tHome("removed"));
}
}
};
Expand Down Expand Up @@ -114,7 +118,7 @@ const HomeDrawer = ({
/>
<ul
ref={drawerListRef}
className="menu bg-base-100 text-base-content min-h-full w-5/6 sm:w-80 p-4"
className="menu bg-base-100 text-base-content min-h-full w-5/6 sm:w-96 p-4"
>
<DictMenu
isUserDict={isUserDict}
Expand All @@ -125,7 +129,7 @@ const HomeDrawer = ({
setting={setting}
onSettingChange={onSettingChange}
onShuffle={onShuffle}
onLocalDictUpdate={handleUserDictUpdate}
onDictUpdate={handleDictUpdate}
/>
{/* Sidebar content here */}
{dict.map((item, index) => (
Expand Down
Loading