Skip to content

Commit

Permalink
Fix attachment issue
Browse files Browse the repository at this point in the history
  • Loading branch information
barshathakuri authored and AdityaKhatri committed Aug 30, 2024
1 parent 4b7f1af commit 21d9c13
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
27 changes: 6 additions & 21 deletions app/components/LeftPaneEntries/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ interface Props {
entries: EntryInput[] | undefined | null;
activeEntry?: string;
onEntryClick?: (entryId: string) => void;
onAttachmentClick: (attachment: LeadPreviewAttachmentType) => void;
onExcerptChange?: (entryClientId: string, newExcerpt: string | undefined) => void;
onEntryDelete?: (entryClientId: string) => void;
onEntryRestore?: (entryClientId: string) => void;
Expand All @@ -165,6 +166,7 @@ interface Props {
projectId: string | undefined;
defaultTab?: 'entries' | 'simplified' | 'original' | 'visuals';
frameworkDetails?: Framework;
leadAttachmentsMap: LeadAttachmentsMap;
activeTabRef?: React.MutableRefObject<{
setActiveTab: React.Dispatch<React.SetStateAction<TabOptions>>;
} | null>;
Expand Down Expand Up @@ -201,6 +203,8 @@ function LeftPane(props: Props) {
frameworkDetails,
onAssistedEntryAdd,
entryAttachmentsMap,
leadAttachmentsMap,
onAttachmentClick,
} = props;

const alert = useAlert();
Expand Down Expand Up @@ -232,8 +236,6 @@ function LeftPane(props: Props) {
: defaultTab,
);

const [leadAttachmentsMap, setLeadAttachmentsMap] = useState<LeadAttachmentsMap>({});

const [
autoEntriesModalShown,
showAutoEntriesModal,
Expand Down Expand Up @@ -396,23 +398,6 @@ function LeftPane(props: Props) {
}
}, [leadId, onEntryCreate]);

const handleAttachmentClick = useCallback((attachment: LeadPreviewAttachmentType) => {
if (onEntryCreate) {
onEntryCreate({
clientId: randomString(),
entryType: 'ATTACHMENT',
lead: leadId,
leadAttachment: attachment.id,
excerpt: '',
droppedExcerpt: '',
});
setLeadAttachmentsMap((oldValue) => ({
...oldValue,
[attachment.id]: attachment,
}));
}
}, [leadId, onEntryCreate]);

const handleExcerptAddFromOriginal = useCallback((selectedText: string | undefined) => {
if (onEntryCreate) {
onEntryCreate({
Expand Down Expand Up @@ -506,12 +491,12 @@ function LeftPane(props: Props) {

return ({
type: 'visual-item' as const,
onClick: handleAttachmentClick,
onClick: onAttachmentClick,
disableClick: isEntrySelectionActive,
attachment,
});
}, [
handleAttachmentClick,
onAttachmentClick,
onApproveButtonClick,
onDiscardButtonClick,
isEntrySelectionActive,
Expand Down
28 changes: 28 additions & 0 deletions app/views/EntryEdit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import {
BulkUpdateEntriesMutationVariables,
LeadUpdateMutation,
LeadUpdateMutationVariables,
LeadPreviewForTextQuery,
LeadPreviewAttachmentType,
} from '#generated/types';
import { BasicOrganization } from '#components/selections/NewOrganizationSelectInput';
import { BasicProjectUser } from '#components/selections/ProjectUserSelectInput';
Expand Down Expand Up @@ -112,6 +114,9 @@ const UPDATE_LEN = 100;
const entryKeySelector = (e: PartialEntryType) => e.clientId;
export type Lead = NonNullable<NonNullable<LeadEntriesQuery['project']>['lead']>;

type LeadAttachment = NonNullable<NonNullable<NonNullable<LeadPreviewForTextQuery['project']>['leadPreviewAttachments']>['results']>[number];
export type LeadAttachmentsMap = { [key: string]: LeadAttachment | undefined };

function transformEntry(entry: Entry): EntryInputType {
// FIXME: make this re-usable
return removeNull({
Expand Down Expand Up @@ -368,6 +373,8 @@ function EntryEdit(props: Props) {
setEntryAttachmentMap,
] = useState<EntryAttachmentsMap | undefined>();

const [leadAttachmentsMap, setLeadAttachmentsMap] = useState<LeadAttachmentsMap>({});

const [
updateLead,
{ loading: leadUpdatePending },
Expand Down Expand Up @@ -1041,6 +1048,23 @@ function EntryEdit(props: Props) {
[onEntryFieldChange, clearRestorePoint],
);

const handleAttachmentClick = useCallback((attachment: LeadPreviewAttachmentType) => {
if (handleEntryCreate) {
handleEntryCreate({
clientId: randomString(),
entryType: 'ATTACHMENT',
lead: leadId,
leadAttachment: attachment.id,
excerpt: '',
droppedExcerpt: '',
});
setLeadAttachmentsMap((oldValue) => ({
...oldValue,
[attachment.id]: attachment,
}));
}
}, [leadId, handleEntryCreate]);

// NOTE: we are creating a map of index and value because we are iterating
// over widgets but modifying attributes
const attributesMap = useMemo(() => (
Expand Down Expand Up @@ -1639,6 +1663,7 @@ function EntryEdit(props: Props) {
onEntryDelete={handleEntryDelete}
onEntryRestore={handleEntryRestore}
onExcerptChange={handleExcerptChange}
onAttachmentClick={handleAttachmentClick}
lead={lead}
leadId={leadId}
listComponentRef={primaryPageListComponentRef}
Expand All @@ -1647,6 +1672,7 @@ function EntryEdit(props: Props) {
isEntrySelectionActive={isEntrySelectionActive}
entriesError={entriesErrorStateMap}
frameworkDetails={frameworkDetails ?? undefined}
leadAttachmentsMap={leadAttachmentsMap}
/>
<Container
className={_cs(className, styles.sections)}
Expand Down Expand Up @@ -1739,6 +1765,8 @@ function EntryEdit(props: Props) {
entriesError={entriesErrorStateMap}
activeTabRef={secondaryPageLeftPaneRef}
frameworkDetails={frameworkDetails ?? undefined}
onAttachmentClick={handleAttachmentClick}
leadAttachmentsMap={leadAttachmentsMap}
/>
<Container
className={styles.rightContainer}
Expand Down

0 comments on commit 21d9c13

Please sign in to comment.