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

feat: remove "syntax validate" button, add toast message to run button #170

Merged
merged 8 commits into from
Nov 29, 2024
Prev Previous commit
Next Next commit
feat: add toast notifications for user feedback in editor component
  • Loading branch information
HashCookie committed Nov 28, 2024
commit 3fe49c3b32cefe17c1ba73b8f4828591a4a5bea7
39 changes: 9 additions & 30 deletions app/components/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useLang } from '@/app/context/LangContext';
import LanguageMenu from '@/app/components/LanguageMenu';
import { linter, lintGutter } from '@codemirror/lint';
import { casbinLinter } from '@/app/utils/casbinLinter';
import { toast, Toaster } from 'react-hot-toast';

export const EditorScreen = () => {
const {
Expand Down Expand Up @@ -54,10 +55,6 @@ export const EditorScreen = () => {
return message;
};
const { t, lang, theme, toggleTheme } = useLang();
const [lastClickTime, setLastClickTime] = useState(0);
const [lastValidResult, setLastValidResult] = useState<React.ReactNode>(null);
const [lastRunClickTime, setLastRunClickTime] = useState(0);
const [lastRunResult, setLastRunResult] = useState<React.ReactNode>(null);
const [isContentLoaded, setIsContentLoaded] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -100,6 +97,7 @@ export const EditorScreen = () => {

return (
<div className="flex flex-col sm:flex-row h-full">
<Toaster position="top-center" />
<div
className={clsx('sm:relative', 'pl-0 sm:pl-2 pr-0 sm:pr-2 border-r border-[#dddddd]', 'transition-all duration-300', {
'hidden sm:block': !showCustomConfig,
Expand Down Expand Up @@ -407,27 +405,17 @@ export const EditorScreen = () => {
onClick={() => {
if (!isContentLoaded) {
setEcho(<div className="text-orange-500">Waiting for content loading</div>);
toast.error('Waiting for content loading');
return;
}
const now = Date.now();
if (now - lastClickTime < 2000) {
setEcho(<div className="text-orange-500">Repeated clicks</div>);
setTimeout(() => {
setEcho(lastValidResult);
}, 2000);
return;
}
setLastClickTime(now);

try {
Config.newConfigFromText(modelText);
const result = <div className="text-green-500">Passed</div>;
setEcho(result);
setLastValidResult(result);
setEcho(<div className="text-green-500">Passed</div>);
toast.success('Syntax validation passed');
} catch (e) {
const result = <div className="text-red-500">{(e as any).message}</div>;
setEcho(result);
setLastValidResult(result);
setEcho(<div className="text-red-500">{(e as any).message}</div>);
toast.error((e as any).message);
}
}}
>
Expand All @@ -444,16 +432,6 @@ export const EditorScreen = () => {
'transition-colors duration-500',
)}
onClick={() => {
const now = Date.now();
if (now - lastRunClickTime < 2000) {
setEcho(<div className="text-orange-500">Repeated clicks</div>);
setTimeout(() => {
setEcho(lastRunResult);
}, 2000);
return;
}
setLastRunClickTime(now);

return enforcer({
modelKind,
model: modelText,
Expand All @@ -464,7 +442,6 @@ export const EditorScreen = () => {
onResponse: (v) => {
if (isValidElement(v)) {
setEcho(v);
setLastRunResult(v);
} else if (Array.isArray(v)) {
const formattedResults = v.map((res) => {
if (typeof res === 'object') {
Expand All @@ -477,6 +454,7 @@ export const EditorScreen = () => {
});
const result = formattedResults.join('\n');
setRequestResult(result);
toast.success(`Test completed`);
}
},
});
Expand Down Expand Up @@ -527,6 +505,7 @@ export const EditorScreen = () => {
() => {
setShare('');
setEcho(<div className="text-green-500">{t('Copied')}</div>);
toast.success(t('Link copied to clipboard'));
},
`${window.location.origin + window.location.pathname}#${share}`,
);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
"codemirror": "^6.0.1",
"next": "14.1.0",
"react": "^18",
"react-dom": "^18"
"react-dom": "^18",
"react-hot-toast": "^2.4.1"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,11 @@ globby@^11.1.0:
merge2 "^1.4.1"
slash "^3.0.0"

goober@^2.1.10:
version "2.1.16"
resolved "https://registry.yarnpkg.com/goober/-/goober-2.1.16.tgz#7d548eb9b83ff0988d102be71f271ca8f9c82a95"
integrity sha512-erjk19y1U33+XAMe1VTvIONHYoSqE4iS7BYUZfHaqeohLmnC0FdxEh7rQU+6MZ4OajItzjZFSRtVANrQwNq6/g==

gopd@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/gopd/-/gopd-1.0.1.tgz#29ff76de69dac7489b7c0918a5788e56477c332c"
Expand Down Expand Up @@ -3584,6 +3589,13 @@ react-dom@^18:
loose-envify "^1.1.0"
scheduler "^0.23.2"

react-hot-toast@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/react-hot-toast/-/react-hot-toast-2.4.1.tgz#df04295eda8a7b12c4f968e54a61c8d36f4c0994"
integrity sha512-j8z+cQbWIM5LY37pR6uZR6D4LfseplqnuAO4co4u8917hBUvXlEqyP1ZzqVLcqoyUesZZv/ImreoCeHVDpE5pQ==
dependencies:
goober "^2.1.10"

react-is@^16.13.1:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
Expand Down