From 9dc8aaa9aa00f9822de60a0ed33b6c8afa85f820 Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Mon, 22 Jan 2024 17:09:59 -0800 Subject: [PATCH] Links: renames --- pages/link/chat/[chatLinkId].tsx | 4 +- src/common/app.nav.ts | 2 +- src/modules/trade/ExportChats.tsx | 2 +- src/modules/trade/chatlink/store-chatlink.ts | 67 ------------------ .../{chatlink => link}/ChatLinkDetails.tsx | 2 +- .../{chatlink => link}/ChatLinkExport.tsx | 2 +- src/modules/trade/link/store-link.ts | 69 +++++++++++++++++++ 7 files changed, 75 insertions(+), 73 deletions(-) delete mode 100644 src/modules/trade/chatlink/store-chatlink.ts rename src/modules/trade/{chatlink => link}/ChatLinkDetails.tsx (99%) rename src/modules/trade/{chatlink => link}/ChatLinkExport.tsx (99%) create mode 100644 src/modules/trade/link/store-link.ts diff --git a/pages/link/chat/[chatLinkId].tsx b/pages/link/chat/[chatLinkId].tsx index 6b933574d..341f9a861 100644 --- a/pages/link/chat/[chatLinkId].tsx +++ b/pages/link/chat/[chatLinkId].tsx @@ -1,6 +1,6 @@ import * as React from 'react'; -import { AppChatLink } from '../../../src/apps/link/AppChatLink'; +import { AppLinkChat } from '../../../src/apps/link/AppLinkChat'; import { useRouterQuery } from '~/common/app.routes'; import { withLayout } from '~/common/layout/withLayout'; @@ -11,5 +11,5 @@ export default function ChatLinkPage() { // external state const { chatLinkId } = useRouterQuery<{ chatLinkId: string | undefined }>(); - return withLayout({ type: 'optima', suspendAutoModelsSetup: true }, ); + return withLayout({ type: 'optima', suspendAutoModelsSetup: true }, ); } \ No newline at end of file diff --git a/src/common/app.nav.ts b/src/common/app.nav.ts index 35cca0ba7..af63b1ae8 100644 --- a/src/common/app.nav.ts +++ b/src/common/app.nav.ts @@ -30,7 +30,7 @@ import SettingsIcon from '@mui/icons-material/Settings'; import { Brand } from '~/common/app.config'; -import { hasNoChatLinkItems } from '~/modules/trade/chatlink/store-chatlink'; +import { hasNoChatLinkItems } from '~/modules/trade/link/store-link'; // enable to show all items, for layout development diff --git a/src/modules/trade/ExportChats.tsx b/src/modules/trade/ExportChats.tsx index 1cda95b38..3bb055646 100644 --- a/src/modules/trade/ExportChats.tsx +++ b/src/modules/trade/ExportChats.tsx @@ -8,7 +8,7 @@ import { backendCaps } from '~/modules/backend/state-backend'; import { DConversationId, getConversation } from '~/common/state/store-chats'; -import { ChatLinkExport } from './chatlink/ChatLinkExport'; +import { ChatLinkExport } from './link/ChatLinkExport'; import { PublishExport } from './publish/PublishExport'; import { downloadAllConversationsJson, downloadConversationJson } from './trade.client'; diff --git a/src/modules/trade/chatlink/store-chatlink.ts b/src/modules/trade/chatlink/store-chatlink.ts deleted file mode 100644 index 1c4246312..000000000 --- a/src/modules/trade/chatlink/store-chatlink.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { create } from 'zustand'; -import { persist } from 'zustand/middleware'; -import { shallow } from 'zustand/shallow'; - - -interface ChatLinkItem { - chatTitle?: string; - objectId: string; - createdAt: string; - expiresAt: string | null; - deletionKey: string; -} - -interface ModuleTradeStore { - - // exported items - chatLinkItems: ChatLinkItem[]; - rememberChatLinkItem: (chatTitle: string | undefined, objectId: string, createdAt: Date, expiresAt: Date | null, deletionKey: string) => void; - forgetChatLinkItem: (objectId: string) => void; - updateChatLinkDeletionKey: (objectId: string, deletionKey: string) => void; - - // ID assigned by the server upon first PUT - linkStorageOwnerId: string | undefined; - setLinkStorageOwnerId: (linkStorageOwnerId: string) => void; - -} - -const useTradeStore = create()( - persist( - (set) => ({ - - chatLinkItems: [], - 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 }], - })), - forgetChatLinkItem: (objectId: string) => set(state => ({ - chatLinkItems: state.chatLinkItems.filter(item => item.objectId !== objectId), - })), - updateChatLinkDeletionKey: (objectId: string, deletionKey: string) => set(state => ({ - chatLinkItems: state.chatLinkItems.map(item => item.objectId === objectId ? { ...item, deletionKey } : item), - })), - - linkStorageOwnerId: undefined, - setLinkStorageOwnerId: (linkStorageOwnerId: string) => set({ linkStorageOwnerId }), - - }), - { - name: 'app-sharing', - }, - ), -); - - -// by AppChatLink -export const useChatLinkItems = () => useTradeStore(state => state.chatLinkItems, shallow); -export const useHasChatLinkItems = () => useTradeStore(state => state.chatLinkItems.length > 0); - -// by ChatLinkExport -export const useLinkStorageOwnerId = () => - useTradeStore(state => ({ - linkStorageOwnerId: state.linkStorageOwnerId, - setLinkStorageOwnerId: state.setLinkStorageOwnerId, - }), shallow); -export const rememberChatLinkItem = useTradeStore.getState().rememberChatLinkItem; -export const forgetChatLinkItem = useTradeStore.getState().forgetChatLinkItem; -export const updateChatLinkDeletionKey = useTradeStore.getState().updateChatLinkDeletionKey; -export const hasNoChatLinkItems = () => !useTradeStore.getState().chatLinkItems.length; diff --git a/src/modules/trade/chatlink/ChatLinkDetails.tsx b/src/modules/trade/link/ChatLinkDetails.tsx similarity index 99% rename from src/modules/trade/chatlink/ChatLinkDetails.tsx rename to src/modules/trade/link/ChatLinkDetails.tsx index 8b58e3237..73e8217f0 100644 --- a/src/modules/trade/chatlink/ChatLinkDetails.tsx +++ b/src/modules/trade/link/ChatLinkDetails.tsx @@ -23,7 +23,7 @@ import { getOriginUrl } from '~/common/util/urlUtils'; import { webShare, webSharePresent } from '~/common/util/pwaUtils'; import type { StorageDeleteSchema, StoragePutSchema } from '../server/link'; -import { forgetChatLinkItem } from './store-chatlink'; +import { forgetChatLinkItem } from './store-link'; export function ChatLinkDetails(props: { diff --git a/src/modules/trade/chatlink/ChatLinkExport.tsx b/src/modules/trade/link/ChatLinkExport.tsx similarity index 99% rename from src/modules/trade/chatlink/ChatLinkExport.tsx rename to src/modules/trade/link/ChatLinkExport.tsx index 8c510cab9..e2b3a504b 100644 --- a/src/modules/trade/chatlink/ChatLinkExport.tsx +++ b/src/modules/trade/link/ChatLinkExport.tsx @@ -15,7 +15,7 @@ import { useUICounter } from '~/common/state/store-ui'; import type { StoragePutSchema, StorageUpdateDeletionKeySchema } from '../server/link'; import { ChatLinkDetails } from './ChatLinkDetails'; import { conversationToJsonV1 } from '../trade.client'; -import { rememberChatLinkItem, updateChatLinkDeletionKey, useLinkStorageOwnerId } from './store-chatlink'; +import { rememberChatLinkItem, updateChatLinkDeletionKey, useLinkStorageOwnerId } from './store-link'; export function ChatLinkExport(props: { diff --git a/src/modules/trade/link/store-link.ts b/src/modules/trade/link/store-link.ts new file mode 100644 index 000000000..e0881e7fa --- /dev/null +++ b/src/modules/trade/link/store-link.ts @@ -0,0 +1,69 @@ +import { create } from 'zustand'; +import { persist } from 'zustand/middleware'; +import { shallow } from 'zustand/shallow'; + + +export interface SharedChatLinkItem { + chatTitle?: string; + objectId: string; + createdAt: string; + expiresAt: string | null; + deletionKey: string; +} + +interface LinkStore { + + // exported items + chatLinkItems: SharedChatLinkItem[]; + chatLinkItemAdd: (chatTitle: string | undefined, objectId: string, createdAt: Date, expiresAt: Date | null, deletionKey: string) => void; + chatLinkItemRemove: (objectId: string) => void; + chatLinkItemChangeDeletionKey: (objectId: string, deletionKey: string) => void; + + // ID assigned by the server upon first PUT + linkStorageOwnerId: string | undefined; + setLinkStorageOwnerId: (linkStorageOwnerId: string) => void; + +} + +const useLinkStore = create()( + persist( + (set) => ({ + + chatLinkItems: [], + chatLinkItemAdd: (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 }], + })), + chatLinkItemRemove: (objectId: string) => set(state => ({ + chatLinkItems: state.chatLinkItems.filter(item => item.objectId !== objectId), + })), + chatLinkItemChangeDeletionKey: (objectId: string, deletionKey: string) => set(state => ({ + chatLinkItems: state.chatLinkItems.map(item => item.objectId === objectId ? { ...item, deletionKey } : item), + })), + + linkStorageOwnerId: undefined, + setLinkStorageOwnerId: (linkStorageOwnerId: string) => set({ linkStorageOwnerId }), + + }), + { + name: 'app-sharing', + }, + ), +); + + +// by AppChatLink +export const useSharedChatLinkItems = () => useLinkStore(state => state.chatLinkItems, shallow); + +// by ChatLinkExport/ChatLinkDetails +export const rememberChatLinkItem = useLinkStore.getState().chatLinkItemAdd; +export const updateChatLinkDeletionKey = useLinkStore.getState().chatLinkItemChangeDeletionKey; +export const forgetChatLinkItem = useLinkStore.getState().chatLinkItemRemove; +export const useLinkStorageOwnerId = () => useLinkStore(state => ({ + linkStorageOwnerId: state.linkStorageOwnerId, + setLinkStorageOwnerId: state.setLinkStorageOwnerId, +}), shallow); + +// by Nav +export function hasNoChatLinkItems() { + return !useLinkStore.getState().chatLinkItems.length; +}