Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,8 @@
"vite-plugin-svgr": "^3.2.0",
"vite-plugin-terminal": "^1.2.0",
"vite-plugin-total-bundle-size": "^1.0.7"
},
"engines": {
"pnpm": "^8.0.0"
}
}
4 changes: 3 additions & 1 deletion src/@types/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3061,5 +3061,7 @@
"downloadTip": "Don't have AppFlowy Apps installed? Download <link/>.",
"here": "here",
"openInBrowser": "Open in browser",
"openInApp": "Open in app"
"openInApp": "Open in app",
"comments": "Comments",
"duplicateAsTemplate": "Duplicate as template"
}
106 changes: 68 additions & 38 deletions src/application/publish/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { notify } from '@/components/_shared/notify';
import { findAncestors, findView } from '@/components/_shared/outline/utils';
import { useService } from '@/components/main/app.hooks';
import { useLiveQuery } from 'dexie-react-hooks';
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';

export interface PublishContextType {
Expand All @@ -31,6 +31,8 @@ export interface PublishContextType {
breadcrumbs: View[];
rendered?: boolean;
onRendered?: () => void;
commentEnabled?: boolean;
duplicateEnabled?: boolean;
}

export const PublishContext = createContext<PublishContextType | null>(null);
Expand Down Expand Up @@ -60,31 +62,38 @@ export const PublishProvider = ({
};
}, []);

