Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Add avatar in workspace form

Hakan Shehu4f9a67fa6b2a
diff --git a/desktop/src/main/handlers/mutations/workspace-create.ts b/desktop/src/main/handlers/mutations/workspace-create.ts
index 88776415..7a7238c9 100644
--- a/desktop/src/main/handlers/mutations/workspace-create.ts
+++ b/desktop/src/main/handlers/mutations/workspace-create.ts
@@ -42,6 +42,7 @@ export class WorkspaceCreateMutationHandler
const { data } = await axios.post<Workspace>(`/v1/workspaces`, {
name: input.name,
description: input.description,
+ avatar: input.avatar,
});
await databaseManager.appDatabase
diff --git a/desktop/src/main/handlers/mutations/workspace-update.ts b/desktop/src/main/handlers/mutations/workspace-update.ts
index cba3b239..bdff5063 100644
--- a/desktop/src/main/handlers/mutations/workspace-update.ts
+++ b/desktop/src/main/handlers/mutations/workspace-update.ts
@@ -39,9 +39,11 @@ export class WorkspaceUpdateMutationHandler
server.attributes,
account.token,
);
- const { data } = await axios.post<Workspace>(`/v1/workspaces/${input.id}`, {
+
+ const { data } = await axios.put<Workspace>(`/v1/workspaces/${input.id}`, {
name: input.name,
description: input.description,
+ avatar: input.avatar,
});
await databaseManager.appDatabase
diff --git a/desktop/src/operations/mutations/workspace-create.ts b/desktop/src/operations/mutations/workspace-create.ts
index aa6b590c..1f7a2d73 100644
--- a/desktop/src/operations/mutations/workspace-create.ts
+++ b/desktop/src/operations/mutations/workspace-create.ts
@@ -3,6 +3,7 @@ export type WorkspaceCreateMutationInput = {
name: string;
description: string;
accountId: string;
+ avatar: string | null;
};
export type WorkspaceCreateMutationOutput = {
diff --git a/desktop/src/operations/mutations/workspace-update.ts b/desktop/src/operations/mutations/workspace-update.ts
index 12aea787..39b226e5 100644
--- a/desktop/src/operations/mutations/workspace-update.ts
+++ b/desktop/src/operations/mutations/workspace-update.ts
@@ -3,6 +3,7 @@ export type WorkspaceUpdateMutationInput = {
id: string;
name: string;
description: string;
+ avatar: string | null;
accountId: string;
};
diff --git a/desktop/src/renderer/components/workspaces/sidebars/sidebar-header.tsx b/desktop/src/renderer/components/workspaces/sidebars/sidebar-header.tsx
index 9f35d80c..2a26ad47 100644
--- a/desktop/src/renderer/components/workspaces/sidebars/sidebar-header.tsx
+++ b/desktop/src/renderer/components/workspaces/sidebars/sidebar-header.tsx
@@ -25,6 +25,7 @@ export const SidebarHeader = () => {
<Avatar
id={workspace.id}
name={workspace.name}
+ avatar={workspace.avatar}
className="h-7 w-7"
/>
<p className="flex-grow">{workspace.name}</p>
@@ -38,6 +39,7 @@ export const SidebarHeader = () => {
<Avatar
id={account.id}
name={account.name}
+ avatar={account.avatar}
className="mt-1 h-7 w-7"
/>
<div className="flex flex-grow flex-col">
@@ -58,7 +60,12 @@ export const SidebarHeader = () => {
setOpen(false);
}}
>
- <Avatar id={w.id} name={w.name} className="h-7 w-7" />
+ <Avatar
+ id={w.id}
+ name={w.name}
+ avatar={w.avatar}
+ className="h-7 w-7"
+ />
<p>{w.name}</p>
</li>
);
diff --git a/desktop/src/renderer/components/workspaces/workspace-create.tsx b/desktop/src/renderer/components/workspaces/workspace-create.tsx
index 7b7d9447..8c220a09 100644
--- a/desktop/src/renderer/components/workspaces/workspace-create.tsx
+++ b/desktop/src/renderer/components/workspaces/workspace-create.tsx
@@ -1,65 +1,18 @@
import React from 'react';
-import { z } from 'zod';
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormMessage,
- FormLabel,
-} from '@/renderer/components/ui/form';
-import { useForm } from 'react-hook-form';
-import { zodResolver } from '@hookform/resolvers/zod';
-import { Button } from '@/renderer/components/ui/button';
-import { Spinner } from '@/renderer/components/ui/spinner';
-import { Input } from '@/renderer/components/ui/input';
-import { Textarea } from '@/renderer/components/ui/textarea';
import { toast } from '@/renderer/components/ui/use-toast';
import { useMutation } from '@/renderer/hooks/use-mutation';
import { useAccount } from '@/renderer/contexts/account';
import { useNavigate } from 'react-router-dom';
-
-const formSchema = z.object({
- name: z.string().min(3, 'Name must be at least 3 characters long.'),
- description: z.string(),
-});
-
-type formSchemaType = z.infer<typeof formSchema>;
+import { WorkspaceForm } from '@/renderer/components/workspaces/workspace-form';
export const WorkspaceCreate = () => {
const account = useAccount();
const navigate = useNavigate();
const { mutate, isPending } = useMutation();
- const form = useForm<formSchemaType>({
- resolver: zodResolver(formSchema),
- defaultValues: {
- name: '',
- description: '',
- },
- });
- const handleSubmit = async (values: formSchemaType) => {
- mutate({
- input: {
- type: 'workspace_create',
- name: values.name,
- description: values.description,
- accountId: account.id,
- },
- onSuccess() {
- window.location.href = '/';
- },
- onError() {
- toast({
- title: 'Failed to create workspace',
- description:
- 'Somewthing went wrong creating the workspace. Please try again!',
- variant: 'destructive',
- });
- },
- });
- };
+ const handleCancel =
+ account.workspaces.length > 1 ? () => navigate('/') : undefined;
return (
<div className="container flex flex-row justify-center">
@@ -69,63 +22,33 @@ export const WorkspaceCreate = () => {
Setup your workspace
</h1>
</div>
- <Form {...form}>
- <form
- className="flex flex-col"
- onSubmit={form.handleSubmit(handleSubmit)}
- >
- <div className="flex-grow space-y-4 py-2 pb-4">
- <FormField
- control={form.control}
- name="name"
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>Name *</FormLabel>
- <FormControl>
- <Input placeholder="Name" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="description"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Description</FormLabel>
- <FormControl>
- <Textarea
- placeholder="Write a short description about the workspace"
- {...field}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- </div>
- <div className="flex flex-row justify-end gap-2">
- {account.workspaces?.length > 0 && (
- <Button
- type="button"
- disabled={isPending}
- variant="outline"
- onClick={() => {
- navigate('/');
- }}
- >
- Cancel
- </Button>
- )}
-
- <Button type="submit" disabled={isPending}>
- {isPending && <Spinner className="mr-1" />}
- Create
- </Button>
- </div>
- </form>
- </Form>
+ <WorkspaceForm
+ onSubmit={(values) => {
+ mutate({
+ input: {
+ type: 'workspace_create',
+ name: values.name,
+ description: values.description,
+ accountId: account.id,
+ avatar: values.avatar,
+ },
+ onSuccess() {
+ window.location.href = '/';
+ },
+ onError() {
+ toast({
+ title: 'Failed to create workspace',
+ description:
+ 'Somewthing went wrong creating the workspace. Please try again!',
+ variant: 'destructive',
+ });
+ },
+ });
+ }}
+ isSaving={isPending}
+ onCancel={handleCancel}
+ saveText="Create"
+ />
</div>
</div>
);
diff --git a/desktop/src/renderer/components/workspaces/workspace-form.tsx b/desktop/src/renderer/components/workspaces/workspace-form.tsx
new file mode 100644
index 00000000..0eabe105
--- /dev/null
+++ b/desktop/src/renderer/components/workspaces/workspace-form.tsx
@@ -0,0 +1,194 @@
+import React from 'react';
+import { z } from 'zod';
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormMessage,
+ FormLabel,
+} from '@/renderer/components/ui/form';
+import { useForm } from 'react-hook-form';
+import { zodResolver } from '@hookform/resolvers/zod';
+import { Button } from '@/renderer/components/ui/button';
+import { Spinner } from '@/renderer/components/ui/spinner';
+import { Input } from '@/renderer/components/ui/input';
+import { Textarea } from '@/renderer/components/ui/textarea';
+import { toast } from '@/renderer/components/ui/use-toast';
+import { useMutation } from '@/renderer/hooks/use-mutation';
+import { useAccount } from '@/renderer/contexts/account';
+import { Avatar } from '@/renderer/components/avatars/avatar';
+import { Icon } from '@/renderer/components/ui/icon';
+import { generateId, IdType } from '@/lib/id';
+import { cn } from '@/lib/utils';
+
+const formSchema = z.object({
+ name: z.string().min(3, 'Name must be at least 3 characters long.'),
+ description: z.string(),
+ avatar: z.string().optional(),
+});
+
+type formSchemaType = z.infer<typeof formSchema>;
+
+interface WorkspaceFormProps {
+ values?: formSchemaType;
+ onSubmit: (values: formSchemaType) => void;
+ isSaving: boolean;
+ onCancel?: () => void;
+ saveText: string;
+}
+
+export const WorkspaceForm = ({
+ values,
+ onSubmit,
+ isSaving,
+ onCancel,
+ saveText,
+}: WorkspaceFormProps) => {
+ const account = useAccount();
+
+ const id = React.useRef(generateId(IdType.Workspace));
+ const [isFileDialogOpen, setIsFileDialogOpen] = React.useState(false);
+ const { mutate, isPending } = useMutation();
+
+ const form = useForm<formSchemaType>({
+ resolver: zodResolver(formSchema),
+ defaultValues: {
+ name: values?.name ?? '',
+ description: values?.description ?? '',
+ avatar: values?.avatar,
+ },
+ });
+
+ const name = form.watch('name');
+ const avatar = form.watch('avatar');
+
+ return (
+ <Form {...form}>
+ <form className="flex flex-col" onSubmit={form.handleSubmit(onSubmit)}>
+ <div className="flex flex-row gap-1">
+ <div className="h-40 w-40 pt-3">
+ <div
+ className="group relative cursor-pointer"
+ onClick={async () => {
+ if (isPending || isFileDialogOpen) {
+ return;
+ }
+
+ setIsFileDialogOpen(true);
+ const result = await window.neuron.openFileDialog({
+ properties: ['openFile'],
+ filters: [
+ { name: 'Images', extensions: ['jpg', 'png', 'jpeg'] },
+ ],
+ });
+
+ if (result.canceled || !result.filePaths.length) {
+ setIsFileDialogOpen(false);
+ return;
+ }
+
+ const filePath = result.filePaths[0];
+ mutate({
+ input: {
+ type: 'avatar_upload',
+ accountId: account.id,
+ filePath: filePath,
+ },
+ onSuccess(output) {
+ if (output.status === 'success' && output.id) {
+ form.setValue('avatar', output.id);
+ }
+ setIsFileDialogOpen(false);
+ },
+ onError() {
+ toast({
+ title: 'Failed to upload avatar',
+ description:
+ 'Something went wrong trying to upload avatar. Please try again.',
+ variant: 'destructive',
+ });
+ setIsFileDialogOpen(false);
+ },
+ });
+ }}
+ >
+ <Avatar
+ id={id.current}
+ name={name.length > 0 ? name : 'New workspace'}
+ avatar={avatar}
+ className="h-32 w-32"
+ />
+ <div
+ className={cn(
+ `absolute left-0 top-0 hidden h-32 w-32 items-center justify-center overflow-hidden bg-gray-50 group-hover:inline-flex`,
+ isPending ? 'inline-flex' : 'hidden',
+ )}
+ >
+ {isPending ? (
+ <Spinner className="h-5 w-5" />
+ ) : (
+ <Icon
+ name="upload-2-line"
+ className="h-5 w-6 text-foreground"
+ />
+ )}
+ </div>
+ </div>
+ </div>
+ <div className="flex-grow space-y-4 py-2 pb-4">
+ <FormField
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItem className="flex-1">
+ <FormLabel>Name *</FormLabel>
+ <FormControl>
+ <Input placeholder="Name" {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="description"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Description</FormLabel>
+ <FormControl>
+ <Textarea
+ placeholder="Write a short description about the workspace"
+ {...field}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </div>
+ </div>
+
+ <div className="flex flex-row justify-end gap-2">
+ {onCancel && (
+ <Button
+ type="button"
+ disabled={isPending || isSaving}
+ variant="outline"
+ onClick={() => {
+ onCancel();
+ }}
+ >
+ Cancel
+ </Button>
+ )}
+
+ <Button type="submit" disabled={isPending || isSaving}>
+ {isSaving && <Spinner className="mr-1" />}
+ {saveText}
+ </Button>
+ </div>
+ </form>
+ </Form>
+ );
+};
diff --git a/desktop/src/renderer/components/workspaces/workspace-update.tsx b/desktop/src/renderer/components/workspaces/workspace-update.tsx
index 02db901c..106025bd 100644
--- a/desktop/src/renderer/components/workspaces/workspace-update.tsx
+++ b/desktop/src/renderer/components/workspaces/workspace-update.tsx
@@ -1,113 +1,49 @@
import React from 'react';
-import { z } from 'zod';
-import {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormMessage,
- FormLabel,
-} from '@/renderer/components/ui/form';
-import { useForm } from 'react-hook-form';
-import { zodResolver } from '@hookform/resolvers/zod';
-import { Button } from '@/renderer/components/ui/button';
-import { Spinner } from '@/renderer/components/ui/spinner';
-import { Input } from '@/renderer/components/ui/input';
-import { Textarea } from '@/renderer/components/ui/textarea';
import { toast } from '@/renderer/components/ui/use-toast';
import { useMutation } from '@/renderer/hooks/use-mutation';
import { useWorkspace } from '@/renderer/contexts/workspace';
-
-const formSchema = z.object({
- name: z.string().min(3, 'Name must be at least 3 characters long.'),
- description: z.string(),
-});
-
-type formSchemaType = z.infer<typeof formSchema>;
+import { WorkspaceForm } from './workspace-form';
export const WorkspaceUpdate = () => {
const workspace = useWorkspace();
const { mutate, isPending } = useMutation();
- const form = useForm<formSchemaType>({
- resolver: zodResolver(formSchema),
- defaultValues: {
- name: workspace.name,
- description: workspace.description,
- },
- });
-
- const handleSubmit = async (values: formSchemaType) => {
- mutate({
- input: {
- type: 'workspace_update',
- id: workspace.id,
- accountId: workspace.accountId,
- name: values.name,
- description: values.description,
- },
- onSuccess() {
- toast({
- title: 'Workspace updated',
- description: 'Workspace was updated successfully',
- variant: 'default',
- });
- },
- onError() {
- toast({
- title: 'Failed to update workspace',
- description:
- 'Something went wrong updating workspace. Please try again!',
- variant: 'destructive',
- });
- },
- });
- };
-
return (
- <Form {...form}>
- <form
- className="flex flex-col"
- onSubmit={form.handleSubmit(handleSubmit)}
- >
- <div className="flex-grow space-y-4 py-2 pb-4">
- <FormField
- control={form.control}
- name="name"
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>Name *</FormLabel>
- <FormControl>
- <Input placeholder="Name" {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="description"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Description</FormLabel>
- <FormControl>
- <Textarea
- placeholder="Write a short description about the workspace"
- {...field}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- </div>
- <div className="flex flex-row justify-end gap-2">
- <Button type="submit" disabled={isPending}>
- {isPending && <Spinner className="mr-1" />}
- Save
- </Button>
- </div>
- </form>
- </Form>
+ <WorkspaceForm
+ values={{
+ name: workspace.name,
+ description: workspace.description,
+ avatar: workspace.avatar,
+ }}
+ onSubmit={(values) => {
+ mutate({
+ input: {
+ type: 'workspace_update',
+ id: workspace.id,
+ accountId: workspace.accountId,
+ name: values.name,
+ description: values.description,
+ avatar: values.avatar,
+ },
+ onSuccess() {
+ toast({
+ title: 'Workspace updated',
+ description: 'Workspace was updated successfully',
+ variant: 'default',
+ });
+ },
+ onError() {
+ toast({
+ title: 'Failed to update workspace',
+ description:
+ 'Something went wrong updating workspace. Please try again!',
+ variant: 'destructive',
+ });
+ },
+ });
+ }}
+ isSaving={isPending}
+ saveText="Update"
+ />
);
};