UNPKG

@assistant-ui/react

Version:

Typescript/React library for AI Chat

1 lines 4.95 kB
{"version":3,"sources":["../../../src/context/react/AttachmentContext.ts"],"sourcesContent":["\"use client\";\n\nimport { createContext } from \"react\";\nimport { ReadonlyStore } from \"../ReadonlyStore\";\nimport { AttachmentRuntime } from \"../../api/AttachmentRuntime\";\nimport { UseBoundStore } from \"zustand\";\nimport { createContextHook } from \"./utils/createContextHook\";\nimport { createStateHookForRuntime } from \"./utils/createStateHookForRuntime\";\n\nexport type AttachmentContextValue = {\n useAttachmentRuntime: UseBoundStore<ReadonlyStore<AttachmentRuntime>>;\n};\n\nexport const AttachmentContext = createContext<AttachmentContextValue | null>(\n null,\n);\n\nconst useAttachmentContext = createContextHook(\n AttachmentContext,\n \"a ComposerPrimitive.Attachments or MessagePrimitive.Attachments component\",\n);\n\nexport function useAttachmentRuntime(options?: {\n optional?: false | undefined;\n}): AttachmentRuntime;\nexport function useAttachmentRuntime(options?: {\n optional?: boolean | undefined;\n}): AttachmentRuntime | null;\nexport function useAttachmentRuntime(options?: {\n optional?: boolean | undefined;\n}): AttachmentRuntime | null {\n const attachmentRuntime = useAttachmentContext(options);\n if (!attachmentRuntime) return null;\n return attachmentRuntime.useAttachmentRuntime();\n}\n\nexport function useThreadComposerAttachmentRuntime(options?: {\n optional?: false | undefined;\n}): AttachmentRuntime<\"thread-composer\">;\nexport function useThreadComposerAttachmentRuntime(options?: {\n optional?: boolean | undefined;\n}): AttachmentRuntime<\"thread-composer\"> | null;\nexport function useThreadComposerAttachmentRuntime(options?: {\n optional?: boolean | undefined;\n}): AttachmentRuntime<\"thread-composer\"> | null {\n const attachmentRuntime = useAttachmentRuntime(options);\n if (!attachmentRuntime) return null;\n if (attachmentRuntime.source !== \"thread-composer\")\n throw new Error(\n \"This component must be used within a thread's ComposerPrimitive.Attachments component.\",\n );\n return attachmentRuntime as AttachmentRuntime<\"thread-composer\">;\n}\n\nexport function useEditComposerAttachmentRuntime(options?: {\n optional?: false | undefined;\n}): AttachmentRuntime<\"edit-composer\">;\nexport function useEditComposerAttachmentRuntime(options?: {\n optional?: boolean | undefined;\n}): AttachmentRuntime<\"edit-composer\"> | null;\nexport function useEditComposerAttachmentRuntime(options?: {\n optional?: boolean | undefined;\n}): AttachmentRuntime<\"edit-composer\"> | null {\n const attachmentRuntime = useAttachmentRuntime(options);\n if (!attachmentRuntime) return null;\n if (attachmentRuntime.source !== \"edit-composer\")\n throw new Error(\n \"This component must be used within a message's ComposerPrimitive.Attachments component.\",\n );\n\n return attachmentRuntime as AttachmentRuntime<\"edit-composer\">;\n}\n\nexport function useMessageAttachmentRuntime(options?: {\n optional?: false | undefined;\n}): AttachmentRuntime<\"message\">;\nexport function useMessageAttachmentRuntime(options?: {\n optional?: boolean | undefined;\n}): AttachmentRuntime<\"message\"> | null;\nexport function useMessageAttachmentRuntime(options?: {\n optional?: boolean | undefined;\n}): AttachmentRuntime<\"message\"> | null {\n const attachmentRuntime = useAttachmentRuntime(options);\n if (!attachmentRuntime) return null;\n if (attachmentRuntime.source !== \"message\")\n throw new Error(\n \"This component must be used within a MessagePrimitive.Attachments component.\",\n );\n return attachmentRuntime as AttachmentRuntime<\"message\">;\n}\n\nexport const useAttachment = createStateHookForRuntime(useAttachmentRuntime);\n\nexport const useThreadComposerAttachment = createStateHookForRuntime(\n useThreadComposerAttachmentRuntime,\n);\nexport const useEditComposerAttachment = createStateHookForRuntime(\n useEditComposerAttachmentRuntime,\n);\nexport const useMessageAttachment = createStateHookForRuntime(\n useMessageAttachmentRuntime,\n);\n"],"mappings":";;;AAEA,SAAS,qBAAqB;AAI9B,SAAS,yBAAyB;AAClC,SAAS,iCAAiC;AAMnC,IAAM,oBAAoB;AAAA,EAC/B;AACF;AAEA,IAAM,uBAAuB;AAAA,EAC3B;AAAA,EACA;AACF;AAQO,SAAS,qBAAqB,SAER;AAC3B,QAAM,oBAAoB,qBAAqB,OAAO;AACtD,MAAI,CAAC,kBAAmB,QAAO;AAC/B,SAAO,kBAAkB,qBAAqB;AAChD;AAQO,SAAS,mCAAmC,SAEH;AAC9C,QAAM,oBAAoB,qBAAqB,OAAO;AACtD,MAAI,CAAC,kBAAmB,QAAO;AAC/B,MAAI,kBAAkB,WAAW;AAC/B,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AACF,SAAO;AACT;AAQO,SAAS,iCAAiC,SAEH;AAC5C,QAAM,oBAAoB,qBAAqB,OAAO;AACtD,MAAI,CAAC,kBAAmB,QAAO;AAC/B,MAAI,kBAAkB,WAAW;AAC/B,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAEF,SAAO;AACT;AAQO,SAAS,4BAA4B,SAEJ;AACtC,QAAM,oBAAoB,qBAAqB,OAAO;AACtD,MAAI,CAAC,kBAAmB,QAAO;AAC/B,MAAI,kBAAkB,WAAW;AAC/B,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AACF,SAAO;AACT;AAEO,IAAM,gBAAgB,0BAA0B,oBAAoB;AAEpE,IAAM,8BAA8B;AAAA,EACzC;AACF;AACO,IAAM,4BAA4B;AAAA,EACvC;AACF;AACO,IAAM,uBAAuB;AAAA,EAClC;AACF;","names":[]}