Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add relations to notes/tasks list view #6971

Merged
merged 7 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -7,7 +7,6 @@ import { RecoilRoot, useSetRecoilState } from 'recoil';
import { useActivityTargetObjectRecords } from '@/activities/hooks/useActivityTargetObjectRecords';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { getObjectMetadataItemsMock } from '@/object-metadata/utils/getObjectMetadataItemsMock';
import { SnackBarProviderScope } from '@/ui/feedback/snack-bar-manager/scopes/SnackBarProviderScope';
import { JestObjectMetadataItemSetter } from '~/testing/jest/JestObjectMetadataItemSetter';
Expand Down Expand Up @@ -130,7 +129,6 @@ describe('useActivityTargetObjectRecords', () => {

const { activityTargetObjectRecords } = useActivityTargetObjectRecords(
task,
CoreObjectNameSingular.Task,
);

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useApolloClient } from '@apollo/client';
import { useRecoilValue } from 'recoil';
import { Nullable } from 'twenty-ui';

Expand All @@ -7,46 +6,40 @@ import { Note } from '@/activities/types/Note';
import { NoteTarget } from '@/activities/types/NoteTarget';
import { Task } from '@/activities/types/Task';
import { TaskTarget } from '@/activities/types/TaskTarget';
import { getJoinObjectNameSingular } from '@/activities/utils/getJoinObjectNameSingular';
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache';
import { isDefined } from '~/utils/isDefined';

export const useActivityTargetObjectRecords = (
activity: Task | Note,
objectNameSingular: CoreObjectNameSingular,
activity?: Task | Note,
activityTargets?: NoteTarget[] | TaskTarget[],
) => {
const objectMetadataItems = useRecoilValue(objectMetadataItemsState);

const activityTargets =
'noteTargets' in activity && activity.noteTargets
if(!isDefined(activity) && !isDefined(activityTargets)) {
throw new Error(
`No activity or activity targets`,
);
}

const targets = activityTargets ? activityTargets :
(activity && 'noteTargets' in activity && activity.noteTargets
? activity.noteTargets
: 'taskTargets' in activity && activity.taskTargets
: activity && 'taskTargets' in activity && activity.taskTargets
? activity.taskTargets
: [];

const getRecordFromCache = useGetRecordFromCache({
objectNameSingular: getJoinObjectNameSingular(objectNameSingular),
});
: []);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Simplify this logic using optional chaining and nullish coalescing

const apolloClient = useApolloClient();

const activityTargetObjectRecords = activityTargets
const activityTargetObjectRecords = targets
.map<Nullable<ActivityTargetWithTargetRecord>>((activityTarget) => {
const activityTargetFromCache = getRecordFromCache<
NoteTarget | TaskTarget
>(activityTarget.id, apolloClient.cache);

if (!isDefined(activityTargetFromCache)) {
if (!isDefined(activityTarget)) {
throw new Error(
`Cannot find activity target ${activityTarget.id} in cache, this shouldn't happen.`,
`Cannot find activity target`,
);
}

const correspondingObjectMetadataItem = objectMetadataItems.find(
(objectMetadataItem) =>
isDefined(activityTargetFromCache[objectMetadataItem.nameSingular]) &&
isDefined(activityTarget[objectMetadataItem.nameSingular]) &&
![CoreObjectNameSingular.Note, CoreObjectNameSingular.Task].includes(
objectMetadataItem.nameSingular as CoreObjectNameSingular,
),
Expand All @@ -57,7 +50,7 @@ export const useActivityTargetObjectRecords = (
}

const targetObjectRecord =
activityTargetFromCache[correspondingObjectMetadataItem.nameSingular];
activityTarget[correspondingObjectMetadataItem.nameSingular];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Ensure this bracket alignment is consistent with project style

if (!targetObjectRecord) {
throw new Error(
Expand All @@ -66,7 +59,7 @@ export const useActivityTargetObjectRecords = (
}

return {
activityTarget: activityTargetFromCache ?? activityTarget,
activityTarget,
targetObject: targetObjectRecord ?? undefined,
targetObjectMetadataItem: correspondingObjectMetadataItem,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const ActivityTargetsInlineCell = ({
}: ActivityTargetsInlineCellProps) => {
const { activityTargetObjectRecords } = useActivityTargetObjectRecords(
activity,
activityObjectNameSingular,
);

const { closeInlineCell } = useInlineCell();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ export const getLabelIdentifierFieldValue = (
return `${record.name?.firstName ?? ''} ${record.name?.lastName ?? ''}`;
}

if (objectNameSingular === CoreObjectNameSingular.NoteTarget) {
return record.note?.title ?? '';
}

if (objectNameSingular === CoreObjectNameSingular.TaskTarget) {
return record.task?.title ?? '';
}

if (isDefined(labelIdentifierFieldMetadataItem?.name)) {
return String(record[labelIdentifierFieldMetadataItem.name]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { useActivityTargetObjectRecords } from '@/activities/hooks/useActivityTargetObjectRecords';
import { NoteTarget } from '@/activities/types/NoteTarget';
import { TaskTarget } from '@/activities/types/TaskTarget';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { RecordChip } from '@/object-record/components/RecordChip';
import { useFieldFocus } from '@/object-record/record-field/hooks/useFieldFocus';
import { useRelationFromManyFieldDisplay } from '@/object-record/record-field/meta-types/hooks/useRelationFromManyFieldDisplay';
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { ExpandableList } from '@/ui/layout/expandable-list/components/ExpandableList';

export const RelationFromManyFieldDisplay = () => {
Expand All @@ -14,17 +19,41 @@ export const RelationFromManyFieldDisplay = () => {
return null;
}

return (
const { fieldName, objectMetadataNameSingular } = fieldDefinition.metadata;

const renderExpandableList = (records: ObjectRecord[], objectNameSingular: string, recordProp = '') => (
<ExpandableList isChipCountDisplayed={isFocused}>
{fieldValue.map((record) => {
return (
<RecordChip
key={record.id}
objectNameSingular={relationObjectNameSingular}
record={record}
/>
);
})}
{records.map((record) => (
<RecordChip
key={record.id}
objectNameSingular={objectNameSingular}
record={recordProp ? record[recordProp] : record}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Potential type error. record[recordProp] may not exist on ObjectRecord.

/>
))}
</ExpandableList>
);

if ((fieldName === 'noteTargets' && objectMetadataNameSingular !== CoreObjectNameSingular.Note) ||
(fieldName === 'taskTargets' && objectMetadataNameSingular !== CoreObjectNameSingular.Task)) {
const objectNameSingular = fieldName === 'noteTargets' ? CoreObjectNameSingular.Note : CoreObjectNameSingular.Task;
const recordProp = fieldName === 'noteTargets' ? 'note' : 'task';
return renderExpandableList(fieldValue, objectNameSingular, recordProp);
}

if ((fieldName === 'taskTargets' && objectMetadataNameSingular === CoreObjectNameSingular.Task) ||
(fieldName === 'noteTargets' && objectMetadataNameSingular === CoreObjectNameSingular.Note)) {
const { activityTargetObjectRecords } = useActivityTargetObjectRecords(undefined, fieldValue as NoteTarget[] | TaskTarget[]);
return <ExpandableList isChipCountDisplayed={isFocused}>
{activityTargetObjectRecords.map((record) => (
<RecordChip
key={record.targetObject.id}
objectNameSingular={record.targetObjectMetadataItem.nameSingular}
record={record.targetObject}
/>
))}
</ExpandableList>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Indentation is inconsistent with the rest of the file.


}

return renderExpandableList(fieldValue, relationObjectNameSingular);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useRecoilValue } from 'recoil';

import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { getObjectMetadataIdentifierFields } from '@/object-metadata/utils/getObjectMetadataIdentifierFields';
import { generateDepthOneRecordGqlFields } from '@/object-record/graphql/utils/generateDepthOneRecordGqlFields';
import { useRecordTableStates } from '@/object-record/record-table/hooks/internal/useRecordTableStates';
import { isDefined } from '~/utils/isDefined';

Expand All @@ -26,6 +29,14 @@ export const useRecordTableRecordGqlFields = ({
if (isDefined(imageIdentifierFieldMetadataItem)) {
identifierQueryFields[imageIdentifierFieldMetadataItem.name] = true;
}

const { objectMetadataItem: noteTargetObjectMetadataItem } = useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.NoteTarget,
});

const { objectMetadataItem: taskTargetObjectMetadataItem } = useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.TaskTarget,
});

const recordGqlFields: Record<string, any> = {
id: true,
Expand All @@ -34,18 +45,8 @@ export const useRecordTableRecordGqlFields = ({
),
...identifierQueryFields,
position: true,
noteTargets: {
note: {
id: true,
title: true,
},
},
taskTargets: {
task: {
id: true,
title: true,
},
},
noteTargets: generateDepthOneRecordGqlFields({ objectMetadataItem: noteTargetObjectMetadataItem }),
taskTargets: generateDepthOneRecordGqlFields({ objectMetadataItem: taskTargetObjectMetadataItem }),
};

return recordGqlFields;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const notesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.note].fields[
NOTE_STANDARD_FIELD_IDS.body
NOTE_STANDARD_FIELD_IDS.noteTargets
],
position: 1,
isVisible: true,
Expand All @@ -39,7 +39,7 @@ export const notesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.note].fields[
NOTE_STANDARD_FIELD_IDS.createdBy
NOTE_STANDARD_FIELD_IDS.body
],
position: 2,
isVisible: true,
Expand All @@ -48,12 +48,21 @@ export const notesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.note].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
NOTE_STANDARD_FIELD_IDS.createdBy
],
position: 3,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.note].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
],
position: 4,
isVisible: true,
size: 150,
},
/*
TODO: Add later, since we don't have real-time it probably doesn't work well?
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
TASK_STANDARD_FIELD_IDS.createdBy
TASK_STANDARD_FIELD_IDS.taskTargets
],
position: 3,
isVisible: true,
Expand All @@ -58,7 +58,7 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
TASK_STANDARD_FIELD_IDS.dueAt
TASK_STANDARD_FIELD_IDS.createdBy
],
position: 4,
isVisible: true,
Expand All @@ -67,7 +67,7 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
TASK_STANDARD_FIELD_IDS.assignee
TASK_STANDARD_FIELD_IDS.dueAt
],
position: 5,
isVisible: true,
Expand All @@ -76,7 +76,7 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
TASK_STANDARD_FIELD_IDS.body
TASK_STANDARD_FIELD_IDS.assignee
],
position: 6,
isVisible: true,
Expand All @@ -85,12 +85,21 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
TASK_STANDARD_FIELD_IDS.body
],
position: 7,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
],
position: 8,
isVisible: true,
size: 150,
},
/*
TODO: Add later, since we don't have real-time it probably doesn't work well?
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ export class NoteWorkspaceEntity extends BaseWorkspaceEntity {

@WorkspaceRelation({
standardId: NOTE_STANDARD_FIELD_IDS.noteTargets,
label: 'Targets',
label: 'Relations',
description: 'Note targets',
icon: 'IconCheckbox',
icon: 'IconArrowUpRight',
type: RelationMetadataType.ONE_TO_MANY,
inverseSideTarget: () => NoteTargetWorkspaceEntity,
onDelete: RelationOnDeleteAction.SET_NULL,
})
@WorkspaceIsNullable()
@WorkspaceIsSystem()
noteTargets: Relation<NoteTargetWorkspaceEntity[]>;

@WorkspaceRelation({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,14 @@ export class TaskWorkspaceEntity extends BaseWorkspaceEntity {

@WorkspaceRelation({
standardId: TASK_STANDARD_FIELD_IDS.taskTargets,
label: 'Targets',
label: 'Relations',
description: 'Task targets',
icon: 'IconCheckbox',
icon: 'IconArrowUpRight',
type: RelationMetadataType.ONE_TO_MANY,
inverseSideTarget: () => TaskTargetWorkspaceEntity,
onDelete: RelationOnDeleteAction.SET_NULL,
})
@WorkspaceIsNullable()
@WorkspaceIsSystem()
taskTargets: Relation<TaskTargetWorkspaceEntity[]>;

@WorkspaceRelation({
Expand Down
Loading