const viewMeta = useLiveQuery(async () => {
const viewMeta = useLiveQuery(async() => {
const name = `${namespace}_${publishName}`;

const view = await db.view_metas.get(name);

if (!view) return;
if(!view) return;

return {
...view,
name: findView(outline, view.view_id)?.name || view.name,
};
}, [namespace, publishName, outline]);

const viewId = viewMeta?.view_id;

const [publishInfo, setPublishInfo] = React.useState<{
commentEnabled: boolean,
duplicateEnabled: boolean,
} | undefined>();

const originalCrumbs = useMemo(() => {
if (!viewMeta || !outline) return [];
if(!viewMeta || !outline) return [];
const ancestors = findAncestors(outline, viewMeta?.view_id);

if (ancestors) return ancestors;
if (!viewMeta?.ancestor_views) return [];
if(ancestors) return ancestors;
if(!viewMeta?.ancestor_views) return [];
const parseToView = (ancestor: ViewInfo): View => {
let extra = null;

try {
extra = ancestor.extra ? JSON.parse(ancestor.extra) : null;
} catch (e) {
} catch(e) {
// do nothing
}

Expand Down Expand Up @@ -113,13 +122,13 @@ export const PublishProvider = ({

const appendBreadcrumb = useCallback((view?: View) => {
setBreadcrumbs((prev) => {
if (!view) {
if(!view) {
return prev.slice(0, -1);
}

const index = prev.findIndex((v) => v.view_id === view.view_id);

if (index === -1) {
if(index === -1) {
return [...prev, view];
}

Expand Down Expand Up @@ -165,24 +174,43 @@ export const PublishProvider = ({

createdRowKeys.current = [];

if (!rowKeys.length) return;
if(!rowKeys.length) return;
rowKeys.forEach((rowKey) => {
try {
service?.deleteRowDoc(rowKey);
} catch (e) {
} catch(e) {
console.error(e);
}
});

}, [service, publishName]);

const loadPublishInfo = useCallback(async() => {
if(!service || !viewId) return;
try {
const res = await service.getPublishInfo(viewId);

setPublishInfo(res);

// eslint-disable-next-line
} catch(e: any) {
// do nothing
}

}, [viewId, service]);

useEffect(() => {
void loadPublishInfo();
}, [loadPublishInfo]);

const navigate = useNavigate();

const loadViewMeta = useCallback(
async (viewId: string, callback?: (meta: View) => void) => {
async(viewId: string, callback?: (meta: View) => void) => {
try {
const info = await service?.getPublishInfo(viewId);

if (!info) {
if(!info) {
throw new Error('View has not been published yet');
}

Expand All @@ -192,7 +220,7 @@ export const PublishProvider = ({

const meta = await service?.getPublishViewMeta(namespace, publishName);

if (!meta) {
if(!meta) {
return Promise.reject(new Error('View meta has not been published yet'));
}

Expand All @@ -214,7 +242,7 @@ export const PublishProvider = ({

callback?.(res);

if (callback) {
if(callback) {
setSubscribers((prev) => {
prev.set(name, (meta) => {
return callback?.(parseMetaToView(meta));
Expand All @@ -225,21 +253,21 @@ export const PublishProvider = ({
}

return res;
} catch (e) {
} catch(e) {
return Promise.reject(e);
}
},
[service],
);

const toView = useCallback(
async (viewId: string, blockId?: string) => {
async(viewId: string, blockId?: string) => {
try {
const view = await loadViewMeta(viewId);

const res = await service?.getPublishInfo(viewId);

if (!res) {
if(!res) {
throw new Error('View has not been published yet');
}

Expand All @@ -248,8 +276,8 @@ export const PublishProvider = ({
prevViewMeta.current = undefined;
const searchParams = new URLSearchParams('');

if (blockId) {
switch (view.layout) {
if(blockId) {
switch(view.layout) {
case ViewLayout.Document:
searchParams.set('blockId', blockId);
break;
Expand All @@ -263,66 +291,66 @@ export const PublishProvider = ({
}
}

if (isTemplate) {
if(isTemplate) {
searchParams.set('template', 'true');
}

let url = `/${viewNamespace}/${publishName}`;

if (searchParams.toString()) {
if(searchParams.toString()) {
url += `?${searchParams.toString()}`;
}

navigate(url, {
replace: true,
});
return;
} catch (e) {
} catch(e) {
return Promise.reject(e);
}
},
[loadViewMeta, service, isTemplate, navigate],
);

const loadOutline = useCallback(async () => {
if (!service || !namespace) return;
const loadOutline = useCallback(async() => {
if(!service || !namespace) return;
try {
const res = await service?.getPublishOutline(namespace);

if (!res) {
if(!res) {
throw new Error('Publish outline not found');
}

setOutline(res);
} catch (e) {
} catch(e) {
notify.error('Publish outline not found');
}
}, [namespace, service]);

const createRowDoc = useCallback(
async (rowKey: string) => {
async(rowKey: string) => {
try {
const doc = await service?.createRowDoc(rowKey);

if (!doc) {
if(!doc) {
throw new Error('Failed to create row doc');
}

createdRowKeys.current.push(rowKey);
return doc;
} catch (e) {
} catch(e) {
return Promise.reject(e);
}
},
[service],
);

const loadView = useCallback(
async (viewId: string, isSubDocument?: boolean) => {
if (isSubDocument) {
async(viewId: string, isSubDocument?: boolean) => {
if(isSubDocument) {
const data = await service?.getPublishRowDocument(viewId);

if (!data) {
if(!data) {
return Promise.reject(new Error('View has not been published yet'));
}

Expand All @@ -332,20 +360,20 @@ export const PublishProvider = ({
try {
const res = await service?.getPublishInfo(viewId);

if (!res) {
if(!res) {
throw new Error('View has not been published yet');
}

const { namespace, publishName } = res;

const data = service?.getPublishView(namespace, publishName);

if (!data) {
if(!data) {
throw new Error('View has not been published yet');
}

return data;
} catch (e) {
} catch(e) {
return Promise.reject(e);
}
},
Expand All @@ -357,7 +385,7 @@ export const PublishProvider = ({
}, []);

useEffect(() => {
if (!viewMeta && prevViewMeta.current) {
if(!viewMeta && prevViewMeta.current) {
window.location.reload();
return;
}
Expand Down Expand Up @@ -385,13 +413,15 @@ export const PublishProvider = ({
appendBreadcrumb,
onRendered,
rendered,
commentEnabled: publishInfo?.commentEnabled,
duplicateEnabled: publishInfo?.duplicateEnabled,
}}
>
{children}
</PublishContext.Provider>
);
};

export function usePublishContext () {
export function usePublishContext() {
return useContext(PublishContext);
}
Loading
Loading