All files / src/store Types.tsx

100% Statements 12/12
100% Branches 0/0
83.33% Functions 5/6
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190                                                              22x                                                                                                                                                                                                                                                   22x   22x         22x 1477x     22x 22x     123x       34x                 123x             22x  
import { PropsWithChildren, ComponentPropsWithoutRef, createContext, useContext, useReducer } from 'react';
export type TagType = React.ElementType | keyof JSX.IntrinsicElements;
 
type TypesElementProps<T extends TagType = 'span'> = {
  as?: T;
  render?: (
    props: TypesElement<T>,
    result: { type: 'type' | 'value'; value?: unknown; keyName: string | number },
  ) => React.ReactNode;
  'data-type'?: string;
};
 
export type TypesElement<T extends TagType> = TypesElementProps<T> & ComponentPropsWithoutRef<T>;
export type InitialTypesState<T extends TagType = 'span'> = {
  displayDataTypes?: boolean;
  Url?: TypesElement<T>;
  Str?: TypesElement<T>;
  Undefined?: TypesElement<T>;
  Null?: TypesElement<T>;
  True?: TypesElement<T>;
  False?: TypesElement<T>;
  Date?: TypesElement<T>;
  Float?: TypesElement<T>;
  Set?: TypesElement<T>;
  Int?: TypesElement<T>;
  Map?: TypesElement<T>;
  Nan?: TypesElement<T>;
  Bigint?: TypesElement<T>;
};
type Dispatch<T extends TagType> = React.Dispatch<InitialTypesState<T>>;
 
const initialState: InitialTypesState<TagType | 'span'> = {
  Str: {
    as: 'span',
    'data-type': 'string',
    style: {
      color: 'var(--w-rjv-type-string-color, #cb4b16)',
    },
    className: 'w-rjv-type',
    children: 'string',
  },
  Url: {
    as: 'a',
    style: {
      color: 'var(--w-rjv-type-url-color, #0969da)',
    },
    'data-type': 'url',
    className: 'w-rjv-type',
    children: 'url',
  },
  Undefined: {
    style: {
      color: 'var(--w-rjv-type-undefined-color, #586e75)',
    },
    as: 'span',
    'data-type': 'undefined',
    className: 'w-rjv-type',
    children: 'undefined',
  },
  Null: {
    style: {
      color: 'var(--w-rjv-type-null-color, #d33682)',
    },
    as: 'span',
    'data-type': 'null',
    className: 'w-rjv-type',
    children: 'null',
  },
  Map: {
    style: {
      color: 'var(--w-rjv-type-map-color, #268bd2)',
      marginRight: 3,
    },
    as: 'span',
    'data-type': 'map',
    className: 'w-rjv-type',
    children: 'Map',
  },
  Nan: {
    style: {
      color: 'var(--w-rjv-type-nan-color, #859900)',
    },
    as: 'span',
    'data-type': 'nan',
    className: 'w-rjv-type',
    children: 'NaN',
  },
  Bigint: {
    style: {
      color: 'var(--w-rjv-type-bigint-color, #268bd2)',
    },
    as: 'span',
    'data-type': 'bigint',
    className: 'w-rjv-type',
    children: 'bigint',
  },
  Int: {
    style: {
      color: 'var(--w-rjv-type-int-color, #268bd2)',
    },
    as: 'span',
    'data-type': 'int',
    className: 'w-rjv-type',
    children: 'int',
  },
  Set: {
    style: {
      color: 'var(--w-rjv-type-set-color, #268bd2)',
      marginRight: 3,
    },
    as: 'span',
    'data-type': 'set',
    className: 'w-rjv-type',
    children: 'Set',
  },
  Float: {
    style: {
      color: 'var(--w-rjv-type-float-color, #859900)',
    },
    as: 'span',
    'data-type': 'float',
    className: 'w-rjv-type',
    children: 'float',
  },
  True: {
    style: {
      color: 'var(--w-rjv-type-boolean-color, #2aa198)',
    },
    as: 'span',
    'data-type': 'bool',
    className: 'w-rjv-type',
    children: 'bool',
  },
  False: {
    style: {
      color: 'var(--w-rjv-type-boolean-color, #2aa198)',
    },
    as: 'span',
    'data-type': 'bool',
    className: 'w-rjv-type',
    children: 'bool',
  },
  Date: {
    style: {
      color: 'var(--w-rjv-type-date-color, #268bd2)',
    },
    as: 'span',
    'data-type': 'date',
    className: 'w-rjv-type',
    children: 'date',
  },
};
 
const Context = createContext<InitialTypesState<TagType>>(initialState);
 
const reducer = <T extends TagType>(state: InitialTypesState<T>, action: InitialTypesState<T>) => ({
  ...state,
  ...action,
});
 
export const useTypesStore = () => {
  return useContext(Context);
};
 
const DispatchTypes = createContext<Dispatch<TagType>>(() => {});
DispatchTypes.displayName = 'JVR.DispatchTypes';
 
export function useTypes() {
  return useReducer(reducer, initialState);
}
 
export function useTypesDispatch() {
  return useContext(DispatchTypes);
}
 
interface TypesProps<T extends TagType> {
  initial: InitialTypesState<T>;
  dispatch: Dispatch<TagType>;
}
 
export function Types<T extends TagType>({ initial, dispatch, children }: PropsWithChildren<TypesProps<T>>) {
  return (
    <Context.Provider value={initial as unknown as InitialTypesState<TagType>}>
      <DispatchTypes.Provider value={dispatch}>{children}</DispatchTypes.Provider>
    </Context.Provider>
  );
}
 
Types.displayName = 'JVR.Types';