Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Feb 25, 2024
1 parent 858457e commit a694cc0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Website/src/hooks/useModFS.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const useModFS = () => {
};

export const ModFSProvider = (props: React.PropsWithChildren) => {
const [modFS, setModFS] = useNativeFileStorage("/data/adb/mmrl/modfs.v7.json", INITIAL_MOD_CONF, { loader: JSON });
const [modFS, setModFS] = useNativeFileStorage("/data/adb/mmrl/modfs.v7.json", INITIAL_MOD_CONF, { loader: "json" });

const contextValue = React.useMemo(
() => ({
Expand Down
43 changes: 32 additions & 11 deletions Website/src/hooks/useNativeFileStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { SetStateAction, useCallback, useEffect, useState } from "react";
import { useLog } from "./native/useLog";
import { SuFile } from "@Native/SuFile";
import INI from "ini";
import YMAL from "yaml";
import YAML from "yaml";
import { os } from "@Native/Os";
import { SetValue } from "./useNativeStorage";
import React from "react";

type Loader = typeof JSON | typeof INI | typeof YMAL | null;
type Loader = "json" | "yaml" | "yml" | "prop" | "properties" | "ini" | null;

export function useNativeFileStorage<T = string>(
key: string,
Expand All @@ -21,10 +21,18 @@ export function useNativeFileStorage<T = string>(
const readValue = useCallback((): T => {
try {
if (file.exist()) {
if (loader) {
return loader.parse(file.read());
} else {
return file.read() as T;
switch (loader) {
case "json":
return JSON.parse(file.read());
case "properties":
case "prop":
case "ini":
return INI.parse(file.read()) as T;
case "yml":
case "yaml":
return YAML.parse(file.read());
default:
return file.read() as T;
}
} else {
return initialValue;
Expand All @@ -39,10 +47,23 @@ export function useNativeFileStorage<T = string>(
const setValue: SetValue<T> = (value) => {
try {
const newValue = value instanceof Function ? value(storedValue) : value;
if (loader) {
file.write((loader as any).stringify(newValue));
} else {
file.write(String(newValue));
switch (loader) {
case "json":
console.log(JSON.stringify(newValue, null, 4))
file.write(JSON.stringify(newValue, null, 4));
break;
case "properties":
case "prop":
case "ini":
file.write(INI.stringify(newValue, { whitespace:true, newline: true }));
break;
case "yml":
case "yaml":
file.write(YAML.stringify(newValue));
break;
default:
file.write(String(newValue));
break;
}
setStoredValue(newValue);
} catch (error) {
Expand Down Expand Up @@ -78,7 +99,7 @@ export interface ConfigProvider extends React.PropsWithChildren {
}

export const ConfigProvider = (props: ConfigProvider) => {
const { loadFromFile, loader = JSON, initialConfig } = props;
const { loadFromFile, loader = "json", initialConfig } = props;

if (!loadFromFile) {
throw new TypeError('"loadFromFile" is undefined');
Expand Down

0 comments on commit a694cc0

Please sign in to comment.