Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useTranslation } from 'next-i18next';
import { Fragment, useMemo } from 'react';
import { tableConfig } from '@/features/i18n/table.config';
import { useContextMenu } from '../../hooks/useContextMenu';
import { useExpandRecord } from '../../hooks/useExpandRecord';
import { useGallery } from '../hooks';
import { CARD_COVER_HEIGHT, CARD_STYLE } from '../utils';
import { CardCarousel } from './CardCarousel';
Expand All @@ -33,16 +34,10 @@ export const Card = (props: IKanbanCardProps) => {
const viewId = useViewId();
const getFieldStatic = useFieldStaticGetter();
const { t } = useTranslation(tableConfig.i18nNamespaces);
const {
coverField,
primaryField,
displayFields,
permission,
isCoverFit,
isFieldNameHidden,
setExpandRecordId,
} = useGallery();
const { coverField, primaryField, displayFields, permission, isCoverFit, isFieldNameHidden } =
useGallery();
const { copyRecordUrl, viewRecordHistory, addRecordComment } = useContextMenu();
const expandRecord = useExpandRecord();

const { cardCreatable, cardDeletable, cardEditable, cardCommentCreatable } = permission;
const coverFieldId = coverField?.id;
Expand All @@ -59,8 +54,8 @@ export const Card = (props: IKanbanCardProps) => {
);
}, [card, primaryField, t]);

const onExpand = () => {
setExpandRecordId(card.id);
const onExpand = async () => {
await expandRecord(card.id);
};

const onDelete = () => {
Expand All @@ -87,7 +82,7 @@ export const Card = (props: IKanbanCardProps) => {
const record = res.data.records[0];

if (record != null) {
setExpandRecordId(record.id);
await expandRecord(record.id);
}
};

Expand All @@ -96,12 +91,10 @@ export const Card = (props: IKanbanCardProps) => {
};

const onViewRecordHistory = async () => {
setExpandRecordId(card.id);
await viewRecordHistory(card.id);
};

const onAddRecordComment = async () => {
setExpandRecordId(card.id);
await addRecordComment(card.id);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IGetRecordsRo } from '@teable/openapi';
import type { AttachmentField, IFieldInstance } from '@teable/sdk/model';
import type { Dispatch, SetStateAction } from 'react';
import { createContext } from 'react';
import type { IGalleryPermission } from '../type';

Expand All @@ -12,7 +11,6 @@ export interface IGalleryContext {
permission: IGalleryPermission;
primaryField: IFieldInstance;
displayFields: IFieldInstance[];
setExpandRecordId: Dispatch<SetStateAction<string | undefined>>;
}

export const GalleryContext = createContext<IGalleryContext>(null!);
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export const GalleryProvider = ({ children }: { children: ReactNode }) => {
coverField,
primaryField,
displayFields,
setExpandRecordId,
};
}, [
recordQuery,
Expand All @@ -115,7 +114,6 @@ export const GalleryProvider = ({ children }: { children: ReactNode }) => {
coverField,
primaryField,
displayFields,
setExpandRecordId,
]);

const onClose = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { renderHook } from '@testing-library/react';
import { useExpandRecord } from './useExpandRecord';

const push = vi.fn();

vi.mock('next/router', () => ({
useRouter: () => ({
pathname: '/base/[baseId]/[tableId]/[viewId]',
query: { baseId: 'bse1', tableId: 'tbl1', viewId: 'viw1' },
push,
}),
}));

describe('useExpandRecord', () => {
beforeEach(() => push.mockReset());

it('puts the record id in the url so the expanded record stays linkable', async () => {
const { result } = renderHook(() => useExpandRecord());

await result.current('rec1');

expect(push).toHaveBeenCalledWith(
{
pathname: '/base/[baseId]/[tableId]/[viewId]',
query: { baseId: 'bse1', tableId: 'tbl1', viewId: 'viw1', recordId: 'rec1' },
},
undefined,
{ shallow: true }
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useRouter } from 'next/router';
import { useCallback } from 'react';

// expanding a record must go through the url, otherwise the expanded record is
// not linkable (copy record url would return the bare view url)
export const useExpandRecord = () => {
const router = useRouter();

return useCallback(
async (recordId: string) => {
await router.push(
{
pathname: router.pathname,
query: { ...router.query, recordId },
},
undefined,
{
shallow: true,
}
);
},
[router]
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useMemo } from 'react';
import { tableConfig } from '@/features/i18n/table.config';
import { CardCarousel } from '../../gallery/components';
import { useContextMenu } from '../../hooks/useContextMenu';
import { useExpandRecord } from '../../hooks/useExpandRecord';
import type { IKanbanContext } from '../context';
import { useKanban } from '../hooks';
import type { IStackData } from '../type';
Expand All @@ -47,9 +48,9 @@ export const KanbanCard = (props: IKanbanCardProps) => {
coverField,
isCoverFit,
isFieldNameHidden,
setExpandRecordId,
} = useKanban() as Required<IKanbanContext>;
const { copyRecordUrl, viewRecordHistory, addRecordComment } = useContextMenu();
const expandRecord = useExpandRecord();

const { cardCreatable, cardDeletable, cardEditable, cardCommentCreatable } = permission;
const { id: fieldId } = stackField;
Expand All @@ -67,8 +68,8 @@ export const KanbanCard = (props: IKanbanCardProps) => {
);
}, [card, primaryField, t]);

const onExpand = () => {
setExpandRecordId(card.id);
const onExpand = async () => {
await expandRecord(card.id);
};

const onDelete = () => {
Expand Down Expand Up @@ -100,7 +101,7 @@ export const KanbanCard = (props: IKanbanCardProps) => {
const record = res.data.records[0];

if (record != null) {
setExpandRecordId(record.id);
await expandRecord(record.id);
}
};

Expand All @@ -109,12 +110,10 @@ export const KanbanCard = (props: IKanbanCardProps) => {
};

const onViewRecordHistory = async () => {
setExpandRecordId(card.id);
await viewRecordHistory(card.id);
};

const onAddRecordComment = async () => {
setExpandRecordId(card.id);
await addRecordComment(card.id);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IGetRecordsRo } from '@teable/openapi';
import type { AttachmentField, IFieldInstance } from '@teable/sdk/model';
import type { Dispatch, SetStateAction } from 'react';
import { createContext } from 'react';
import type { IKanbanPermission, IStackData } from '../type';

Expand All @@ -15,7 +14,6 @@ export interface IKanbanContext {
permission: IKanbanPermission;
primaryField: IFieldInstance;
displayFields: IFieldInstance[];
setExpandRecordId: Dispatch<SetStateAction<string | undefined>>;
}

export const KanbanContext = createContext<IKanbanContext>(null!);
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ export const KanbanProvider = ({ children }: { children: ReactNode }) => {
primaryField,
displayFields,
stackCollection,
setExpandRecordId,
};
}, [
recordQuery,
Expand All @@ -283,7 +282,6 @@ export const KanbanProvider = ({ children }: { children: ReactNode }) => {
primaryField,
displayFields,
stackCollection,
setExpandRecordId,
]);

const onClose = () => {
Expand Down