-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #688 from isucon/simulator-exclusive-control-view
[FE] シミュレーターの排他制御とその表示
- Loading branch information
Showing
8 changed files
with
241 additions
and
182 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
frontend/app/components/modules/simulator-configs/simulator-chair-active-toggle.tsx
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,48 @@ | ||
import { FC, useCallback, useState } from "react"; | ||
import { fetchChairPostActivity } from "~/api/api-components"; | ||
import { Toggle } from "~/components/primitives/form/toggle"; | ||
import { ConfigFrame } from "~/components/primitives/frame/config-frame"; | ||
import { Text } from "~/components/primitives/text/text"; | ||
import { useSimulatorContext } from "~/contexts/simulator-context"; | ||
|
||
export const SimulatorChairActiveToggle: FC = () => { | ||
const { chair, isAnotherSimulatorBeingUsed } = useSimulatorContext(); | ||
const [activate, setActivate] = useState<boolean>(true); | ||
|
||
const toggleActivate = useCallback( | ||
(activity: boolean) => { | ||
try { | ||
void fetchChairPostActivity({ body: { is_active: activity } }); | ||
setActivate(activity); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}, | ||
[setActivate], | ||
); | ||
|
||
if (!chair) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ConfigFrame aria-disabled={isAnotherSimulatorBeingUsed}> | ||
<div className="flex justify-between items-center"> | ||
<Text size="sm" className="text-neutral-500" bold> | ||
配車を受け付ける | ||
</Text> | ||
<Toggle | ||
checked={activate} | ||
onUpdate={(v) => toggleActivate(v)} | ||
id="chair-activity" | ||
/> | ||
</div> | ||
{!isAnotherSimulatorBeingUsed && ( | ||
<div | ||
role="presentation" | ||
className="absolute top-0 left-0 w-full h-full bg-neutral-500 bg-opacity-60 flex items-center justify-center cursor-not-allowed" | ||
/> | ||
)} | ||
</ConfigFrame> | ||
); | ||
}; |
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
60 changes: 60 additions & 0 deletions
60
frontend/app/components/modules/simulator-configs/simulator-ghost-chair-toggle.tsx
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,60 @@ | ||
import { FC, RefObject, useEffect, useState } from "react"; | ||
import { Toggle } from "~/components/primitives/form/toggle"; | ||
import { ConfigFrame } from "~/components/primitives/frame/config-frame"; | ||
import { Text } from "~/components/primitives/text/text"; | ||
import { | ||
Message, | ||
MessageTypes, | ||
sendSimulatorConfig, | ||
} from "~/utils/post-message"; | ||
|
||
type SimulatorConfigType = { | ||
ghostChairEnabled: boolean; | ||
}; | ||
|
||
export const SimulatorGhostChairToggle: FC<{ | ||
simulatorRef: RefObject<HTMLIFrameElement>; | ||
}> = ({ simulatorRef }) => { | ||
const [ready, setReady] = useState<boolean>(false); | ||
|
||
const [config, setConfig] = useState<SimulatorConfigType>({ | ||
ghostChairEnabled: true, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!ready) return; | ||
if (simulatorRef.current?.contentWindow) { | ||
sendSimulatorConfig(simulatorRef.current.contentWindow, config); | ||
} | ||
}, [config, ready, simulatorRef]); | ||
|
||
useEffect(() => { | ||
const onMessage = ({ data }: MessageEvent<Message["ClientReady"]>) => { | ||
const isSameOrigin = origin == location.origin; | ||
if (isSameOrigin && data.type === MessageTypes.ClientReady) { | ||
setReady(Boolean(data?.payload?.ready)); | ||
} | ||
}; | ||
window.addEventListener("message", onMessage); | ||
return () => { | ||
window.removeEventListener("message", onMessage); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<ConfigFrame> | ||
<div className="flex justify-between items-center"> | ||
<Text size="sm" className="text-neutral-500" bold> | ||
疑似チェアを表示する | ||
</Text> | ||
<Toggle | ||
id="ghost-chair" | ||
checked={config.ghostChairEnabled} | ||
onUpdate={(v) => { | ||
setConfig((c) => ({ ...c, ghostChairEnabled: v })); | ||
}} | ||
/> | ||
</div> | ||
</ConfigFrame> | ||
); | ||
}; |
91 changes: 0 additions & 91 deletions
91
frontend/app/components/modules/simulator-display/simulator-config-display.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.