fix(core): add toDocSearchParams for better typeschecking doc search params (#11383)

This commit is contained in:
pengx17
2025-04-02 04:46:22 +00:00
parent 1b5df6b75e
commit 08eb248cb4
10 changed files with 41 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
import { toURLSearchParams } from '@affine/core/modules/navigation';
import { toDocSearchParams } from '@affine/core/modules/navigation';
import type { IndexerSyncState } from '@affine/nbstore';
import type { ReferenceParams } from '@blocksuite/affine/model';
import { fromPromise, LiveData, Service } from '@toeverything/infra';
@@ -193,7 +193,7 @@ export class DocsSearchService extends Service {
docId: doc.id,
params: isEmpty(params)
? undefined
: toURLSearchParams(params),
: toDocSearchParams(params),
};
})
.filter(ref => !!ref);

View File

@@ -2,6 +2,7 @@ export { Navigator } from './entities/navigator';
export {
resolveLinkToDoc,
resolveRouteLinkMeta,
toDocSearchParams,
toURLSearchParams,
} from './utils';
export { NavigationButtons } from './view/navigation-buttons';

View File

@@ -189,3 +189,12 @@ export function toURLSearchParams(
.map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v])
);
}
// a type safe version of toURLSearchParams for doc search params
export function toDocSearchParams(
params?: ReferenceParams & {
refreshKey?: string;
}
) {
return toURLSearchParams(params);
}

View File

@@ -26,7 +26,7 @@ import {
import { ServerService } from '../../cloud';
import { WorkspaceDialogService } from '../../dialogs';
import { DocsService } from '../../doc/services/docs';
import { toURLSearchParams } from '../../navigation';
import { toDocSearchParams } from '../../navigation';
import { WorkbenchService } from '../../workbench';
import type {
AttachmentPeekViewInfo,
@@ -153,7 +153,7 @@ export const DocPeekViewControls = ({
name: t['com.affine.peek-view-controls.copy-link'](),
onClick: async () => {
const preferredMode = docsService.list.getPrimaryMode(docRef.docId);
const search = toURLSearchParams({
const search = toDocSearchParams({
mode: docRef.mode || preferredMode,
blockIds: docRef.blockIds,
elementIds: docRef.elementIds,

View File

@@ -1,4 +1,4 @@
import { toURLSearchParams } from '@affine/core/modules/navigation/utils';
import { toDocSearchParams } from '@affine/core/modules/navigation/utils';
import { Unreachable } from '@affine/env/constant';
import type { ReferenceParams } from '@blocksuite/affine/model';
import { Entity, LiveData } from '@toeverything/infra';
@@ -156,10 +156,11 @@ export class Workbench extends Entity {
openDoc(
id:
| string
| ({ docId: string } & (
| ReferenceParams
| Record<string, string | undefined>
)),
| ({
docId: string;
refreshKey?: string;
fromTab?: string;
} & ReferenceParams),
options?: WorkbenchOpenOptions
) {
const isString = typeof id === 'string';
@@ -167,7 +168,7 @@ export class Workbench extends Entity {
let query = '';
if (!isString) {
const search = toURLSearchParams(omit(id, ['docId']));
const search = toDocSearchParams(omit(id, ['docId']));
if (search?.size) {
query = `?${search.toString()}`;
}