Skip to content

Commit

Permalink
Trade: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Jan 22, 2024
1 parent a94f2c6 commit db3a435
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/modules/trade/chatlink/ChatLinkDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getOriginUrl } from '~/common/util/urlUtils';
import { webShare, webSharePresent } from '~/common/util/pwaUtils';

import type { StorageDeleteSchema, StoragePutSchema } from '../server/link';
import { removeChatLinkItem } from './store-chatlink';
import { forgetChatLinkItem } from './store-chatlink';


export function ChatLinkDetails(props: { onClose: () => void, storageItem: StoragePutSchema, open: boolean }) {
Expand Down Expand Up @@ -64,7 +64,7 @@ export function ChatLinkDetails(props: { onClose: () => void, storageItem: Stora
const result: StorageDeleteSchema = await apiAsyncNode.trade.storageDelete.mutate({ objectId, deletionKey });
setDeletionResponse(result);
if (result.type === 'success')
removeChatLinkItem(objectId);
forgetChatLinkItem(objectId);
setConfirmDeletion(false);
};

Expand Down
50 changes: 27 additions & 23 deletions src/modules/trade/chatlink/ChatLinkExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { useUICounter } from '~/common/state/store-ui';

import type { StoragePutSchema } from '../server/link';
import { ChatLinkDetails } from './ChatLinkDetails';
import { addChatLinkItem, useLinkStorageOwnerId } from './store-chatlink';
import { conversationToJsonV1 } from '../trade.client';
import { rememberChatLinkItem, useLinkStorageOwnerId } from './store-chatlink';


export function ChatLinkExport(props: {
Expand All @@ -24,25 +24,27 @@ export function ChatLinkExport(props: {
}) {

// state
const [chatLinkConfirmId, setChatLinkConfirmId] = React.useState<DConversationId | null>(null);
const [chatLinkUploading, setChatLinkUploading] = React.useState(false);
const [chatLinkResponse, setChatLinkResponse] = React.useState<StoragePutSchema | null>(null);
const [confirmConversationId, setConfirmConversationId] = React.useState<DConversationId | null>(null);
const [isUploading, setIsUploading] = React.useState(false);
const [linkPutResult, setLinkPutResult] = React.useState<StoragePutSchema | null>(null);

// external state
const { novel: chatLinkBadge, touch: clearChatLinkBadge } = useUICounter('share-chat-link');
const { linkStorageOwnerId, setLinkStorageOwnerId } = useLinkStorageOwnerId();


const handleChatLinkCreate = () => setChatLinkConfirmId(props.conversationId);
const handleConfirm = () => setConfirmConversationId(props.conversationId);

const handleChatLinkConfirmed = async () => {
if (!chatLinkConfirmId) return;
const handleCancel = () => setConfirmConversationId(null);

const conversation = getConversation(chatLinkConfirmId);
setChatLinkConfirmId(null);
const handleCreate = async () => {
if (!confirmConversationId) return;

const conversation = getConversation(confirmConversationId);
setConfirmConversationId(null);
if (!conversation) return;

setChatLinkUploading(true);
setIsUploading(true);
try {
const chatV1 = conversationToJsonV1(conversation);
const chatTitle = conversationTitle(conversation) || undefined;
Expand All @@ -52,43 +54,45 @@ export function ChatLinkExport(props: {
dataTitle: chatTitle,
dataObject: chatV1,
});
setChatLinkResponse(response);
setLinkPutResult(response);
if (response.type === 'success') {
addChatLinkItem(chatTitle, response.objectId, response.createdAt, response.expiresAt, response.deletionKey);
if (!linkStorageOwnerId)
setLinkStorageOwnerId(response.ownerId);
rememberChatLinkItem(chatTitle, response.objectId, response.createdAt, response.expiresAt, response.deletionKey);
}
clearChatLinkBadge();
} catch (error: any) {
setChatLinkResponse({
setLinkPutResult({
type: 'error',
error: error?.message ?? error?.toString() ?? 'unknown error',
});
}
setChatLinkUploading(false);
setIsUploading(false);
};

const handleCloseDetails = () => setLinkPutResult(null);


const hasConversation = !!props.conversationId;


return <>

<Badge color='danger' invisible={!chatLinkBadge}>
<Button variant='soft' disabled={!hasConversation || chatLinkUploading}
loading={chatLinkUploading}
color={chatLinkResponse ? 'success' : 'primary'}
endDecorator={chatLinkResponse ? <DoneIcon /> : <IosShareIcon />}
<Button variant='soft' disabled={!hasConversation || isUploading}
loading={isUploading}
color={linkPutResult ? 'success' : 'primary'}
endDecorator={linkPutResult ? <DoneIcon /> : <IosShareIcon />}
sx={{ minWidth: 240, justifyContent: 'space-between' }}
onClick={handleChatLinkCreate}>
onClick={handleConfirm}>
Share on {Brand.Title.Base}
</Button>
</Badge>

{/* [chat link] confirmation */}
{!!chatLinkConfirmId && (
{!!confirmConversationId && (
<ConfirmationModal
open onClose={() => setChatLinkConfirmId(null)} onPositive={handleChatLinkConfirmed}
open onClose={handleCancel} onPositive={handleCreate}
title='Upload Confirmation'
confirmationText={<>
Everyone who has the unlisted link will be able to access this chat.
Expand All @@ -102,8 +106,8 @@ export function ChatLinkExport(props: {
)}

{/* [chat link] response */}
{!!chatLinkResponse && (
<ChatLinkDetails open onClose={() => setChatLinkResponse(null)} storageItem={chatLinkResponse} />
{!!linkPutResult && (
<ChatLinkDetails open storageItem={linkPutResult} onClose={handleCloseDetails} />
)}

</>;
Expand Down
12 changes: 6 additions & 6 deletions src/modules/trade/chatlink/store-chatlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ interface ModuleTradeStore {

// exported items
chatLinkItems: ChatLinkItem[];
addChatLinkItem: (chatTitle: string | undefined, objectId: string, createdAt: Date, expiresAt: Date | null, deletionKey: string) => void;
removeChatLinkItem: (objectId: string) => void;
rememberChatLinkItem: (chatTitle: string | undefined, objectId: string, createdAt: Date, expiresAt: Date | null, deletionKey: string) => void;
forgetChatLinkItem: (objectId: string) => void;

// ID assigned by the server upon first PUT
linkStorageOwnerId: string | undefined;
Expand All @@ -29,10 +29,10 @@ const useTradeStore = create<ModuleTradeStore>()(
(set) => ({

chatLinkItems: [],
addChatLinkItem: (chatTitle: string | undefined, objectId: string, createdAt: Date, expiresAt: Date | null, deletionKey: string) => set(state => ({
rememberChatLinkItem: (chatTitle: string | undefined, objectId: string, createdAt: Date, expiresAt: Date | null, deletionKey: string) => set(state => ({
chatLinkItems: [...state.chatLinkItems, { chatTitle, objectId, createdAt: createdAt.toISOString(), expiresAt: expiresAt?.toISOString() ?? null, deletionKey }],
})),
removeChatLinkItem: (objectId: string) => set(state => ({
forgetChatLinkItem: (objectId: string) => set(state => ({
chatLinkItems: state.chatLinkItems.filter(item => item.objectId !== objectId),
})),

Expand All @@ -57,5 +57,5 @@ export const useLinkStorageOwnerId = () =>
linkStorageOwnerId: state.linkStorageOwnerId,
setLinkStorageOwnerId: state.setLinkStorageOwnerId,
}), shallow);
export const addChatLinkItem = useTradeStore.getState().addChatLinkItem;
export const removeChatLinkItem = useTradeStore.getState().removeChatLinkItem;
export const rememberChatLinkItem = useTradeStore.getState().rememberChatLinkItem;
export const forgetChatLinkItem = useTradeStore.getState().forgetChatLinkItem;

0 comments on commit db3a435

Please sign in to comment.