Skip to content

Commit

Permalink
proj: add uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Mar 3, 2025
1 parent cdc3bb4 commit 53ad764
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/components/AddChannelModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function create() {
// Create channel
await wksp.chat.newChannel({
id: 0, // auto
uuid: String(), // auto
name: name.value,
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/AddProjectModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async function create() {
if (!wksp) return;
// Check if project already exists
const projs = await wksp.proj.getProjects();
const projs = wksp.proj.getProjects();
if (projs.some((c) => c.name === name.value)) {
Toast.error('Project with this name already exists');
return;
Expand Down
8 changes: 4 additions & 4 deletions src/components/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
<template v-if="routeIsWorkspace">
<p class="menu-label">Projects</p>
<ul class="menu-list">
<li v-for="proj in projects" :key="proj.name">
<li v-for="proj in projects" :key="proj.uuid">
<router-link :to="linkProject(proj)">
<div class="link-inner">
<FontAwesomeIcon class="mr-1" :icon="faLayerGroup" size="sm" />
{{ proj.name }}
</div>

<ProjectTreeMenu
v-if="activeProjectName === proj.name"
v-if="activeProjectName === proj.uuid"
class="link-button"
:allow-new="true"
:allow-delete="false"
Expand All @@ -37,7 +37,7 @@
</router-link>

<ProjectTree
v-if="activeProjectName == proj.name"
v-if="activeProjectName == proj.uuid"
class="outermost"
ref="projectTree"
:project="proj"
Expand All @@ -55,7 +55,7 @@

<p class="menu-label">Discussion</p>
<ul class="menu-list">
<li v-for="chan in channels" :key="chan.id">
<li v-for="chan in channels" :key="chan.uuid">
<router-link :to="linkDiscuss(chan)">
<FontAwesomeIcon class="mr-1" :icon="faHashtag" size="sm" />
{{ chan.name }}
Expand Down
4 changes: 2 additions & 2 deletions src/services/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export const GlobalBus = new EventEmitter() as TypedEmitter<{

/**
* Event when the list of files in the active project is updated.
* @param project Name of the project that is active
* @param uuid UUID of the project that is active
* @param files List of files in the project
*/
'project-files': (project: string, files: IProjectFile[]) => void;
'project-files': (uuid: string, files: IProjectFile[]) => void;

/**
* Various informational errors may be received here.
Expand Down
6 changes: 4 additions & 2 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type IWkspStats = {

export type IChatMessage = {
/** Unique ID for each message */
uuid: number;
uuid: string;
/** User who sent the message */
user: string;
/** Timestamp when the message was sent */
Expand All @@ -27,12 +27,14 @@ export type IChatMessage = {

export type IChatChannel = {
/** Channel ID */
id: number;
uuid: string;
/** Channel name */
name: string;
};

export type IProject = {
/** Project ID */
uuid: string;
/** Project name */
name: string;
};
Expand Down
9 changes: 5 additions & 4 deletions src/services/workspace-chat.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { EventEmitter } from 'events';
import * as Y from 'yjs';
import { nanoid } from 'nanoid';

import type { IChatChannel, IChatMessage } from '@/services/types';
import { GlobalBus } from '@/services/event-bus';
import { SvsProvider } from '@/services/svs-provider';

import type { IChatChannel, IChatMessage } from '@/services/types';
import type TypedEmitter from 'typed-emitter';
import type { WorkspaceAPI } from './ndn';

Expand Down Expand Up @@ -81,13 +82,13 @@ export class WorkspaceChat {
throw new Error('Channel already exists');
}

channel.id ||= Math.random() * 1e16;
channel.uuid = nanoid();
this.chatChannels.push([channel]);
this.chatMessages.set(channel.name, new Y.Array<IChatMessage>());

// Push initial message
await this.sendMessage(channel.name, {
uuid: 0, // auto
uuid: String(), // auto
user: 'ownly-bot',
ts: Date.now(),
message: `#${channel.name} was created by ${this.api.name}`,
Expand Down Expand Up @@ -122,7 +123,7 @@ export class WorkspaceChat {
* @param message Chat message
*/
public async sendMessage(channel: string, message: IChatMessage) {
message.uuid ||= Math.random() * 1e16;
message.uuid = nanoid();
(await this.getMsgArray(channel)).push([message]);
}
}
27 changes: 14 additions & 13 deletions src/services/workspace-proj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class WorkspaceProjManager {
) {
this.list = this.root.getMap<IProject>('list');

const listObserver = async () => GlobalBus.emit('project-list', await this.getProjects());
const listObserver = async () => GlobalBus.emit('project-list', this.getProjects());
this.list.observe(listObserver);
listObserver();
}
Expand Down Expand Up @@ -55,28 +55,29 @@ export class WorkspaceProjManager {
}

/** Get the list of projects */
public async getProjects(): Promise<IProject[]> {
public getProjects(): IProject[] {
return Array.from(this.list.values());
}

/** Create a new project */
public async newProject(name: string) {
if (!name) throw new Error('Project name is required');
if (this.list.has(name)) throw new Error('Project already exists');
this.list.set(name, { name });
const uuid = nanoid();
this.list.set(uuid, { uuid, name });
}

/** Get a project instance */
public async get(name: string): Promise<WorkspaceProj> {
let proj = this.instances.get(name);
if (proj) return proj;
const pid = this.getProjects().find((p) => p.name === name)?.uuid;
if (!pid) throw new Error('Project not found');

// Get metadata from project list
if (!this.list.has(name)) throw new Error('Project not found');
let proj = this.instances.get(pid);
if (proj) return proj;

// Create project instance
proj = await WorkspaceProj.create(name, this.wksp, this);
this.instances.set(name, proj);
proj = await WorkspaceProj.create(pid, this.wksp, this);
this.instances.set(pid, proj);
return proj;
}
}
Expand All @@ -102,23 +103,23 @@ export class WorkspaceProj {
/**
* Create a new project instance
*
* @param name Project name (slug)
* @param uuid Project uuid (slug)
* @param wksp Workspace API
* @param manager Project manager instance
*/
public static async create(
name: string,
uuid: string,
wksp: WorkspaceAPI,
manager: WorkspaceProjManager,
): Promise<WorkspaceProj> {
// Start SVS for project
const provider = await SvsProvider.create(wksp, name);
const provider = await SvsProvider.create(wksp, uuid);

// Create root document
const root = await provider.getDoc('root');

// Create project object
return new WorkspaceProj(name, root, provider, manager);
return new WorkspaceProj(uuid, root, provider, manager);
}

/** Destroy the project instance */
Expand Down
2 changes: 1 addition & 1 deletion src/views/SpaceDiscussView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ async function send(event: Event) {
// Send the message to the workspace
const message = {
uuid: 0, // auto
uuid: String(), // auto
user: wksp.value!.username,
ts: Date.now(),
message: outMessage.value,
Expand Down
2 changes: 1 addition & 1 deletion src/views/SpaceHomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function setup() {
for (const name of ['general', 'random']) {
try {
await wksp.value.chat.newChannel({
id: 0, // auto
uuid: String(), // auto
name: name,
});
} catch (err) {
Expand Down

0 comments on commit 53ad764

Please sign in to comment.