-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tried async import and am running into issues with imports, attemptin…
…g to make static imports work
- Loading branch information
Showing
15 changed files
with
223 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use client" | ||
import React, { useState } from "react"; | ||
import { FidgetConfig, FidgetSettings, GenericFidget } from "@/common/fidgets/makeFidget"; | ||
import { reduce } from "lodash"; | ||
import { FidgetWrapper, FidgetWrapperConfig } from "@/common/fidgets/FidgetWrapper"; | ||
|
||
|
||
export default function LazyFidgetViewer({ fidget }: { fidget: GenericFidget }) { | ||
const defaultConfig: FidgetWrapperConfig = { | ||
editConfig: fidget.fieldConfig, | ||
fidgetConfig: { | ||
editable: true, | ||
size: [1, 2], | ||
settings: reduce( | ||
fidget.fieldConfig.fields, | ||
(acc, f) => ({ | ||
...acc, | ||
[f.fieldName]: f.default || null, | ||
}), | ||
{}, | ||
) | ||
}, | ||
}; | ||
const [config, setConfig] = useState<FidgetWrapperConfig>(defaultConfig); | ||
const saveConifg = async (conf: FidgetConfig<FidgetSettings>) => { | ||
setConfig({ | ||
editConfig: config.editConfig, | ||
fidgetConfig: conf, | ||
}); | ||
return true; | ||
}; | ||
|
||
return ( | ||
<FidgetWrapper | ||
config={config} | ||
saveConfig={saveConifg} | ||
fidget={fidget} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"use client" | ||
import React, { useEffect, useState } from "react"; | ||
import { FidgetConfig, FidgetSettings, Fidget } from "@/common/fidgets/makeFidget"; | ||
import { reduce } from "lodash"; | ||
import { FidgetWrapper, FidgetWrapperConfig } from "@/common/fidgets/FidgetWrapper"; | ||
import { useLazyFidget } from "./useLazyFidget"; | ||
|
||
|
||
export default function LazyFidgetViewer({ fidgetPath }: { fidgetPath: string }) { | ||
// This doesn't work if the FidgetPath is relative | ||
// Building a separate Fidget's package might solve the issue | ||
// But that will take testing | ||
const lazyFidget = useLazyFidget(fidgetPath); | ||
const [config, setConfig] = useState<FidgetWrapperConfig>({ | ||
editConfig: { fields: [] }, | ||
fidgetConfig: { editable: true, size: [1,1], settings: {} } | ||
}); | ||
const saveConifg = async (conf: FidgetConfig<FidgetSettings>) => { | ||
setConfig({ | ||
editConfig: config.editConfig, | ||
fidgetConfig: conf, | ||
}); | ||
return true; | ||
}; | ||
|
||
useEffect(() => { | ||
if (lazyFidget.status === "Success") { | ||
const fidget = lazyFidget.result; | ||
const defaultConfig: FidgetWrapperConfig = { | ||
editConfig: fidget.fieldConfig, | ||
fidgetConfig: { | ||
editable: true, | ||
size: [1, 2], | ||
settings: reduce( | ||
fidget.fieldConfig.fields, | ||
(acc, f) => ({ | ||
...acc, | ||
[f.fieldName]: f.default || null, | ||
}), | ||
{}, | ||
) | ||
}, | ||
}; | ||
setConfig(defaultConfig); | ||
} | ||
}, [lazyFidget]); | ||
|
||
return ( | ||
<> | ||
{ | ||
lazyFidget.status === "Loading" ? | ||
<div>Loading...</div> | ||
: ( | ||
lazyFidget.status === "Success" ? | ||
<FidgetWrapper | ||
config={config} | ||
saveConfig={saveConifg} | ||
fidget={lazyFidget.result} | ||
/> | ||
: <div>Error!</div> | ||
) | ||
} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useState, useEffect } from "react"; | ||
import { GenericFidget } from "@/common/fidgets/makeFidget"; | ||
|
||
type LazyFidgetResult<T> = T | Error | null; | ||
type LazyFidgetStatus = "Loading" | "Error" | "Success"; | ||
|
||
type LazyFidgetPending = { | ||
status: "Loading", | ||
result: null, | ||
} | ||
|
||
type LazyFidgetError = { | ||
status: "Error", | ||
result: Error, | ||
} | ||
|
||
type LazyFidgetLoaded<T> = { | ||
status: "Success", | ||
result: T, | ||
} | ||
|
||
export type LazyFidget<T extends GenericFidget> = LazyFidgetError | LazyFidgetPending | LazyFidgetLoaded<T>; | ||
|
||
export function useLazyFidget<T extends GenericFidget>(fidgetPath: string): LazyFidget<T> { | ||
const [result, setResult] = useState<LazyFidgetResult<T>>(null); | ||
const [status, setStatus] = useState<LazyFidgetStatus>("Loading"); | ||
|
||
useEffect(() => { | ||
import(fidgetPath).then( | ||
moduleData => { | ||
setResult(moduleData.default); | ||
setStatus("Success"); | ||
}, | ||
error => { | ||
setResult(error); | ||
setStatus("Error"); | ||
console.log(error); | ||
} | ||
); | ||
}, []); | ||
|
||
return { | ||
status, result | ||
} as LazyFidget<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from "react"; | ||
import { FidgetEditConfig, makeFidget } from "../../common/fidgets/makeFidget"; | ||
import TextInput from "@/common/ui/molecules/TextInput"; | ||
|
||
export type ExampleFidgetSettings = { | ||
text: string, | ||
} | ||
|
||
const exampleConfig: FidgetEditConfig = { | ||
fields: [ | ||
{ | ||
fieldName: "displayText", | ||
default: "Hello World!", | ||
required: true, | ||
inputSelector: TextInput, | ||
} | ||
], | ||
}; | ||
|
||
const Example: React.FC<ExampleFidgetSettings> = ({ text }: ExampleFidgetSettings) => { | ||
return ( | ||
<div className=""> | ||
{ text } | ||
</div> | ||
); | ||
}; | ||
|
||
const ExampleFidget = makeFidget<ExampleFidgetSettings>(Example, exampleConfig); | ||
|
||
export default ExampleFidget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// TO DO: Figure out how to do this importing dynamically | ||
import ExampleFidget from "./example"; | ||
|
||
export default { | ||
example: ExampleFidget, | ||
}; |