Skip to content

Commit

Permalink
sing box fit: rule provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Elziy committed Aug 26, 2024
1 parent 4cdc8cb commit 0caeedd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 59 deletions.
1 change: 1 addition & 0 deletions src/components/rules/RuleProviderItem.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
display: grid;
grid-template-columns: 40px 1fr 95px;
height: 100%;
font-weight: bold;
}

.left {
Expand Down
141 changes: 83 additions & 58 deletions src/components/rules/RuleProviderItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import ShowCodeModal from '~/components/rules/ShowCodeModal';
import { notifyError, notifySuccess } from '~/misc/message';
import { useStoreActions } from '~/components/StateProvider';

export function RuleProviderItem(
{
idx,
name,
vehicleType,
behavior,
updatedAt,
ruleCount,
apiConfig,
utilsApiUrl,
unReloadConfig
}) {
export function RuleProviderItem({
idx,
name,
vehicleType,
behavior,
updatedAt,
ruleCount,
apiConfig,
utilsApiUrl,
unReloadConfig,
sing_box,
}) {
const [onClickRefreshButton, isRefreshing] = useUpdateRuleProviderItem(name, apiConfig);
const [data, setData] = useState(null);
const [isModalOpen, setIsModalOpen] = useState(false);
Expand All @@ -39,77 +39,102 @@ export function RuleProviderItem(
fetch(utilsApiUrl + '/delete_rule_set', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body: JSON.stringify({ name })
}).then(async (res) => {
const response = await res.json();
if (response.code === 200) {
notifySuccess(response.message);
unReloadConfig?.push('删除规则资源 : ' + name);
updateAppConfig('unReloadConfig', unReloadConfig);
setDeleteModal(false);
} else {
notifyError(response.message);
}
}).catch(() => {
notifyError('网络错误');
});
body: JSON.stringify({ name }),
})
.then(async (res) => {
const response = await res.json();
if (response.code === 200) {
notifySuccess(response.message);
unReloadConfig?.push('删除规则资源 : ' + name);
updateAppConfig('unReloadConfig', unReloadConfig);
setDeleteModal(false);
} else {
notifyError(response.message);
}
})
.catch(() => {
notifyError('网络错误');
});
};

function get_rule_set() {
if (sing_box) {
return;
}
setIsModalOpen(true);
fetch(utilsApiUrl + '/get_rule_set?name=' + name, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(async (res) => {
const response = await res.json();
if (response.code === 200) {
setData(response.data);
} else {
'Content-Type': 'application/json',
},
})
.then(async (res) => {
const response = await res.json();
if (response.code === 200) {
setData(response.data);
} else {
setIsModalOpen(false);
notifyError(response.message);
}
})
.catch(() => {
setIsModalOpen(false);
notifyError(response.message);
}
}).catch(() => {
setIsModalOpen(false);
notifyError('网络错误');
});
notifyError('网络错误');
});
}

return (
<div className={s.RuleProviderItem}>
<span className={s.left}>{idx}</span>
<div className={s.middle}>
<SectionNameType name={name} type={`${vehicleType} / ${behavior}`} now={undefined} icon={undefined} nowProxy={undefined} />
<SectionNameType
name={name}
type={`${vehicleType} / ${behavior}`}
now={undefined}
icon={undefined}
nowProxy={undefined}
/>
<div style={{ paddingRight: '0.5em' }}>
<span onClick={get_rule_set} className={s0.qty}>{ruleCount}</span>
<span onClick={get_rule_set} className={s0.qty}>
{ruleCount}
</span>
</div>
{/*<div style={{ paddingLeft: '1em' }} className={s.gray}>{`${ruleCount}条规则`}</div>*/}
<div className={s.update_time}><span>{`更新于${timeAgo}`}</span></div>
<div className={s.update_time}>
<span>{`更新于${timeAgo}`}</span>
</div>
</div>
<span className={s.refreshButtonWrapper}>
<Button className={s.button} onClick={() => setDeleteModal(true)
}>
<HiX size={18} />
</Button>
{ !sing_box &&
<Button className={s.button} onClick={() => setDeleteModal(true)}>
<HiX size={18} />
</Button>
}
<Button className={s.button} onClick={onClickRefreshButton} disabled={isRefreshing}>
<RotateIcon isRotating={isRefreshing} />
</Button>
</span>

<ModalCloseAllConnections
confirm={'delete_rule'}
isOpen={delete_modal}
primaryButtonOnTap={() => delete_rule_set(name)}
onRequestClose={() => setDeleteModal(false)}
/>

<ShowCodeModal isOpen={isModalOpen} onRequestClose={() => {
setIsModalOpen(false);
setData(null);
}} data={data} />
{!sing_box && (
<>
<ModalCloseAllConnections
confirm={'delete_rule'}
isOpen={delete_modal}
primaryButtonOnTap={() => delete_rule_set(name)}
onRequestClose={() => setDeleteModal(false)}
/>
<ShowCodeModal
isOpen={isModalOpen}
onRequestClose={() => {
setIsModalOpen(false);
setData(null);
}}
data={data}
/>
</>
)}
</div>
);
}
2 changes: 1 addition & 1 deletion src/components/rules/Rules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ function Rules({ dispatch, apiConfig, groups, utilsApiUrl, unReloadConfig }) {
const item = provider.byName[name];
return (
<div key={index} className={s.RuleProviderItemWrapper}>
<RuleProviderItem apiConfig={apiConfig} utilsApiUrl={utilsApiUrl} {...item} unReloadConfig={unReloadConfig} />
<RuleProviderItem sing_box={version.premium && version.meta} apiConfig={apiConfig} utilsApiUrl={utilsApiUrl} {...item} unReloadConfig={unReloadConfig} />
</div>
);
})}
Expand Down

0 comments on commit 0caeedd

Please sign in to comment.