import { useMemo } from 'react'; import { useClientConfig } from './useClientConfig'; import { isTauri } from './useTauri'; import { getOriginBaseUrl } from '../pages/pathUtils'; import { resolveWebAppUrl } from '../utils/callInBrowser'; /** * [Gitea #248] Base URL for Lotus links meant for other people. * * On the web that's this page's origin. In the desktop app the page is served * from a local address (and hash-routed), which nobody else can open, so use * the public web app from config `webAppUrl` (web path routing) instead. * Without a valid `webAppUrl` it falls back to the origin, as before. */ export const lotusShareBase = (originBase: string, desktop: boolean, webAppUrl: unknown): string => (desktop && resolveWebAppUrl(webAppUrl)) || originBase; /** * Every base a Lotus link to this deployment may start with: the origin, plus * the public web app in the desktop app, so a shared https link opens in-app. */ export const lotusLinkBases = ( originBase: string, desktop: boolean, webAppUrl: unknown, ): string[] => { const shared = lotusShareBase(originBase, desktop, webAppUrl); return shared === originBase ? [originBase] : [originBase, shared]; }; export const useLotusShareBase = (): string => { const { hashRouter, webAppUrl } = useClientConfig(); return useMemo( () => lotusShareBase(getOriginBaseUrl(hashRouter), isTauri(), webAppUrl), [hashRouter, webAppUrl], ); };