feat/onboarding (#337)

* feat(onboarding): add cookie helper, types and mock data

* feat(onboarding): add stepper, contextual preview and shell

* feat(onboarding): add sso-gate step

* feat(onboarding): add account-info step with real signup

* feat(onboarding): add security step

* feat(onboarding): add preferences step

* fix(onboarding): validate avatar upload size/type and handle FileReader errors

* feat(onboarding): add org-create step

* feat(onboarding): add invite-members step

* feat(onboarding): add notifier step reusing real provider list

* feat(onboarding): add storage step reusing real provider list

* feat(onboarding): add defaults step

* feat(onboarding): add agent-create step

* feat(onboarding): add agent-waiting step with simulated ping

* feat(onboarding): add project-create step

* feat(onboarding): add db-settings step with per-db repeat

* feat(onboarding): add finish step with confetti

* feat(onboarding): wire full step graph

* feat(onboarding): add /welcome route

* feat(onboarding): gate landing, auth and dashboard routes behind onboarding cookie

* fix(onboarding): call next() in finish step so onFlowComplete fires and cookie is written

* docs: add onboarding channel config + style refactor spec

* docs: add conditional step skip when no agents to spec

* docs(onboarding): add implementation plan for channel config flow and style refactor

* feat(onboarding): extend OnboardingChannel with name and config fields

* feat(onboarding): add two-phase config flow and org-combobox style to notifier step

* fix(onboarding): prevent duplicate notifier providers and guard Continue button

* feat(onboarding): add two-phase config flow and org-combobox style to storage step

* style(onboarding): restyle sso-gate provider buttons to org-combobox card style

* style(onboarding): restyle security method choice to org-combobox card style

* style(onboarding): restyle theme toggles in preferences step to org-combobox card style

* style(onboarding): restyle DB toggle buttons in project-create step to org-combobox card style

* feat(onboarding): skip agent-waiting/project-create/db-settings when no agents created

* feat(onboarding): update type system — meta, firstName/lastName, org.id, databases

* feat(onboarding): add settings.onboarding column + markOnboardingDoneAction

* feat(onboarding): add resolveOnboardingState server function

* fix(onboarding): remove unused imports from onboarding-state.ts

* style(onboarding): add branding to shell, fix progress bar full width

* feat(onboarding): replace preview panel with stepper checklist

* feat(onboarding): add step-login replacing sso-gate

* fix(onboarding): step-login — conform to global mutation pattern, handle SSO errors

* feat(onboarding): step-account-info — firstName/lastName, conditional password, findOrCreate

* feat(onboarding): avatar API route (next/og) + AvatarPicker in preferences step

* feat(onboarding): step-security reads passkeyEnabled from flowData.meta

* feat(onboarding): step-org-create calls real createOrganizationAction

* fix(onboarding): step-org-create — simplify error handling, throw on empty name

* feat(onboarding): notifier + storage steps call real actions + findOrCreate on back

* feat(onboarding): step-agent-create calls real createAgentAction + findOrCreate

* feat(onboarding): step-agent-waiting polls real agent lastContact via useQuery

* feat(onboarding): add step-agent-key with edgeKey generation via server action

* feat(onboarding): step-project-create uses real action + real DBs from flowData

* feat(onboarding): wire steps — add login + agent-key, remove sso-gate

* feat(onboarding): page.tsx uses resolveOnboardingState — server-driven resume

* chore(onboarding): delete mock/cookie/preview/sso-gate files — replaced by real implementation

* feat(onboarding): add emailPasswordEnabled to meta, remove logoDataUrl from org

* feat(onboarding): DB-driven resume — query notifiers/storages, check agent lastContact

* feat(onboarding): agent-waiting step is now skippable

* feat(onboarding): login — auto-advance if session, callbackURL on SSO, hide email/pw when disabled, empty state

* fix(onboarding): step-login — add next to useEffect deps, default emailPasswordEnabled to false

* fix(onboarding): verify session after passkey registration, fallback to signIn.passkey()

* feat(onboarding): theme applied dynamically via setTheme, avatar saved to user.image

* feat(onboarding): remove org logo upload — orgs have no logo

* feat(onboarding): show Next button in shell when navigating backwards

* docs: add onboarding refactor design spec

* docs: add onboarding refactor implementation plan

* refactor(onboarding): extract useCreateAgent and useDeleteAgent hooks

* refactor(onboarding): extract useCreateOrg and useCreateProject hooks

* fix(onboarding): trim project name, standardize error narrowing in org/project hooks

* fix(onboarding): use narrowed createData.value in use-create-org

* refactor(onboarding): extract useUpdateAccount hook

* refactor(onboarding): extract useAddNotifier and useRemoveNotifier hooks

* refactor(onboarding): extract useAddStorage and useRemoveStorage hooks

* refactor(onboarding): extract useAgentStatus and useGenerateEdgeKey hooks

* refactor(onboarding): extract useMarkOnboardingDone hook, remove .gitkeep files

* docs: add onboarding theme persistence & progress bar fix spec

* docs: add onboarding theme & progress bar implementation plan

* fix(onboarding): persist theme to db on selection in StepPreferences

* fix(onboarding): recalculate progress bar from full STEP_ORDER position

* fix(theme): sync user theme from DB on all routes, not only dashboard

* fix(theme): use current theme as fallback in preferences, prevent session override of localStorage

* fix(onboarding): correct resume steps and skip agent-waiting flash when agent is connected

* fix(onboarding): properly resolve onboarding state, remove redirect trap in step-defaults, block waiting flash

* fix(onboarding): resume at db-settings instead of finish if no database exists for the project

* fix(onboarding): redirect to agent-create if project exists but no agent is found

* fix(onboarding): allow going from agent-create to project-create if a project already exists

* fix(onboarding): skip intermediate agent steps when going back from project-create to agent-create

* fix(onboarding): skip default step when going back from agent-create if no notifiers and storages exist

* feat(onboarding): allow user to choose between passkey and password during registration if both are enabled

* feat(onboarding): replace tabs with single form and multiple buttons for auth choice

* style(onboarding): improve empty auth state UI with lucide icon and better layout

* fix(onboarding): completely hide back button on the first step instead of disabling it

* fix(onboarding): skip default step if no notifiers and storages exist when advancing from storage step

* fix(onboarding): correctly show select placeholder when default value does not exist in the list

* refactor(onboarding): remove duplicate generate-edge-key action and use core utility directly

* feat(onboarding): compute edgeKey directly on the server side to avoid lazy loading

* fix(onboarding): skip db-settings step if no database is found on the agent

* fix(onboarding): do not skip db settings if agent is connected but no databases found

* refactor(onboarding): skip db settings entirely if no database is selected in project create

* feat(onboarding): auto-save database selections on project create instantly

* style(onboarding): replace button opacity flash with specific spinner animation on db select

* feat(onboarding): extend OnboardingDbSettings with retention, schedule, and policy fields

* feat(onboarding): add applyOnboardingDbSettingsAction server action

* feat(onboarding): add BackupScheduleSelector component

* feat(onboarding): rewrite step-db-settings with three-level phase navigation and full policy persistence

* fix(onboarding): wire applyMutation.mutateAsync everywhere and stabilize list keys

* docs(sdd): append hotfix summary for applyMutation and list keys

* fix(onboarding): address final review findings - atomicity, scheduling guard, apply-all recovery, client validation

* add: onboarding

* fix(migration): merge

* fix(migration): apply for onboarding

* fix(settings): make avatar selector, dicebear picker and tab bar responsive

* chore: checkpoint before features reorganization

* Task 2: Add migration script for features directory reorganization

* refactor(agents): reorganize into actions/components/schemas/hooks

* refactor(auth): reorganize into actions/components/schemas

* refactor(channel): reorganize into components/schemas, nest notifications+storages under components

* refactor(database): reorganize into actions/components/schemas

* refactor(layout): move all components to components/

* refactor(logs): reorganize into components/types

* refactor(migration): reorganize into actions/components

* refactor(notifications): reorganize into components/types/utils

* refactor(organizations): reorganize into actions/components/schemas/hooks/utils

* refactor(profile): reorganize into actions/components/schemas

* refactor(projects): reorganize into actions/components/schemas

* refactor(settings): reorganize into actions/components/schemas/hooks

* refactor(statistics): reorganize into components/utils

* refactor(storages): reorganize into types/utils

* refactor(theme): move all components to components/

* refactor(updates): reorganize into components/hooks

* refactor(upload): move action to actions/

* refactor(users): reorganize into actions/components/schemas

* fix: update broken imports after features reorganization (double-quote relative imports, channel subdir absolute paths)

* fix: .gitignore

* fix: onboarding

* fix: removed dead files

* feat: backfill onboarding flag for instances with existing data

* fix: persist onboarding=true on boot when SKIP_ONBOARDING is set

* feat: remove avatarMode/dicebearStyle from DB schema and types

* feat: simplify resolveAvatarUrl — custom image first, gravatar fallback

* feat: allow avatar upload regardless of mode

* feat: remove avatarMode from layout and profile prop chains

* feat: remove avatar mode admin tab and supporting files

* feat: remove avatar mode selector from onboarding defaults step

* fix: remove preferences onboarding step and fix routing

* fix: remove stale preferences entry from onboarding checklist

* feat: add avatar reset button and explanatory tooltip on profile page

* fix: move avatar tooltip to top-right badge overlay on avatar

* fix: use small destructive button for avatar removal

* fix

* Update step-defaults.tsx

* fix: default dev password for keycloak refer to env

* fix

* fix(migration): refactoring

* fix: api agent helpers.ts

* Update .env.example

---------

Co-authored-by: Théo LAGACHE <theo.lagache@soluce-technologies.com>
Co-authored-by: charles-gauthereau <charles.gauthereau@soluce-technologies.com>
This commit is contained in:
Théo LAGACHE
2026-06-27 17:08:38 +02:00
committed by GitHub
co-authored by Théo LAGACHE charles-gauthereau
parent 40d3228db6
commit 40247efddd
451 changed files with 15904 additions and 5054 deletions
@@ -0,0 +1,87 @@
import { UseFormReturn } from "react-hook-form";
import {
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
import { PasswordInput } from "@/components/ui/password-input";
type StorageBlobFormProps = {
form: UseFormReturn<any, any, any>;
};
export const StorageBlobForm = ({ form }: StorageBlobFormProps) => {
return (
<>
<Separator className="my-1" />
<FormField
control={form.control}
name="config.accountName"
render={({ field }) => (
<FormItem>
<FormLabel>Account Name *</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. mystorageaccount" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.accountKey"
render={({ field }) => (
<FormItem>
<FormLabel>Account Key</FormLabel>
<FormControl>
<PasswordInput {...field} placeholder="e.g. base64-encoded-key" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.connectionString"
render={({ field }) => (
<FormItem>
<FormLabel>Connection String</FormLabel>
<FormControl>
<PasswordInput {...field} placeholder="e.g. DefaultEndpointsProtocol=https;..." />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.containerName"
render={({ field }) => (
<FormItem>
<FormLabel>Container Name *</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. backups-prod" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.endpointUrl"
render={({ field }) => (
<FormItem>
<FormLabel>Endpoint URL</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. https://myaccount.blob.core.windows.net" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
);
};
@@ -0,0 +1,15 @@
import {z} from "zod";
export const BlobChannelConfigSchema = z.object({
accountName: z.string().min(1, "Account name is required"),
accountKey: z.string().optional(),
connectionString: z.string().optional(),
containerName: z.string().min(1, "Container name is required"),
endpointUrl: z.preprocess(
(v) => (v === "" ? undefined : v),
z.string().url("Endpoint URL must be a valid URL").optional(),
),
}).refine(
(data) => data.accountKey || data.connectionString,
{message: "Either account key or connection string is required"}
);
@@ -0,0 +1,185 @@
import {BlobSASPermissions, BlobServiceClient, StorageSharedKeyCredential} from "@azure/storage-blob";
import {
StorageCopyInput,
StorageDeleteInput,
StorageGetInput,
StorageMetaData,
StorageResult,
StorageUploadInput
} from '@/features/storages/types';
import {Readable} from "node:stream";
type BlobConfig = {
accountName: string;
accountKey?: string;
connectionString?: string;
containerName: string;
endpointUrl?: string;
};
async function getBlobClient(config: BlobConfig) {
if (config.connectionString) {
return BlobServiceClient.fromConnectionString(config.connectionString);
}
const url = config.endpointUrl ?? `https://${config.accountName}.blob.core.windows.net`;
const credential = new StorageSharedKeyCredential(config.accountName, config.accountKey!);
return new BlobServiceClient(url, credential);
}
const BASE_DIR = "";
async function ensureContainer(config: BlobConfig) {
const client = await getBlobClient(config);
const containerClient = client.getContainerClient(config.containerName);
await containerClient.createIfNotExists();
}
export async function uploadBlob(
config: BlobConfig,
input: { data: StorageUploadInput, metadata?: StorageMetaData }
): Promise<StorageResult> {
const client = await getBlobClient(config);
await ensureContainer(config);
const key = `${BASE_DIR}${input.data.path}`;
const file = input.data.file;
let uploadStream: Readable;
if (Buffer.isBuffer(file) || file instanceof Uint8Array) {
uploadStream = Readable.from(file);
} else if ((file as any).pipe) {
uploadStream = file;
} else {
return {success: false, provider: "blob", error: "Unsupported file type for streaming upload"};
}
try {
const containerClient = client.getContainerClient(config.containerName);
const blockBlobClient = containerClient.getBlockBlobClient(key);
await blockBlobClient.uploadStream(uploadStream);
} catch (err: any) {
return {success: false, provider: "blob", error: err.message};
}
return {success: true, provider: "blob"};
}
export async function getBlob(
config: BlobConfig,
input: { data: StorageGetInput, metadata: StorageMetaData }
): Promise<StorageResult> {
const client = await getBlobClient(config);
const key = `${BASE_DIR}${input.data.path}`;
const containerClient = client.getContainerClient(config.containerName);
const blockBlobClient = containerClient.getBlockBlobClient(key);
if (!(await blockBlobClient.exists())) {
return {success: false, provider: "blob", error: "File not found"};
}
const downloadResponse = await blockBlobClient.download();
const fileStream = downloadResponse.readableStreamBody as unknown as Readable;
let presignedUrl: string | undefined;
if (input.data.signedUrl) {
presignedUrl = await blockBlobClient.generateSasUrl({
expiresOn: new Date(Date.now() + (input.data.expiresInSeconds ?? 60) * 1000),
permissions: BlobSASPermissions.parse("r"),
});
}
return {
success: true,
provider: "blob",
file: fileStream,
url: presignedUrl,
};
}
export async function deleteBlob(config: BlobConfig, input: {
data: StorageDeleteInput,
metadata?: StorageMetaData
}): Promise<StorageResult> {
const client = await getBlobClient(config);
const key = `${BASE_DIR}${input.data.path}`;
try {
const containerClient = client.getContainerClient(config.containerName);
const blockBlobClient = containerClient.getBlockBlobClient(key);
await blockBlobClient.delete();
return {success: true, provider: "blob"};
} catch (err: any) {
return {success: false, provider: "blob", error: err.message};
}
}
export async function pingBlob(config: BlobConfig): Promise<StorageResult> {
try {
const client = await getBlobClient(config);
const containerClient = client.getContainerClient(config.containerName);
const exists = await containerClient.exists();
if (!exists) return {
success: false,
provider: "blob",
response: "Container does not exist"
};
const key = `${BASE_DIR}ping-${Date.now()}-${Math.random().toString(36).slice(2)}.txt`;
const blockBlobClient = containerClient.getBlockBlobClient(key);
try {
await blockBlobClient.upload(Buffer.from("ping"), 4);
await blockBlobClient.download();
} finally {
await blockBlobClient.delete().catch(() => undefined);
}
return {
success: true,
provider: "blob",
response: "Blob storage OK"
};
} catch (err: any) {
return {
success: false,
provider: "blob",
response: err.message
};
}
}
export async function copyBlob(
config: BlobConfig,
input: {
data: StorageCopyInput,
},
): Promise<StorageResult> {
const client = await getBlobClient(config);
await ensureContainer(config);
const sourceKey = `${BASE_DIR}${input.data.from}`;
const destinationKey = `${BASE_DIR}${input.data.to}`;
try {
const containerClient = client.getContainerClient(config.containerName);
const sourceBlob = containerClient.getBlockBlobClient(sourceKey);
const destBlob = containerClient.getBlockBlobClient(destinationKey);
const poller = await destBlob.beginCopyFromURL(sourceBlob.url);
await poller.pollUntilDone();
return {
success: true,
provider: "blob",
};
} catch (err: any) {
return {
success: false,
provider: "blob",
error: err.message,
};
}
}
@@ -0,0 +1,189 @@
"use server";
import { z } from "zod";
import { ServerActionResult } from "@/types/action-type";
import * as drizzleDb from "@/db";
import { userAction } from "@/lib/safe-actions/actions";
import { db } from "@/db";
import { and, eq } from "drizzle-orm";
import { withUpdatedAt } from "@/db/utils";
import { StorageChannelFormSchema } from "@/features/channel/schemas/channel-form.schema";
import { StorageChannel } from "@/db/schema/12_storage-channel";
export const addStorageChannelAction = userAction
.schema(
z.object({
organizationId: z.string().optional(),
data: StorageChannelFormSchema,
}),
)
.action(
async ({ parsedInput }): Promise<ServerActionResult<StorageChannel>> => {
const { organizationId, data } = parsedInput;
try {
const [channel] = await db
.insert(drizzleDb.schemas.storageChannel)
.values({
provider: data.provider,
name: data.name,
config: data.config,
enabled: data.enabled ?? true,
organizationId: organizationId ?? null,
})
.returning();
if (organizationId) {
await db.insert(drizzleDb.schemas.organizationStorageChannel).values({
organizationId,
storageChannelId: channel.id,
});
}
return {
success: true,
value: {
...channel,
config: channel.config as JSON,
},
actionSuccess: {
message: "Storage channel has been successfully created.",
messageParams: { id: channel.id },
},
};
} catch (_error) {
const error = _error;
return {
success: false,
actionError: {
message: "Failed to create storage channel.",
status: 500,
cause: error instanceof Error ? error.message : "Unknown error",
messageParams: { id: "" },
},
};
}
},
);
export const removeStorageChannelAction = userAction
.schema(
z.object({
organizationId: z.string().optional(),
id: z.string(),
}),
)
.action(
async ({ parsedInput }): Promise<ServerActionResult<StorageChannel>> => {
const { organizationId, id } = parsedInput;
try {
if (organizationId) {
await db
.delete(drizzleDb.schemas.organizationStorageChannel)
.where(
and(
eq(
drizzleDb.schemas.organizationStorageChannel.organizationId,
organizationId,
),
eq(
drizzleDb.schemas.organizationStorageChannel.storageChannelId,
id,
),
),
);
}
const [deletedChannel] = await db
.delete(drizzleDb.schemas.storageChannel)
.where(eq(drizzleDb.schemas.storageChannel.id, id))
.returning();
if (!deletedChannel) {
return {
success: false,
actionError: {
message: "Storage channel not found.",
status: 404,
messageParams: { id: id },
},
};
}
return {
success: true,
value: {
...deletedChannel,
config: deletedChannel.config as JSON,
},
actionSuccess: {
message: "Storage channel has been successfully removed.",
messageParams: { id: id },
},
};
} catch (_error) {
const error = _error;
return {
success: false,
actionError: {
message: "Failed to remove storage channel.",
status: 500,
cause: error instanceof Error ? error.message : "Unknown error",
messageParams: { id: id },
},
};
}
},
);
export const updateStorageChannelAction = userAction
.schema(
z.object({
id: z.string(),
data: StorageChannelFormSchema,
}),
)
.action(
async ({ parsedInput }): Promise<ServerActionResult<StorageChannel>> => {
const { id, data } = parsedInput;
try {
const [channel] = await db
.update(drizzleDb.schemas.storageChannel)
.set(
withUpdatedAt({
provider: data.provider,
name: data.name,
config: data.config,
enabled: data.enabled ?? true,
}),
)
.where(eq(drizzleDb.schemas.storageChannel.id, id))
.returning();
return {
success: true,
value: {
...channel,
config: channel.config as JSON,
},
actionSuccess: {
message: `Storage channel "${channel.name}" has been successfully updated.`,
messageParams: { id: channel.id },
},
};
} catch (_error) {
const error = _error;
return {
success: false,
actionError: {
message: "Failed to update storage channel.",
status: 500,
cause: error instanceof Error ? error.message : "Unknown error",
messageParams: { id: "" },
},
};
}
},
);
@@ -0,0 +1,52 @@
"use server"
import {userAction} from "@/lib/safe-actions/actions";
import {z} from "zod";
import {ServerActionResult} from "@/types/action-type";
export const googleDriveRefreshTokenAction = userAction.schema(
z.object({
code: z.string(),
clientId: z.string(),
clientSecret: z.string(),
redirectUri: z.string(),
})).action(async ({parsedInput}): Promise<ServerActionResult<string>> => {
const {code, clientId, clientSecret, redirectUri} = parsedInput;
try {
const tokenRes = await fetch("https://oauth2.googleapis.com/token", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: clientId,
client_secret: clientSecret,
code: code,
grant_type: "authorization_code",
redirect_uri: redirectUri,
}),
});
const tokens = await tokenRes.json();
return {
success: true,
value: tokens.refresh_token,
actionSuccess: {
message: "Refresh token successfully fetched",
messageParams: {code: code},
},
};
}catch {
return {
success: false,
actionError: {
message: "An error occurred",
status: 404,
messageParams: {code: code},
},
};
}
});
@@ -0,0 +1,151 @@
"use client";
import {UseFormReturn} from "react-hook-form";
import {FormControl, FormField, FormItem, FormLabel, FormMessage} from "@/components/ui/form";
import {Input} from "@/components/ui/input";
import {Separator} from "@/components/ui/separator";
import {Button} from "@/components/ui/button";
import {PasswordInput} from "@/components/ui/password-input";
import {
googleDriveRefreshTokenAction
} from "@/features/channel/components/storages/google-drive/google-drive-refresh.action";
import {toast} from "sonner";
type StorageGoogleDriveFormProps = {
form: UseFormReturn<any>;
};
export const StorageGoogleDriveForm = ({form}: StorageGoogleDriveFormProps) => {
const refreshToken = form.watch("config.refreshToken");
const isConnected = Boolean(refreshToken);
const handleConnect = () => {
const clientId = form.getValues("config.clientId");
const clientSecret = form.getValues("config.clientSecret");
const redirectUri = `${window.location.origin}/api/google/drive/callback`;
if (!clientId || !clientSecret) {
form.setError("config.clientId", {message: "Client ID and Secret are required"});
return;
}
const scope = encodeURIComponent("https://www.googleapis.com/auth/drive.file");
const oauthUrl =
`https://accounts.google.com/o/oauth2/v2/auth` +
`?client_id=${clientId}` +
`&redirect_uri=${encodeURIComponent(redirectUri)}` +
`&response_type=code` +
`&scope=${scope}` +
`&access_type=offline` +
`&prompt=consent`;
const oauthWindow = window.open(oauthUrl, "_blank", "width=500,height=600");
const interval = setInterval(async () => {
try {
if (!oauthWindow || oauthWindow.closed) {
clearInterval(interval);
return;
}
if (oauthWindow.location.href.startsWith(redirectUri)) {
const code = new URL(oauthWindow.location.href).searchParams.get("code");
if (!code) return;
const result = await googleDriveRefreshTokenAction({
code,
clientId,
clientSecret,
redirectUri,
});
const inner = result?.data;
if (inner?.success) {
toast.success(inner.actionSuccess?.message);
if (!inner.value) {
form.setError("config", {message: "OAuth succeeded but no refresh token returned"});
return;
}
form.setValue("config.refreshToken", inner.value, {
shouldValidate: true,
shouldDirty: true,
});
} else {
toast.error(inner?.actionError?.message);
}
oauthWindow.close();
clearInterval(interval);
}
} catch {
// Ignore cross-origin errors until redirect
}
}, 500);
};
return (
<>
<Separator className="my-1"/>
<FormField
control={form.control}
name="config.clientId"
render={({field}) => (
<FormItem>
<FormLabel>Client ID *</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. xxxx.apps.googleusercontent.com"/>
</FormControl>
<FormMessage/>
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.clientSecret"
render={({field}) => (
<FormItem>
<FormLabel>Client Secret *</FormLabel>
<FormControl>
<PasswordInput {...field} />
</FormControl>
<FormMessage/>
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.folderId"
render={({field}) => (
<FormItem>
<FormLabel>Folder ID *</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. 1AbCdEfGhIjKlMnOpQrStUvWxYz"/>
</FormControl>
<FormMessage/>
</FormItem>
)}
/>
<input type="hidden" {...form.register("config.refreshToken")} />
<div className="flex items-center gap-3">
<Button type="button" variant={"secondary"} className="hover:cursor-pointer" onClick={handleConnect}>
{isConnected ? "Reconnect Google Drive" : "Connect Google Drive"}
</Button>
{isConnected && (
<span className="text-sm text-green-600 font-medium">
Google Drive connected
</span>
)}
</div>
</>
);
};
@@ -0,0 +1,8 @@
import {z} from "zod";
export const GoogleDriveChannelConfigSchema = z.object({
clientId: z.string().min(1, "Client ID is required"),
clientSecret: z.string().min(1, "Client Secret is required"),
folderId: z.string().min(1, "Folder ID is required"),
refreshToken: z.string().optional(),
});
@@ -0,0 +1,101 @@
import {drive_v3, google} from "googleapis";
import {GoogleDriveConfig} from "@/features/channel/components/storages/google-drive/types";
import Drive = drive_v3.Drive;
import {getServerUrl} from "@/utils/get-server-url";
export async function getGoogleDriveClient(config: GoogleDriveConfig): Promise<Drive> {
const baseUrl = getServerUrl();
const oauth2Client = new google.auth.OAuth2(
config.clientId,
config.clientSecret,
baseUrl
);
oauth2Client.setCredentials({
refresh_token: config.refreshToken
});
return google.drive({
version: "v3",
auth: oauth2Client
});
}
export async function findFileByName(
drive: drive_v3.Drive,
name: string,
folderId: string
): Promise<string | null> {
const res = await drive.files.list({
q: `name='${name}' and '${folderId}' in parents and trashed=false`,
fields: "files(id)",
pageSize: 1,
supportsAllDrives: true,
includeItemsFromAllDrives: true,
});
return res.data.files?.[0]?.id ?? null;
}
export async function ensureFolderPath(client: any, path: string, rootFolderId: string): Promise<string> {
const parts = path.split("/").filter(Boolean); // ["backups", "project-1"]
let parentId = rootFolderId;
for (const part of parts) {
const res = await client.files.list({
q: `'${parentId}' in parents and name='${part}' and mimeType='application/vnd.google-apps.folder' and trashed=false`,
fields: "files(id, name)",
supportsAllDrives: true,
includeItemsFromAllDrives: true,
});
if (res.data.files && res.data.files.length > 0) {
parentId = res.data.files[0].id!;
} else {
const folder = await client.files.create({
requestBody: {
name: part,
mimeType: "application/vnd.google-apps.folder",
parents: [parentId],
},
fields: "id",
supportsAllDrives: true,
});
parentId = folder.data.id!;
}
}
return parentId;
}
export async function resolveFilePath(client: any, fullPath: string, rootFolderId: string): Promise<string | null> {
const parts = fullPath.split("/").filter(Boolean);
const fileName = parts.pop()!;
let parentId = rootFolderId;
for (const part of parts) {
const res = await client.files.list({
q: `'${parentId}' in parents and name='${part}' and mimeType='application/vnd.google-apps.folder' and trashed=false`,
fields: "files(id, name)",
supportsAllDrives: true,
includeItemsFromAllDrives: true,
});
if (res.data.files && res.data.files.length > 0) {
parentId = res.data.files[0].id!;
} else {
return null;
}
}
const fileRes = await client.files.list({
q: `'${parentId}' in parents and name='${fileName}' and trashed=false`,
fields: "files(id, name)",
supportsAllDrives: true,
includeItemsFromAllDrives: true,
});
return fileRes.data.files?.[0]?.id || null;
}
@@ -0,0 +1,221 @@
"use server"
import {
StorageCopyInput,
StorageDeleteInput,
StorageGetInput,
StorageMetaData,
StorageResult,
StorageUploadInput
} from '@/features/storages/types';
import {GoogleDriveConfig} from "@/features/channel/components/storages/google-drive/types";
import {
ensureFolderPath,
findFileByName,
getGoogleDriveClient, resolveFilePath
} from "@/features/channel/components/storages/google-drive/helpers";
import {Readable} from "node:stream";
import {generateFileUrl} from "@/features/storages/utils/storages.helpers";
export async function uploadGoogleDrive(
config: GoogleDriveConfig,
input: { data: StorageUploadInput, metadata?: StorageMetaData },
): Promise<StorageResult> {
const client = await getGoogleDriveClient(config);
const fullPath = input.data.path;
const pathParts = fullPath.split("/").filter(Boolean);
const fileName = pathParts.pop()!;
const folderPath = pathParts.join("/");
const folderId = folderPath
? await ensureFolderPath(client, folderPath, config.folderId)
: config.folderId;
const existing = await findFileByName(client, fileName, folderId);
if (existing) return {success: false, provider: "google-drive", error: "File already exists"};
let fileStream: Readable;
const file = input.data.file;
if (Buffer.isBuffer(file) || file instanceof Uint8Array) {
fileStream = Readable.from(file);
} else if ((file as any).pipe) {
fileStream = file as Readable;
} else {
throw new Error("Unsupported file type for streaming upload");
}
await client.files.create({
requestBody: {name: fileName, parents: [folderId]},
media: {body: fileStream},
fields: "id",
supportsAllDrives: true,
});
if (input.data.url) {
const url = await generateFileUrl(input);
if (!url) {
return {
success: false,
provider: "google-drive",
response: "Unable to get url file"
};
}
return {
success: true,
provider: 'google-drive',
url: url
};
}
return {
success: true,
provider: 'google-drive',
};
}
export async function getGoogleDrive(
config: GoogleDriveConfig,
input: { data: StorageGetInput, metadata: StorageMetaData },
): Promise<StorageResult> {
const client = await getGoogleDriveClient(config);
const fileId = await resolveFilePath(client, input.data.path, config.folderId);
if (!fileId) return {success: false, provider: "google-drive", error: "File not found"};
const res = await client.files.get(
{fileId, alt: "media", supportsAllDrives: true},
{responseType: "stream"}
);
const stream = res.data as Readable;
if (input.data.signedUrl) {
const url = await generateFileUrl(input);
if (!url) {
return {
success: false,
provider: "google-drive",
response: "Unable to get url"
};
}
return {
success: true,
provider: "google-drive",
file: stream,
url: url,
};
}
return {
success: true,
provider: "google-drive",
file: stream,
};
}
export async function deleteGoogleDrive(
config: GoogleDriveConfig,
input: { data: StorageDeleteInput, metadata?: StorageMetaData },
): Promise<StorageResult> {
const client = await getGoogleDriveClient(config);
const fileId = await resolveFilePath(client, input.data.path, config.folderId);
if (!fileId) return {success: false, provider: "google-drive", error: "File not found"};
await client.files.delete({fileId, supportsAllDrives: true});
return {success: true, provider: "google-drive"};
}
export async function pingGoogleDrive(config: GoogleDriveConfig): Promise<StorageResult> {
try {
const drive = await getGoogleDriveClient(config);
const name = `ping-${Date.now()}.txt`;
const buffer = Buffer.from("ping");
const file = await drive.files.create({
requestBody: {name, parents: [config.folderId]},
media: {mimeType: "text/plain", body: Readable.from(buffer)},
fields: "id",
supportsAllDrives: true,
});
await drive.files.get({fileId: file.data.id!, supportsAllDrives: true});
await drive.files.delete({fileId: file.data.id!, supportsAllDrives: true});
return {success: true, provider: "google-drive", response: "Google Drive storage OK"};
} catch (err: any) {
return {success: false, provider: "google-drive", response: err.message};
}
}
export async function copyGoogleDrive(
config: GoogleDriveConfig,
input: {
data: StorageCopyInput,
metadata?: StorageMetaData;
},
): Promise<StorageResult> {
const client = await getGoogleDriveClient(config);
const sourceFileId = await resolveFilePath(
client,
input.data.from,
config.folderId,
);
if (!sourceFileId) {
return {
success: false,
provider: "google-drive",
error: "Source file not found",
};
}
const fullPath = input.data.to;
const parts = fullPath.split("/").filter(Boolean);
const fileName = parts.pop()!;
const folderPath = parts.join("/");
const folderId = folderPath
? await ensureFolderPath(client, folderPath, config.folderId)
: config.folderId;
try {
const copied = await client.files.copy({
fileId: sourceFileId,
requestBody: {
name: fileName,
parents: [folderId],
},
fields: "id",
supportsAllDrives: true,
});
const newFileId = copied.data.id;
if (!newFileId) {
return {
success: false,
provider: "google-drive",
error: "Copy failed (no file id returned)",
};
}
return {
success: true,
provider: "google-drive",
};
} catch (err: any) {
return {
success: false,
provider: "google-drive",
error: err.message || "Copy failed",
};
}
}
@@ -0,0 +1,7 @@
export type GoogleDriveConfig = {
clientId: string;
clientSecret: string;
refreshToken: string;
folderId: string;
};
@@ -0,0 +1,81 @@
import {
StorageProviderKind,
StorageInput,
StorageResult,
} from '@/features/storages/types';
import {uploadLocal, getLocal, deleteLocal, pingLocal, copyLocal} from './local';
import {copyS3, deleteS3, getS3, pingS3, uploadS3} from "@/features/channel/components/storages/s3";
import {copyBlob, deleteBlob, getBlob, pingBlob, uploadBlob} from "@/features/channel/components/storages/az-blob";
import {
copyGoogleDrive,
deleteGoogleDrive,
getGoogleDrive,
pingGoogleDrive,
uploadGoogleDrive
} from "@/features/channel/components/storages/google-drive";
type ProviderHandler = {
upload: (config: any, input: StorageInput & { action: 'upload' }) => Promise<StorageResult>;
get: (config: any, input: StorageInput & { action: 'get' }) => Promise<StorageResult>;
delete: (config: any, input: StorageInput & { action: 'delete' }) => Promise<StorageResult>;
ping: (config: any, input: { action: 'ping' }) => Promise<StorageResult>;
copy: (config: any, input: StorageInput & { action: 'copy' }) => Promise<StorageResult>;
};
const handlers: Record<StorageProviderKind, ProviderHandler> = {
local: {
upload: uploadLocal,
get: getLocal,
delete: deleteLocal,
ping: pingLocal,
copy: copyLocal,
},
s3: {
upload: uploadS3,
get: getS3,
delete: deleteS3,
ping: pingS3,
copy: copyS3
},
"google-drive": {
upload: uploadGoogleDrive,
get: getGoogleDrive,
delete: deleteGoogleDrive,
ping: pingGoogleDrive,
copy: copyGoogleDrive,
},
blob: {
upload: uploadBlob,
get: getBlob,
delete: deleteBlob,
ping: pingBlob,
copy: copyBlob,
}
};
export async function dispatchViaProvider(
kind: StorageProviderKind,
config: any,
input: StorageInput,
): Promise<StorageResult> {
const provider = handlers[kind];
if (!provider) {
return {
success: false,
provider: kind,
error: `Unsupported storage provider: ${kind}`,
};
}
try {
return await provider[input.action](config, input as any);
} catch (err: any) {
return {
success: false,
provider: kind,
error: err.message || 'Storage provider error',
};
}
}
@@ -0,0 +1,3 @@
import {z} from "zod";
export const LocalChannelConfigSchema = z.object({});
@@ -0,0 +1,215 @@
"use server";
import { mkdir, unlink } from "fs/promises";
import path from "path";
import {
StorageCopyInput,
StorageDeleteInput,
StorageGetInput,
StorageMetaData,
StorageResult,
StorageUploadInput,
} from '@/features/storages/types';
import fs from "node:fs";
import { generateFileUrl } from "@/features/storages/utils/storages.helpers";
import { Readable } from "node:stream";
import { env } from "@/env.mjs";
const BASE_DIR = path.join(env.PRIVATE_PATH!, "/uploads");
export async function uploadLocal(
config: { baseDir?: string },
input: { data: StorageUploadInput; metadata?: StorageMetaData },
): Promise<StorageResult> {
const base = config.baseDir
? path.join(process.cwd(), config.baseDir ?? "")
: BASE_DIR;
const fullPath = path.join(base, input.data.path);
const dir = path.dirname(fullPath);
await mkdir(dir, { recursive: true });
try {
const file = input.data.file;
if (Buffer.isBuffer(file)) {
await fs.promises.writeFile(fullPath, input.data.file);
} else if (file instanceof Readable) {
await new Promise<void>((resolve, reject) => {
const writable = fs.createWriteStream(fullPath);
file.pipe(writable);
writable.on("finish", resolve);
writable.on("error", reject);
});
} else {
return {
success: false,
provider: "local",
error: "Unsupported file type. Must be Buffer or ReadableStream",
};
}
if (input.data.url) {
const url = await generateFileUrl(input);
if (!url) {
return {
success: false,
provider: "local",
response: "Unable to get URL",
};
}
return { success: true, provider: "local", url };
}
return { success: true, provider: "local" };
} catch (err: any) {
try {
await unlink(fullPath);
} catch {}
return {
success: false,
provider: "local",
error: err.message || "Upload failed",
};
}
}
export async function getLocal(
config: { baseDir?: string },
input: { data: StorageGetInput; metadata: StorageMetaData },
): Promise<StorageResult> {
const base = config.baseDir
? path.join(process.cwd(), config.baseDir ?? "")
: BASE_DIR;
const filePath = path.join(base, input.data.path);
if (!fs.existsSync(filePath)) {
return {
success: false,
provider: "local",
error: "File not found",
};
}
let fileStream: fs.ReadStream | undefined;
try {
fileStream = fs.createReadStream(filePath);
} catch (err: any) {
return {
success: false,
provider: "local",
error: err.message,
};
}
if (input.data.signedUrl) {
const url = await generateFileUrl(input);
if (!url) {
return {
success: false,
provider: "local",
error: "Unable to generate signed URL",
};
}
return {
success: true,
provider: "local",
file: fileStream,
url,
};
}
return {
success: true,
provider: "local",
file: fileStream,
};
}
export async function deleteLocal(
config: { baseDir?: string },
input: { data: StorageDeleteInput; metadata?: StorageMetaData },
): Promise<StorageResult> {
const base = config.baseDir
? path.join(process.cwd(), config.baseDir ?? "")
: BASE_DIR;
const fullPath = path.join(base, input.data.path);
await unlink(fullPath);
return {
success: true,
provider: "local",
};
}
export async function pingLocal(config: {
baseDir?: string;
}): Promise<StorageResult> {
const base = path.join(process.cwd(), config.baseDir ?? "") || BASE_DIR;
const fullPath = path.join(base, "ping.txt");
await fs.promises.writeFile(fullPath, "ping");
await fs.promises.readFile(fullPath);
await fs.promises.unlink(fullPath);
return {
success: true,
provider: "local",
response: "Local storage OK",
};
}
export async function copyLocal(
config: { baseDir?: string },
input: {
data: StorageCopyInput,
metadata?: StorageMetaData;
},
): Promise<StorageResult> {
const base = config.baseDir
? path.join(process.cwd(), config.baseDir ?? "")
: BASE_DIR;
const sourcePath = path.join(base, input.data.from);
const destinationPath = path.join(base, input.data.to);
const dir = path.dirname(destinationPath);
await mkdir(dir, { recursive: true });
if (!fs.existsSync(sourcePath)) {
return {
success: false,
provider: "local",
error: "Source file not found",
};
}
try {
await new Promise<void>((resolve, reject) => {
const readStream = fs.createReadStream(sourcePath);
const writeStream = fs.createWriteStream(destinationPath);
readStream.on("error", reject);
writeStream.on("error", reject);
writeStream.on("finish", resolve);
readStream.pipe(writeStream);
});
return {
success: true,
provider: "local",
};
} catch (err: any) {
try {
await unlink(destinationPath);
} catch {}
return {
success: false,
provider: "local",
error: err.message || "Copy failed",
};
}
}
@@ -0,0 +1,116 @@
import { UseFormReturn } from "react-hook-form";
import {
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
import { PasswordInput } from "@/components/ui/password-input";
import { Switch } from "@/components/ui/switch";
type StorageS3FormProps = {
form: UseFormReturn<any, any, any>;
};
export const StorageS3Form = ({ form }: StorageS3FormProps) => {
return (
<>
<Separator className="my-1" />
<FormField
control={form.control}
name="config.endPointUrl"
render={({ field }) => (
<FormItem>
<FormLabel>Endpoint URL *</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. s3.amazonaws.com" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.region"
render={({ field }) => (
<FormItem>
<FormLabel>Region</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. us-east-1" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.accessKey"
render={({ field }) => (
<FormItem>
<FormLabel>Access Key *</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. AKIA..." />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.secretKey"
render={({ field }) => (
<FormItem>
<FormLabel>Secret Key *</FormLabel>
<FormControl>
<PasswordInput {...field} placeholder="e.g. s3-secret-key" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.bucketName"
render={({ field }) => (
<FormItem>
<FormLabel>Bucket name *</FormLabel>
<FormControl>
<Input {...field} placeholder="e.g. backups-prod" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.port"
render={({ field }) => (
<FormItem>
<FormLabel>Port</FormLabel>
<FormControl>
<Input {...field} type="number" placeholder="e.g. 443" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="config.ssl"
render={({ field }) => (
<FormItem>
<FormLabel>Use SSL</FormLabel>
<FormControl>
<Switch checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
);
};
@@ -0,0 +1,15 @@
import {z} from "zod";
export const S3ChannelConfigSchema = z.object({
endPointUrl: z.string().min(1, "Endpoint URL is required"),
region: z.string().optional(),
accessKey: z.string().min(1, "Access Key is required"),
secretKey: z.string().min(1, "Secret Key is required"),
bucketName: z.string().min(1, "Bucket name is required"),
port: z.union([
z.literal("").transform(() => ""),
z.literal("").transform(() => ""),
z.coerce.number()
]).optional(),
ssl: z.boolean().optional().default(true),
});
@@ -0,0 +1,174 @@
import * as Minio from "minio";
import {
StorageCopyInput,
StorageDeleteInput,
StorageGetInput,
StorageMetaData,
StorageResult,
StorageUploadInput
} from '@/features/storages/types';
import {Readable} from "node:stream";
type S3Config = {
endPointUrl: string;
region?: string;
accessKey: string;
secretKey: string;
bucketName: string;
port?: number;
ssl?: boolean;
};
async function getS3Client(config: S3Config) {
return new Minio.Client({
endPoint: config.endPointUrl,
region: config.region ?? "us-east-1",
accessKey: config.accessKey,
secretKey: config.secretKey,
port: config.port ? Number(config.port) : 443,
useSSL: config.ssl ?? true,
});
}
const BASE_DIR = "";
async function ensureBucket(config: S3Config) {
const client = await getS3Client(config);
const exists = await client.bucketExists(config.bucketName);
if (!exists) await client.makeBucket(config.bucketName);
}
export async function uploadS3(
config: S3Config,
input: { data: StorageUploadInput, metadata?: StorageMetaData }
): Promise<StorageResult> {
const client = await getS3Client(config);
await ensureBucket(config);
const key = `${BASE_DIR}${input.data.path}`;
const file = input.data.file;
let uploadStream: Readable;
if (Buffer.isBuffer(file) || file instanceof Uint8Array) {
uploadStream = Readable.from(file);
} else if ((file as any).pipe) {
uploadStream = file;
} else {
return {success: false, provider: "s3", error: "Unsupported file type for streaming upload"};
}
try {
const result = await client.putObject(config.bucketName, key, uploadStream, input.data.size);
} catch (err: any) {
return {success: false, provider: "s3", error: err.message};
}
return {success: true, provider: "s3"};
}
export async function getS3(
config: S3Config,
input: { data: StorageGetInput, metadata: StorageMetaData }
): Promise<StorageResult> {
const client = await getS3Client(config);
const key = `${BASE_DIR}${input.data.path}`;
try {
await client.statObject(config.bucketName, key);
} catch {
return {success: false, provider: "s3", error: "File not found"};
}
const fileStream = await client.getObject(config.bucketName, key);
let presignedUrl: string | undefined;
if (input.data.signedUrl) {
presignedUrl = await client.presignedGetObject(config.bucketName, key, input.data.expiresInSeconds ?? 60);
}
return {
success: true,
provider: "s3",
file: fileStream as unknown as Buffer | Readable,
url: presignedUrl,
};
}
export async function deleteS3(config: S3Config, input: {
data: StorageDeleteInput,
metadata?: StorageMetaData
}): Promise<StorageResult> {
const client = await getS3Client(config);
const key = `${BASE_DIR}${input.data.path}`;
try {
await client.removeObject(config.bucketName, key);
return {success: true, provider: "s3"};
} catch (err: any) {
return {success: false, provider: "s3", error: err.message};
}
}
export async function pingS3(config: S3Config): Promise<StorageResult> {
try {
const client = await getS3Client(config);
const exists = await client.bucketExists(config.bucketName);
if (!exists) return {
success: false,
provider: "s3",
response: "Bucket does not exist"
};
const key = `${BASE_DIR}ping.txt`;
await client.putObject(config.bucketName, key, Buffer.from("ping"));
await client.getObject(config.bucketName, key);
await client.removeObject(config.bucketName, key);
return {
success: true,
provider: "s3",
response: "S3 storage OK"
};
} catch (err: any) {
return {
success: false,
provider: "s3",
response: err.message
};
}
}
export async function copyS3(
config: S3Config,
input: {
data: StorageCopyInput,
},
): Promise<StorageResult> {
const client = await getS3Client(config);
await ensureBucket(config);
const sourceKey = `${BASE_DIR}${input.data.from}`;
const destinationKey = `${BASE_DIR}${input.data.to}`;
try {
await client.copyObject(
config.bucketName,
destinationKey,
`/${config.bucketName}/${sourceKey}`
);
return {
success: true,
provider: "s3",
};
} catch (err: any) {
return {
success: false,
provider: "s3",
error: err.message,
};
}
}