Skip to content

Commit a6bed46

Browse files
authored
Merge pull request #35 from RanitManik/dev
Safe
2 parents 9e2a0d3 + 33eb8e0 commit a6bed46

File tree

17 files changed

+121
-86
lines changed

17 files changed

+121
-86
lines changed

actions/create-snippet.ts renamed to actions/create-snap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const CreateSnippet = async (
1+
export const CreateSnap = async (
22
values: { snapName: string; language: string; visibility: string },
33
userId: string | undefined,
44
code: string,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { db } from "@/lib/db";
22

3-
export const GetSnippet = (snapID: string) => {
3+
export const GetSnap = (snapID: string) => {
44
return db.snap.findUnique({ where: { id: snapID } });
55
};

actions/get-snippets.ts renamed to actions/get-snaps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { db } from "@/lib/db";
22

3-
export const GetSnippets = (userID: string | undefined) => {
3+
export const GetSnaps = (userID: string | undefined) => {
44
return db.snap.findMany({
55
where: { authorId: userID },
66
});

actions/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ export { SignUp } from "./sign-up";
33
export { SignOut } from "./sign-out";
44
export { GoogleSignIn } from "./google-sign-in";
55
export { ExecuteCode } from "./execute-code";
6-
export { CreateSnippet } from "./create-snippet";
7-
export { GetSnippets } from "./get-snippets";
8-
export { GetSnippet } from "./get-snippet";
6+
export { CreateSnap } from "./create-snap";
7+
export { GetSnaps } from "./get-snaps";
8+
export { GetSnap } from "./get-snap";

app/(auth)/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ interface AuthLayout {
44
children: ReactNode;
55
}
66

7-
export default async function AuthLayout({ children }: AuthLayout) {
7+
export default async function layout({ children }: AuthLayout) {
88
return <div className="m-auto max-w-[1920px]">{children}</div>;
99
}

app/(dashboard)/_components/control-panel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { Button } from "@nextui-org/button";
1212
import { useState, useMemo } from "react";
1313

14-
import CreateSnap from "@/app/(dashboard)/_components/create-snap";
14+
import CreateSnapModal from "@/app/(dashboard)/_components/create-snap-modal";
1515
import useMediaQuery from "@/hooks/media-query";
1616

1717
export default function ControlPanel() {
@@ -67,7 +67,7 @@ export default function ControlPanel() {
6767
</DropdownItem>
6868
</DropdownMenu>
6969
</Dropdown>
70-
<CreateSnap isMobile={isMobile} />
70+
<CreateSnapModal isMobile={isMobile} />
7171
</div>
7272
</div>
7373
);

app/(dashboard)/_components/create-snap.tsx renamed to app/(dashboard)/_components/create-snap-modal.tsx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import NProgress from "nprogress";
2727

2828
import { codeSnaps, languageOptions } from "@/config/languages";
2929
import { CustomRadio } from "@/components/custom-radio";
30-
import { CreateSnippet } from "@/actions";
30+
import { CreateSnap } from "@/actions";
3131
import { usePRouter } from "@/components/custom-router";
3232

33-
export default function CreateSnap({ isMobile }: { isMobile: boolean }) {
33+
export default function CreateSnapModal({ isMobile }: { isMobile: boolean }) {
3434
const { isOpen, onOpen, onOpenChange } = useDisclosure();
3535
const { data: session } = useSession();
3636
const [isLoading, setIsLoading] = useState(false);
@@ -70,31 +70,34 @@ export default function CreateSnap({ isMobile }: { isMobile: boolean }) {
7070
}
7171

7272
const onSubmit = async (values: z.infer<typeof FormSchema>) => {
73-
try {
74-
setIsLoading(true);
75-
const userId = session?.user?.id;
76-
77-
const code = getCodeByLanguageName(values.language);
78-
79-
const response = await CreateSnippet(values, userId, code);
73+
setIsLoading(true);
74+
const userId = session?.user?.id;
8075

81-
if (response?.snap) {
82-
toast.success("snap created");
76+
const code = getCodeByLanguageName(values.language);
77+
let response;
8378

84-
form.reset();
85-
onOpenChange();
86-
87-
router.push(`/snap/${response.snap.id}`);
88-
} else {
89-
toast.error("Failed creating snap. Try again later.");
90-
}
79+
try {
80+
response = await CreateSnap(values, userId, code);
9181
} catch (error) {
9282
toast.error(
9383
"An unexpected error occurred. Please try again later.",
9484
);
85+
86+
return;
9587
} finally {
9688
setIsLoading(false);
9789
}
90+
91+
if (response?.snap) {
92+
toast.success("snap created");
93+
94+
form.reset();
95+
onOpenChange();
96+
97+
router.push(`/snap/${response.snap.id}`);
98+
} else {
99+
toast.error("Failed creating snap. Try again later.");
100+
}
98101
};
99102

100103
const handleModalClose = () => {

app/(dashboard)/layout.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ interface DashboardLayoutProps {
88
children: ReactNode;
99
}
1010

11-
export default async function DashboardLayout({
12-
children,
13-
}: DashboardLayoutProps) {
11+
export default async function layout({ children }: DashboardLayoutProps) {
1412
const session = await getServerSession(authOptions);
1513

1614
if (!session) {

app/(dashboard)/page.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@ import SnapInfoCard from "@/app/(dashboard)/_components/snap-info-card";
66
import DashboardNavBar from "@/app/(dashboard)/_components/dashboard-nav-bar";
77
import { CommandMenu } from "@/components/command-menu";
88
import { authOptions } from "@/lib/auth";
9-
import { GetSnippets } from "@/actions";
9+
import { GetSnaps } from "@/actions";
1010

1111
export default async function Page() {
1212
const session = await getServerSession(authOptions);
1313

14-
const SnapData = await GetSnippets(session?.user.id);
14+
let snapData;
15+
16+
try {
17+
snapData = await GetSnaps(session?.user.id);
18+
} catch (error) {
19+
// TODO: Make Page for the failed loading Snippets [No Internet / Server Error]
20+
return <div>Unable to load snaps. Please try again later.</div>;
21+
}
22+
23+
// If no snap is found
24+
if (!snapData) {
25+
// TODO: Make component for the failed loading Snippets
26+
return <div>Unable to load snaps. Please try again later.</div>;
27+
}
1528

1629
return (
1730
<div className="space-y-6">
@@ -24,8 +37,8 @@ export default async function Page() {
2437
<h1>Snaps</h1>
2538
</div>
2639
<div className="m-auto grid grid-cols-[repeat(auto-fill,minmax(330px,1fr))] gap-4 pb-8 lg:gap-6 xl:gap-8">
27-
{SnapData &&
28-
SnapData.map((snap, index) => (
40+
{snapData &&
41+
snapData.map((snap, index) => (
2942
<SnapInfoCard
3043
key={index}
3144
createdAt={snap.createdAt.toISOString()}

app/(dashboard)/snap/[id]/_components/code-editor.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import { Editor } from "@monaco-editor/react";
44
import { useTheme } from "next-themes";
5-
import { useRef } from "react";
5+
import { useEffect, useRef } from "react";
6+
import { editor } from "monaco-editor";
67

78
import CodeEditorSkeleton from "@/app/(dashboard)/snap/[id]/_components/code-editor-skeleton";
8-
import { useCodeStore } from "@/stores/code-store";
9+
import { useCodeStore } from "@/stores";
910

1011
export default function CodeEditor({
1112
initialCode,
@@ -17,15 +18,20 @@ export default function CodeEditor({
1718
version?: string;
1819
}) {
1920
const { theme } = useTheme();
20-
const editorRef = useRef(null);
21-
const { code, setCode, setLanguage, setOutput } = useCodeStore();
21+
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
22+
const { code, setCode, setLanguage, setOutput, setEditorLoading } =
23+
useCodeStore();
2224

23-
function handleEditorDidMount(editor: any) {
25+
useEffect(() => {
26+
setOutput([]);
27+
}, []);
28+
29+
function handleEditorDidMount(editor: editor.IStandaloneCodeEditor) {
2430
editorRef.current = editor;
2531
editor.focus();
2632
setCode(initialCode || "");
2733
setLanguage({ name: language || "", version: version || "" });
28-
setOutput([]);
34+
setEditorLoading(false);
2935
}
3036

3137
function handleOnchange(value: string | undefined) {

0 commit comments

Comments
 (0)