mirror of
https://github.com/documenso/documenso.git
synced 2026-07-26 01:45:08 +10:00
fix: wip
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
|
||||
|
||||
/**
|
||||
* getAssetBuffer is used to retrieve array buffers for various assets
|
||||
* that are hosted in the `public` folder.
|
||||
*
|
||||
* This exists due to a breakage with `import.meta.url` imports and open graph images,
|
||||
* once we can identify a fix for this, we can remove this helper.
|
||||
*
|
||||
* @param path The path to the asset, relative to the `public` folder.
|
||||
*/
|
||||
export const getAssetBuffer = async (path: string) => {
|
||||
const baseUrl = NEXT_PUBLIC_WEBAPP_URL() || 'http://localhost:3000';
|
||||
|
||||
return fetch(new URL(path, baseUrl)).then(async (res) => res.arrayBuffer());
|
||||
};
|
||||
@@ -0,0 +1,57 @@
|
||||
/* eslint-disable @typescript-eslint/consistent-type-assertions */
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
/**
|
||||
* https://github.com/kiliman/remix-superjson/
|
||||
*/
|
||||
import { useActionData, useLoaderData } from 'react-router';
|
||||
import * as _superjson from 'superjson';
|
||||
|
||||
export type SuperJsonFunction = <Data extends unknown>(
|
||||
data: Data,
|
||||
init?: number | ResponseInit,
|
||||
) => SuperTypedResponse<Data>;
|
||||
|
||||
export declare type SuperTypedResponse<T extends unknown = unknown> = Response & {
|
||||
superjson(): Promise<T>;
|
||||
};
|
||||
|
||||
type AppData = any;
|
||||
type DataFunction = (...args: any[]) => unknown; // matches any function
|
||||
type DataOrFunction = AppData | DataFunction;
|
||||
|
||||
export type UseDataFunctionReturn<T extends DataOrFunction> = T extends (
|
||||
...args: any[]
|
||||
) => infer Output
|
||||
? Awaited<Output> extends SuperTypedResponse<infer U>
|
||||
? U
|
||||
: Awaited<ReturnType<T>>
|
||||
: Awaited<T>;
|
||||
|
||||
export const superLoaderJson: SuperJsonFunction = (data, init = {}) => {
|
||||
const responseInit = typeof init === 'number' ? { status: init } : init;
|
||||
const headers = new Headers(responseInit.headers);
|
||||
|
||||
if (!headers.has('Content-Type')) {
|
||||
headers.set('Content-Type', 'application/json; charset=utf-8');
|
||||
}
|
||||
|
||||
return new Response(_superjson.stringify(data), {
|
||||
...responseInit,
|
||||
headers,
|
||||
}) as SuperTypedResponse<typeof data>;
|
||||
};
|
||||
|
||||
export function useSuperLoaderData<T = AppData>(): UseDataFunctionReturn<T> {
|
||||
const data = useLoaderData();
|
||||
|
||||
return _superjson.deserialize(data);
|
||||
}
|
||||
export function useSuperActionData<T = AppData>(): UseDataFunctionReturn<T> | null {
|
||||
const data = useActionData();
|
||||
|
||||
return data ? _superjson.deserialize(data) : null;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
export const truncateTitle = (title: string, maxLength: number = 16) => {
|
||||
if (title.length <= maxLength) {
|
||||
return title;
|
||||
}
|
||||
|
||||
const start = title.slice(0, maxLength / 2);
|
||||
const end = title.slice(-maxLength / 2);
|
||||
|
||||
return `${start}.....${end}`;
|
||||
};
|
||||
Reference in New Issue
Block a user