Files
AFFiNE/libs/utils/src/utils.ts
zuomeng wang b35c9b2c14 Feat/land page (#391)
* feat: update landing page

* feat: update landing page

* fix: react warning

* feat(route): rename variables

* feat(route): better refresh

* fix: update logo and i18n

* feat(code): remove unused comment

* refactor(workspace): rename workspaceId

* refactor(page): rename pageId

* feat(edit): more robust editing experience

* fix: landing page mobile

* fix: landing page mobile

Co-authored-by: tzhangchi <terry.zhangchi@outlook.com>
Co-authored-by: DarkSky <25152247+darkskygit@users.noreply.github.com>
2022-09-09 02:56:10 +00:00

70 lines
1.9 KiB
TypeScript

import type { UserInfo } from './types';
export function getUserDisplayName(user?: UserInfo) {
const { nickname, username, email } = user || {};
return nickname || username || email || 'Unknown User';
}
/**
* Get pageId from URL
* @returns pageId
*/
export function getPageId() {
const path = window.location.pathname.match(/\/(\w+)\/(\w+)/);
return path ? path[2] : undefined;
}
export async function sleep(delay = 100) {
return new Promise(res => {
window.setTimeout(() => {
res(true);
}, delay);
});
}
export function isInternalPageUrl(url: string) {
if (!url) return false;
return (
url.startsWith(window.location.origin) &&
/^https?:\/\/(localhost:4200|(nightly|app)\.affine\.pro|.*?\.ligo-virgo\.pages\.dev)/.test(
url
)
);
}
export function getRelativeUrlForInternalPageUrl(url: string) {
if (url?.startsWith(window.location.origin)) {
return url.slice(window.location.origin.length);
}
return url;
}
/**
* Generate a fragile object that will throw error at any operation.
*/
export const genErrorObj = (
...args: ConstructorParameters<ErrorConstructor>
): unknown =>
new Proxy(
{},
{
get(target, prop, receiver) {
console.error('You are trying to access a property', prop);
throw new Error(...args);
},
set(target, prop, val, receiver) {
console.error('You are trying to set a property', prop, val);
throw new Error(...args);
},
apply(target, thisArg, argumentsList) {
console.error(
'You are trying to call a function',
thisArg,
argumentsList
);
throw new Error(...args);
},
}
);