From 5459d8d5850b3ca8c5e7797d8fa695ac40de218c Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Wed, 6 Dec 2023 16:00:28 +0530 Subject: [PATCH] fix: Default name for token, let it be Token-1 Token-2 Token-3 and such --- packages/nc-gui/components/account/Token.vue | 28 ++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/nc-gui/components/account/Token.vue b/packages/nc-gui/components/account/Token.vue index 4e665c30ab..e7338bdcde 100644 --- a/packages/nc-gui/components/account/Token.vue +++ b/packages/nc-gui/components/account/Token.vue @@ -29,7 +29,7 @@ const showNewTokenModal = ref(false) const currentLimit = ref(10) -const defaultTokenName = t('labels.untitledToken') +const defaultTokenName = t('labels.token') const selectedTokenData = ref({ description: defaultTokenName, @@ -42,6 +42,30 @@ const pagination = reactive({ pageSize: 10, }) +const getDefaultTokenValue = (tokens: IApiTokenInfo[]) => { + let extractedSortedTokenNumbers = + [...tokens] + .map((e) => { + let tokenName = e.description?.split('-') + if (tokenName && tokenName[tokenName.length - 1] && !isNaN(Number(tokenName[tokenName.length - 1]?.trim()))) { + return Number(tokenName[tokenName.length - 1]?.trim()) + } + }) + .filter((e) => e) + .sort((a, b) => { + if (a !== undefined && b !== undefined) { + return a - b + } + return 0 + }) || [] + + if (extractedSortedTokenNumbers.length) { + return `${defaultTokenName}-${(extractedSortedTokenNumbers[extractedSortedTokenNumbers.length - 1] || 0) + 1}` + } else { + return `${defaultTokenName}-1` + } +} + const hideOrShowToken = (tokenId: string) => { if (selectedToken.isShow && selectedToken.id === tokenId) { selectedToken.isShow = false @@ -67,6 +91,7 @@ const loadTokens = async (page = currentPage.value, limit = currentLimit.value) pagination.pageSize = 10 tokens.value = response.list as IApiTokenInfo[] + selectedTokenData.value.description = getDefaultTokenValue(tokens.value) } catch (e: any) { message.error(await extractSdkResponseErrorMsg(e)) } @@ -116,7 +141,6 @@ const generateToken = async () => { } catch (e: any) { message.error(await extractSdkResponseErrorMsg(e)) } finally { - selectedTokenData.value.description = defaultTokenName $e('a:api-token:generate') } }