Skip to content

Commit

Permalink
Merge pull request #118 from powerhouse-inc/76-implement-switchboard-…
Browse files Browse the repository at this point in the history
…link-into-rwa-editor

feat: enabled switchboard link in RWA editor
  • Loading branch information
gpuente authored Feb 21, 2024
2 parents 7d7206c + 05dfd07 commit a498058
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@typescript-eslint/parser": "^6.18.1",
"@vitejs/plugin-react": "^3.1.0",
"autoprefixer": "^10.4.14",
"document-model-libs": "^1.1.51",
"document-model-libs": "^1.1.52",
"electron": "28.2.3",
"eslint": "^8.56.0",
"eslint-config-prettier": "^8.8.0",
Expand Down
3 changes: 3 additions & 0 deletions src/components/editors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface IProps extends EditorProps {
onClose: () => void;
onExport: () => void;
onAddOperation: (operation: Operation) => void;
onOpenSwitchboardLink?: () => Promise<void>;
}

export const DocumentEditor: React.FC<IProps> = ({
Expand All @@ -41,6 +42,7 @@ export const DocumentEditor: React.FC<IProps> = ({
onClose,
onExport,
onAddOperation,
onOpenSwitchboardLink,
}) => {
const documentModel = useDocumentModel(initialDocument.documentType);
const editor = useEditor(initialDocument.documentType);
Expand Down Expand Up @@ -124,6 +126,7 @@ export const DocumentEditor: React.FC<IProps> = ({
dispatch={dispatch}
onClose={onClose}
onExport={onExport}
onSwitchboardLinkClick={onOpenSwitchboardLink}
/>
</div>
);
Expand Down
20 changes: 4 additions & 16 deletions src/components/file-item/file-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { useDocumentDriveById } from 'src/hooks/useDocumentDriveById';
import { useDrivesContainer } from 'src/hooks/useDrivesContainer';
import { useGetDocumentById } from 'src/hooks/useGetDocumentById';
import { useGetReadableItemPath } from 'src/hooks/useGetReadableItemPath';
import { getSwitchboardUrl } from 'src/utils/getSwitchboardUrl';
import { openUrl } from 'src/utils/openUrl';
import { useOpenSwitchboardLink } from 'src/hooks/useOpenSwitchboardLink';

const allowedItemOptions = ['delete', 'rename'];

Expand Down Expand Up @@ -42,7 +41,8 @@ export const FileItem: React.FC<IProps> = ({
const { updateNodeName } = useDrivesContainer();

const decodedDriveID = decodeID(drive);
const { isRemoteDrive, remoteUrl } = useDocumentDriveById(decodedDriveID);
const openSwitchboardLink = useOpenSwitchboardLink(decodedDriveID);
const { isRemoteDrive } = useDocumentDriveById(decodedDriveID);

const onFileOptionsClick = async (optionId: string, fileNode: TreeItem) => {
if (optionId === 'delete') {
Expand All @@ -54,23 +54,11 @@ export const FileItem: React.FC<IProps> = ({
}

if (optionId === 'switchboard-link') {
if (!remoteUrl) return;
const url = new URL(remoteUrl);
const baseUrl = url.origin;
const document = getDocumentById(decodedDriveID, fileNode.id) as
| FileNode
| undefined;

if (document) {
const url = getSwitchboardUrl(
baseUrl,
decodedDriveID,
document.documentType,
document.id,
);

await openUrl(url);
}
await openSwitchboardLink(document);
}
};

Expand Down
27 changes: 27 additions & 0 deletions src/hooks/useOpenSwitchboardLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FileNode } from 'document-model-libs/document-drive';
import { getSwitchboardUrl } from 'src/utils/getSwitchboardUrl';
import { openUrl } from 'src/utils/openUrl';
import { useDocumentDriveById } from './useDocumentDriveById';

export const useOpenSwitchboardLink = (driveId: string) => {
const { isRemoteDrive, remoteUrl } = useDocumentDriveById(driveId);

return async (document?: FileNode) => {
if (!remoteUrl) return;
if (!isRemoteDrive) return;

const url = new URL(remoteUrl);
const baseUrl = url.origin;

if (document) {
const url = getSwitchboardUrl(
baseUrl,
driveId,
document.documentType,
document.id,
);

await openUrl(url);
}
};
};
29 changes: 28 additions & 1 deletion src/pages/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useItemActions,
useItemsContext,
} from '@powerhousedao/design-system';
import { FileNode } from 'document-model-libs/document-drive';
import { Document, DocumentModel, Operation } from 'document-model/document';
import path from 'path';
import { useEffect, useState } from 'react';
Expand All @@ -16,8 +17,11 @@ import { DocumentEditor } from 'src/components/editors';
import FolderView from 'src/components/folder-view';
import { useModal } from 'src/components/modal';
import { SearchBar } from 'src/components/search-bar';
import { useDocumentDriveById } from 'src/hooks/useDocumentDriveById';
import { useDocumentDriveServer } from 'src/hooks/useDocumentDriveServer';
import { useDrivesContainer } from 'src/hooks/useDrivesContainer';
import { useGetDocumentById } from 'src/hooks/useGetDocumentById';
import { useOpenSwitchboardLink } from 'src/hooks/useOpenSwitchboardLink';
import { preloadTabs, useFileNodeDocument, useSelectedPath } from 'src/store';
import {
useFilteredDocumentModels,
Expand All @@ -26,6 +30,14 @@ import {
import { exportFile } from 'src/utils';
import { v4 as uuid } from 'uuid';

const getDocumentModelName = (name: string) => {
if (name === 'RealWorldAssets') {
return 'RWA Portfolio';
}

return name;
};

const Content = () => {
const { items } = useItemsContext();
const [selectedPath, setSelectedPath] = useSelectedPath();
Expand All @@ -37,6 +49,9 @@ const Content = () => {
const driveID = getRootPath(selectedFolder?.path ?? '');
const decodedDriveID = decodeID(driveID);
const { showModal } = useModal();
const getDocumentById = useGetDocumentById();
const { isRemoteDrive } = useDocumentDriveById(decodedDriveID);
const openSwitchboardLink = useOpenSwitchboardLink(decodedDriveID);

const { addFile, deleteNode, documentDrives, renameNode } =
useDocumentDriveServer();
Expand Down Expand Up @@ -149,6 +164,15 @@ const Content = () => {
}
};

const onOpenSwitchboardLink = async () => {
const doc = getDocumentById(
decodedDriveID,
selectedFileNode?.id || '',
) as FileNode | undefined;

await openSwitchboardLink(doc);
};

return (
<div className="flex h-full flex-col overflow-auto bg-gray-100 p-6">
{selectedFileNode && selectedDocument ? (
Expand All @@ -159,6 +183,7 @@ const Content = () => {
onChange={onDocumentChangeHandler}
onExport={() => exportDocument(selectedDocument)}
onAddOperation={handleAddOperation}
{...(isRemoteDrive && { onOpenSwitchboardLink })}
/>
</div>
) : (
Expand Down Expand Up @@ -205,7 +230,9 @@ const Content = () => {
onClick={() => createDocument(doc)}
>
<span className="text-sm">
{doc.documentModel.name}
{getDocumentModelName(
doc.documentModel.name,
)}
</span>
</Button>
))}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5280,10 +5280,10 @@ document-drive@^1.0.0-alpha.9:
nanoevents "^9.0.0"
sanitize-filename "^1.6.3"

document-model-libs@^1.1.51:
version "1.1.51"
resolved "https://registry.yarnpkg.com/document-model-libs/-/document-model-libs-1.1.51.tgz#7b7869fa8d6bb08aa83469189b339cadd06c30f7"
integrity sha512-58SYxLfSklaImunLD/kcfXLpKrlNz1yIbIHYtBFto2SQmAhEoBpr8d1bMDbqX03Q7HrPHzRf0qnUfwlS5y22wA==
document-model-libs@^1.1.52:
version "1.1.52"
resolved "https://registry.yarnpkg.com/document-model-libs/-/document-model-libs-1.1.52.tgz#85859770fae18ef56b4287a7eebcc488415217e2"
integrity sha512-cZmFhk6tWY3NABqFQFtto6Woz3EIsJmA+voqNEhyuxAIsLRKzn2seLLPxy0DFUVttntumEsEfCgR8c92aOXK+g==
dependencies:
"@acaldas/graphql-codegen-typescript-validation-schema" "^0.12.3"
"@graphql-codegen/core" "^4.0.2"
Expand Down

0 comments on commit a498058

Please sign in to comment.