fix: restore code block loading states (#728)
This commit is contained in:
parent
38527ff92e
commit
085f4a8ac4
6 changed files with 269 additions and 136 deletions
|
|
@ -4,22 +4,22 @@ import { ComponentType, Dispatch, ReactNode, SetStateAction } from 'react';
|
|||
import { DataStreamDelta } from './data-stream-handler';
|
||||
import { UIBlock } from './block';
|
||||
|
||||
export type BlockActionContext = {
|
||||
export type BlockActionContext<M = any> = {
|
||||
content: string;
|
||||
handleVersionChange: (type: 'next' | 'prev' | 'toggle' | 'latest') => void;
|
||||
currentVersionIndex: number;
|
||||
isCurrentVersion: boolean;
|
||||
mode: 'edit' | 'diff';
|
||||
metadata: any;
|
||||
setMetadata: Dispatch<SetStateAction<any>>;
|
||||
metadata: M;
|
||||
setMetadata: Dispatch<SetStateAction<M>>;
|
||||
};
|
||||
|
||||
type BlockAction = {
|
||||
type BlockAction<M = any> = {
|
||||
icon: ReactNode;
|
||||
label?: string;
|
||||
description: string;
|
||||
onClick: (context: BlockActionContext) => void;
|
||||
isDisabled?: (context: BlockActionContext) => boolean;
|
||||
onClick: (context: BlockActionContext<M>) => Promise<void> | void;
|
||||
isDisabled?: (context: BlockActionContext<M>) => boolean;
|
||||
};
|
||||
|
||||
export type BlockToolbarContext = {
|
||||
|
|
@ -32,7 +32,7 @@ export type BlockToolbarItem = {
|
|||
onClick: (context: BlockToolbarContext) => void;
|
||||
};
|
||||
|
||||
type BlockContent = {
|
||||
interface BlockContent<M = any> {
|
||||
title: string;
|
||||
content: string;
|
||||
mode: 'edit' | 'diff';
|
||||
|
|
@ -44,9 +44,9 @@ type BlockContent = {
|
|||
isInline: boolean;
|
||||
getDocumentContentById: (index: number) => string;
|
||||
isLoading: boolean;
|
||||
metadata: any;
|
||||
setMetadata: Dispatch<SetStateAction<any>>;
|
||||
};
|
||||
metadata: M;
|
||||
setMetadata: Dispatch<SetStateAction<M>>;
|
||||
}
|
||||
|
||||
interface InitializeParameters<M = any> {
|
||||
documentId: string;
|
||||
|
|
@ -56,17 +56,11 @@ interface InitializeParameters<M = any> {
|
|||
type BlockConfig<T extends string, M = any> = {
|
||||
kind: T;
|
||||
description: string;
|
||||
content: ComponentType<
|
||||
Omit<BlockContent, 'metadata' | 'setMetadata'> & {
|
||||
metadata: M;
|
||||
setMetadata: Dispatch<SetStateAction<M>>;
|
||||
}
|
||||
>;
|
||||
actions?: BlockAction[];
|
||||
toolbar?: BlockToolbarItem[];
|
||||
metadata?: M;
|
||||
content: ComponentType<BlockContent<M>>;
|
||||
actions: Array<BlockAction<M>>;
|
||||
toolbar: BlockToolbarItem[];
|
||||
initialize?: (parameters: InitializeParameters<M>) => void;
|
||||
onStreamPart?: (args: {
|
||||
onStreamPart: (args: {
|
||||
setMetadata: Dispatch<SetStateAction<M>>;
|
||||
setBlock: Dispatch<SetStateAction<UIBlock>>;
|
||||
streamPart: DataStreamDelta;
|
||||
|
|
@ -76,12 +70,11 @@ type BlockConfig<T extends string, M = any> = {
|
|||
export class Block<T extends string, M = any> {
|
||||
readonly kind: T;
|
||||
readonly description: string;
|
||||
readonly content: ComponentType<BlockContent>;
|
||||
readonly actions: BlockAction[];
|
||||
readonly content: ComponentType<BlockContent<M>>;
|
||||
readonly actions: Array<BlockAction<M>>;
|
||||
readonly toolbar: BlockToolbarItem[];
|
||||
readonly metadata: M;
|
||||
readonly initialize: (parameters: InitializeParameters) => void;
|
||||
readonly onStreamPart?: (args: {
|
||||
readonly initialize?: (parameters: InitializeParameters) => void;
|
||||
readonly onStreamPart: (args: {
|
||||
setMetadata: Dispatch<SetStateAction<M>>;
|
||||
setBlock: Dispatch<SetStateAction<UIBlock>>;
|
||||
streamPart: DataStreamDelta;
|
||||
|
|
@ -93,7 +86,6 @@ export class Block<T extends string, M = any> {
|
|||
this.content = config.content;
|
||||
this.actions = config.actions || [];
|
||||
this.toolbar = config.toolbar || [];
|
||||
this.metadata = config.metadata as M;
|
||||
this.initialize = config.initialize || (async () => ({}));
|
||||
this.onStreamPart = config.onStreamPart;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue