TagSpaces is an offline, open source, document manager with tagging support
chore: consolidating generation of sharing links tslinks
Ilian Sapundshiev078990249d07
diff --git a/src/renderer/components/dialogs/FilePickerDialog.tsx b/src/renderer/components/dialogs/FilePickerDialog.tsxindex ae3b6228d..597918ff3 100644--- a/src/renderer/components/dialogs/FilePickerDialog.tsx+++ b/src/renderer/components/dialogs/FilePickerDialog.tsx@@ -27,6 +27,7 @@ import TsDialogActions from '-/components/dialogs/components/TsDialogActions';import TsDialogTitle from '-/components/dialogs/components/TsDialogTitle';import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';+import { useIOActionsContext } from '-/hooks/useIOActionsContext';import {actions as SettingsActions,getLastLinkType,@@ -109,6 +110,7 @@ function FilePickerDialog(props: FilePickerDialogProps) {const { locations, findLocation } = useCurrentLocationContext();const { currentDirectoryPath } = useDirectoryContentContext();+ const { getMetadataID } = useIOActionsContext();const dispatch = useDispatch();const lastLinkType = useSelector(getLastLinkType);const theme = useTheme();@@ -261,7 +263,7 @@ function FilePickerDialog(props: FilePickerDialogProps) {const primaryDisabled =!selectedEntry || !modeAccepts(selectedEntry, mode) || !targetLocation;- function handleConfirm() {+ async function handleConfirm() {if (!selectedEntry || !targetLocation) return;let link: string;if (linkType === 'relative' && canUseRelative && sourceDir) {@@ -270,9 +272,19 @@ function FilePickerDialog(props: FilePickerDialogProps) {targetLocation,sourceDir,);- link = rel ?? buildSharingLinkForEntry(selectedEntry, targetLocation);+ link =+ rel ??+ (await buildSharingLinkForEntry(+ selectedEntry,+ targetLocation,+ getMetadataID,+ ));} else {- link = buildSharingLinkForEntry(selectedEntry, targetLocation);+ link = await buildSharingLinkForEntry(+ selectedEntry,+ targetLocation,+ getMetadataID,+ );}const finalLabel =(linkLabel && linkLabel.trim()) || selectedEntry.name || '';diff --git a/src/renderer/components/menus/DirectoryMenu.tsx b/src/renderer/components/menus/DirectoryMenu.tsxindex bf4820d3f..f32a1d877 100644--- a/src/renderer/components/menus/DirectoryMenu.tsx+++ b/src/renderer/components/menus/DirectoryMenu.tsx@@ -25,10 +25,10 @@ import {extractDirectoryName,getThumbFileLocationForDirectory,normalizePath,- generateSharingLink,} from '@tagspaces/tagspaces-common/paths';import { Pro } from '-/pro';import {+ buildSharingLinkForEntry,createNewInstance,getRelativeEntryPath,openDirectoryMessage,@@ -135,33 +135,28 @@ function DirectoryMenu(props: Props) {const directoryPath = props.directoryPath || currentDirectoryPath;function generateFolderLink(): Promise<any> {- let locationID = undefined;- let entryPath = currentDirectoryPath;- if (selectedEntries && selectedEntries.length > 0) {- if (selectedEntries[0]['locationID']) {- locationID = selectedEntries[0]['locationID'];- }- entryPath = selectedEntries[0].path;- }- const tmpLoc = findLocation(locationID);- return getLocationPath(tmpLoc).then((locationPath) => {- const relativePath = getRelativeEntryPath(locationPath, entryPath);- const folderName = extractDirectoryName(- selectedEntries[0] ? selectedEntries[0].name : currentDirectoryPath,- currentLocation?.getDirSeparator(),- );- return getMetadataID(- entryPath,- selectedEntries[0]?.uuid,- tmpLoc,- false,- ).then((id) => {- return {- url: generateSharingLink(locationID, undefined, relativePath, id),+ const entry = selectedEntries?.[0];+ const entryPath = entry?.path || currentDirectoryPath;+ const tmpLoc = findLocation(entry?.['locationID']);+ const folderName = extractDirectoryName(+ entry ? entry.name : currentDirectoryPath,+ currentLocation?.getDirSeparator(),+ );+ const folderEntry: TS.FileSystemEntry = entry+ ? ({ ...entry, isFile: false } as TS.FileSystemEntry)+ : ({+ uuid: '',name: folderName,- };- });- });+ isFile: false,+ path: entryPath,+ extension: '',+ tags: [],+ size: 0,+ lmdt: 0,+ } as TS.FileSystemEntry);+ return buildSharingLinkForEntry(folderEntry, tmpLoc, getMetadataID).then(+ (url) => ({ url, name: folderName }),+ );}function copyRelativePath() {diff --git a/src/renderer/components/menus/FileMenu.tsx b/src/renderer/components/menus/FileMenu.tsxindex 872a510db..652021b95 100644--- a/src/renderer/components/menus/FileMenu.tsx+++ b/src/renderer/components/menus/FileMenu.tsx@@ -57,6 +57,7 @@ import {} from '-/reducers/settings';import { supportedImgs } from '-/services/thumbsgenerator';import {+ buildSharingLinkForEntry,createNewInstance,findCandidateExtensionsForFile,getRelativeEntryPath,@@ -72,7 +73,6 @@ import {extractContainingDirectoryPath,extractParentDirectoryPath,extractTitle,- generateSharingLink,} from '@tagspaces/tagspaces-common/paths';import { useEffect, useReducer, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';@@ -139,15 +139,10 @@ function FileMenu(props: Props) {}, [currentLocationId, lastSelectedEntry]);function generateFileLink(): Promise<string> {- const entryPath = selectedEntries[0].path;- const relativePath = getRelativeEntryPath(currentLocationPath, entryPath);- return getMetadataID(- selectedEntries[0].path,- selectedEntries[0].uuid,+ return buildSharingLinkForEntry(+ selectedEntries[0],currentLocation,- selectedEntries[0].isFile,- ).then((id) =>- generateSharingLink(currentLocationId, relativePath, undefined, id),+ getMetadataID,);}diff --git a/src/renderer/services/utils-io.ts b/src/renderer/services/utils-io.tsindex 56dcccccc..ed9da1312 100644--- a/src/renderer/services/utils-io.ts+++ b/src/renderer/services/utils-io.ts@@ -728,25 +728,40 @@ export function getRelativeEntryPath(/*** Build a TagSpaces `ts://?...` sharing link for the given entry inside its- * location. Mirrors the canonical recipe used by LinksTab.tsx and- * HistoryContextProvider.tsx so all `ts://` links across the renderer have an- * identical shape.+ * location. Canonical recipe — all callers that produce a shareable link+ * (FileMenu, DirectoryMenu, FilePickerDialog) should go through this.+ *+ * Files go in the `tsepath` slot, folders in the `tsdpath` slot.+ *+ * `getMetadataID` is required: it produces a stable sidecar UUID that+ * survives renames (the runtime `entry.uuid` does not). Pass the function+ * from `useIOActionsContext()`.*/export function buildSharingLinkForEntry(entry: TS.FileSystemEntry,location: CommonLocation,-): string {+ getMetadataID: (+ path: string,+ id: string,+ location: CommonLocation,+ isFile: boolean,+ ) => Promise<string>,+): Promise<string> {const sep = location.getDirSeparator?.() || '/';const relativeEntryPath = cleanRootPath(entry.path || '',location.path || '',sep,);- return generateSharingLink(- location.uuid,- relativeEntryPath,- undefined,- entry.uuid,+ return getMetadataID(+ entry.path,+ entry.uuid || '',+ location,+ entry.isFile,+ ).then((id) =>+ entry.isFile+ ? generateSharingLink(location.uuid, relativeEntryPath, undefined, id)+ : generateSharingLink(location.uuid, undefined, relativeEntryPath, id),);}