mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-27 12:26:29 -08:00
27 lines
743 B
TypeScript
27 lines
743 B
TypeScript
import { useMemo } from 'react';
|
|
import { trimLeadingSlash, trimSlash, trimTrailingSlash } from '$utils/common';
|
|
import { useClientConfig } from './useClientConfig';
|
|
|
|
export const usePathWithOrigin = (path: string): string => {
|
|
const { hashRouter } = useClientConfig();
|
|
const { origin } = window.location;
|
|
|
|
const pathWithOrigin = useMemo(() => {
|
|
let url: string = trimSlash(origin);
|
|
|
|
url += `/${trimSlash(import.meta.env.BASE_URL ?? '')}`;
|
|
url = trimTrailingSlash(url);
|
|
|
|
if (hashRouter?.enabled) {
|
|
url += `/#/${trimSlash(hashRouter.basename ?? '')}`;
|
|
url = trimTrailingSlash(url);
|
|
}
|
|
|
|
url += `/${trimLeadingSlash(path)}`;
|
|
|
|
return url;
|
|
}, [path, hashRouter, origin]);
|
|
|
|
return pathWithOrigin;
|
|
};
|