Skip to content

Commit

Permalink
🐛 fix(config): SubLink
Browse files Browse the repository at this point in the history
  • Loading branch information
web-ppanel committed Dec 9, 2024
1 parent 2cc18cf commit 1c61966
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
35 changes: 16 additions & 19 deletions apps/user/app/(main)/(user)/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useState } from 'react';

import useGlobalStore from '@/config/use-global';
import { getStat } from '@/services/common/common';
import { getPlatform } from '@/utils/common';
import CopyToClipboard from 'react-copy-to-clipboard';
import Renewal from '../order/renewal';
import ResetTraffic from '../order/reset-traffic';
Expand All @@ -58,16 +59,7 @@ export default function Page() {
return data.data as API.ApplicationResponse;
},
});
const [platform, setPlatform] = useState<keyof API.ApplicationResponse>('windows');

const handleCopy = async (text: string) => {
try {
await navigator.clipboard.writeText(text);
toast.success(t('copySuccess'));
} catch {
toast.error(t('copyFailure'));
}
};
const [platform, setPlatform] = useState<keyof API.ApplicationResponse>(getPlatform());

const { data } = useQuery({
queryKey: ['getStat'],
Expand Down Expand Up @@ -235,21 +227,26 @@ export default function Page() {
<Button size='sm' variant='secondary' className='px-1.5' asChild>
<Link href={app.url!}>{t('download')}</Link>
</Button>
<Button
size='sm'
onClick={() => {
handleCopy(url);
<CopyToClipboard
text={url}
onCopy={(text, result) => {
const href = getAppSubLink(app.subscribe_type, url);
if (isBrowser() && href) {
window.location.href = href;
} else {
toast.info(t('manualImportMessage'));
} else if (result) {
toast.success(
<>
<p>{t('copySuccess')}</p>
<p>{t('manualImportMessage')}</p>
</>,
);
}
}}
className='p-2'
>
{t('import')}
</Button>
<Button size='sm' className='p-2'>
{t('import')}
</Button>
</CopyToClipboard>
</div>
</div>
))}
Expand Down
14 changes: 9 additions & 5 deletions apps/user/config/use-global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,24 @@ export const useGlobalStore = create<GlobalStore>((set, get) => ({
getAppSubLink: (type: string, url: string) => {
const name = get().common.site.site_name || '';
switch (type) {
// case 'Clash':
// return `clash://install-config?url=${url}&name=${name}`;
case 'Clash':
return `clash://install-config?url=${url}&name=${name}`;
case 'Hiddify':
return `hiddify://import/${url}#${name}`;
case 'Loon':
return `loon://import?sub=${encodeURI(url)}${name}`;
return `loon://import?sub=${encodeURI(url)}`;
case 'NekoBox':
return `sn://subscription?url=${url}&name=${name}`;
case 'NekoRay':
return `sn://subscription?url=${url}&name=${name}`;
// case 'Netch':
// return ``;
case 'Quantumult X':
return `quantumult-x://add-resource?remote-resource=${url}`;
return `quantumult-x://add-resource?remote-resource=${encodeURIComponent(
JSON.stringify({
server_remote: [`${url}, tag=${name}`],
}),
)}`;
case 'Shadowrocket':
return `shadowrocket://add/sub://${window.btoa(url)}?remark=${encodeURI(name)}`;
case 'Singbox':
Expand All @@ -124,7 +128,7 @@ export const useGlobalStore = create<GlobalStore>((set, get) => ({
case 'V2rayNg':
return `v2rayng://install-sub?url=${encodeURI(url)}&name=${name}`;
default:
return `clash://install-config?url=${encodeURI(url)}&name=${name}`;
return '';
}
},
}));
Expand Down
25 changes: 25 additions & 0 deletions apps/user/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,28 @@ export function Logout() {
location.href = `/`;
}
}

export function getPlatform(): 'windows' | 'mac' | 'linux' | 'android' | 'ios' {
if (typeof navigator === 'undefined') {
console.log('This function can only run in a browser environment.');
return 'windows';
}

const userAgent = navigator.userAgent;

const platformPatterns: Record<string, RegExp> = {
windows: /Windows NT/,
mac: /Mac OS X/,
linux: /Linux/,
android: /Android/,
ios: /iPhone OS|iPad; CPU OS/,
};

for (const [platform, regex] of Object.entries(platformPatterns)) {
if (regex.test(userAgent)) {
return platform as 'windows' | 'mac' | 'linux' | 'android' | 'ios';
}
}

return 'windows';
}

0 comments on commit 1c61966

Please sign in to comment.