Skip to content

Commit

Permalink
Web: export decodeUrlQueryParam function (#42597)
Browse files Browse the repository at this point in the history
* Web: export decodeUrlQueryParam funciton

* Pull out decodeUrlQueryParam into own file
  • Loading branch information
kimlisa authored Jun 11, 2024
1 parent 07d6658 commit 14c30b3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { useEffect, useState } from 'react';

import { ResourceFilter } from 'teleport/services/agents';

import { encodeUrlQueryParams } from 'teleport/components/hooks/useUrlFiltering';
import {
decodeUrlQueryParam,
encodeUrlQueryParams,
} from 'teleport/components/hooks/useUrlFiltering';

export default function useServersideSearchPanel({
pathname,
Expand Down Expand Up @@ -90,15 +93,6 @@ export default function useServersideSearchPanel({
};
}

function decodeUrlQueryParam(param: string) {
// Prevents URI malformed error by replacing lone % with %25
const decodedQuery = decodeURIComponent(
param.replace(/%(?![0-9][0-9a-fA-F]+)/g, '%25')
);

return decodedQuery;
}

export type HookProps = {
pathname: string;
replaceHistory: (path: string) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* calls native decodeURIComponent under the hood but goes
* one step further by replacing lone % with %25
* to prevent URI malformed error.
*/
export function decodeUrlQueryParam(param: string) {
const decodedQuery = decodeURIComponent(
param.replace(/%(?![0-9][0-9a-fA-F]+)/g, '%25')
);

return decodedQuery;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@

export { useUrlFiltering } from './useUrlFiltering';
export { encodeUrlQueryParams } from './encodeUrlQueryParams';
export { decodeUrlQueryParam } from './decodeUrlQueryParam';

0 comments on commit 14c30b3

Please sign in to comment.