Skip to content

Commit

Permalink
refactor: Better typings for flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jun 4, 2024
1 parent 7b8fddf commit d25794d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
11 changes: 7 additions & 4 deletions app/flags/ls-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { FlagName, FlagValue } from "./types";

export const prefix = "flag__";
export const getKey = (name: FlagName) => prefix + name;

type FlagNameOrString = FlagName | (string & {});

export const getKey = (name: FlagNameOrString) => prefix + name;

const listFlagLocalStorage = () => {
return Object.keys(localStorage)
Expand All @@ -14,7 +17,7 @@ const listFlagLocalStorage = () => {
*
* @param {String} flag
*/
const getItem = (flag: FlagName) => {
const getItem = (flag: FlagNameOrString) => {
const val = localStorage.getItem(getKey(flag));
const parsed = val ? JSON.parse(val) : val;
return parsed;
Expand All @@ -26,7 +29,7 @@ const getItem = (flag: FlagName) => {
* @param {String} flag
* @param {String} value
*/
const setItem = (flag: FlagName, value: FlagValue) => {
const setItem = (flag: FlagNameOrString, value: FlagValue) => {
const str = JSON.stringify(value);
return localStorage.setItem(getKey(flag), str);
};
Expand All @@ -36,7 +39,7 @@ const setItem = (flag: FlagName, value: FlagValue) => {
*
* @param {String} flag
*/
const removeItem = (flag: FlagName) => {
const removeItem = (flag: FlagNameOrString) => {
return localStorage.removeItem(getKey(flag));
};

Expand Down
4 changes: 3 additions & 1 deletion app/flags/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export type FlagName =
/** Whether we can use the free canvas dashboard layout */
| "layouter.dashboard.free-canvas"
/** Whether we can use shared filters on dashboard layout */
| "layouter.dashboard.shared-filters";
| "layouter.dashboard.shared-filters"
/** Whether server side cache is disabled */
| "server-side-cache.disable";
2 changes: 1 addition & 1 deletion app/flags/useFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export function useFlags() {
flag.store.removeListener("change", handleChange);
};
}, [setFlags]);
return flags;
return flags as FlagName[];
}
3 changes: 2 additions & 1 deletion app/gql-flamegraph/devtool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { pipe, tap } from "wonka";

import useDisclosure from "@/components/use-disclosure";
import { flag, useFlag, useFlags } from "@/flags";
import { FlagName } from "@/flags/types";
import { RequestQueryMeta } from "@/graphql/query-meta";
import useEvent from "@/utils/use-event";

Expand Down Expand Up @@ -543,7 +544,7 @@ const FlagSwitch = ({
flagName,
onChange,
...props
}: { flagName: string; onChange?: (value: Boolean) => void } & Omit<
}: { flagName: FlagName; onChange?: (value: Boolean) => void } & Omit<
SwitchProps,
"onChange"
>) => {
Expand Down

0 comments on commit d25794d

Please sign in to comment.