chore: rename blocks to artifacts (#793)
This commit is contained in:
parent
01f589b603
commit
81f909ac3a
41 changed files with 473 additions and 473 deletions
99
lib/artifacts/server.ts
Normal file
99
lib/artifacts/server.ts
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
import { codeDocumentHandler } from '@/artifacts/code/server';
|
||||
import { imageDocumentHandler } from '@/artifacts/image/server';
|
||||
import { sheetDocumentHandler } from '@/artifacts/sheet/server';
|
||||
import { textDocumentHandler } from '@/artifacts/text/server';
|
||||
import { ArtifactKind } from '@/components/artifact';
|
||||
import { DataStreamWriter } from 'ai';
|
||||
import { Document } from '../db/schema';
|
||||
import { saveDocument } from '../db/queries';
|
||||
import { Session } from 'next-auth';
|
||||
|
||||
export interface SaveDocumentProps {
|
||||
id: string;
|
||||
title: string;
|
||||
kind: ArtifactKind;
|
||||
content: string;
|
||||
userId: string;
|
||||
}
|
||||
|
||||
export interface CreateDocumentCallbackProps {
|
||||
id: string;
|
||||
title: string;
|
||||
dataStream: DataStreamWriter;
|
||||
session: Session;
|
||||
}
|
||||
|
||||
export interface UpdateDocumentCallbackProps {
|
||||
document: Document;
|
||||
description: string;
|
||||
dataStream: DataStreamWriter;
|
||||
session: Session;
|
||||
}
|
||||
|
||||
export interface DocumentHandler<T = ArtifactKind> {
|
||||
kind: T;
|
||||
onCreateDocument: (args: CreateDocumentCallbackProps) => Promise<void>;
|
||||
onUpdateDocument: (args: UpdateDocumentCallbackProps) => Promise<void>;
|
||||
}
|
||||
|
||||
export function createDocumentHandler<T extends ArtifactKind>(config: {
|
||||
kind: T;
|
||||
onCreateDocument: (params: CreateDocumentCallbackProps) => Promise<string>;
|
||||
onUpdateDocument: (params: UpdateDocumentCallbackProps) => Promise<string>;
|
||||
}): DocumentHandler<T> {
|
||||
return {
|
||||
kind: config.kind,
|
||||
onCreateDocument: async (args: CreateDocumentCallbackProps) => {
|
||||
const draftContent = await config.onCreateDocument({
|
||||
id: args.id,
|
||||
title: args.title,
|
||||
dataStream: args.dataStream,
|
||||
session: args.session,
|
||||
});
|
||||
|
||||
if (args.session?.user?.id) {
|
||||
await saveDocument({
|
||||
id: args.id,
|
||||
title: args.title,
|
||||
content: draftContent,
|
||||
kind: config.kind,
|
||||
userId: args.session.user.id,
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
},
|
||||
onUpdateDocument: async (args: UpdateDocumentCallbackProps) => {
|
||||
const draftContent = await config.onUpdateDocument({
|
||||
document: args.document,
|
||||
description: args.description,
|
||||
dataStream: args.dataStream,
|
||||
session: args.session,
|
||||
});
|
||||
|
||||
if (args.session?.user?.id) {
|
||||
await saveDocument({
|
||||
id: args.document.id,
|
||||
title: args.document.title,
|
||||
content: draftContent,
|
||||
kind: config.kind,
|
||||
userId: args.session.user.id,
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* Use this array to define the document handlers for each artifact kind.
|
||||
*/
|
||||
export const documentHandlersByArtifactKind: Array<DocumentHandler> = [
|
||||
textDocumentHandler,
|
||||
codeDocumentHandler,
|
||||
imageDocumentHandler,
|
||||
sheetDocumentHandler,
|
||||
];
|
||||
|
||||
export const artifactKinds = ['text', 'code', 'image', 'sheet'] as const;
|
||||
Loading…
Add table
Add a link
Reference in a new issue