1
- import DEFAULT_NODES from '../../../algorithms/parameters/HSParam.js' ;
2
1
import React , { useState , useEffect , useMemo } from 'react' ;
3
2
import algorithms from '../../../algorithms' ;
4
3
@@ -18,21 +17,31 @@ import algorithms from '../../../algorithms';
18
17
// const DEFAULT_MIN = '1'
19
18
// const DEFAULT_MAX = '10'
20
19
21
- const DEFAULT_ALGORITHM = 'heapSort' ;
22
- const DEFAULT_MODE = 'sort' ;
23
- const DEFAULT_LIST = '' ;
24
- const DEFAULT_VALUE = '' ;
25
- const DEFAULT_XY_COORDS = '' ;
26
- const DEFAULT_EDGE_WEIGHTS = '' ;
27
- const DEFAULT_SIZE = '' ;
28
- const DEFAULT_START = '' ;
29
- const DEFAULT_END = '' ;
30
- const DEFAULT_STRING = '' ;
31
- const DEFAULT_PATTERN = '' ;
32
- const DEFAULT_UNION = '' ;
33
- const DEFAULT_HEURISTIC = '' ;
34
- const DEFAULT_MIN = '' ;
35
- const DEFAULT_MAX = '' ;
20
+ // List of valid parameter names
21
+ const VALID_PARAM_NAMES = [
22
+ 'alg' , 'mode' , 'list' , 'value' , 'xyCoords' , 'edgeWeights' ,
23
+ 'size' , 'start' , 'end' , 'string' , 'pattern' , 'union' ,
24
+ 'heuristic' , 'min' , 'max'
25
+ ] ;
26
+
27
+ // Default values for each parameter
28
+ const DEFAULT_VALUES = {
29
+ alg : 'heapSort' ,
30
+ mode : 'sort' ,
31
+ list : '' ,
32
+ value : '' ,
33
+ xyCoords : '' ,
34
+ edgeWeights : '' ,
35
+ size : '' ,
36
+ start : '' ,
37
+ end : '' ,
38
+ string : '' ,
39
+ pattern : '' ,
40
+ union : '' ,
41
+ heuristic : '' ,
42
+ min : '' ,
43
+ max : ''
44
+ } ;
36
45
37
46
38
47
export function useUrlParams ( ) {
@@ -50,39 +59,30 @@ export function useUrlParams() {
50
59
} , [ ] ) ;
51
60
52
61
const urlParams = useMemo ( ( ) => new URLSearchParams ( search ) , [ search ] ) ;
53
- const alg = urlParams . get ( 'alg' ) || DEFAULT_ALGORITHM ; // Default algorithm
54
- const mode = urlParams . get ( 'mode' ) || DEFAULT_MODE ; // Default mode
55
-
56
- // Helper function to handle both missing and 'null' values
57
- const getParamOrDefault = ( param , defaultValue ) => {
58
- return param === null || param === "null" ? defaultValue : param ;
59
- } ;
60
-
61
- // Parse individual parameters directly from URL
62
- const list = getParamOrDefault ( urlParams . get ( 'list' ) , DEFAULT_LIST ) ;
63
- const value = getParamOrDefault ( urlParams . get ( 'value' ) , DEFAULT_VALUE ) ;
64
- const xyCoords = getParamOrDefault ( urlParams . get ( 'xyCoords' ) , DEFAULT_XY_COORDS ) ;
65
- const edgeWeights = getParamOrDefault ( urlParams . get ( 'edgeWeights' ) , DEFAULT_EDGE_WEIGHTS ) ;
66
- const size = getParamOrDefault ( urlParams . get ( 'size' ) , DEFAULT_SIZE ) ;
67
- const start = getParamOrDefault ( urlParams . get ( 'start' ) , DEFAULT_START ) ;
68
- const end = getParamOrDefault ( urlParams . get ( 'end' ) , DEFAULT_END ) ;
69
- const string = urlParams . get ( 'string' ) || DEFAULT_STRING
70
- const pattern = urlParams . get ( 'pattern' ) || DEFAULT_PATTERN ;
71
- const union = getParamOrDefault ( urlParams . get ( 'union' ) , DEFAULT_UNION ) ;
72
- const heuristic = getParamOrDefault ( urlParams . get ( 'heuristic' ) , DEFAULT_HEURISTIC ) ;
73
- const min = getParamOrDefault ( urlParams . get ( 'min' ) , DEFAULT_MIN ) ;
74
- const max = getParamOrDefault ( urlParams . get ( 'max' ) , DEFAULT_MAX ) ;
75
-
76
-
77
- console . log ( "Raw URL alg:" , urlParams . get ( 'alg' ) ) ;
78
- console . log ( "Raw URL mode:" , urlParams . get ( 'mode' ) ) ;
79
- console . log ( "Parsed URL Params:" , { list, value, xyCoords, edgeWeights, size, start, end, string, pattern, union, heuristic, min, max } ) ;
62
+ const params = { } ;
63
+
64
+ // Filter and parse valid URL parameters
65
+ VALID_PARAM_NAMES . forEach ( ( name ) => {
66
+ const value = urlParams . get ( name ) ;
67
+ params [ name ] = value !== null ? value : DEFAULT_VALUES [ name ] ;
68
+ } ) ;
69
+
70
+ // Log a warning if there are any invalid parameters in the URL
71
+ urlParams . forEach ( ( _ , key ) => {
72
+ if ( ! VALID_PARAM_NAMES . includes ( key ) ) {
73
+ console . warn ( `Invalid URL parameter ignored: ${ key } ` ) ;
74
+ }
75
+ } ) ;
80
76
81
- return { alg, mode, list, value, xyCoords, edgeWeights, size, start, end, string, pattern, union, heuristic, min, max } ;
77
+ // console.log("Raw URL alg:", urlParams.get('alg'));
78
+ // console.log("Raw URL mode:", urlParams.get('mode'));
79
+ // console.log("Parsed URL Params:", { list, value, xyCoords, edgeWeights, size, start, end, string, pattern, union, heuristic, min, max });
82
80
81
+ return params ;
83
82
}
84
83
85
84
85
+
86
86
function extractValue ( paramString , key ) {
87
87
const regex = new RegExp ( `${ key } =([^;]*)` ) ;
88
88
const match = paramString . match ( regex ) ;
@@ -98,6 +98,10 @@ export const withAlgorithmParams = (WrappedComponent) => {
98
98
return < div > Invalid algorithm specified</ div > ;
99
99
}
100
100
101
+ if ( ! mode || ! ( mode in algorithms [ alg ] . pseudocode ) ) {
102
+ return < div > Invalid mode specified</ div > ;
103
+ }
104
+
101
105
return < WrappedComponent
102
106
alg = { alg }
103
107
mode = { mode }
0 commit comments