Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Remove config button + cleanup

Richie McIlroy939c160c1768
diff --git a/apps/desktop/src/routes/(window-chrome)/settings/apps/s3-config.tsx b/apps/desktop/src/routes/(window-chrome)/settings/apps/s3-config.tsx
index 14e7d1602..086a828af 100644
--- a/apps/desktop/src/routes/(window-chrome)/settings/apps/s3-config.tsx
+++ b/apps/desktop/src/routes/(window-chrome)/settings/apps/s3-config.tsx
@@ -14,51 +14,103 @@ interface S3Config {
region: string | null;
}
+const DEFAULT_CONFIG = {
+ provider: "aws",
+ accessKeyId: "",
+ secretAccessKey: "",
+ endpoint: "https://s3.amazonaws.com",
+ bucketName: "",
+ region: "us-east-1",
+};
+
export default function S3ConfigPage() {
- const [provider, setProvider] = createSignal("aws");
- const [accessKeyId, setAccessKeyId] = createSignal("");
- const [secretAccessKey, setSecretAccessKey] = createSignal("");
- const [endpoint, setEndpoint] = createSignal("https://s3.amazonaws.com");
- const [bucketName, setBucketName] = createSignal("");
- const [region, setRegion] = createSignal("us-east-1");
+ const [provider, setProvider] = createSignal(DEFAULT_CONFIG.provider);
+ const [accessKeyId, setAccessKeyId] = createSignal(
+ DEFAULT_CONFIG.accessKeyId
+ );
+ const [secretAccessKey, setSecretAccessKey] = createSignal(
+ DEFAULT_CONFIG.secretAccessKey
+ );
+ const [endpoint, setEndpoint] = createSignal(DEFAULT_CONFIG.endpoint);
+ const [bucketName, setBucketName] = createSignal(DEFAULT_CONFIG.bucketName);
+ const [region, setRegion] = createSignal(DEFAULT_CONFIG.region);
const [saving, setSaving] = createSignal(false);
const [loading, setLoading] = createSignal(true);
+ const [deleting, setDeleting] = createSignal(false);
+ const [hasConfig, setHasConfig] = createSignal(false);
- onMount(async () => {
- try {
- const auth = await authStore.get();
+ const resetForm = () => {
+ setProvider(DEFAULT_CONFIG.provider);
+ setAccessKeyId(DEFAULT_CONFIG.accessKeyId);
+ setSecretAccessKey(DEFAULT_CONFIG.secretAccessKey);
+ setEndpoint(DEFAULT_CONFIG.endpoint);
+ setBucketName(DEFAULT_CONFIG.bucketName);
+ setRegion(DEFAULT_CONFIG.region);
+ setHasConfig(false);
+ };
+
+ const handleAuthError = async () => {
+ console.error("User not authenticated");
+ const window = getCurrentWindow();
+ window.close();
+ };
- if (!auth) {
- console.error("User not authenticated");
- const window = getCurrentWindow();
- window.close();
- return;
+ const makeAuthenticatedRequest = async (
+ url: string,
+ options: RequestInit
+ ) => {
+ const auth = await authStore.get();
+ if (!auth) {
+ await handleAuthError();
+ return null;
+ }
+
+ const response = await fetch(
+ `${clientEnv.VITE_SERVER_URL}${url}?origin=${window.location.origin}`,
+ {
+ ...options,
+ credentials: "include",
+ headers: {
+ ...options.headers,
+ Authorization: `Bearer ${auth.token}`,
+ },
}
+ );
+
+ if (!response.ok) {
+ throw new Error(
+ `Failed to ${options.method?.toLowerCase() || "fetch"} S3 configuration`
+ );
+ }
+
+ return response;
+ };
- const response = await fetch(
- `${clientEnv.VITE_SERVER_URL}/api/desktop/s3/config/get?origin=${window.location.origin}`,
+ onMount(async () => {
+ try {
+ const response = await makeAuthenticatedRequest(
+ "/api/desktop/s3/config/get",
{
method: "GET",
- credentials: "include",
- headers: {
- Authorization: `Bearer ${auth.token}`,
- },
}
);
- if (!response.ok) {
- throw new Error("Failed to fetch S3 configuration");
- }
+ if (!response) return;
const data = await response.json();
if (data.config) {
const config = data.config as S3Config;
- setProvider(config.provider || "aws");
- setAccessKeyId(config.accessKeyId || "");
- setSecretAccessKey(config.secretAccessKey || "");
- setEndpoint(config.endpoint || "https://s3.amazonaws.com");
- setBucketName(config.bucketName || "");
- setRegion(config.region || "us-east-1");
+ if (!config.accessKeyId) return;
+
+ setProvider(config.provider || DEFAULT_CONFIG.provider);
+ setAccessKeyId(config.accessKeyId || DEFAULT_CONFIG.accessKeyId);
+ setSecretAccessKey(
+ config.secretAccessKey || DEFAULT_CONFIG.secretAccessKey
+ );
+ setEndpoint(config.endpoint || DEFAULT_CONFIG.endpoint);
+ setBucketName(config.bucketName || DEFAULT_CONFIG.bucketName);
+ setRegion(config.region || DEFAULT_CONFIG.region);
+ setHasConfig(true);
}
} catch (error) {
console.error("Failed to fetch S3 config:", error);
@@ -70,23 +122,12 @@ export default function S3ConfigPage() {
const handleSave = async () => {
setSaving(true);
try {
- const auth = await authStore.get();
-
- if (!auth) {
- console.error("User not authenticated");
- const window = getCurrentWindow();
- window.close();
- return;
- }
-
- const response = await fetch(
- `${clientEnv.VITE_SERVER_URL}/api/desktop/s3/config?origin=${window.location.origin}`,
+ const response = await makeAuthenticatedRequest(
+ "/api/desktop/s3/config",
{
method: "POST",
- credentials: "include",
headers: {
"Content-Type": "application/json",
- Authorization: `Bearer ${auth.token}`,
},
body: JSON.stringify({
provider: provider(),
@@ -99,11 +140,12 @@ export default function S3ConfigPage() {
}
);
- if (!response.ok) {
- throw new Error("Failed to save S3 configuration");
+ if (response) {
+ setHasConfig(true);
+ await commands.globalMessageDialog(
+ "S3 configuration saved successfully"
+ );
}
-
- await commands.globalMessageDialog("S3 configuration saved successfully");
} catch (error) {
console.error("Failed to save S3 config:", error);
await commands.globalMessageDialog(
@@ -114,6 +156,57 @@ export default function S3ConfigPage() {
}
};
+ const handleDelete = async () => {
+ setDeleting(true);
+ try {
+ const response = await makeAuthenticatedRequest(
+ "/api/desktop/s3/config/delete",
+ {
+ method: "DELETE",
+ }
+ );
+
+ if (response) {
+ resetForm();
+ await commands.globalMessageDialog(
+ "S3 configuration deleted successfully"
+ );
+ }
+ } catch (error) {
+ console.error("Failed to delete S3 config:", error);
+ await commands.globalMessageDialog(
+ "Failed to delete S3 configuration. Please try again."
+ );
+ } finally {
+ setDeleting(false);
+ }
+ };
+
+ const renderInput = (
+ label: string,
+ value: () => string,
+ setter: (value: string) => void,
+ placeholder: string,
+ type: "text" | "password" = "text"
+ ) => (
+ <div>
+ <label class="text-gray-500 text-sm">{label}</label>
+ <input
+ type={type}
+ value={value()}
+ onInput={(e: InputEvent & { currentTarget: HTMLInputElement }) =>
+ setter(e.currentTarget.value)
+ }
+ placeholder={placeholder}
+ class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
+ autocomplete="off"
+ autocapitalize="off"
+ autocorrect="off"
+ spellcheck={false}
+ />
+ </div>
+ );
+
return (
<div class="h-full flex flex-col">
<div class="flex-1 overflow-y-auto">
@@ -168,102 +261,67 @@ export default function S3ConfigPage() {
</div>
</div>
- <div>
- <label class="text-gray-500 text-sm">Access Key ID</label>
- <input
- type="password"
- value={accessKeyId()}
- onInput={(
- e: InputEvent & { currentTarget: HTMLInputElement }
- ) => setAccessKeyId(e.currentTarget.value)}
- placeholder="PL31OADSQNK"
- class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
- autocomplete="off"
- autocapitalize="off"
- autocorrect="off"
- spellcheck={false}
- />
- </div>
-
- <div>
- <label class="text-gray-500 text-sm">Secret Access Key</label>
- <input
- type="password"
- value={secretAccessKey()}
- onInput={(
- e: InputEvent & { currentTarget: HTMLInputElement }
- ) => setSecretAccessKey(e.currentTarget.value)}
- placeholder="PL31OADSQNK"
- class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
- autocomplete="off"
- autocapitalize="off"
- autocorrect="off"
- spellcheck={false}
- />
- </div>
-
- <div>
- <label class="text-gray-500 text-sm">Endpoint</label>
- <input
- type="text"
- value={endpoint()}
- onInput={(
- e: InputEvent & { currentTarget: HTMLInputElement }
- ) => setEndpoint(e.currentTarget.value)}
- placeholder="https://s3.amazonaws.com"
- class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
- autocomplete="off"
- autocapitalize="off"
- autocorrect="off"
- spellcheck={false}
- />
- </div>
-
- <div>
- <label class="text-gray-500 text-sm">Bucket Name</label>
- <input
- type="text"
- value={bucketName()}
- onInput={(
- e: InputEvent & { currentTarget: HTMLInputElement }
- ) => setBucketName(e.currentTarget.value)}
- placeholder="my-bucket"
- class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
- autocomplete="off"
- autocapitalize="off"
- autocorrect="off"
- spellcheck={false}
- />
- </div>
-
- <div>
- <label class="text-gray-500 text-sm">Region</label>
- <input
- type="text"
- value={region()}
- onInput={(
- e: InputEvent & { currentTarget: HTMLInputElement }
- ) => setRegion(e.currentTarget.value)}
- placeholder="us-east-1"
- class="w-full px-3 py-2 border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
- autocomplete="off"
- autocapitalize="off"
- autocorrect="off"
- spellcheck={false}
- />
- </div>
+ {renderInput(
+ "Access Key ID",
+ accessKeyId,
+ setAccessKeyId,
+ "PL31OADSQNK",
+ "password"
+ )}
+ {renderInput(
+ "Secret Access Key",
+ secretAccessKey,
+ setSecretAccessKey,
+ "PL31OADSQNK",
+ "password"
+ )}
+ {renderInput(
+ "Endpoint",
+ endpoint,
+ setEndpoint,
+ "https://s3.amazonaws.com"
+ )}
+ {renderInput(
+ "Bucket Name",
+ bucketName,
+ setBucketName,
+ "my-bucket"
+ )}
+ {renderInput("Region", region, setRegion, "us-east-1")}
</div>
)}
</div>
</div>
<div class="flex-shrink-0 p-4 border-t">
- <div class="flex justify-end">
+ <div
+ class={`flex ${
+ hasConfig() ? "justify-between" : "justify-end"
+ } items-center`}
+ >
+ {hasConfig() && (
+ <Button
+ variant="destructive"
+ onClick={handleDelete}
+ disabled={deleting() || loading() || saving()}
+ class={
+ deleting() || loading() || saving()
+ ? "opacity-50 cursor-not-allowed"
+ : ""
+ }
+ >
+ {deleting() ? "Removing..." : "Remove Config"}
+ </Button>
+ )}
<Button
variant="primary"
onClick={handleSave}
- disabled={saving() || loading()}
- class={saving() || loading() ? "opacity-50 cursor-not-allowed" : ""}
+ disabled={saving() || loading() || deleting()}
+ class={
+ saving() || loading() || deleting()
+ ? "opacity-50 cursor-not-allowed"
+ : ""
+ }
>
{saving() ? "Saving..." : "Save"}
</Button>
diff --git a/apps/web/app/api/desktop/s3/config/delete/route.ts b/apps/web/app/api/desktop/s3/config/delete/route.ts
new file mode 100644
index 000000000..aca13d248
--- /dev/null
+++ b/apps/web/app/api/desktop/s3/config/delete/route.ts
@@ -0,0 +1,72 @@
+import { type NextRequest } from "next/server";
+import { db } from "@cap/database";
+import { s3Buckets } from "@cap/database/schema";
+import { eq } from "drizzle-orm";
+import { getCurrentUser } from "@cap/database/auth/session";
+import { cookies } from "next/headers";
+
+export async function DELETE(request: NextRequest) {
+ try {
+ const token = request.headers.get("authorization")?.split(" ")[1];
+ if (token) {
+ cookies().set({
+ name: "next-auth.session-token",
+ value: token,
+ path: "/",
+ sameSite: "none",
+ secure: true,
+ httpOnly: true,
+ });
+ }
+
+ const user = await getCurrentUser();
+ const origin = request.headers.get("origin") as string;
+
+ if (!user) {
+ return new Response(JSON.stringify({ error: "Unauthorized" }), {
+ status: 401,
+ headers: {
+ "Access-Control-Allow-Origin": origin,
+ "Access-Control-Allow-Credentials": "true",
+ },
+ });
+ }
+
+ // Delete the S3 configuration for the user
+ await db.delete(s3Buckets).where(eq(s3Buckets.ownerId, user.id));
+
+ return new Response(JSON.stringify({ success: true }), {
+ headers: {
+ "Access-Control-Allow-Origin": origin,
+ "Access-Control-Allow-Credentials": "true",
+ },
+ });
+ } catch (error) {
+ console.error("Error in S3 config delete route:", error);
+ return new Response(
+ JSON.stringify({
+ error: "Failed to delete S3 configuration",
+ details: error instanceof Error ? error.message : String(error)
+ }),
+ {
+ status: 500,
+ headers: {
+ "Access-Control-Allow-Origin": request.headers.get("origin") as string,
+ "Access-Control-Allow-Credentials": "true",
+ },
+ }
+ );
+ }
+}
+
+export async function OPTIONS(request: NextRequest) {
+ return new Response(null, {
+ status: 200,
+ headers: {
+ "Access-Control-Allow-Origin": request.headers.get("origin") as string,
+ "Access-Control-Allow-Methods": "DELETE, OPTIONS",
+ "Access-Control-Allow-Headers": "Content-Type, Authorization",
+ "Access-Control-Allow-Credentials": "true",
+ },
+ });
+}
\ No newline at end of file
diff --git a/apps/web/app/api/playlist/route.ts b/apps/web/app/api/playlist/route.ts
index 9dcbe8982..de55e963b 100644
--- a/apps/web/app/api/playlist/route.ts
+++ b/apps/web/app/api/playlist/route.ts
@@ -3,7 +3,6 @@ import { db } from "@cap/database";
import { s3Buckets, videos } from "@cap/database/schema";
import { eq } from "drizzle-orm";
import {
- S3Client,
ListObjectsV2Command,
GetObjectCommand,
HeadObjectCommand,